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;
14 import static org.mockito.Mockito.when;
16 import com.google.common.collect.ImmutableClassToInstanceMap;
17 import java.io.FileNotFoundException;
19 import java.net.URISyntaxException;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
25 import java.util.Map.Entry;
27 import javax.ws.rs.core.MultivaluedMap;
28 import javax.ws.rs.core.UriBuilder;
29 import javax.ws.rs.core.UriInfo;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.mdsal.common.api.CommitInfo;
39 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
42 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
43 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
44 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
45 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
46 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
47 import org.opendaylight.restconf.common.util.SimpleUriInfo;
48 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
49 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
50 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
51 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
52 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
53 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
54 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
55 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
56 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
57 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
58 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
64 @RunWith(MockitoJUnitRunner.StrictStubs.class)
65 public class RestconfStreamsSubscriptionServiceImplTest {
67 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
68 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
71 private DOMDataBrokerHandler dataBrokerHandler;
73 private UriInfo uriInfo;
75 private NotificationServiceHandler notificationServiceHandler;
77 private Configuration configurationWs;
78 private Configuration configurationSse;
80 private TransactionChainHandler transactionHandler;
81 private SchemaContextHandler schemaHandler;
83 @SuppressWarnings("unchecked")
85 public void setUp() throws FileNotFoundException, URISyntaxException {
86 final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
87 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
88 when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
89 final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
90 doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
91 when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
92 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
94 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
95 doReturn(domTx).when(dataBroker).createTransactionChain(any());
97 transactionHandler = new TransactionChainHandler(dataBroker);
98 schemaHandler = new SchemaContextHandler(transactionHandler, mock(DOMSchemaService.class));
100 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
101 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
102 .registerDataTreeChangeListener(any(), any());
104 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
105 .when(dataBroker).getExtensions();
107 doReturn(dataBroker).when(this.dataBrokerHandler).get();
109 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
110 final Set<Entry<String, List<String>>> set = new HashSet<>();
111 when(map.entrySet()).thenReturn(set);
112 when(this.uriInfo.getQueryParameters()).thenReturn(map);
113 final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
114 when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
115 final URI uri = new URI("http://127.0.0.1/" + URI);
116 when(uriInfo.getAbsolutePath()).thenReturn(uri);
117 this.schemaHandler.onModelContextUpdated(
118 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
119 configurationWs = new Configuration(0, 100, 10, false);
120 configurationSse = new Configuration(0, 100, 10, true);
123 private static class LocalUriInfo extends SimpleUriInfo {
130 public URI getBaseUri() {
131 return UriBuilder.fromUri("http://localhost:8181").build();
136 public static void setUpBeforeTest() {
137 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
138 final ListenerAdapter adapter = mock(ListenerAdapter.class);
139 final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
140 final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
141 final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
142 Mockito.when(adapter.getPath()).thenReturn(yiid);
143 Mockito.when(adapter.getOutputType()).thenReturn("JSON");
144 Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
145 Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
146 listenersByStreamNameSetter.put(
147 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
149 ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
153 public static void setUpAfterTest() {
154 ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
158 public void testSubscribeToStreamSSE() {
159 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
160 ListenersBroker.getInstance().registerDataChangeListener(
161 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
162 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
163 NotificationOutputType.XML);
164 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
165 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
166 this.schemaHandler, this.transactionHandler, configurationSse);
167 final NormalizedNodeContext response = streamsSubscriptionService
169 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
171 assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
172 + "/" + RestconfConstants.NOTIF
173 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
174 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
178 public void testSubscribeToStreamWS() {
179 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
180 ListenersBroker.getInstance().registerDataChangeListener(
181 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
182 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
183 NotificationOutputType.XML);
184 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
185 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
186 this.schemaHandler, this.transactionHandler, configurationWs);
187 final NormalizedNodeContext response = streamsSubscriptionService
189 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
191 assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
192 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
193 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
196 @Test(expected = RestconfDocumentedException.class)
197 public void testSubscribeToStreamMissingDatastoreInPath() {
198 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
199 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
200 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
201 this.schemaHandler, this.transactionHandler, configurationWs);
202 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
205 @Test(expected = RestconfDocumentedException.class)
206 public void testSubscribeToStreamMissingScopeInPath() {
207 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
208 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
209 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
210 this.schemaHandler, this.transactionHandler, configurationWs);
211 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",