Remove NotificationServiceHandler
[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.DOMNotificationService;
44 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
45 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
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.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 DOMNotificationService notificationService;
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         LocalUriInfo() {
124             super("/");
125         }
126
127         @Override
128         public URI getBaseUri() {
129             return UriBuilder.fromUri("http://localhost:8181").build();
130         }
131     }
132
133     @BeforeClass
134     public static void setUpBeforeTest() {
135         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
136         final ListenerAdapter adapter = mock(ListenerAdapter.class);
137         final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
138         Mockito.when(adapter.getPath()).thenReturn(yiid);
139         Mockito.when(adapter.getOutputType()).thenReturn("JSON");
140         listenersByStreamNameSetter.put(
141                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
142                 adapter);
143         ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
144     }
145
146     @AfterClass
147     public static void setUpAfterTest() {
148         ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
149     }
150
151     @Test
152     public void testSubscribeToStreamSSE() {
153         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
154         ListenersBroker.getInstance().registerDataChangeListener(
155                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
156                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
157                 NotificationOutputType.XML);
158         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
159                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationService,
160                         this.schemaHandler, this.transactionHandler, configurationSse);
161         final NormalizedNodeContext response = streamsSubscriptionService
162                 .subscribeToStream(
163                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
164                         this.uriInfo);
165         assertEquals("http://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
166                 + "/" + RestconfConstants.NOTIF
167                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
168                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
169     }
170
171     @Test
172     public void testSubscribeToStreamWS() {
173         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
174         ListenersBroker.getInstance().registerDataChangeListener(
175                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
176                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
177                 NotificationOutputType.XML);
178         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
179                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationService,
180                         this.schemaHandler, this.transactionHandler, configurationWs);
181         final NormalizedNodeContext response = streamsSubscriptionService
182                 .subscribeToStream(
183                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
184                         this.uriInfo);
185         assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
186                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
187                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
188     }
189
190     @Test(expected = RestconfDocumentedException.class)
191     public void testSubscribeToStreamMissingDatastoreInPath() {
192         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
193                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationService,
194                         this.schemaHandler, this.transactionHandler, configurationWs);
195         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
196     }
197
198     @Test(expected = RestconfDocumentedException.class)
199     public void testSubscribeToStreamMissingScopeInPath() {
200         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
201                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationService,
202                         this.schemaHandler, this.transactionHandler, configurationWs);
203         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
204                 this.uriInfo);
205     }
206 }