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;
20 import java.util.HashMap;
22 import javax.ws.rs.core.MultivaluedHashMap;
23 import javax.ws.rs.core.UriBuilder;
24 import javax.ws.rs.core.UriInfo;
25 import org.junit.AfterClass;
26 import org.junit.Before;
27 import org.junit.BeforeClass;
28 import org.junit.Test;
29 import org.junit.runner.RunWith;
30 import org.mockito.Mock;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.opendaylight.mdsal.common.api.CommitInfo;
33 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
34 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
35 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
36 import org.opendaylight.mdsal.dom.api.DOMNotificationService;
37 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
38 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
39 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
40 import org.opendaylight.restconf.common.util.SimpleUriInfo;
41 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
42 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
43 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
44 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
45 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
46 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
47 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
48 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
49 import org.opendaylight.yangtools.concepts.ListenerRegistration;
50 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
51 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
53 @RunWith(MockitoJUnitRunner.StrictStubs.class)
54 public class RestconfStreamsSubscriptionServiceImplTest {
56 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
57 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
60 private DOMDataBroker dataBroker;
62 private UriInfo uriInfo;
64 private DOMNotificationService notificationService;
66 private Configuration configurationWs;
67 private Configuration configurationSse;
69 private SchemaContextHandler schemaHandler;
72 public void setUp() throws FileNotFoundException, URISyntaxException {
73 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
74 doReturn(wTx).when(dataBroker).newWriteOnlyTransaction();
75 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
77 schemaHandler = new SchemaContextHandler(dataBroker, mock(DOMSchemaService.class));
79 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
80 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
81 .registerDataTreeChangeListener(any(), any());
83 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
84 .when(dataBroker).getExtensions();
86 doReturn(new MultivaluedHashMap<>()).when(uriInfo).getQueryParameters();
87 doReturn(new LocalUriInfo().getBaseUriBuilder()).when(uriInfo).getBaseUriBuilder();
88 doReturn(new URI("http://127.0.0.1/" + URI)).when(uriInfo).getAbsolutePath();
89 schemaHandler.onModelContextUpdated(
90 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
91 configurationWs = new Configuration(0, 100, 10, false);
92 configurationSse = new Configuration(0, 100, 10, true);
95 private static class LocalUriInfo extends SimpleUriInfo {
101 public URI getBaseUri() {
102 return UriBuilder.fromUri("http://localhost:8181").build();
107 public static void setUpBeforeTest() {
108 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
109 final ListenerAdapter adapter = mock(ListenerAdapter.class);
110 final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
111 doReturn(yiid).when(adapter).getPath();
112 doReturn("JSON").when(adapter).getOutputType();
113 listenersByStreamNameSetter.put(
114 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
116 ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
120 public static void setUpAfterTest() {
121 ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
125 public void testSubscribeToStreamSSE() {
126 ListenersBroker.getInstance().registerDataChangeListener(
127 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
128 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
129 NotificationOutputType.XML);
130 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
131 new RestconfStreamsSubscriptionServiceImpl(this.dataBroker, this.notificationService,
132 this.schemaHandler, this.configurationSse);
133 final NormalizedNodeContext response = streamsSubscriptionService
135 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
137 assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
138 + "/" + RestconfConstants.NOTIF
139 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
140 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
144 public void testSubscribeToStreamWS() {
145 ListenersBroker.getInstance().registerDataChangeListener(
146 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
147 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
148 NotificationOutputType.XML);
149 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
150 new RestconfStreamsSubscriptionServiceImpl(this.dataBroker, this.notificationService,
151 this.schemaHandler, this.configurationWs);
152 final NormalizedNodeContext response = streamsSubscriptionService
154 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
156 assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
157 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
158 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
161 @Test(expected = RestconfDocumentedException.class)
162 public void testSubscribeToStreamMissingDatastoreInPath() {
163 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
164 new RestconfStreamsSubscriptionServiceImpl(this.dataBroker, this.notificationService,
165 this.schemaHandler, this.configurationWs);
166 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
169 @Test(expected = RestconfDocumentedException.class)
170 public void testSubscribeToStreamMissingScopeInPath() {
171 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
172 new RestconfStreamsSubscriptionServiceImpl(this.dataBroker, this.notificationService,
173 this.schemaHandler, this.configurationWs);
174 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",