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
9 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
11 import static org.junit.Assert.assertEquals;
12 import static org.mockito.Matchers.any;
13 import static org.mockito.Mockito.doReturn;
14 import static org.mockito.Mockito.mock;
16 import com.google.common.util.concurrent.CheckedFuture;
17 import com.google.common.util.concurrent.Futures;
18 import java.lang.reflect.Field;
20 import java.util.HashMap;
21 import java.util.HashSet;
22 import java.util.List;
24 import java.util.Map.Entry;
26 import java.util.concurrent.ConcurrentHashMap;
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.mockito.Mock;
35 import org.mockito.Mockito;
36 import org.mockito.MockitoAnnotations;
37 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
38 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
39 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
43 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
44 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
45 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
46 import org.opendaylight.restconf.common.util.SimpleUriInfo;
47 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
48 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
49 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
50 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
51 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
52 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
53 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.Notificator;
54 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
55 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
56 import org.opendaylight.yangtools.concepts.ListenerRegistration;
57 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
59 @SuppressWarnings("deprecation")
60 public class RestconfStreamsSubscriptionServiceImplTest {
62 private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
63 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
64 private static Field listenersByStreamName;
67 private DOMDataBrokerHandler dataBrokerHandler;
69 private UriInfo uriInfo;
71 private NotificationServiceHandler notificationServiceHandler;
73 private TransactionChainHandler transactionHandler;
75 private SchemaContextHandler schemaHandler;
77 @SuppressWarnings("unchecked")
79 public void setUp() throws Exception {
80 MockitoAnnotations.initMocks(this);
82 final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
83 final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
84 Mockito.when(this.transactionHandler.get()).thenReturn(domTx);
85 Mockito.when(txHandler.get()).thenReturn(domTx);
86 final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
87 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
88 final DOMDataReadWriteTransaction rwTx = Mockito.mock(DOMDataReadWriteTransaction.class);
89 final CheckedFuture<Boolean, ReadFailedException> checkedFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
90 Mockito.when(rwTx.exists(Mockito.any(), Mockito.any())).thenReturn(checkedFuture);
91 final CheckedFuture<Void, TransactionCommitFailedException> checkedFutureEmpty =
92 Futures.immediateCheckedFuture(null);
93 Mockito.when(rwTx.submit()).thenReturn(checkedFutureEmpty);
94 Mockito.when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
95 final CheckedFuture<Void, TransactionCommitFailedException> checked = Mockito.mock(CheckedFuture.class);
96 Mockito.when(wTx.submit()).thenReturn(checked);
97 Mockito.when(checked.checkedGet()).thenReturn(null);
98 this.schemaHandler = new SchemaContextHandler(txHandler);
100 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
101 final ListenerRegistration<DOMDataChangeListener> listener = mock(ListenerRegistration.class);
102 doReturn(dataBroker).when(this.dataBrokerHandler).get();
103 doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
104 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
105 final Set<Entry<String, List<String>>> set = new HashSet<>();
106 Mockito.when(map.entrySet()).thenReturn(set);
107 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
108 final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
109 Mockito.when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
110 Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
111 this.schemaHandler.onGlobalContextUpdated(
112 YangParserTestUtils.parseYangSources(TestRestconfUtils.loadFiles("/notifications")));
115 private static class LocalUriInfo extends SimpleUriInfo {
122 public URI getBaseUri() {
123 return UriBuilder.fromUri("http://localhost:8181").build();
128 public static void setUpBeforeTest() throws Exception {
129 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
130 final ListenerAdapter adapter = mock(ListenerAdapter.class);
131 listenersByStreamNameSetter.put(
132 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
134 listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
136 listenersByStreamName.setAccessible(true);
137 listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
141 public static void setUpAfterTest() throws Exception {
142 listenersByStreamName.set(Notificator.class, null);
143 listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
144 listenersByStreamName.setAccessible(false);
148 public void testSubscribeToStream() throws Exception {
149 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
150 Notificator.createListener(
151 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
152 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
153 NotificationOutputType.XML);
154 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
155 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
156 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
157 this.schemaHandler, this.transactionHandler);
158 final NormalizedNodeContext response = streamsSubscriptionService
160 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
163 "ws://localhost:8181/data-change-event-subscription"
164 + "/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
165 response.getNewHeaders().get("Location").toString());
168 @Test(expected = RestconfDocumentedException.class)
169 public void testSubscribeToStreamMissingDatastoreInPath() {
170 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
171 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
172 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
173 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
174 this.schemaHandler, this.transactionHandler);
175 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
178 @Test(expected = RestconfDocumentedException.class)
179 public void testSubscribeToStreamMissingScopeInPath() {
180 final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
181 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
182 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
183 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
184 this.schemaHandler, this.transactionHandler);
185 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",