Bug 6935 - RPC in latest draft doesn't work - problem of parsing
[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.Map;
18 import java.util.concurrent.ConcurrentHashMap;
19 import javax.ws.rs.core.Response;
20 import javax.ws.rs.core.UriBuilder;
21 import javax.ws.rs.core.UriInfo;
22 import org.junit.AfterClass;
23 import org.junit.Before;
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26 import org.mockito.Mock;
27 import org.mockito.MockitoAnnotations;
28 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
29 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
30 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
31 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
32 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
33 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
34 import org.opendaylight.yangtools.concepts.ListenerRegistration;
35
36 public class RestconfStreamsSubscriptionServiceImplTest {
37
38     private static final String uri = "/restconf/17/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
39             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
40     private static Field listenersByStreamName;
41
42     @Mock
43     private DOMDataBrokerHandler dataBrokerHandler;
44     @Mock
45     private UriInfo uriInfo;
46
47     @Before
48     public void setUp() {
49         MockitoAnnotations.initMocks(this);
50         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
51         final ListenerRegistration<DOMDataChangeListener> listener = mock(ListenerRegistration.class);
52         doReturn(dataBroker).when(this.dataBrokerHandler).get();
53         doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
54     }
55
56     @BeforeClass
57     public static void setUpBeforeTest() throws Exception {
58         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
59         final ListenerAdapter adapter = mock(ListenerAdapter.class);
60         doReturn(false).when(adapter).isListening();
61         listenersByStreamNameSetter.put("toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", adapter);
62         listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
63
64         listenersByStreamName.setAccessible(true);
65         listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
66     }
67
68     @AfterClass
69     public static void setUpAfterTest() throws Exception {
70         listenersByStreamName.set(Notificator.class, null);
71         listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
72         listenersByStreamName.setAccessible(false);
73     }
74
75     @Test
76     public void testSubscribeToStream() {
77         final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
78         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
79         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
80                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler);
81         final Response response = streamsSubscriptionService
82                 .subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE", this.uriInfo);
83         assertEquals(200, response.getStatus());
84         assertEquals("ws://:8181/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
85                 response.getHeaderString("Location"));
86     }
87
88     @Test(expected = RestconfDocumentedException.class)
89     public void testSubscribeToStreamMissingDatastoreInPath() {
90         final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
91         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
92         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
93                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler);
94         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
95     }
96
97     @Test(expected = RestconfDocumentedException.class)
98     public void testSubscribeToStreamMissingScopeInPath() {
99         final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
100         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
101         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
102                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler);
103         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
104                 this.uriInfo);
105     }
106
107 }