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