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.time.Instant;
11 import java.util.AbstractMap.SimpleImmutableEntry;
12 import java.util.ArrayList;
13 import java.util.HashSet;
14 import java.util.List;
15 import java.util.Map.Entry;
17 import javax.ws.rs.core.MultivaluedMap;
18 import javax.ws.rs.core.UriBuilder;
19 import javax.ws.rs.core.UriInfo;
20 import org.junit.Assert;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.Mock;
24 import org.mockito.Mockito;
25 import org.mockito.MockitoAnnotations;
26 import org.opendaylight.controller.md.sal.common.api.data.AsyncDataChangeEvent;
27 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
28 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
29 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
30 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
31 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
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.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
36 import org.opendaylight.yangtools.yang.common.QName;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
40 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
43 public class RestconfImplNotificationSubscribingTest {
45 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
48 private BrokerFacade broker;
51 private DOMDataBroker domDataBroker;
54 private UriInfo uriInfo;
57 public void setup() throws Exception {
58 MockitoAnnotations.initMocks(this);
60 this.broker.setDomDataBroker(this.domDataBroker);
61 RestconfImpl.getInstance().setBroker(this.broker);
62 ControllerContext.getInstance()
63 .setGlobalSchema(YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/notifications")));
65 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
66 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
67 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
68 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
72 public void startTimeTest() {
73 final List<Entry<String, List<String>>> list = new ArrayList<>();
74 final List<String> time = new ArrayList<>();
75 time.add("2014-10-25T10:02:00Z");
76 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
80 Notificator.removeAllListeners();
84 public void milisecsTest() {
85 final List<Entry<String, List<String>>> list = new ArrayList<>();
86 final List<String> time = new ArrayList<>();
87 time.add("2014-10-25T10:02:00.12345Z");
88 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
92 Notificator.removeAllListeners();
96 public void zonesPlusTest() {
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+01:00");
100 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
104 Notificator.removeAllListeners();
108 public void zonesMinusTest() {
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 startAndStopTimeTest() {
121 final List<Entry<String, List<String>>> list = new ArrayList<>();
122 final List<String> time = new ArrayList<>();
123 time.add("2014-10-25T10:02:00Z");
124 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
126 final List<String> time2 = new ArrayList<>();
127 time2.add("2014-10-25T12:31:00Z");
128 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
134 Notificator.removeAllListeners();
137 @Test(expected = RestconfDocumentedException.class)
138 public void stopTimeTest() {
139 final List<Entry<String, List<String>>> list = new ArrayList<>();
140 final List<String> time = new ArrayList<>();
141 time.add("2014-10-25T12:31:00Z");
142 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
146 Notificator.removeAllListeners();
149 @Test(expected = RestconfDocumentedException.class)
150 public void badParamTest() {
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<>("time", time);
158 Notificator.removeAllListeners();
161 @Test(expected = IllegalArgumentException.class)
162 public void badValueTest() {
163 final List<Entry<String, List<String>>> list = new ArrayList<>();
164 final List<String> time = new ArrayList<>();
165 time.add("badvalue");
166 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
170 Notificator.removeAllListeners();
173 @Test(expected = IllegalArgumentException.class)
174 public void badZonesTest() {
175 final List<Entry<String, List<String>>> list = new ArrayList<>();
176 final List<String> time = new ArrayList<>();
177 time.add("2014-10-25T10:02:00Z+1:00");
178 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
182 Notificator.removeAllListeners();
185 @Test(expected = IllegalArgumentException.class)
186 public void badMilisecsTest() {
187 final List<Entry<String, List<String>>> list = new ArrayList<>();
188 final List<String> time = new ArrayList<>();
189 time.add("2014-10-25T10:02:00:0026Z");
190 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
194 Notificator.removeAllListeners();
198 public void onNotifiTest() throws Exception {
199 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
200 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-14-12", "localName"));
201 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
202 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
204 final List<Entry<String, List<String>>> list = new ArrayList<>();
205 final List<String> time = new ArrayList<>();
206 time.add("2014-10-25T10:02:00Z");
207 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
212 final AsyncDataChangeEvent<YangInstanceIdentifier, NormalizedNode<?, ?>> change =
213 Mockito.mock(AsyncDataChangeEvent.class);
214 Instant startOrig = listener.getStart();
215 Assert.assertNotNull(startOrig);
216 listener.onDataChanged(change);
218 startOrig = listener.getStart();
219 Assert.assertNull(startOrig);
222 private void subscribe(final List<Entry<String, List<String>>> entries) {
223 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
224 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
225 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
226 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
227 final Set<Entry<String, List<String>>> set = new HashSet<>();
228 for (final Entry<String, List<String>> entry : entries) {
231 Mockito.when(map.entrySet()).thenReturn(set);
232 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);