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.restconf.nb.rfc8040.rests.services.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertThrows;
12 import static org.mockito.ArgumentMatchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.collect.ImmutableClassToInstanceMap;
17 import java.io.FileNotFoundException;
19 import java.net.URISyntaxException;
21 import javax.ws.rs.core.MultivaluedHashMap;
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.mdsal.common.api.CommitInfo;
32 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
33 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
35 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
36 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
37 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
38 import org.opendaylight.restconf.common.util.SimpleUriInfo;
39 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
40 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
41 import org.opendaylight.restconf.nb.rfc8040.legacy.NormalizedNodePayload;
42 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
43 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
44 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
45 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
46 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
47 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
48 import org.opendaylight.yangtools.concepts.ListenerRegistration;
49 import org.opendaylight.yangtools.yang.common.ErrorTag;
50 import org.opendaylight.yangtools.yang.common.ErrorType;
51 import org.opendaylight.yangtools.yang.common.QName;
52 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
53 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
54 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
56 @RunWith(MockitoJUnitRunner.StrictStubs.class)
57 public class RestconfStreamsSubscriptionServiceImplTest {
59 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
60 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
63 private DOMDataBroker dataBroker;
65 private UriInfo uriInfo;
67 private DOMNotificationService notificationService;
69 private Configuration configurationWs;
70 private Configuration configurationSse;
72 private SchemaContextHandler schemaHandler;
75 public void setUp() throws FileNotFoundException, URISyntaxException {
76 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
77 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
78 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
80 schemaHandler = new SchemaContextHandler(dataBroker, mock(DOMSchemaService.class));
82 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
83 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
84 .registerDataTreeChangeListener(any(), any());
86 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
87 .when(dataBroker).getExtensions();
89 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
90 doReturn(new LocalUriInfo().getBaseUriBuilder()).when(uriInfo).getBaseUriBuilder();
91 doReturn(new URI("http://127.0.0.1/" + URI)).when(uriInfo).getAbsolutePath();
92 schemaHandler.onModelContextUpdated(
93 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
94 configurationWs = new Configuration(0, 100, 10, false);
95 configurationSse = new Configuration(0, 100, 10, true);
98 private static class LocalUriInfo extends SimpleUriInfo {
104 public URI getBaseUri() {
105 return UriBuilder.fromUri("http://localhost:8181").build();
110 public static void setUpBeforeTest() {
112 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
113 final ListenerAdapter adapter = new ListenerAdapter(YangInstanceIdentifier.create(new NodeIdentifier(
114 QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toaster"))),
115 name, NotificationOutputType.JSON);
116 ListenersBroker.getInstance().setDataChangeListeners(Map.of(name, adapter));
120 public static void setUpAfterTest() {
121 ListenersBroker.getInstance().setDataChangeListeners(Map.of());
125 public void testSubscribeToStreamSSE() {
126 ListenersBroker.getInstance().registerDataChangeListener(
127 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", schemaHandler.get()),
128 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
129 NotificationOutputType.XML);
130 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
131 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
132 schemaHandler, configurationSse);
133 final NormalizedNodePayload response = streamsSubscriptionService.subscribeToStream(
134 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", uriInfo);
135 assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
136 + "/" + RestconfConstants.NOTIF
137 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
138 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
142 public void testSubscribeToStreamWS() {
143 ListenersBroker.getInstance().registerDataChangeListener(
144 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", schemaHandler.get()),
145 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
146 NotificationOutputType.XML);
147 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
148 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
149 schemaHandler, configurationWs);
150 final NormalizedNodePayload response = streamsSubscriptionService.subscribeToStream(
151 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", uriInfo);
152 assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
153 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
154 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
158 public void testSubscribeToStreamMissingDatastoreInPath() {
159 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
160 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
161 schemaHandler, configurationWs);
162 final var errors = assertThrows(RestconfDocumentedException.class,
163 () -> streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", uriInfo))
165 assertEquals(1, errors.size());
166 final var error = errors.get(0);
167 assertEquals(ErrorType.APPLICATION, error.getErrorType());
168 assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
169 assertEquals("Bad type of notification of sal-remote", error.getErrorMessage());
173 public void testSubscribeToStreamMissingScopeInPath() {
174 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
175 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
176 schemaHandler, configurationWs);
177 final var errors = assertThrows(RestconfDocumentedException.class,
178 () -> streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
179 uriInfo)).getErrors();
180 assertEquals(1, errors.size());
181 final var error = errors.get(0);
182 assertEquals(ErrorType.APPLICATION, error.getErrorType());
183 assertEquals(ErrorTag.OPERATION_FAILED, error.getErrorTag());
184 assertEquals("Bad type of notification of sal-remote", error.getErrorMessage());