Use ControllerContext non-statically
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / RestconfImplNotificationSubscribingTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.controller.sal.restconf.impl.test;
9
10 import java.io.FileNotFoundException;
11 import java.time.Instant;
12 import java.util.AbstractMap.SimpleImmutableEntry;
13 import java.util.ArrayList;
14 import java.util.HashSet;
15 import java.util.List;
16 import java.util.Map.Entry;
17 import java.util.Set;
18 import javax.ws.rs.core.MultivaluedMap;
19 import javax.ws.rs.core.UriBuilder;
20 import javax.ws.rs.core.UriInfo;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
34 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
44
45 public class RestconfImplNotificationSubscribingTest {
46
47     private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
48
49     private static SchemaContext schemaContext;
50
51     @Mock
52     private BrokerFacade broker;
53
54     @Mock
55     private DOMDataBroker domDataBroker;
56
57     @Mock
58     private UriInfo uriInfo;
59
60     private ControllerContext controllerContext;
61
62     @BeforeClass
63     public static void init() throws FileNotFoundException {
64         schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"));
65     }
66
67     @Before
68     public void setup() throws Exception {
69         MockitoAnnotations.initMocks(this);
70
71         this.broker.setDomDataBroker(this.domDataBroker);
72         RestconfImpl.getInstance().setBroker(this.broker);
73
74         controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
75         RestconfImpl.getInstance().setControllerContext(controllerContext);
76
77         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
78         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
79         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
80         Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
81     }
82
83     @Test
84     public void startTimeTest() {
85         final List<Entry<String, List<String>>> list = new ArrayList<>();
86         final List<String> time = new ArrayList<>();
87         time.add("2014-10-25T10:02:00Z");
88         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
89         list.add(entry);
90
91         subscribe(list);
92         Notificator.removeAllListeners();
93     }
94
95     @Test
96     public void milisecsTest() {
97         final List<Entry<String, List<String>>> list = new ArrayList<>();
98         final List<String> time = new ArrayList<>();
99         time.add("2014-10-25T10:02:00.12345Z");
100         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
101         list.add(entry);
102
103         subscribe(list);
104         Notificator.removeAllListeners();
105     }
106
107     @Test
108     public void zonesPlusTest() {
109         final List<Entry<String, List<String>>> list = new ArrayList<>();
110         final List<String> time = new ArrayList<>();
111         time.add("2014-10-25T10:02:00+01:00");
112         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
113         list.add(entry);
114
115         subscribe(list);
116         Notificator.removeAllListeners();
117     }
118
119     @Test
120     public void zonesMinusTest() {
121         final List<Entry<String, List<String>>> list = new ArrayList<>();
122         final List<String> time = new ArrayList<>();
123         time.add("2014-10-25T10:02:00-01:00");
124         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
125         list.add(entry);
126
127         subscribe(list);
128         Notificator.removeAllListeners();
129     }
130
131     @Test
132     public void startAndStopTimeTest() {
133         final List<Entry<String, List<String>>> list = new ArrayList<>();
134         final List<String> time = new ArrayList<>();
135         time.add("2014-10-25T10:02:00Z");
136         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
137
138         final List<String> time2 = new ArrayList<>();
139         time2.add("2014-10-25T12:31:00Z");
140         final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
141
142         list.add(entry);
143         list.add(entry2);
144
145         subscribe(list);
146         Notificator.removeAllListeners();
147     }
148
149     @Test(expected = RestconfDocumentedException.class)
150     public void stopTimeTest() {
151         final List<Entry<String, List<String>>> list = new ArrayList<>();
152         final List<String> time = new ArrayList<>();
153         time.add("2014-10-25T12:31:00Z");
154         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
155         list.add(entry);
156
157         subscribe(list);
158         Notificator.removeAllListeners();
159     }
160
161     @Test(expected = RestconfDocumentedException.class)
162     public void badParamTest() {
163         final List<Entry<String, List<String>>> list = new ArrayList<>();
164         final List<String> time = new ArrayList<>();
165         time.add("2014-10-25T12:31:00Z");
166         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", time);
167         list.add(entry);
168
169         subscribe(list);
170         Notificator.removeAllListeners();
171     }
172
173     @Test(expected = IllegalArgumentException.class)
174     public void badValueTest() {
175         final List<Entry<String, List<String>>> list = new ArrayList<>();
176         final List<String> time = new ArrayList<>();
177         time.add("badvalue");
178         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
179         list.add(entry);
180
181         subscribe(list);
182         Notificator.removeAllListeners();
183     }
184
185     @Test(expected = IllegalArgumentException.class)
186     public void badZonesTest() {
187         final List<Entry<String, List<String>>> list = new ArrayList<>();
188         final List<String> time = new ArrayList<>();
189         time.add("2014-10-25T10:02:00Z+1:00");
190         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
191         list.add(entry);
192
193         subscribe(list);
194         Notificator.removeAllListeners();
195     }
196
197     @Test(expected = IllegalArgumentException.class)
198     public void badMilisecsTest() {
199         final List<Entry<String, List<String>>> list = new ArrayList<>();
200         final List<String> time = new ArrayList<>();
201         time.add("2014-10-25T10:02:00:0026Z");
202         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
203         list.add(entry);
204
205         subscribe(list);
206         Notificator.removeAllListeners();
207     }
208
209     @Test
210     public void onNotifiTest() throws Exception {
211         final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
212         final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
213         Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
214         final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
215                 controllerContext);
216
217         final List<Entry<String, List<String>>> list = new ArrayList<>();
218         final List<String> time = new ArrayList<>();
219         time.add("2014-10-25T10:02:00Z");
220         final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
221         list.add(entry);
222
223         subscribe(list);
224
225         ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
226         Instant startOrig = listener.getStart();
227         Assert.assertNotNull(startOrig);
228         listener.onDataTreeChanged(candidates);
229
230         startOrig = listener.getStart();
231         Assert.assertNull(startOrig);
232     }
233
234     private void subscribe(final List<Entry<String, List<String>>> entries) {
235         final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
236         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
237         final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
238         Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
239         final Set<Entry<String, List<String>>> set = new HashSet<>();
240         for (final Entry<String, List<String>> entry : entries) {
241             set.add(entry);
242         }
243         Mockito.when(map.entrySet()).thenReturn(set);
244         RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);
245     }
246
247 }