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;
63 public static void init() throws FileNotFoundException {
64 schemaContext = YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications"));
68 public void setup() throws Exception {
69 MockitoAnnotations.initMocks(this);
71 this.broker.setDomDataBroker(this.domDataBroker);
72 RestconfImpl.getInstance().setBroker(this.broker);
74 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
75 RestconfImpl.getInstance().setControllerContext(controllerContext);
77 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
78 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
79 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
80 Notificator.createListener(path, this.identifier, NotificationOutputType.XML, controllerContext);
84 public void startTimeTest() {
85 final List<Entry<String, List<String>>> list = new ArrayList<>();
86 final List<String> time = new ArrayList<>();
87 time.add("2014-10-25T10:02:00Z");
88 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
92 Notificator.removeAllListeners();
96 public void milisecsTest() {
97 final List<Entry<String, List<String>>> list = new ArrayList<>();
98 final List<String> time = new ArrayList<>();
99 time.add("2014-10-25T10:02:00.12345Z");
100 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
104 Notificator.removeAllListeners();
108 public void zonesPlusTest() {
109 final List<Entry<String, List<String>>> list = new ArrayList<>();
110 final List<String> time = new ArrayList<>();
111 time.add("2014-10-25T10:02:00+01:00");
112 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
116 Notificator.removeAllListeners();
120 public void zonesMinusTest() {
121 final List<Entry<String, List<String>>> list = new ArrayList<>();
122 final List<String> time = new ArrayList<>();
123 time.add("2014-10-25T10:02:00-01:00");
124 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
128 Notificator.removeAllListeners();
132 public void startAndStopTimeTest() {
133 final List<Entry<String, List<String>>> list = new ArrayList<>();
134 final List<String> time = new ArrayList<>();
135 time.add("2014-10-25T10:02:00Z");
136 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
138 final List<String> time2 = new ArrayList<>();
139 time2.add("2014-10-25T12:31:00Z");
140 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
146 Notificator.removeAllListeners();
149 @Test(expected = RestconfDocumentedException.class)
150 public void stopTimeTest() {
151 final List<Entry<String, List<String>>> list = new ArrayList<>();
152 final List<String> time = new ArrayList<>();
153 time.add("2014-10-25T12:31:00Z");
154 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
158 Notificator.removeAllListeners();
161 @Test(expected = RestconfDocumentedException.class)
162 public void badParamTest() {
163 final List<Entry<String, List<String>>> list = new ArrayList<>();
164 final List<String> time = new ArrayList<>();
165 time.add("2014-10-25T12:31:00Z");
166 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("time", 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 List<String> time = new ArrayList<>();
177 time.add("badvalue");
178 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
182 Notificator.removeAllListeners();
185 @Test(expected = IllegalArgumentException.class)
186 public void badZonesTest() {
187 final List<Entry<String, List<String>>> list = new ArrayList<>();
188 final List<String> time = new ArrayList<>();
189 time.add("2014-10-25T10:02:00Z+1:00");
190 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
194 Notificator.removeAllListeners();
197 @Test(expected = IllegalArgumentException.class)
198 public void badMilisecsTest() {
199 final List<Entry<String, List<String>>> list = new ArrayList<>();
200 final List<String> time = new ArrayList<>();
201 time.add("2014-10-25T10:02:00:0026Z");
202 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
206 Notificator.removeAllListeners();
210 public void onNotifiTest() throws Exception {
211 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
212 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
213 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
214 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
217 final List<Entry<String, List<String>>> list = new ArrayList<>();
218 final List<String> time = new ArrayList<>();
219 time.add("2014-10-25T10:02:00Z");
220 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
225 ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
226 Instant startOrig = listener.getStart();
227 Assert.assertNotNull(startOrig);
228 listener.onDataTreeChanged(candidates);
230 startOrig = listener.getStart();
231 Assert.assertNull(startOrig);
234 private void subscribe(final List<Entry<String, List<String>>> entries) {
235 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
236 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
237 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
238 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
239 final Set<Entry<String, List<String>>> set = new HashSet<>();
240 for (final Entry<String, List<String>> entry : entries) {
243 Mockito.when(map.entrySet()).thenReturn(set);
244 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);