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