6b273d2a34b3ae6d6acadb2804c1e280069931c1
[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
16 import com.google.common.collect.ImmutableClassToInstanceMap;
17 import java.io.FileNotFoundException;
18 import java.net.URI;
19 import java.net.URISyntaxException;
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 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.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.Mockito;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.opendaylight.mdsal.common.api.CommitInfo;
39 import org.opendaylight.mdsal.dom.api.DOMDataBroker;
40 import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
41 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
42 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
43 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
44 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
45 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
46 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
47 import org.opendaylight.restconf.common.util.SimpleUriInfo;
48 import org.opendaylight.restconf.nb.rfc8040.TestRestconfUtils;
49 import org.opendaylight.restconf.nb.rfc8040.handlers.DOMDataBrokerHandler;
50 import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
51 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
52 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
53 import org.opendaylight.restconf.nb.rfc8040.streams.Configuration;
54 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
55 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
56 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
57 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
58 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
59 import org.opendaylight.yangtools.concepts.ListenerRegistration;
60 import org.opendaylight.yangtools.yang.common.QName;
61 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
62 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
63
64 @RunWith(MockitoJUnitRunner.StrictStubs.class)
65 public class RestconfStreamsSubscriptionServiceImplTest {
66
67     private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
68             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
69
70     @Mock
71     private DOMDataBrokerHandler dataBrokerHandler;
72     @Mock
73     private UriInfo uriInfo;
74     @Mock
75     private NotificationServiceHandler notificationServiceHandler;
76
77     private Configuration configurationWs;
78     private Configuration configurationSse;
79
80     private TransactionChainHandler transactionHandler;
81     private SchemaContextHandler schemaHandler;
82
83     @SuppressWarnings("unchecked")
84     @Before
85     public void setUp() throws FileNotFoundException, URISyntaxException {
86         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
87         final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
88         when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
89         final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
90         doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
91         when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
92         doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
93
94         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
95         doReturn(domTx).when(dataBroker).createTransactionChain(any());
96
97         transactionHandler = new TransactionChainHandler(dataBroker);
98         schemaHandler = new SchemaContextHandler(transactionHandler, mock(DOMSchemaService.class));
99
100         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
101         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
102                 .registerDataTreeChangeListener(any(), any());
103
104         doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
105                 .when(dataBroker).getExtensions();
106
107         doReturn(dataBroker).when(this.dataBrokerHandler).get();
108
109         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
110         final Set<Entry<String, List<String>>> set = new HashSet<>();
111         when(map.entrySet()).thenReturn(set);
112         when(this.uriInfo.getQueryParameters()).thenReturn(map);
113         final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
114         when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
115         final URI uri = new URI("http://127.0.0.1/" + URI);
116         when(uriInfo.getAbsolutePath()).thenReturn(uri);
117         this.schemaHandler.onModelContextUpdated(
118                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
119         configurationWs = new Configuration(0, 100, 10, false);
120         configurationSse = new Configuration(0, 100, 10, true);
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() {
137         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
138         final ListenerAdapter adapter = mock(ListenerAdapter.class);
139         final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
140         final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
141         final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
142         Mockito.when(adapter.getPath()).thenReturn(yiid);
143         Mockito.when(adapter.getOutputType()).thenReturn("JSON");
144         Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
145         Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
146         listenersByStreamNameSetter.put(
147                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
148                 adapter);
149         ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
150     }
151
152     @AfterClass
153     public static void setUpAfterTest() {
154         ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
155     }
156
157     @Test
158     public void testSubscribeToStreamSSE() {
159         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
160         ListenersBroker.getInstance().registerDataChangeListener(
161                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
162                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
163                 NotificationOutputType.XML);
164         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
165                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
166                         this.schemaHandler, this.transactionHandler, configurationSse);
167         final NormalizedNodeContext response = streamsSubscriptionService
168                 .subscribeToStream(
169                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
170                         this.uriInfo);
171         assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
172                 + "/" + RestconfConstants.NOTIF
173                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
174                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
175     }
176
177     @Test
178     public void testSubscribeToStreamWS() {
179         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
180         ListenersBroker.getInstance().registerDataChangeListener(
181                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
182                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
183                 NotificationOutputType.XML);
184         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
185                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
186                         this.schemaHandler, this.transactionHandler, configurationWs);
187         final NormalizedNodeContext response = streamsSubscriptionService
188                 .subscribeToStream(
189                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
190                         this.uriInfo);
191         assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
192                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
193                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
194     }
195
196     @Test(expected = RestconfDocumentedException.class)
197     public void testSubscribeToStreamMissingDatastoreInPath() {
198         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
199         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
200                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
201                         this.schemaHandler, this.transactionHandler, configurationWs);
202         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
203     }
204
205     @Test(expected = RestconfDocumentedException.class)
206     public void testSubscribeToStreamMissingScopeInPath() {
207         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
208         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
209                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
210                         this.schemaHandler, this.transactionHandler, configurationWs);
211         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
212                 this.uriInfo);
213     }
214
215 }