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