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