Bug 5679 - ietf-yang-library module implemetation
[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 com.google.common.util.concurrent.CheckedFuture;
16 import java.lang.reflect.Field;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.List;
20 import java.util.Map;
21 import java.util.Map.Entry;
22 import java.util.Set;
23 import java.util.concurrent.ConcurrentHashMap;
24 import javax.ws.rs.core.MultivaluedMap;
25 import javax.ws.rs.core.UriBuilder;
26 import javax.ws.rs.core.UriInfo;
27 import org.junit.AfterClass;
28 import org.junit.Before;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31 import org.mockito.Mock;
32 import org.mockito.Mockito;
33 import org.mockito.MockitoAnnotations;
34 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
35 import org.opendaylight.controller.md.sal.dom.api.DOMDataChangeListener;
36 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
37 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
38 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
39 import org.opendaylight.netconf.sal.restconf.impl.NormalizedNodeContext;
40 import org.opendaylight.netconf.sal.restconf.impl.RestconfDocumentedException;
41 import org.opendaylight.netconf.sal.streams.listeners.ListenerAdapter;
42 import org.opendaylight.netconf.sal.streams.listeners.Notificator;
43 import org.opendaylight.restconf.handlers.DOMDataBrokerHandler;
44 import org.opendaylight.restconf.handlers.NotificationServiceHandler;
45 import org.opendaylight.restconf.handlers.SchemaContextHandler;
46 import org.opendaylight.restconf.handlers.TransactionChainHandler;
47 import org.opendaylight.yangtools.concepts.ListenerRegistration;
48
49 public class RestconfStreamsSubscriptionServiceImplTest {
50
51     private static final String uri = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
52             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
53     private static Field listenersByStreamName;
54
55     @Mock
56     private DOMDataBrokerHandler dataBrokerHandler;
57     @Mock
58     private UriInfo uriInfo;
59     @Mock
60     private NotificationServiceHandler notificationServiceHandler;
61
62     private SchemaContextHandler schemaHandler;
63
64     @Before
65     public void setUp() throws Exception {
66         MockitoAnnotations.initMocks(this);
67
68         final TransactionChainHandler txHandler = Mockito.mock(TransactionChainHandler.class);
69         final DOMTransactionChain domTx = Mockito.mock(DOMTransactionChain.class);
70         Mockito.when(txHandler.get()).thenReturn(domTx);
71         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
72         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
73         final CheckedFuture checked = Mockito.mock(CheckedFuture.class);
74         Mockito.when(wTx.submit()).thenReturn(checked);
75         final Object valueObj = null;
76         Mockito.when(checked.checkedGet()).thenReturn(valueObj);
77         this.schemaHandler = new SchemaContextHandler(txHandler);
78
79         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
80         final ListenerRegistration<DOMDataChangeListener> listener = mock(ListenerRegistration.class);
81         doReturn(dataBroker).when(this.dataBrokerHandler).get();
82         doReturn(listener).when(dataBroker).registerDataChangeListener(any(), any(), any(), any());
83         final MultivaluedMap<String, String> map = Mockito.mock(MultivaluedMap.class);
84         final Set<Entry<String, List<String>>> set = new HashSet<>();
85         Mockito.when(map.entrySet()).thenReturn(set);
86         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
87         this.schemaHandler.onGlobalContextUpdated(TestRestconfUtils.loadSchemaContext("/notifications"));
88     }
89
90     @BeforeClass
91     public static void setUpBeforeTest() throws Exception {
92         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
93         final ListenerAdapter adapter = mock(ListenerAdapter.class);
94         doReturn(false).when(adapter).isListening();
95         listenersByStreamNameSetter.put(
96                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
97                 adapter);
98         listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
99
100         listenersByStreamName.setAccessible(true);
101         listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
102     }
103
104     @AfterClass
105     public static void setUpAfterTest() throws Exception {
106         listenersByStreamName.set(Notificator.class, null);
107         listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
108         listenersByStreamName.setAccessible(false);
109     }
110
111     @Test
112     public void testSubscribeToStream() {
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         final NormalizedNodeContext response = streamsSubscriptionService
119                 .subscribeToStream(
120                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
121                         this.uriInfo);
122         assertEquals(
123                 "ws://:8181/data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
124                 response.getNewHeaders().get("Location").toString());
125     }
126
127     @Test(expected = RestconfDocumentedException.class)
128     public void testSubscribeToStreamMissingDatastoreInPath() {
129         final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
130         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
131         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
132                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
133                         this.schemaHandler);
134         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
135     }
136
137     @Test(expected = RestconfDocumentedException.class)
138     public void testSubscribeToStreamMissingScopeInPath() {
139         final UriBuilder uriBuilder = UriBuilder.fromUri(uri);
140         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
141         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
142                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
143                         this.schemaHandler);
144         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
145                 this.uriInfo);
146     }
147
148 }