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.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;
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.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
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.SchemaContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 public class RestconfImplNotificationSubscribingTest {
49 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
51 private static SchemaContext schemaContext;
54 private BrokerFacade broker;
57 private DOMDataBroker domDataBroker;
60 private UriInfo uriInfo;
62 private ControllerContext controllerContext;
63 private RestconfImpl restconfImpl;
66 public static void init() throws FileNotFoundException {
67 schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"));
71 public static void cleanUp() {
72 WebSocketServer.destroyInstance(); // NETCONF-604
76 public void setup() throws Exception {
77 MockitoAnnotations.initMocks(this);
79 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
80 restconfImpl = RestconfImpl.newInstance(broker, controllerContext);
82 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
83 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
84 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
85 Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
89 public void startTimeTest() {
90 final List<Entry<String, List<String>>> list = new ArrayList<>();
91 final List<String> time = new ArrayList<>();
92 time.add("2014-10-25T10:02:00Z");
93 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
97 Notificator.removeAllListeners();
101 public void milisecsTest() {
102 final List<Entry<String, List<String>>> list = new ArrayList<>();
103 final List<String> time = new ArrayList<>();
104 time.add("2014-10-25T10:02:00.12345Z");
105 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
109 Notificator.removeAllListeners();
113 public void zonesPlusTest() {
114 final List<Entry<String, List<String>>> list = new ArrayList<>();
115 final List<String> time = new ArrayList<>();
116 time.add("2014-10-25T10:02:00+01:00");
117 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
121 Notificator.removeAllListeners();
125 public void zonesMinusTest() {
126 final List<Entry<String, List<String>>> list = new ArrayList<>();
127 final List<String> time = new ArrayList<>();
128 time.add("2014-10-25T10:02:00-01:00");
129 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
133 Notificator.removeAllListeners();
137 public void startAndStopTimeTest() {
138 final List<Entry<String, List<String>>> list = new ArrayList<>();
139 final List<String> time = new ArrayList<>();
140 time.add("2014-10-25T10:02:00Z");
141 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
143 final List<String> time2 = new ArrayList<>();
144 time2.add("2014-10-25T12:31:00Z");
145 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
151 Notificator.removeAllListeners();
154 @Test(expected = RestconfDocumentedException.class)
155 public void stopTimeTest() {
156 final List<Entry<String, List<String>>> list = new ArrayList<>();
157 final List<String> time = new ArrayList<>();
158 time.add("2014-10-25T12:31:00Z");
159 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
163 Notificator.removeAllListeners();
166 @Test(expected = RestconfDocumentedException.class)
167 public void badParamTest() {
168 final List<Entry<String, List<String>>> list = new ArrayList<>();
169 final List<String> time = new ArrayList<>();
170 time.add("2014-10-25T12:31:00Z");
171 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", 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 List<String> time = new ArrayList<>();
182 time.add("badvalue");
183 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", 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 List<String> time = new ArrayList<>();
194 time.add("2014-10-25T10:02:00Z+1:00");
195 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
199 Notificator.removeAllListeners();
202 @Test(expected = IllegalArgumentException.class)
203 public void badMilisecsTest() {
204 final List<Entry<String, List<String>>> list = new ArrayList<>();
205 final List<String> time = new ArrayList<>();
206 time.add("2014-10-25T10:02:00:0026Z");
207 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
211 Notificator.removeAllListeners();
215 public void onNotifiTest() throws Exception {
216 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
217 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
218 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
219 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
222 final List<Entry<String, List<String>>> list = new ArrayList<>();
223 final List<String> time = new ArrayList<>();
224 time.add("2014-10-25T10:02:00Z");
225 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
230 ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
231 Instant startOrig = listener.getStart();
232 Assert.assertNotNull(startOrig);
233 listener.onDataTreeChanged(candidates);
235 startOrig = listener.getStart();
236 Assert.assertNull(startOrig);
239 private void subscribe(final List<Entry<String, List<String>>> entries) {
240 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
241 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
242 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
243 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
244 final Set<Entry<String, List<String>>> set = new HashSet<>();
245 for (final Entry<String, List<String>> entry : entries) {
248 Mockito.when(map.entrySet()).thenReturn(set);
249 restconfImpl.subscribeToStream(this.identifier, this.uriInfo);