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.time.Instant;
11 import java.util.ArrayList;
12 import java.util.HashSet;
13 import java.util.List;
14 import java.util.Map.Entry;
16 import javax.ws.rs.core.MultivaluedMap;
17 import javax.ws.rs.core.UriBuilder;
18 import javax.ws.rs.core.UriInfo;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.mockito.Mock;
23 import org.mockito.Mockito;
24 import org.mockito.MockitoAnnotations;
25 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
26 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
32 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
33 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
34 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
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()
62 .setGlobalSchema(YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/notifications")));
64 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
65 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
66 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
67 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
71 public void startTimeTest() {
72 final List<Entry<String, List<String>>> list = new ArrayList<>();
73 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
74 Mockito.when(entry.getKey()).thenReturn("start-time");
75 final List<String> time = new ArrayList<>();
76 time.add("2014-10-25T10:02:00Z");
77 Mockito.when(entry.getValue()).thenReturn(time);
81 Notificator.removeAllListeners();
85 public void milisecsTest() {
86 final List<Entry<String, List<String>>> list = new ArrayList<>();
87 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
88 Mockito.when(entry.getKey()).thenReturn("start-time");
89 final List<String> time = new ArrayList<>();
90 time.add("2014-10-25T10:02:00.12345Z");
91 Mockito.when(entry.getValue()).thenReturn(time);
95 Notificator.removeAllListeners();
99 public void zonesPlusTest() {
100 final List<Entry<String, List<String>>> list = new ArrayList<>();
101 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
102 Mockito.when(entry.getKey()).thenReturn("start-time");
103 final List<String> time = new ArrayList<>();
104 time.add("2014-10-25T10:02:00+01:00");
105 Mockito.when(entry.getValue()).thenReturn(time);
109 Notificator.removeAllListeners();
113 public void zonesMinusTest() {
114 final List<Entry<String, List<String>>> list = new ArrayList<>();
115 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
116 Mockito.when(entry.getKey()).thenReturn("start-time");
117 final List<String> time = new ArrayList<>();
118 time.add("2014-10-25T10:02:00-01:00");
119 Mockito.when(entry.getValue()).thenReturn(time);
123 Notificator.removeAllListeners();
127 public void startAndStopTimeTest() {
128 final List<Entry<String, List<String>>> list = new ArrayList<>();
129 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
130 Mockito.when(entry.getKey()).thenReturn("start-time");
131 final List<String> time = new ArrayList<>();
132 time.add("2014-10-25T10:02:00Z");
133 Mockito.when(entry.getValue()).thenReturn(time);
135 final Entry<String, List<String>> entry2 = Mockito.mock(Entry.class);
136 Mockito.when(entry2.getKey()).thenReturn("stop-time");
137 final List<String> time2 = new ArrayList<>();
138 time2.add("2014-10-25T12:31:00Z");
139 Mockito.when(entry2.getValue()).thenReturn(time2);
145 Notificator.removeAllListeners();
148 @Test(expected = RestconfDocumentedException.class)
149 public void stopTimeTest() {
150 final List<Entry<String, List<String>>> list = new ArrayList<>();
151 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
152 Mockito.when(entry.getKey()).thenReturn("stop-time");
153 final List<String> time = new ArrayList<>();
154 time.add("2014-10-25T12:31:00Z");
155 Mockito.when(entry.getValue()).thenReturn(time);
159 Notificator.removeAllListeners();
162 @Test(expected = RestconfDocumentedException.class)
163 public void badParamTest() {
164 final List<Entry<String, List<String>>> list = new ArrayList<>();
165 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
166 Mockito.when(entry.getKey()).thenReturn("time");
167 final List<String> time = new ArrayList<>();
168 time.add("2014-10-25T12:31:00Z");
169 Mockito.when(entry.getValue()).thenReturn(time);
173 Notificator.removeAllListeners();
176 @Test(expected = IllegalArgumentException.class)
177 public void badValueTest() {
178 final List<Entry<String, List<String>>> list = new ArrayList<>();
179 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
180 Mockito.when(entry.getKey()).thenReturn("start-time");
181 final List<String> time = new ArrayList<>();
182 time.add("badvalue");
183 Mockito.when(entry.getValue()).thenReturn(time);
187 Notificator.removeAllListeners();
190 @Test(expected = IllegalArgumentException.class)
191 public void badZonesTest() {
192 final List<Entry<String, List<String>>> list = new ArrayList<>();
193 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
194 Mockito.when(entry.getKey()).thenReturn("start-time");
195 final List<String> time = new ArrayList<>();
196 time.add("2014-10-25T10:02:00Z+1:00");
197 Mockito.when(entry.getValue()).thenReturn(time);
201 Notificator.removeAllListeners();
204 @Test(expected = IllegalArgumentException.class)
205 public void badMilisecsTest() {
206 final List<Entry<String, List<String>>> list = new ArrayList<>();
207 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
208 Mockito.when(entry.getKey()).thenReturn("start-time");
209 final List<String> time = new ArrayList<>();
210 time.add("2014-10-25T10:02:00:0026Z");
211 Mockito.when(entry.getValue()).thenReturn(time);
215 Notificator.removeAllListeners();
219 public void onNotifiTest() throws Exception {
220 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
221 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
222 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
223 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
225 final List<Entry<String, List<String>>> list = new ArrayList<>();
226 final Entry<String, List<String>> entry = Mockito.mock(Entry.class);
227 Mockito.when(entry.getKey()).thenReturn("start-time");
228 final List<String> time = new ArrayList<>();
229 time.add("2014-10-25T10:02:00Z");
230 Mockito.when(entry.getValue()).thenReturn(time);
235 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
236 Mockito.mock(AsyncDataChangeEvent.class);
237 Instant startOrig = listener.getStart();
238 Assert.assertNotNull(startOrig);
239 listener.onDataChanged(change);
241 startOrig = listener.getStart();
242 Assert.assertNull(startOrig);
245 private void subscribe(final List<Entry<String, List<String>>> entries) {
246 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
247 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
248 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
249 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
250 final Set<Entry<String, List<String>>> set = new HashSet<>();
251 for(final Entry<String, List<String>> entry : entries){
254 Mockito.when(map.entrySet()).thenReturn(set);
255 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);