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.data.api.YangInstanceIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
42 public class RestconfImplNotificationSubscribingTest {
44 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
47 private BrokerFacade broker;
50 private DOMDataBroker domDataBroker;
53 private UriInfo uriInfo;
56 public void setup() throws Exception {
57 MockitoAnnotations.initMocks(this);
59 this.broker.setDomDataBroker(this.domDataBroker);
60 RestconfImpl.getInstance().setBroker(this.broker);
61 ControllerContext.getInstance().setGlobalSchema(TestRestconfUtils.loadSchemaContext("/notifications"));
63 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
64 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
68 public void startTimeTest() {
69 final List<Entry<String, List<String>>> list = new ArrayList<>();
70 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
71 Mockito.when(entry.getKey()).thenReturn("start-time");
72 final List<String> time = new ArrayList<>();
73 time.add("2014-10-25T10:02:00Z");
74 Mockito.when(entry.getValue()).thenReturn(time);
78 Notificator.removeAllListeners();
82 public void milisecsTest() {
83 final List<Entry<String, List<String>>> list = new ArrayList<>();
84 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
85 Mockito.when(entry.getKey()).thenReturn("start-time");
86 final List<String> time = new ArrayList<>();
87 time.add("2014-10-25T10:02:00.12345Z");
88 Mockito.when(entry.getValue()).thenReturn(time);
92 Notificator.removeAllListeners();
96 public void zonesPlusTest() {
97 final List<Entry<String, List<String>>> list = new ArrayList<>();
98 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
99 Mockito.when(entry.getKey()).thenReturn("start-time");
100 final List<String> time = new ArrayList<>();
101 time.add("2014-10-25T10:02:00+01:00");
102 Mockito.when(entry.getValue()).thenReturn(time);
106 Notificator.removeAllListeners();
110 public void zonesMinusTest() {
111 final List<Entry<String, List<String>>> list = new ArrayList<>();
112 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
113 Mockito.when(entry.getKey()).thenReturn("start-time");
114 final List<String> time = new ArrayList<>();
115 time.add("2014-10-25T10:02:00-01:00");
116 Mockito.when(entry.getValue()).thenReturn(time);
120 Notificator.removeAllListeners();
124 public void startAndStopTimeTest() {
125 final List<Entry<String, List<String>>> list = new ArrayList<>();
126 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
127 Mockito.when(entry.getKey()).thenReturn("start-time");
128 final List<String> time = new ArrayList<>();
129 time.add("2014-10-25T10:02:00Z");
130 Mockito.when(entry.getValue()).thenReturn(time);
132 final Entry<String, List<String>> entry2 = Mockito.mock(Entry.class);
133 Mockito.when(entry2.getKey()).thenReturn("stop-time");
134 final List<String> time2 = new ArrayList<>();
135 time2.add("2014-10-25T12:31:00Z");
136 Mockito.when(entry2.getValue()).thenReturn(time2);
142 Notificator.removeAllListeners();
145 @Test(expected = RestconfDocumentedException.class)
146 public void stopTimeTest() {
147 final List<Entry<String, List<String>>> list = new ArrayList<>();
148 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
149 Mockito.when(entry.getKey()).thenReturn("stop-time");
150 final List<String> time = new ArrayList<>();
151 time.add("2014-10-25T12:31:00Z");
152 Mockito.when(entry.getValue()).thenReturn(time);
156 Notificator.removeAllListeners();
159 @Test(expected = RestconfDocumentedException.class)
160 public void badParamTest() {
161 final List<Entry<String, List<String>>> list = new ArrayList<>();
162 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
163 Mockito.when(entry.getKey()).thenReturn("time");
164 final List<String> time = new ArrayList<>();
165 time.add("2014-10-25T12:31:00Z");
166 Mockito.when(entry.getValue()).thenReturn(time);
170 Notificator.removeAllListeners();
173 @Test(expected = IllegalArgumentException.class)
174 public void badValueTest() {
175 final List<Entry<String, List<String>>> list = new ArrayList<>();
176 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
177 Mockito.when(entry.getKey()).thenReturn("start-time");
178 final List<String> time = new ArrayList<>();
179 time.add("badvalue");
180 Mockito.when(entry.getValue()).thenReturn(time);
184 Notificator.removeAllListeners();
187 @Test(expected = IllegalArgumentException.class)
188 public void badZonesTest() {
189 final List<Entry<String, List<String>>> list = new ArrayList<>();
190 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
191 Mockito.when(entry.getKey()).thenReturn("start-time");
192 final List<String> time = new ArrayList<>();
193 time.add("2014-10-25T10:02:00Z+1:00");
194 Mockito.when(entry.getValue()).thenReturn(time);
198 Notificator.removeAllListeners();
201 @Test(expected = IllegalArgumentException.class)
202 public void badMilisecsTest() {
203 final List<Entry<String, List<String>>> list = new ArrayList<>();
204 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
205 Mockito.when(entry.getKey()).thenReturn("start-time");
206 final List<String> time = new ArrayList<>();
207 time.add("2014-10-25T10:02:00:0026Z");
208 Mockito.when(entry.getValue()).thenReturn(time);
212 Notificator.removeAllListeners();
216 public void onNotifiTest() throws Exception {
217 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
218 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
220 final List<Entry<String, List<String>>> list = new ArrayList<>();
221 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
222 Mockito.when(entry.getKey()).thenReturn("start-time");
223 final List<String> time = new ArrayList<>();
224 time.add("2014-10-25T10:02:00Z");
225 Mockito.when(entry.getValue()).thenReturn(time);
230 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
231 Mockito.mock(AsyncDataChangeEvent.class);
232 Field start = listener.getClass().getDeclaredField("start");
233 start.setAccessible(true);
234 Date startOrig = (Date) start.get(listener);
235 Assert.assertNotNull(startOrig);
236 listener.onDataChanged(change);
238 start = listener.getClass().getDeclaredField("start");
239 start.setAccessible(true);
240 startOrig = (Date) start.get(listener);
241 Assert.assertNull(startOrig);
244 private void subscribe(final List<Entry<String, List<String>>> entries) {
245 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
246 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
247 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
248 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
249 final Set<Entry<String, List<String>>> set = new HashSet<>();
250 for(final Entry<String, List<String>> entry : entries){
253 Mockito.when(map.entrySet()).thenReturn(set);
254 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);