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.dom.api.DOMDataBroker;
27 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
28 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
29 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
31 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
32 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
33 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
34 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
35 import org.opendaylight.yangtools.yang.common.QName;
36 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
37 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
39 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeCandidate;
40 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42 public class RestconfImplNotificationSubscribingTest {
44 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
47 private BrokerFacade broker;
50 private DOMDataBroker domDataBroker;
53 private UriInfo uriInfo;
56 public void setup() throws Exception {
57 MockitoAnnotations.initMocks(this);
59 this.broker.setDomDataBroker(this.domDataBroker);
60 RestconfImpl.getInstance().setBroker(this.broker);
61 ControllerContext.getInstance()
62 .setGlobalSchema(YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
64 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
65 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
66 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
67 Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
71 public void startTimeTest() {
72 final List<Entry<String, List<String>>> list = new ArrayList<>();
73 final List<String> time = new ArrayList<>();
74 time.add("2014-10-25T10:02:00Z");
75 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
79 Notificator.removeAllListeners();
83 public void milisecsTest() {
84 final List<Entry<String, List<String>>> list = new ArrayList<>();
85 final List<String> time = new ArrayList<>();
86 time.add("2014-10-25T10:02:00.12345Z");
87 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
91 Notificator.removeAllListeners();
95 public void zonesPlusTest() {
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+01:00");
99 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
103 Notificator.removeAllListeners();
107 public void zonesMinusTest() {
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 startAndStopTimeTest() {
120 final List<Entry<String, List<String>>> list = new ArrayList<>();
121 final List<String> time = new ArrayList<>();
122 time.add("2014-10-25T10:02:00Z");
123 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
125 final List<String> time2 = new ArrayList<>();
126 time2.add("2014-10-25T12:31:00Z");
127 final Entry<String, List<String>> entry2 = new SimpleImmutableEntry<>("stop-time", time2);
133 Notificator.removeAllListeners();
136 @Test(expected = RestconfDocumentedException.class)
137 public void stopTimeTest() {
138 final List<Entry<String, List<String>>> list = new ArrayList<>();
139 final List<String> time = new ArrayList<>();
140 time.add("2014-10-25T12:31:00Z");
141 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("stop-time", time);
145 Notificator.removeAllListeners();
148 @Test(expected = RestconfDocumentedException.class)
149 public void badParamTest() {
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<>("time", time);
157 Notificator.removeAllListeners();
160 @Test(expected = IllegalArgumentException.class)
161 public void badValueTest() {
162 final List<Entry<String, List<String>>> list = new ArrayList<>();
163 final List<String> time = new ArrayList<>();
164 time.add("badvalue");
165 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
169 Notificator.removeAllListeners();
172 @Test(expected = IllegalArgumentException.class)
173 public void badZonesTest() {
174 final List<Entry<String, List<String>>> list = new ArrayList<>();
175 final List<String> time = new ArrayList<>();
176 time.add("2014-10-25T10:02:00Z+1:00");
177 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
181 Notificator.removeAllListeners();
184 @Test(expected = IllegalArgumentException.class)
185 public void badMilisecsTest() {
186 final List<Entry<String, List<String>>> list = new ArrayList<>();
187 final List<String> time = new ArrayList<>();
188 time.add("2014-10-25T10:02:00:0026Z");
189 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
193 Notificator.removeAllListeners();
197 public void onNotifiTest() throws Exception {
198 final YangInstanceIdentifier path = Mockito.mock(YangInstanceIdentifier.class);
199 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
200 Mockito.when(path.getLastPathArgument()).thenReturn(pathValue);
201 final ListenerAdapter listener = Notificator.createListener(path, this.identifier, NotificationOutputType.XML);
203 final List<Entry<String, List<String>>> list = new ArrayList<>();
204 final List<String> time = new ArrayList<>();
205 time.add("2014-10-25T10:02:00Z");
206 final Entry<String, List<String>> entry = new SimpleImmutableEntry<>("start-time", time);
211 ArrayList<DataTreeCandidate> candidates = new ArrayList<DataTreeCandidate>(0);
212 Instant startOrig = listener.getStart();
213 Assert.assertNotNull(startOrig);
214 listener.onDataTreeChanged(candidates);
216 startOrig = listener.getStart();
217 Assert.assertNull(startOrig);
220 private void subscribe(final List<Entry<String, List<String>>> entries) {
221 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
222 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
223 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + this.identifier);
224 Mockito.when(this.uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
225 final Set<Entry<String, List<String>>> set = new HashSet<>();
226 for (final Entry<String, List<String>> entry : entries) {
229 Mockito.when(map.entrySet()).thenReturn(set);
230 RestconfImpl.getInstance().subscribeToStream(this.identifier, this.uriInfo);