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