Update MRI projects for Aluminium
[netconf.git] / restconf / restconf-nb-rfc8040 / src / test / java / org / opendaylight / restconf / nb / rfc8040 / rests / services / impl / RestconfStreamsSubscriptionServiceImplTest.java
index 55ca771e879c769eec98720cf7c37a324e89af10..f535851fd5e6dc65bb71a7baa0c4cdb33484391e 100644 (file)
@@ -5,18 +5,19 @@
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-
 package org.opendaylight.restconf.nb.rfc8040.rests.services.impl;
 
 import static org.junit.Assert.assertEquals;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doReturn;
 import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.opendaylight.yangtools.util.concurrent.FluentFutures.immediateTrueFluentFuture;
 
-import com.google.common.util.concurrent.CheckedFuture;
-import com.google.common.util.concurrent.Futures;
-import java.lang.reflect.Field;
+import com.google.common.collect.ImmutableClassToInstanceMap;
+import java.io.FileNotFoundException;
 import java.net.URI;
+import java.net.URISyntaxException;
 import java.util.Collections;
 import java.util.HashMap;
 import java.util.HashSet;
@@ -24,7 +25,6 @@ import java.util.List;
 import java.util.Map;
 import java.util.Map.Entry;
 import java.util.Set;
-import java.util.concurrent.ConcurrentHashMap;
 import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.UriBuilder;
 import javax.ws.rs.core.UriInfo;
@@ -35,14 +35,13 @@ import org.junit.Test;
 import org.mockito.Mock;
 import org.mockito.Mockito;
 import org.mockito.MockitoAnnotations;
-import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
-import org.opendaylight.controller.md.sal.common.api.data.TransactionCommitFailedException;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataBroker;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataReadWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataTreeChangeService;
-import org.opendaylight.controller.md.sal.dom.api.DOMDataWriteTransaction;
-import org.opendaylight.controller.md.sal.dom.api.DOMTransactionChain;
+import org.opendaylight.mdsal.common.api.CommitInfo;
+import org.opendaylight.mdsal.dom.api.DOMDataBroker;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeChangeService;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeReadWriteTransaction;
+import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMSchemaService;
+import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
 import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
 import org.opendaylight.restconf.common.util.SimpleUriInfo;
@@ -52,18 +51,19 @@ import org.opendaylight.restconf.nb.rfc8040.handlers.NotificationServiceHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.SchemaContextHandler;
 import org.opendaylight.restconf.nb.rfc8040.handlers.TransactionChainHandler;
 import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenerAdapter;
-import org.opendaylight.restconf.nb.rfc8040.streams.listeners.Notificator;
+import org.opendaylight.restconf.nb.rfc8040.streams.listeners.ListenersBroker;
+import org.opendaylight.restconf.nb.rfc8040.utils.RestconfConstants;
 import org.opendaylight.restconf.nb.rfc8040.utils.parser.IdentifierCodec;
 import org.opendaylight.yang.gen.v1.urn.sal.restconf.event.subscription.rev140708.NotificationOutputTypeGrouping.NotificationOutputType;
 import org.opendaylight.yangtools.concepts.ListenerRegistration;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
-@SuppressWarnings("deprecation")
 public class RestconfStreamsSubscriptionServiceImplTest {
 
     private static final String URI = "/restconf/18/data/ietf-restconf-monitoring:restconf-state/streams/stream/"
             + "toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE";
-    private static Field listenersByStreamName;
 
     @Mock
     private DOMDataBrokerHandler dataBrokerHandler;
@@ -77,46 +77,43 @@ public class RestconfStreamsSubscriptionServiceImplTest {
 
     @SuppressWarnings("unchecked")
     @Before
-    public void setUp() throws Exception {
+    public void setUp() throws FileNotFoundException, URISyntaxException {
         MockitoAnnotations.initMocks(this);
 
         final DOMTransactionChain domTx = mock(DOMTransactionChain.class);
-        final DOMDataWriteTransaction wTx = Mockito.mock(DOMDataWriteTransaction.class);
-        Mockito.when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
-        final DOMDataReadWriteTransaction rwTx = Mockito.mock(DOMDataReadWriteTransaction.class);
-        final CheckedFuture<Boolean, ReadFailedException> checkedFuture = Futures.immediateCheckedFuture(Boolean.TRUE);
-        Mockito.when(rwTx.exists(Mockito.any(), Mockito.any())).thenReturn(checkedFuture);
-        final CheckedFuture<Void, TransactionCommitFailedException> checkedFutureEmpty =
-                Futures.immediateCheckedFuture(null);
-        Mockito.when(rwTx.submit()).thenReturn(checkedFutureEmpty);
-        Mockito.when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
-        final CheckedFuture<Void, TransactionCommitFailedException> checked = mock(CheckedFuture.class);
-        Mockito.when(wTx.submit()).thenReturn(checked);
-        Mockito.when(checked.checkedGet()).thenReturn(null);
+        final DOMDataTreeWriteTransaction wTx = mock(DOMDataTreeWriteTransaction.class);
+        when(domTx.newWriteOnlyTransaction()).thenReturn(wTx);
+        final DOMDataTreeReadWriteTransaction rwTx = mock(DOMDataTreeReadWriteTransaction.class);
+        when(rwTx.exists(any(), any())).thenReturn(immediateTrueFluentFuture());
+        doReturn(CommitInfo.emptyFluentFuture()).when(rwTx).commit();
+        when(domTx.newReadWriteTransaction()).thenReturn(rwTx);
+        doReturn(CommitInfo.emptyFluentFuture()).when(wTx).commit();
 
         final DOMDataBroker dataBroker = mock(DOMDataBroker.class);
         doReturn(domTx).when(dataBroker).createTransactionChain(any());
 
         transactionHandler = new TransactionChainHandler(dataBroker);
-        schemaHandler = SchemaContextHandler.newInstance(transactionHandler, Mockito.mock(DOMSchemaService.class));
+        schemaHandler = SchemaContextHandler.newInstance(transactionHandler, mock(DOMSchemaService.class));
 
         DOMDataTreeChangeService dataTreeChangeService = mock(DOMDataTreeChangeService.class);
         doReturn(mock(ListenerRegistration.class)).when(dataTreeChangeService)
                 .registerDataTreeChangeListener(any(), any());
 
-        doReturn(Collections.singletonMap(DOMDataTreeChangeService.class, dataTreeChangeService))
-                .when(dataBroker).getSupportedExtensions();
+        doReturn(ImmutableClassToInstanceMap.of(DOMDataTreeChangeService.class, dataTreeChangeService))
+                .when(dataBroker).getExtensions();
 
         doReturn(dataBroker).when(this.dataBrokerHandler).get();
 
         final MultivaluedMap<String, String> map = mock(MultivaluedMap.class);
         final Set<Entry<String, List<String>>> set = new HashSet<>();
-        Mockito.when(map.entrySet()).thenReturn(set);
-        Mockito.when(this.uriInfo.getQueryParameters()).thenReturn(map);
+        when(map.entrySet()).thenReturn(set);
+        when(this.uriInfo.getQueryParameters()).thenReturn(map);
         final UriBuilder baseUriBuilder = new LocalUriInfo().getBaseUriBuilder();
-        Mockito.when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
-        Mockito.when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
-        this.schemaHandler.onGlobalContextUpdated(
+        when(uriInfo.getBaseUri()).thenReturn(baseUriBuilder.build());
+        when(uriInfo.getBaseUriBuilder()).thenReturn(baseUriBuilder);
+        final URI uri = new URI("http://127.0.0.1/" + URI);
+        when(uriInfo.getAbsolutePath()).thenReturn(uri);
+        this.schemaHandler.onModelContextUpdated(
                 YangParserTestUtils.parseYangFiles(TestRestconfUtils.loadFiles("/notifications")));
     }
 
@@ -133,29 +130,31 @@ public class RestconfStreamsSubscriptionServiceImplTest {
     }
 
     @BeforeClass
-    public static void setUpBeforeTest() throws Exception {
+    public static void setUpBeforeTest() {
         final Map<String, ListenerAdapter> listenersByStreamNameSetter = new HashMap<>();
         final ListenerAdapter adapter = mock(ListenerAdapter.class);
+        final YangInstanceIdentifier yiid = mock(YangInstanceIdentifier.class);
+        final YangInstanceIdentifier.PathArgument lastPathArgument = mock(YangInstanceIdentifier.PathArgument.class);
+        final QName qname = QName.create("toaster", "2009-11-20", "toasterStatus");
+        Mockito.when(adapter.getPath()).thenReturn(yiid);
+        Mockito.when(adapter.getOutputType()).thenReturn("JSON");
+        Mockito.when(yiid.getLastPathArgument()).thenReturn(lastPathArgument);
+        Mockito.when(lastPathArgument.getNodeType()).thenReturn(qname);
         listenersByStreamNameSetter.put(
                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
                 adapter);
-        listenersByStreamName = Notificator.class.getDeclaredField("dataChangeListener");
-
-        listenersByStreamName.setAccessible(true);
-        listenersByStreamName.set(Notificator.class, listenersByStreamNameSetter);
+        ListenersBroker.getInstance().setDataChangeListeners(listenersByStreamNameSetter);
     }
 
     @AfterClass
-    public static void setUpAfterTest() throws Exception {
-        listenersByStreamName.set(Notificator.class, null);
-        listenersByStreamName.set(Notificator.class, new ConcurrentHashMap<>());
-        listenersByStreamName.setAccessible(false);
+    public static void setUpAfterTest() {
+        ListenersBroker.getInstance().setDataChangeListeners(Collections.emptyMap());
     }
 
     @Test
-    public void testSubscribeToStream() throws Exception {
+    public void testSubscribeToStream() {
         final UriBuilder uriBuilder = UriBuilder.fromUri(URI);
-        Notificator.createListener(
+        ListenersBroker.getInstance().registerDataChangeListener(
                 IdentifierCodec.deserialize("toaster:toaster/toasterStatus", this.schemaHandler.get()),
                 "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
                 NotificationOutputType.XML);
@@ -167,10 +166,9 @@ public class RestconfStreamsSubscriptionServiceImplTest {
                 .subscribeToStream(
                         "data-change-event-subscription/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
                         this.uriInfo);
-        assertEquals(
-                "ws://localhost:8181/data-change-event-subscription"
-                        + "/toaster:toaster/toasterStatus/datastore=OPERATIONAL/scope=ONE",
-            response.getNewHeaders().get("Location").toString());
+        assertEquals("ws://localhost:8181/" + RestconfConstants.BASE_URI_PATTERN
+                + "/data-change-event-subscription/toaster:toaster/toasterStatus/"
+                + "datastore=OPERATIONAL/scope=ONE", response.getNewHeaders().get("Location").toString());
     }
 
     @Test(expected = RestconfDocumentedException.class)