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