c964ba36d33fabfbaae94fdf8702089b6ceafc36
[netconf.git] / restconf / sal-rest-connector / src / test / java / org / opendaylight / restconf / restful / services / impl / RestconfStreamsSubscriptionServiceImplTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.restconf.restful.services.impl;
10
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;
19 import java.util.Map;
20 import java.util.Map.Entry;
21 import java.util.Set;
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;
44
45 public class RestconfStreamsSubscriptionServiceImplTest {
46
47     private static final String uri = "/restconf/17/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
48             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
49     private static Field listenersByStreamName;
50
51     @Mock
52     private DOMDataBrokerHandler dataBrokerHandler;
53     @Mock
54     private UriInfo uriInfo;
55     @Mock
56     private NotificationServiceHandler notificationServiceHandler;
57
58     private final SchemaContextHandler schemaHandler = new SchemaContextHandler();
59
60     @Before
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"));
72     }
73
74     @BeforeClass
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",
81                 adapter);
82         listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
83
84         listenersByStreamName.setAccessible(true);
85         listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
86     }
87
88     @AfterClass
89     public static void setUpAfterTest() throws Exception {
90         listenersByStreamName.set(Notificator.class, null);
91         listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
92         listenersByStreamName.setAccessible(false);
93     }
94
95     @Test
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,
101                         this.schemaHandler);
102         final NormalizedNodeContext response = streamsSubscriptionService
103                 .subscribeToStream(
104                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
105                         this.uriInfo);
106         assertEquals(
107                 "ws://:8181/data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
108                 response.getNewHeaders().get("Location").toString());
109     }
110
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,
117                         this.schemaHandler);
118         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
119     }
120
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,
127                         this.schemaHandler);
128         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
129                 this.uriInfo);
130     }
131
132 }