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.restful.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;
15 import com.google.common.util.concurrent.CheckedFuture;
16 import com.google.common.util.concurrent.Futures;
17 import java.lang.reflect.Field;
18 import java.util.HashMap;
19 import java.util.HashSet;
20 import java.util.List;
22 import java.util.Map.Entry;
24 import java.util.concurrent.ConcurrentHashMap;
25 import javax.ws.rs.core.MultivaluedMap;
26 import javax.ws.rs.core.UriBuilder;
27 import javax.ws.rs.core.UriInfo;
28 import org.junit.AfterClass;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
37 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
38 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
39 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
40 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
41 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
42 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
43 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
44 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
45 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
46 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
47 import org.opendaylight.restconf.handlers.SchemaContextHandler;
48 import org.opendaylight.restconf.handlers.TransactionChainHandler;
49 import org.opendaylight.restconf.parser.IdentifierCodec;
50 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
51 import org.opendaylight.yangtools.concepts.ListenerRegistration;
53 public class RestconfStreamsSubscriptionServiceImplTest {
55 private static final String uri = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
56 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
57 private static Field listenersByStreamName;
60 private DOMDataBrokerHandler dataBrokerHandler;
62 private UriInfo uriInfo;
64 private NotificationServiceHandler notificationServiceHandler;
66 private TransactionChainHandler transactionHandler;
68 private SchemaContextHandler schemaHandler;
71 public void setUp() throws Exception {
72 MockitoAnnotations.initMocks(this);
74 final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
75 final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
76 Mockito.when(this.transactionHandler.get()).thenReturn(domTx);
77 Mockito.when(txHandler.get()).thenReturn(domTx);
78 final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
79 Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
80 final DOMDataReadWriteTransaction rwTx = Mockito.mock(DOMDataReadWriteTransaction.class);
81 final CheckedFuture checkedFuture = Futures.immediateCheckedFuture(true);
82 Mockito.when(rwTx.exists(Mockito.any(), Mockito.any())).thenReturn(checkedFuture);
83 final CheckedFuture checkedFutureEmpty = Futures.immediateCheckedFuture("");
84 Mockito.when(rwTx.submit()).thenReturn(checkedFutureEmpty);
85 Mockito.when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
86 final CheckedFuture checked = Mockito.mock(CheckedFuture.class);
87 Mockito.when(wTx.submit()).thenReturn(checked);
88 final Object valueObj = null;
89 Mockito.when(checked.checkedGet()).thenReturn(valueObj);
90 this.schemaHandler = new SchemaContextHandler(txHandler);
92 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
93 final ListenerRegistration<DOMDataChangeListener> listener = mock(ListenerRegistration.class);
94 doReturn(dataBroker).when(this.dataBrokerHandler).get();
95 doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
96 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
97 final Set<Entry<String, List<String>>> set = new HashSet<>();
98 Mockito.when(map.entrySet()).thenReturn(set);
99 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
100 this.schemaHandler.onGlobalContextUpdated(TestRestconfUtils.loadSchemaContext("/notifications"));
104 public static void setUpBeforeTest() throws Exception {
105 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
106 final ListenerAdapter adapter = mock(ListenerAdapter.class);
107 listenersByStreamNameSetter.put(
108 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
110 listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
112 listenersByStreamName.setAccessible(true);
113 listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
117 public static void setUpAfterTest() throws Exception {
118 listenersByStreamName.set(Notificator.class, null);
119 listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
120 listenersByStreamName.setAccessible(false);
124 public void testSubscribeToStream() throws Exception {
125 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
126 final ListenerAdapter createListener = Notificator.createListener(
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 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
131 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
132 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
133 this.schemaHandler, this.transactionHandler);
134 final NormalizedNodeContext response = streamsSubscriptionService
136 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
139 "ws://:8181/data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
140 response.getNewHeaders().get("Location").toString());
143 @Test(expected = RestconfDocumentedException.class)
144 public void testSubscribeToStreamMissingDatastoreInPath() {
145 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
146 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
147 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
148 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
149 this.schemaHandler, this.transactionHandler);
150 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
153 @Test(expected = RestconfDocumentedException.class)
154 public void testSubscribeToStreamMissingScopeInPath() {
155 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
156 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
157 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
158 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
159 this.schemaHandler, this.transactionHandler);
160 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",