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 static org.junit.Assert.assertNotNull;
11 import static org.junit.Assert.assertNull;
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
15 import java.io.FileNotFoundException;
16 import java.time.Instant;
17 import java.util.List;
19 import java.util.Map.Entry;
21 import javax.ws.rs.core.MultivaluedMap;
22 import javax.ws.rs.core.UriBuilder;
23 import javax.ws.rs.core.UriInfo;
24 import org.junit.AfterClass;
25 import org.junit.Before;
26 import org.junit.BeforeClass;
27 import org.junit.Test;
28 import org.junit.runner.RunWith;
29 import org.mockito.Mock;
30 import org.mockito.junit.MockitoJUnitRunner;
31 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
32 import org.opendaylight.netconf.sal.restconf.impl.BrokerFacade;
33 import org.opendaylight.netconf.sal.restconf.impl.ControllerContext;
34 import org.opendaylight.netconf.sal.restconf.impl.RestconfImpl;
35 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
36 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
37 import org.opendaylight.netconf.sal.streams.websockets.WebSocketServer;
38 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
39 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
40 import org.opendaylight.yangtools.yang.common.QName;
41 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
42 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
43 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
44 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
45 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
47 @RunWith(MockitoJUnitRunner.StrictStubs.class)
48 public class RestconfImplNotificationSubscribingTest {
50 private final String identifier = "data-change-event-subscription/datastore=OPERATIONAL/scope=ONE";
52 private static EffectiveModelContext schemaContext;
55 private BrokerFacade broker;
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 static void cleanUp() {
70 WebSocketServer.destroyInstance(); // NETCONF-604
75 controllerContext = TestRestconfUtils.newControllerContext(schemaContext);
76 restconfImpl = RestconfImpl.newInstance(broker, controllerContext);
78 final YangInstanceIdentifier path = mock(YangInstanceIdentifier.class);
79 Notificator.createListener(path, identifier, NotificationOutputType.XML, controllerContext);
83 public void startTimeTest() {
84 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z"))));
85 Notificator.removeAllListeners();
89 public void milisecsTest() {
90 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00.12345Z"))));
91 Notificator.removeAllListeners();
95 public void zonesPlusTest() {
96 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00+01:00"))));
97 Notificator.removeAllListeners();
101 public void zonesMinusTest() {
102 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00-01:00"))));
103 Notificator.removeAllListeners();
107 public void startAndStopTimeTest() {
108 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z")),
109 Map.entry("stop-time", List.of("2014-10-25T12:31:00Z"))));
110 Notificator.removeAllListeners();
113 @Test(expected = RestconfDocumentedException.class)
114 public void stopTimeTest() {
115 subscribe(Set.of(Map.entry("stop-time", List.of("2014-10-25T12:31:00Z"))));
116 Notificator.removeAllListeners();
119 @Test(expected = RestconfDocumentedException.class)
120 public void badParamTest() {
121 subscribe(Set.of(Map.entry("time", List.of("2014-10-25T12:31:00Z"))));
122 Notificator.removeAllListeners();
125 @Test(expected = IllegalArgumentException.class)
126 public void badValueTest() {
127 subscribe(Set.of(Map.entry("start-time", List.of("badvalue"))));
128 Notificator.removeAllListeners();
131 @Test(expected = IllegalArgumentException.class)
132 public void badZonesTest() {
133 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z+1:00"))));
134 Notificator.removeAllListeners();
137 @Test(expected = IllegalArgumentException.class)
138 public void badMilisecsTest() {
139 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00:0026Z"))));
140 Notificator.removeAllListeners();
144 public void onNotifiTest() throws Exception {
145 final YangInstanceIdentifier path = mock(YangInstanceIdentifier.class);
146 final PathArgument pathValue = NodeIdentifier.create(QName.create("module", "2016-12-14", "localName"));
147 final ListenerAdapter listener = Notificator.createListener(path, identifier, NotificationOutputType.XML,
150 subscribe(Set.of(Map.entry("start-time", List.of("2014-10-25T10:02:00Z"))));
152 Instant startOrig = listener.getStart();
153 assertNotNull(startOrig);
154 listener.onDataTreeChanged(List.of());
156 startOrig = listener.getStart();
157 assertNull(startOrig);
160 private void subscribe(final Set<Entry<String, List<String>>> entries) {
161 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
162 when(uriInfo.getQueryParameters()).thenReturn(map);
163 final UriBuilder uriBuilder = UriBuilder.fromPath("http://localhost:8181/" + identifier);
164 when(uriInfo.getAbsolutePathBuilder()).thenReturn(uriBuilder);
165 when(map.entrySet()).thenReturn(entries);
166 restconfImpl.subscribeToStream(identifier, uriInfo);