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.junit.runner.RunWith;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.junit.MockitoJUnitRunner;
30 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
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.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class RestconfImplNotificationSubscribingTest {
50 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
52 private static EffectiveModelContext schemaContext;
55 private BrokerFacade broker;
58 private UriInfo uriInfo;
60 private ControllerContext controllerContext;
61 private RestconfImpl restconfImpl;
64 public static void init() throws FileNotFoundException {
65 schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"));
69 public static void cleanUp() {
70 WebSocketServer.destroyInstance(); // NETCONF-604
75 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
76 restconfImpl = RestconfImpl.newInstance(broker, controllerContext);
78 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
79 Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
83 public void startTimeTest() {
84 final List<Entry<String, List<String>>> list = new ArrayList<>();
85 final List<String> time = new ArrayList<>();
86 time.add("2014-10-25T10:02:00Z");
87 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
91 Notificator.removeAllListeners();
95 public void milisecsTest() {
96 final List<Entry<String, List<String>>> list = new ArrayList<>();
97 final List<String> time = new ArrayList<>();
98 time.add("2014-10-25T10:02:00.12345Z");
99 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
103 Notificator.removeAllListeners();
107 public void zonesPlusTest() {
108 final List<Entry<String, List<String>>> list = new ArrayList<>();
109 final List<String> time = new ArrayList<>();
110 time.add("2014-10-25T10:02:00+01:00");
111 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
115 Notificator.removeAllListeners();
119 public void zonesMinusTest() {
120 final List<Entry<String, List<String>>> list = new ArrayList<>();
121 final List<String> time = new ArrayList<>();
122 time.add("2014-10-25T10:02:00-01:00");
123 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
127 Notificator.removeAllListeners();
131 public void startAndStopTimeTest() {
132 final List<Entry<String, List<String>>> list = new ArrayList<>();
133 final List<String> time = new ArrayList<>();
134 time.add("2014-10-25T10:02:00Z");
135 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
137 final List<String> time2 = new ArrayList<>();
138 time2.add("2014-10-25T12:31:00Z");
139 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", 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 List<String> time = new ArrayList<>();
152 time.add("2014-10-25T12:31:00Z");
153 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
157 Notificator.removeAllListeners();
160 @Test(expected = RestconfDocumentedException.class)
161 public void badParamTest() {
162 final List<Entry<String, List<String>>> list = new ArrayList<>();
163 final List<String> time = new ArrayList<>();
164 time.add("2014-10-25T12:31:00Z");
165 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", time);
169 Notificator.removeAllListeners();
172 @Test(expected = IllegalArgumentException.class)
173 public void badValueTest() {
174 final List<Entry<String, List<String>>> list = new ArrayList<>();
175 final List<String> time = new ArrayList<>();
176 time.add("badvalue");
177 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
181 Notificator.removeAllListeners();
184 @Test(expected = IllegalArgumentException.class)
185 public void badZonesTest() {
186 final List<Entry<String, List<String>>> list = new ArrayList<>();
187 final List<String> time = new ArrayList<>();
188 time.add("2014-10-25T10:02:00Z+1:00");
189 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
193 Notificator.removeAllListeners();
196 @Test(expected = IllegalArgumentException.class)
197 public void badMilisecsTest() {
198 final List<Entry<String, List<String>>> list = new ArrayList<>();
199 final List<String> time = new ArrayList<>();
200 time.add("2014-10-25T10:02:00:0026Z");
201 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
205 Notificator.removeAllListeners();
209 public void onNotifiTest() throws Exception {
210 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
211 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
212 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
215 final List<Entry<String, List<String>>> list = new ArrayList<>();
216 final List<String> time = new ArrayList<>();
217 time.add("2014-10-25T10:02:00Z");
218 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
223 ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
224 Instant startOrig = listener.getStart();
225 Assert.assertNotNull(startOrig);
226 listener.onDataTreeChanged(candidates);
228 startOrig = listener.getStart();
229 Assert.assertNull(startOrig);
232 private void subscribe(final List<Entry<String, List<String>>> entries) {
233 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
234 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
235 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
236 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
237 final Set<Entry<String, List<String>>> set = new HashSet<>();
238 for (final Entry<String, List<String>> entry : entries) {
241 Mockito.when(map.entrySet()).thenReturn(set);
242 restconfImpl.subscribeToStream(this.identifier, this.uriInfo);