Added full YIID to ietf-restconf-monitoring DS
[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.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
62
63 @RunWith(MockitoJUnitRunner.StrictStubs.class)
64 public class RestconfStreamsSubscriptionServiceImplTest {
65
66     private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
67             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
68
69     @Mock
70     private DOMDataBrokerHandler dataBrokerHandler;
71     @Mock
72     private UriInfo uriInfo;
73     @Mock
74     private NotificationServiceHandler notificationServiceHandler;
75
76     private Configuration configurationWs;
77     private Configuration configurationSse;
78
79     private TransactionChainHandler transactionHandler;
80     private SchemaContextHandler schemaHandler;
81
82     @SuppressWarnings("unchecked")
83     @Before
84     public void setUp() throws FileNotFoundException, URISyntaxException {
85         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
86         final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
87         when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
88         final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
89         doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
90         when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
91         doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
92
93         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
94         doReturn(domTx).when(dataBroker).createTransactionChain(any());
95
96         transactionHandler = new TransactionChainHandler(dataBroker);
97         schemaHandler = new SchemaContextHandler(transactionHandler, mock(DOMSchemaService.class));
98
99         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
100         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
101                 .registerDataTreeChangeListener(any(), any());
102
103         doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
104                 .when(dataBroker).getExtensions();
105
106         doReturn(dataBroker).when(this.dataBrokerHandler).get();
107
108         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
109         final Set<Entry<String, List<String>>> set = new HashSet<>();
110         when(map.entrySet()).thenReturn(set);
111         when(this.uriInfo.getQueryParameters()).thenReturn(map);
112         final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
113         when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
114         final URI uri = new URI("http://127.0.0.1/" + URI);
115         when(uriInfo.getAbsolutePath()).thenReturn(uri);
116         this.schemaHandler.onModelContextUpdated(
117                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
118         configurationWs = new Configuration(0, 100, 10, false);
119         configurationSse = new Configuration(0, 100, 10, true);
120     }
121
122     private static class LocalUriInfo extends SimpleUriInfo {
123
124         LocalUriInfo() {
125             super("/");
126         }
127
128         @Override
129         public URI getBaseUri() {
130             return UriBuilder.fromUri("http://localhost:8181").build();
131         }
132     }
133
134     @BeforeClass
135     public static void setUpBeforeTest() {
136         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
137         final ListenerAdapter adapter = mock(ListenerAdapter.class);
138         final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
139         Mockito.when(adapter.getPath()).thenReturn(yiid);
140         Mockito.when(adapter.getOutputType()).thenReturn("JSON");
141         listenersByStreamNameSetter.put(
142                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
143                 adapter);
144         ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
145     }
146
147     @AfterClass
148     public static void setUpAfterTest() {
149         ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
150     }
151
152     @Test
153     public void testSubscribeToStreamSSE() {
154         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
155         ListenersBroker.getInstance().registerDataChangeListener(
156                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
157                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
158                 NotificationOutputType.XML);
159         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
160                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
161                         this.schemaHandler, this.transactionHandler, configurationSse);
162         final NormalizedNodeContext response = streamsSubscriptionService
163                 .subscribeToStream(
164                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
165                         this.uriInfo);
166         assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
167                 + "/" + RestconfConstants.NOTIF
168                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
169                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
170     }
171
172     @Test
173     public void testSubscribeToStreamWS() {
174         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
175         ListenersBroker.getInstance().registerDataChangeListener(
176                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
177                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
178                 NotificationOutputType.XML);
179         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
180                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
181                         this.schemaHandler, this.transactionHandler, configurationWs);
182         final NormalizedNodeContext response = streamsSubscriptionService
183                 .subscribeToStream(
184                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
185                         this.uriInfo);
186         assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
187                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
188                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
189     }
190
191     @Test(expected = RestconfDocumentedException.class)
192     public void testSubscribeToStreamMissingDatastoreInPath() {
193         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
194         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
195                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
196                         this.schemaHandler, this.transactionHandler, configurationWs);
197         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
198     }
199
200     @Test(expected = RestconfDocumentedException.class)
201     public void testSubscribeToStreamMissingScopeInPath() {
202         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
203         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
204                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
205                         this.schemaHandler, this.transactionHandler, configurationWs);
206         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
207                 this.uriInfo);
208     }
209
210 }