Use SchemaContextHandler non-statically
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / 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.nb.rfc8040.rests.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.net.URI;
20 import java.util.Collections;
21 import java.util.HashMap;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Set;
27 import java.util.concurrent.ConcurrentHashMap;
28 import javax.ws.rs.core.MultivaluedMap;
29 import javax.ws.rs.core.UriBuilder;
30 import javax.ws.rs.core.UriInfo;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.MockitoAnnotations;
38 import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
39 import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
40 import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
41 import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
42 import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
43 import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
44 import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
45 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
46 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
47 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
48 import org.opendaylight.restconf.common.util.SimpleUriInfo;
49 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
50 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
51 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
52 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
53 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
54 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
55 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.Notificator;
56 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
57 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
58 import org.opendaylight.yangtools.concepts.ListenerRegistration;
59 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
60
61 @SuppressWarnings("deprecation")
62 public class RestconfStreamsSubscriptionServiceImplTest {
63
64     private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
65             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
66     private static Field listenersByStreamName;
67
68     @Mock
69     private DOMDataBrokerHandler dataBrokerHandler;
70     @Mock
71     private UriInfo uriInfo;
72     @Mock
73     private NotificationServiceHandler notificationServiceHandler;
74
75     private TransactionChainHandler transactionHandler;
76     private SchemaContextHandler schemaHandler;
77
78     @SuppressWarnings("unchecked")
79     @Before
80     public void setUp() throws Exception {
81         MockitoAnnotations.initMocks(this);
82
83         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
84         final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
85         Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
86         final DOMDataReadWriteTransaction rwTx = Mockito.mock(DOMDataReadWriteTransaction.class);
87         final CheckedFuture<Boolean, ReadFailedException> checkedFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
88         Mockito.when(rwTx.exists(Mockito.any(), Mockito.any())).thenReturn(checkedFuture);
89         final CheckedFuture<Void, TransactionCommitFailedException> checkedFutureEmpty =
90                 Futures.immediateCheckedFuture(null);
91         Mockito.when(rwTx.submit()).thenReturn(checkedFutureEmpty);
92         Mockito.when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
93         final CheckedFuture<Void, TransactionCommitFailedException> checked = mock(CheckedFuture.class);
94         Mockito.when(wTx.submit()).thenReturn(checked);
95         Mockito.when(checked.checkedGet()).thenReturn(null);
96
97         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
98         doReturn(domTx).when(dataBroker).createTransactionChain(any());
99
100         transactionHandler = new TransactionChainHandler(dataBroker);
101         schemaHandler = SchemaContextHandler.newInstance(transactionHandler, Mockito.mock(DOMSchemaService.class));
102
103         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
104         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
105                 .registerDataTreeChangeListener(any(), any());
106
107         doReturn(Collections.singletonMap(DOMDataTreeChangeService.class, dataTreeChangeService))
108                 .when(dataBroker).getSupportedExtensions();
109
110         doReturn(dataBroker).when(this.dataBrokerHandler).get();
111
112         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
113         final Set<Entry<String, List<String>>> set = new HashSet<>();
114         Mockito.when(map.entrySet()).thenReturn(set);
115         Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
116         final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
117         Mockito.when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
118         Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
119         this.schemaHandler.onGlobalContextUpdated(
120                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
121     }
122
123     private static class LocalUriInfo extends SimpleUriInfo {
124
125         LocalUriInfo() {
126             super("/");
127         }
128
129         @Override
130         public URI getBaseUri() {
131             return UriBuilder.fromUri("http://localhost:8181").build();
132         }
133     }
134
135     @BeforeClass
136     public static void setUpBeforeTest() throws Exception {
137         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
138         final ListenerAdapter adapter = mock(ListenerAdapter.class);
139         listenersByStreamNameSetter.put(
140                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
141                 adapter);
142         listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
143
144         listenersByStreamName.setAccessible(true);
145         listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
146     }
147
148     @AfterClass
149     public static void setUpAfterTest() throws Exception {
150         listenersByStreamName.set(Notificator.class, null);
151         listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
152         listenersByStreamName.setAccessible(false);
153     }
154
155     @Test
156     public void testSubscribeToStream() throws Exception {
157         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
158         Notificator.createListener(
159                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
160                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
161                 NotificationOutputType.XML);
162         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
163         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
164                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
165                         this.schemaHandler, this.transactionHandler);
166         final NormalizedNodeContext response = streamsSubscriptionService
167                 .subscribeToStream(
168                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
169                         this.uriInfo);
170         assertEquals(
171                 "ws://localhost:8181/data-change-event-subscription"
172                         + "/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
173             response.getNewHeaders().get("Location").toString());
174     }
175
176     @Test(expected = RestconfDocumentedException.class)
177     public void testSubscribeToStreamMissingDatastoreInPath() {
178         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
179         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
180         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
181                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
182                         this.schemaHandler, this.transactionHandler);
183         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
184     }
185
186     @Test(expected = RestconfDocumentedException.class)
187     public void testSubscribeToStreamMissingScopeInPath() {
188         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
189         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
190         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
191                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
192                         this.schemaHandler, this.transactionHandler);
193         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
194                 this.uriInfo);
195     }
196
197 }