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 java.lang.reflect.Field;
16 import java.util.HashMap;
17 import java.util.HashSet;
18 import java.util.List;
20 import java.util.Map.Entry;
22 import java.util.concurrent.ConcurrentHashMap;
23 import javax.ws.rs.core.MultivaluedMap;
24 import javax.ws.rs.core.UriBuilder;
25 import javax.ws.rs.core.UriInfo;
26 import org.junit.AfterClass;
27 import org.junit.Before;
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30 import org.mockito.Mock;
31 import org.mockito.Mockito;
32 import org.mockito.MockitoAnnotations;
33 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
35 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
36 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
37 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
38 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
39 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
40 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
41 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
42 import org.opendaylight.restconf.handlers.SchemaContextHandler;
43 import org.opendaylight.yangtools.concepts.ListenerRegistration;
45 public class RestconfStreamsSubscriptionServiceImplTest {
47 private static final String uri = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
48 + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
49 private static Field listenersByStreamName;
52 private DOMDataBrokerHandler dataBrokerHandler;
54 private UriInfo uriInfo;
56 private NotificationServiceHandler notificationServiceHandler;
58 private final SchemaContextHandler schemaHandler = new SchemaContextHandler();
61 public void setUp() throws Exception {
62 MockitoAnnotations.initMocks(this);
63 final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
64 final ListenerRegistration<DOMDataChangeListener> listener = mock(ListenerRegistration.class);
65 doReturn(dataBroker).when(this.dataBrokerHandler).get();
66 doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
67 final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
68 final Set<Entry<String, List<String>>> set = new HashSet<>();
69 Mockito.when(map.entrySet()).thenReturn(set);
70 Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
71 this.schemaHandler.onGlobalContextUpdated(TestRestconfUtils.loadSchemaContext("/notifications"));
75 public static void setUpBeforeTest() throws Exception {
76 final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
77 final ListenerAdapter adapter = mock(ListenerAdapter.class);
78 doReturn(false).when(adapter).isListening();
79 listenersByStreamNameSetter.put(
80 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
82 listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
84 listenersByStreamName.setAccessible(true);
85 listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
89 public static void setUpAfterTest() throws Exception {
90 listenersByStreamName.set(Notificator.class, null);
91 listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
92 listenersByStreamName.setAccessible(false);
96 public void testSubscribeToStream() {
97 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
98 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
99 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
100 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
102 final NormalizedNodeContext response = streamsSubscriptionService
104 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
107 "ws://:8181/data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
108 response.getNewHeaders().get("Location").toString());
111 @Test(expected = RestconfDocumentedException.class)
112 public void testSubscribeToStreamMissingDatastoreInPath() {
113 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
114 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
115 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
116 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
118 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
121 @Test(expected = RestconfDocumentedException.class)
122 public void testSubscribeToStreamMissingScopeInPath() {
123 final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
124 doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
125 final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
126 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
128 streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",