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 RestconfImpl.getInstance().setBroker(this.broker);
73 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
74 RestconfImpl.getInstance().setControllerContext(controllerContext);
76 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
77 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
78 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
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 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
213 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML,
216 final List<Entry<String, List<String>>> list = new ArrayList<>();
217 final List<String> time = new ArrayList<>();
218 time.add("2014-10-25T10:02:00Z");
219 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
224 ArrayList<DataTreeCandidate> candidates = new ArrayList<>(0);
225 Instant startOrig = listener.getStart();
226 Assert.assertNotNull(startOrig);
227 listener.onDataTreeChanged(candidates);
229 startOrig = listener.getStart();
230 Assert.assertNull(startOrig);
233 private void subscribe(final List<Entry<String, List<String>>> entries) {
234 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
235 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
236 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
237 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
238 final Set<Entry<String, List<String>>> set = new HashSet<>();
239 for (final Entry<String, List<String>> entry : entries) {
242 Mockito.when(map.entrySet()).thenReturn(set);
243 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);