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.Assert;
22 import org.junit.Before;
23 import org.junit.BeforeClass;
24 import org.junit.Test;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
30 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
31 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
32 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
33 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
34 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
35 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
36 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
37 import org.opendaylight.yangtools.yang.common.QName;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
40 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
41 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
42 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
45 public class RestconfImplNotificationSubscribingTest {
47 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
49 private static SchemaContext schemaContext;
52 private BrokerFacade broker;
55 private DOMDataBroker domDataBroker;
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 void setup() throws Exception {
70 MockitoAnnotations.initMocks(this);
72 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
73 restconfImpl = RestconfImpl.newInstance(broker, controllerContext);
75 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
76 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
77 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
78 Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
82 public void startTimeTest() {
83 final List<Entry<String, List<String>>> list = new ArrayList<>();
84 final List<String> time = new ArrayList<>();
85 time.add("2014-10-25T10:02:00Z");
86 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
90 Notificator.removeAllListeners();
94 public void milisecsTest() {
95 final List<Entry<String, List<String>>> list = new ArrayList<>();
96 final List<String> time = new ArrayList<>();
97 time.add("2014-10-25T10:02:00.12345Z");
98 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
102 Notificator.removeAllListeners();
106 public void zonesPlusTest() {
107 final List<Entry<String, List<String>>> list = new ArrayList<>();
108 final List<String> time = new ArrayList<>();
109 time.add("2014-10-25T10:02:00+01:00");
110 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
114 Notificator.removeAllListeners();
118 public void zonesMinusTest() {
119 final List<Entry<String, List<String>>> list = new ArrayList<>();
120 final List<String> time = new ArrayList<>();
121 time.add("2014-10-25T10:02:00-01:00");
122 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
126 Notificator.removeAllListeners();
130 public void startAndStopTimeTest() {
131 final List<Entry<String, List<String>>> list = new ArrayList<>();
132 final List<String> time = new ArrayList<>();
133 time.add("2014-10-25T10:02:00Z");
134 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
136 final List<String> time2 = new ArrayList<>();
137 time2.add("2014-10-25T12:31:00Z");
138 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
144 Notificator.removeAllListeners();
147 @Test(expected = RestconfDocumentedException.class)
148 public void stopTimeTest() {
149 final List<Entry<String, List<String>>> list = new ArrayList<>();
150 final List<String> time = new ArrayList<>();
151 time.add("2014-10-25T12:31:00Z");
152 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", 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 List<String> time = new ArrayList<>();
163 time.add("2014-10-25T12:31:00Z");
164 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", time);
168 Notificator.removeAllListeners();
171 @Test(expected = IllegalArgumentException.class)
172 public void badValueTest() {
173 final List<Entry<String, List<String>>> list = new ArrayList<>();
174 final List<String> time = new ArrayList<>();
175 time.add("badvalue");
176 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
180 Notificator.removeAllListeners();
183 @Test(expected = IllegalArgumentException.class)
184 public void badZonesTest() {
185 final List<Entry<String, List<String>>> list = new ArrayList<>();
186 final List<String> time = new ArrayList<>();
187 time.add("2014-10-25T10:02:00Z+1:00");
188 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
192 Notificator.removeAllListeners();
195 @Test(expected = IllegalArgumentException.class)
196 public void badMilisecsTest() {
197 final List<Entry<String, List<String>>> list = new ArrayList<>();
198 final List<String> time = new ArrayList<>();
199 time.add("2014-10-25T10:02:00:0026Z");
200 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
204 Notificator.removeAllListeners();
208 public void onNotifiTest() throws Exception {
209 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
210 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
211 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
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);