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.mockito.ArgumentMatchers.any;
12 import static org.mockito.Mockito.doReturn;
13 import static org.mockito.Mockito.mock;
15 import com.google.common.collect.ImmutableClassToInstanceMap;
16 import java.io.FileNotFoundException;
18 import java.net.URISyntaxException;
19 import java.util.Collections;
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.QName;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
52 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
54 @RunWith(MockitoJUnitRunner.StrictStubs.class)
55 public class RestconfStreamsSubscriptionServiceImplTest {
57 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
58 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
61 private DOMDataBroker dataBroker;
63 private UriInfo uriInfo;
65 private DOMNotificationService notificationService;
67 private Configuration configurationWs;
68 private Configuration configurationSse;
70 private SchemaContextHandler schemaHandler;
73 public void setUp() throws FileNotFoundException, URISyntaxException {
74 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
75 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
76 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
78 schemaHandler = new SchemaContextHandler(dataBroker, mock(DOMSchemaService.class));
80 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
81 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
82 .registerDataTreeChangeListener(any(), any());
84 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
85 .when(dataBroker).getExtensions();
87 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
88 doReturn(new LocalUriInfo().getBaseUriBuilder()).when(uriInfo).getBaseUriBuilder();
89 doReturn(new URI("http://127.0.0.1/" + URI)).when(uriInfo).getAbsolutePath();
90 schemaHandler.onModelContextUpdated(
91 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
92 configurationWs = new Configuration(0, 100, 10, false);
93 configurationSse = new Configuration(0, 100, 10, true);
96 private static class LocalUriInfo extends SimpleUriInfo {
102 public URI getBaseUri() {
103 return UriBuilder.fromUri("http://localhost:8181").build();
108 public static void setUpBeforeTest() {
110 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
111 final ListenerAdapter adapter = new ListenerAdapter(YangInstanceIdentifier.create(new NodeIdentifier(
112 QName.create("http://netconfcentral.org/ns/toaster", "2009-11-20", "toaster"))),
113 name, NotificationOutputType.JSON);
114 ListenersBroker.getInstance().setDataChangeListeners(Map.of(name, adapter));
118 public static void setUpAfterTest() {
119 ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
123 public void testSubscribeToStreamSSE() {
124 ListenersBroker.getInstance().registerDataChangeListener(
125 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", schemaHandler.get()),
126 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
127 NotificationOutputType.XML);
128 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
129 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
130 schemaHandler, configurationSse);
131 final NormalizedNodePayload response = streamsSubscriptionService.subscribeToStream(
132 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", uriInfo);
133 assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
134 + "/" + RestconfConstants.NOTIF
135 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
136 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
140 public void testSubscribeToStreamWS() {
141 ListenersBroker.getInstance().registerDataChangeListener(
142 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", schemaHandler.get()),
143 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
144 NotificationOutputType.XML);
145 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
146 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
147 schemaHandler, configurationWs);
148 final NormalizedNodePayload response = streamsSubscriptionService.subscribeToStream(
149 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", uriInfo);
150 assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
151 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
152 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
155 @Test(expected = RestconfDocumentedException.class)
156 public void testSubscribeToStreamMissingDatastoreInPath() {
157 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
158 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
159 schemaHandler, configurationWs);
160 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", uriInfo);
163 @Test(expected = RestconfDocumentedException.class)
164 public void testSubscribeToStreamMissingScopeInPath() {
165 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
166 new RestconfStreamsSubscriptionServiceImpl(dataBroker, notificationService,
167 schemaHandler, configurationWs);
168 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",