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
11 package org.opendaylight.controller.sal.restconf.impl.test;
13 import java.lang.reflect.Field;
14 import java.util.ArrayList;
15 import java.util.Date;
16 import java.util.HashSet;
17 import java.util.List;
18 import java.util.Map.Entry;
20 import javax.ws.rs.core.MultivaluedMap;
21 import javax.ws.rs.core.UriBuilder;
22 import javax.ws.rs.core.UriInfo;
23 import org.junit.Assert;
24 import org.junit.Before;
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.common.api.data.AsyncDataChangeEvent;
30 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
31 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
32 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
35 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
36 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
37 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
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.NormalizedNode;
45 public class RestconfImplNotificationSubscribingTest {
47 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
50 private BrokerFacade broker;
53 private DOMDataBroker domDataBroker;
56 private UriInfo uriInfo;
59 public void setup() throws Exception {
60 MockitoAnnotations.initMocks(this);
62 this.broker.setDomDataBroker(this.domDataBroker);
63 RestconfImpl.getInstance().setBroker(this.broker);
64 ControllerContext.getInstance().setGlobalSchema(TestRestconfUtils.loadSchemaContext("/notifications"));
66 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
67 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
68 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
69 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
73 public void startTimeTest() {
74 final List<Entry<String, List<String>>> list = new ArrayList<>();
75 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
76 Mockito.when(entry.getKey()).thenReturn("start-time");
77 final List<String> time = new ArrayList<>();
78 time.add("2014-10-25T10:02:00Z");
79 Mockito.when(entry.getValue()).thenReturn(time);
83 Notificator.removeAllListeners();
87 public void milisecsTest() {
88 final List<Entry<String, List<String>>> list = new ArrayList<>();
89 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
90 Mockito.when(entry.getKey()).thenReturn("start-time");
91 final List<String> time = new ArrayList<>();
92 time.add("2014-10-25T10:02:00.12345Z");
93 Mockito.when(entry.getValue()).thenReturn(time);
97 Notificator.removeAllListeners();
101 public void zonesPlusTest() {
102 final List<Entry<String, List<String>>> list = new ArrayList<>();
103 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
104 Mockito.when(entry.getKey()).thenReturn("start-time");
105 final List<String> time = new ArrayList<>();
106 time.add("2014-10-25T10:02:00+01:00");
107 Mockito.when(entry.getValue()).thenReturn(time);
111 Notificator.removeAllListeners();
115 public void zonesMinusTest() {
116 final List<Entry<String, List<String>>> list = new ArrayList<>();
117 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
118 Mockito.when(entry.getKey()).thenReturn("start-time");
119 final List<String> time = new ArrayList<>();
120 time.add("2014-10-25T10:02:00-01:00");
121 Mockito.when(entry.getValue()).thenReturn(time);
125 Notificator.removeAllListeners();
129 public void startAndStopTimeTest() {
130 final List<Entry<String, List<String>>> list = new ArrayList<>();
131 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
132 Mockito.when(entry.getKey()).thenReturn("start-time");
133 final List<String> time = new ArrayList<>();
134 time.add("2014-10-25T10:02:00Z");
135 Mockito.when(entry.getValue()).thenReturn(time);
137 final Entry<String, List<String>> entry2 = Mockito.mock(Entry.class);
138 Mockito.when(entry2.getKey()).thenReturn("stop-time");
139 final List<String> time2 = new ArrayList<>();
140 time2.add("2014-10-25T12:31:00Z");
141 Mockito.when(entry2.getValue()).thenReturn(time2);
147 Notificator.removeAllListeners();
150 @Test(expected = RestconfDocumentedException.class)
151 public void stopTimeTest() {
152 final List<Entry<String, List<String>>> list = new ArrayList<>();
153 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
154 Mockito.when(entry.getKey()).thenReturn("stop-time");
155 final List<String> time = new ArrayList<>();
156 time.add("2014-10-25T12:31:00Z");
157 Mockito.when(entry.getValue()).thenReturn(time);
161 Notificator.removeAllListeners();
164 @Test(expected = RestconfDocumentedException.class)
165 public void badParamTest() {
166 final List<Entry<String, List<String>>> list = new ArrayList<>();
167 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
168 Mockito.when(entry.getKey()).thenReturn("time");
169 final List<String> time = new ArrayList<>();
170 time.add("2014-10-25T12:31:00Z");
171 Mockito.when(entry.getValue()).thenReturn(time);
175 Notificator.removeAllListeners();
178 @Test(expected = IllegalArgumentException.class)
179 public void badValueTest() {
180 final List<Entry<String, List<String>>> list = new ArrayList<>();
181 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
182 Mockito.when(entry.getKey()).thenReturn("start-time");
183 final List<String> time = new ArrayList<>();
184 time.add("badvalue");
185 Mockito.when(entry.getValue()).thenReturn(time);
189 Notificator.removeAllListeners();
192 @Test(expected = IllegalArgumentException.class)
193 public void badZonesTest() {
194 final List<Entry<String, List<String>>> list = new ArrayList<>();
195 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
196 Mockito.when(entry.getKey()).thenReturn("start-time");
197 final List<String> time = new ArrayList<>();
198 time.add("2014-10-25T10:02:00Z+1:00");
199 Mockito.when(entry.getValue()).thenReturn(time);
203 Notificator.removeAllListeners();
206 @Test(expected = IllegalArgumentException.class)
207 public void badMilisecsTest() {
208 final List<Entry<String, List<String>>> list = new ArrayList<>();
209 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
210 Mockito.when(entry.getKey()).thenReturn("start-time");
211 final List<String> time = new ArrayList<>();
212 time.add("2014-10-25T10:02:00:0026Z");
213 Mockito.when(entry.getValue()).thenReturn(time);
217 Notificator.removeAllListeners();
221 public void onNotifiTest() throws Exception {
222 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
223 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
224 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
225 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
227 final List<Entry<String, List<String>>> list = new ArrayList<>();
228 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
229 Mockito.when(entry.getKey()).thenReturn("start-time");
230 final List<String> time = new ArrayList<>();
231 time.add("2014-10-25T10:02:00Z");
232 Mockito.when(entry.getValue()).thenReturn(time);
237 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
238 Mockito.mock(AsyncDataChangeEvent.class);
239 final Class<?> superclass = listener.getClass().getSuperclass().getSuperclass();
240 Field start = superclass.getDeclaredField("start");
241 start.setAccessible(true);
242 Date startOrig = (Date) start.get(listener);
243 Assert.assertNotNull(startOrig);
244 listener.onDataChanged(change);
246 start = superclass.getDeclaredField("start");
247 start.setAccessible(true);
248 startOrig = (Date) start.get(listener);
249 Assert.assertNull(startOrig);
252 private void subscribe(final List<Entry<String, List<String>>> entries) {
253 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
254 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
255 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
256 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
257 final Set<Entry<String, List<String>>> set = new HashSet<>();
258 for(final Entry<String, List<String>> entry : entries){
261 Mockito.when(map.entrySet()).thenReturn(set);
262 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);