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.parser.IdentifierCodec;
56 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
57 import org.opendaylight.yangtools.concepts.ListenerRegistration;
58 import org.opendaylight.yangtools.yang.common.QName;
59 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
60 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
62 public class RestconfStreamsSubscriptionServiceImplTest {
64 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
65 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
68 private DOMDataBrokerHandler dataBrokerHandler;
70 private UriInfo uriInfo;
72 private NotificationServiceHandler notificationServiceHandler;
74 private TransactionChainHandler transactionHandler;
75 private SchemaContextHandler schemaHandler;
77 @SuppressWarnings("unchecked")
79 public void setUp() throws FileNotFoundException, URISyntaxException {
80 MockitoAnnotations.initMocks(this);
82 final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
83 final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
84 when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
85 final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
86 when(rwTx.exists(any(), any())).thenReturn(immediateTrueFluentFuture());
87 doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
88 when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
89 doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
91 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
92 doReturn(domTx).when(dataBroker).createTransactionChain(any());
94 transactionHandler = new TransactionChainHandler(dataBroker);
95 schemaHandler = SchemaContextHandler.newInstance(transactionHandler, mock(DOMSchemaService.class));
97 DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
98 doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
99 .registerDataTreeChangeListener(any(), any());
101 doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
102 .when(dataBroker).getExtensions();
104 doReturn(dataBroker).when(this.dataBrokerHandler).get();
106 final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
107 final Set<Entry<String, List<String>>> set = new HashSet<>();
108 when(map.entrySet()).thenReturn(set);
109 when(this.uriInfo.getQueryParameters()).thenReturn(map);
110 final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
111 when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
112 when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
113 final URI uri = new URI("http://127.0.0.1/" + URI);
114 when(uriInfo.getAbsolutePath()).thenReturn(uri);
115 this.schemaHandler.onGlobalContextUpdated(
116 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
119 private static class LocalUriInfo extends SimpleUriInfo {
126 public URI getBaseUri() {
127 return UriBuilder.fromUri("http://localhost:8181").build();
132 public static void setUpBeforeTest() {
133 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
134 final ListenerAdapter adapter = mock(ListenerAdapter.class);
135 final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
136 final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
137 final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
138 Mockito.when(adapter.getPath()).thenReturn(yiid);
139 Mockito.when(adapter.getOutputType()).thenReturn("JSON");
140 Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
141 Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
142 listenersByStreamNameSetter.put(
143 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
145 ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
149 public static void setUpAfterTest() {
150 ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
154 public void testSubscribeToStream() {
155 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
156 ListenersBroker.getInstance().registerDataChangeListener(
157 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
158 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
159 NotificationOutputType.XML);
160 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
161 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
162 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
163 this.schemaHandler, this.transactionHandler);
164 final NormalizedNodeContext response = streamsSubscriptionService
166 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
169 "ws://localhost:8181/data-change-event-subscription"
170 + "/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
171 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",