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