Update MRI projects for Aluminium
[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.io.FileNotFoundException;
19 import java.net.URI;
20 import java.net.URISyntaxException;
21 import java.util.Collections;
22 import java.util.HashMap;
23 import java.util.HashSet;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Map.Entry;
27 import java.util.Set;
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.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.listeners.ListenerAdapter;
54 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
55 import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
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.common.QName;
60 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
61 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
62
63 public class RestconfStreamsSubscriptionServiceImplTest {
64
65     private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
66             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
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 FileNotFoundException, URISyntaxException {
81         MockitoAnnotations.initMocks(this);
82
83         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
84         final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
85         when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
86         final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
87         when(rwTx.exists(any(), any())).thenReturn(immediateTrueFluentFuture());
88         doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
89         when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
90         doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
91
92         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
93         doReturn(domTx).when(dataBroker).createTransactionChain(any());
94
95         transactionHandler = new TransactionChainHandler(dataBroker);
96         schemaHandler = SchemaContextHandler.newInstance(transactionHandler, mock(DOMSchemaService.class));
97
98         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
99         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
100                 .registerDataTreeChangeListener(any(), any());
101
102         doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
103                 .when(dataBroker).getExtensions();
104
105         doReturn(dataBroker).when(this.dataBrokerHandler).get();
106
107         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
108         final Set<Entry<String, List<String>>> set = new HashSet<>();
109         when(map.entrySet()).thenReturn(set);
110         when(this.uriInfo.getQueryParameters()).thenReturn(map);
111         final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
112         when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
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     }
119
120     private static class LocalUriInfo extends SimpleUriInfo {
121
122         LocalUriInfo() {
123             super("/");
124         }
125
126         @Override
127         public URI getBaseUri() {
128             return UriBuilder.fromUri("http://localhost:8181").build();
129         }
130     }
131
132     @BeforeClass
133     public static void setUpBeforeTest() {
134         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
135         final ListenerAdapter adapter = mock(ListenerAdapter.class);
136         final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
137         final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
138         final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
139         Mockito.when(adapter.getPath()).thenReturn(yiid);
140         Mockito.when(adapter.getOutputType()).thenReturn("JSON");
141         Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
142         Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
143         listenersByStreamNameSetter.put(
144                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
145                 adapter);
146         ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
147     }
148
149     @AfterClass
150     public static void setUpAfterTest() {
151         ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
152     }
153
154     @Test
155     public void testSubscribeToStream() {
156         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
157         ListenersBroker.getInstance().registerDataChangeListener(
158                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
159                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
160                 NotificationOutputType.XML);
161         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
162         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
163                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
164                         this.schemaHandler, this.transactionHandler);
165         final NormalizedNodeContext response = streamsSubscriptionService
166                 .subscribeToStream(
167                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
168                         this.uriInfo);
169         assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
170                 + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
171                 + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
172     }
173
174     @Test(expected = RestconfDocumentedException.class)
175     public void testSubscribeToStreamMissingDatastoreInPath() {
176         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
177         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
178         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
179                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
180                         this.schemaHandler, this.transactionHandler);
181         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/scope=ONE", this.uriInfo);
182     }
183
184     @Test(expected = RestconfDocumentedException.class)
185     public void testSubscribeToStreamMissingScopeInPath() {
186         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
187         doReturn(uriBuilder).when(this.uriInfo).getAbsolutePathBuilder();
188         final RestconfStreamsSubscriptionServiceImpl streamsSubscriptionService =
189                 new RestconfStreamsSubscriptionServiceImpl(this.dataBrokerHandler, this.notificationServiceHandler,
190                         this.schemaHandler, this.transactionHandler);
191         streamsSubscriptionService.subscribeToStream("toaster:toaster/toasterStatus/datastore=OPERATIONAL",
192                 this.uriInfo);
193     }
194
195 }