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