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;
15 import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
17 import com.google.common.collect.ImmutableClassToInstanceMap;
18 import java.io.FileNotFoundException;
20 import java.net.URISyntaxException;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
26 import java.util.Map.Entry;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriBuilder;
30 import javax.ws.rs.core.UriInfo;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
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.listeners.ListenerAdapter;
54 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
55 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
56 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
57 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
59 import org.opendaylight.yangtools.yang.common.QName;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
63 public class RestconfStreamsSubscriptionServiceImplTest {
65 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
66 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
69 private DOMDataBrokerHandler dataBrokerHandler;
71 private UriInfo uriInfo;
73 private NotificationServiceHandler notificationServiceHandler;
75 private TransactionChainHandler transactionHandler;
76 private SchemaContextHandler schemaHandler;
78 @SuppressWarnings("unchecked")
80 public void setUp() throws FileNotFoundException, URISyntaxException {
81 MockitoAnnotations.initMocks(this);
83 final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
84 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
85 when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
86 final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
87 when(rwTx.exists(any(), any())).thenReturn(immediateTrueFluentFuture());
88 doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
89 when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
90 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
92 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
93 doReturn(domTx).when(dataBroker).createTransactionChain(any());
95 transactionHandler = new TransactionChainHandler(dataBroker);
96 schemaHandler = new SchemaContextHandler(transactionHandler, mock(DOMSchemaService.class));
98 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
99 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
100 .registerDataTreeChangeListener(any(), any());
102 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
103 .when(dataBroker).getExtensions();
105 doReturn(dataBroker).when(this.dataBrokerHandler).get();
107 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
108 final Set<Entry<String, List<String>>> set = new HashSet<>();
109 when(map.entrySet()).thenReturn(set);
110 when(this.uriInfo.getQueryParameters()).thenReturn(map);
111 final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
112 when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
113 when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
114 final URI uri = new URI("http://127.0.0.1/" + URI);
115 when(uriInfo.getAbsolutePath()).thenReturn(uri);
116 this.schemaHandler.onModelContextUpdated(
117 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
120 private static class LocalUriInfo extends SimpleUriInfo {
127 public URI getBaseUri() {
128 return UriBuilder.fromUri("http://localhost:8181").build();
133 public static void setUpBeforeTest() {
134 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
135 final ListenerAdapter adapter = mock(ListenerAdapter.class);
136 final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
137 final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
138 final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
139 Mockito.when(adapter.getPath()).thenReturn(yiid);
140 Mockito.when(adapter.getOutputType()).thenReturn("JSON");
141 Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
142 Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
143 listenersByStreamNameSetter.put(
144 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
146 ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
150 public static void setUpAfterTest() {
151 ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
155 public void testSubscribeToStream() {
156 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
157 ListenersBroker.getInstance().registerDataChangeListener(
158 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
159 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
160 NotificationOutputType.XML);
161 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
162 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
163 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
164 this.schemaHandler, this.transactionHandler);
165 final NormalizedNodeContext response = streamsSubscriptionService
167 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
169 assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
170 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
171 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
174 @Test(expected = RestconfDocumentedException.class)
175 public void testSubscribeToStreamMissingDatastoreInPath() {
176 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
177 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
178 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
179 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
180 this.schemaHandler, this.transactionHandler);
181 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
184 @Test(expected = RestconfDocumentedException.class)
185 public void testSubscribeToStreamMissingScopeInPath() {
186 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
187 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
188 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
189 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
190 this.schemaHandler, this.transactionHandler);
191 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",