2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.sal.restconf.impl.test;
10 import java.lang.reflect.Field;
11 import java.util.ArrayList;
12 import java.util.Date;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.core.UriBuilder;
19 import javax.ws.rs.core.UriInfo;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
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.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class RestconfImplNotificationSubscribingTest {
45 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
48 private BrokerFacade broker;
51 private DOMDataBroker domDataBroker;
54 private UriInfo uriInfo;
57 public void setup() throws Exception {
58 MockitoAnnotations.initMocks(this);
60 this.broker.setDomDataBroker(this.domDataBroker);
61 RestconfImpl.getInstance().setBroker(this.broker);
62 ControllerContext.getInstance()
63 .setGlobalSchema(YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/notifications")));
65 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
66 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
67 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
68 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
72 public void startTimeTest() {
73 final List<Entry<String, List<String>>> list = new ArrayList<>();
74 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
75 Mockito.when(entry.getKey()).thenReturn("start-time");
76 final List<String> time = new ArrayList<>();
77 time.add("2014-10-25T10:02:00Z");
78 Mockito.when(entry.getValue()).thenReturn(time);
82 Notificator.removeAllListeners();
86 public void milisecsTest() {
87 final List<Entry<String, List<String>>> list = new ArrayList<>();
88 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
89 Mockito.when(entry.getKey()).thenReturn("start-time");
90 final List<String> time = new ArrayList<>();
91 time.add("2014-10-25T10:02:00.12345Z");
92 Mockito.when(entry.getValue()).thenReturn(time);
96 Notificator.removeAllListeners();
100 public void zonesPlusTest() {
101 final List<Entry<String, List<String>>> list = new ArrayList<>();
102 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
103 Mockito.when(entry.getKey()).thenReturn("start-time");
104 final List<String> time = new ArrayList<>();
105 time.add("2014-10-25T10:02:00+01:00");
106 Mockito.when(entry.getValue()).thenReturn(time);
110 Notificator.removeAllListeners();
114 public void zonesMinusTest() {
115 final List<Entry<String, List<String>>> list = new ArrayList<>();
116 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
117 Mockito.when(entry.getKey()).thenReturn("start-time");
118 final List<String> time = new ArrayList<>();
119 time.add("2014-10-25T10:02:00-01:00");
120 Mockito.when(entry.getValue()).thenReturn(time);
124 Notificator.removeAllListeners();
128 public void startAndStopTimeTest() {
129 final List<Entry<String, List<String>>> list = new ArrayList<>();
130 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
131 Mockito.when(entry.getKey()).thenReturn("start-time");
132 final List<String> time = new ArrayList<>();
133 time.add("2014-10-25T10:02:00Z");
134 Mockito.when(entry.getValue()).thenReturn(time);
136 final Entry<String, List<String>> entry2 = Mockito.mock(Entry.class);
137 Mockito.when(entry2.getKey()).thenReturn("stop-time");
138 final List<String> time2 = new ArrayList<>();
139 time2.add("2014-10-25T12:31:00Z");
140 Mockito.when(entry2.getValue()).thenReturn(time2);
146 Notificator.removeAllListeners();
149 @Test(expected = RestconfDocumentedException.class)
150 public void stopTimeTest() {
151 final List<Entry<String, List<String>>> list = new ArrayList<>();
152 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
153 Mockito.when(entry.getKey()).thenReturn("stop-time");
154 final List<String> time = new ArrayList<>();
155 time.add("2014-10-25T12:31:00Z");
156 Mockito.when(entry.getValue()).thenReturn(time);
160 Notificator.removeAllListeners();
163 @Test(expected = RestconfDocumentedException.class)
164 public void badParamTest() {
165 final List<Entry<String, List<String>>> list = new ArrayList<>();
166 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
167 Mockito.when(entry.getKey()).thenReturn("time");
168 final List<String> time = new ArrayList<>();
169 time.add("2014-10-25T12:31:00Z");
170 Mockito.when(entry.getValue()).thenReturn(time);
174 Notificator.removeAllListeners();
177 @Test(expected = IllegalArgumentException.class)
178 public void badValueTest() {
179 final List<Entry<String, List<String>>> list = new ArrayList<>();
180 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
181 Mockito.when(entry.getKey()).thenReturn("start-time");
182 final List<String> time = new ArrayList<>();
183 time.add("badvalue");
184 Mockito.when(entry.getValue()).thenReturn(time);
188 Notificator.removeAllListeners();
191 @Test(expected = IllegalArgumentException.class)
192 public void badZonesTest() {
193 final List<Entry<String, List<String>>> list = new ArrayList<>();
194 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
195 Mockito.when(entry.getKey()).thenReturn("start-time");
196 final List<String> time = new ArrayList<>();
197 time.add("2014-10-25T10:02:00Z+1:00");
198 Mockito.when(entry.getValue()).thenReturn(time);
202 Notificator.removeAllListeners();
205 @Test(expected = IllegalArgumentException.class)
206 public void badMilisecsTest() {
207 final List<Entry<String, List<String>>> list = new ArrayList<>();
208 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
209 Mockito.when(entry.getKey()).thenReturn("start-time");
210 final List<String> time = new ArrayList<>();
211 time.add("2014-10-25T10:02:00:0026Z");
212 Mockito.when(entry.getValue()).thenReturn(time);
216 Notificator.removeAllListeners();
220 public void onNotifiTest() throws Exception {
221 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
222 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
223 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
224 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
226 final List<Entry<String, List<String>>> list = new ArrayList<>();
227 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
228 Mockito.when(entry.getKey()).thenReturn("start-time");
229 final List<String> time = new ArrayList<>();
230 time.add("2014-10-25T10:02:00Z");
231 Mockito.when(entry.getValue()).thenReturn(time);
236 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
237 Mockito.mock(AsyncDataChangeEvent.class);
238 final Class<?> superclass = listener.getClass().getSuperclass().getSuperclass();
239 Field start = superclass.getDeclaredField("start");
240 start.setAccessible(true);
241 Date startOrig = (Date) start.get(listener);
242 Assert.assertNotNull(startOrig);
243 listener.onDataChanged(change);
245 start = superclass.getDeclaredField("start");
246 start.setAccessible(true);
247 startOrig = (Date) start.get(listener);
248 Assert.assertNull(startOrig);
251 private void subscribe(final List<Entry<String, List<String>>> entries) {
252 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
253 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
254 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
255 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
256 final Set<Entry<String, List<String>>> set = new HashSet<>();
257 for(final Entry<String, List<String>> entry : entries){
260 Mockito.when(map.entrySet()).thenReturn(set);
261 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);