Improve DOMRpcRouterTest 51/97951/1
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 17 Oct 2021 09:13:20 +0000 (11:13 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Sun, 17 Oct 2021 09:44:42 +0000 (11:44 +0200)
The test is testing a router with no RPCs, add a simple test model
and use its constants to improve actual coverage.

Change-Id: Id2db74f5d321d2857ab3f1237711b4d654e4087b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Actions.java [new file with mode: 0644]
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/DOMRpcRouterTest.java
dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Rpcs.java [new file with mode: 0644]
dom/mdsal-dom-broker/src/test/resources/rpcs.yang [new file with mode: 0644]

diff --git a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Actions.java b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Actions.java
new file mode 100644 (file)
index 0000000..a0d5ab7
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+@NonNullByDefault
+final class Actions {
+    static final EffectiveModelContext CONTEXT = YangParserTestUtils.parseYangResource("/actions.yang");
+    static final QName FOO = QName.create("actions", "foo");
+    static final QName BAR = QName.create(FOO, "bar");
+    static final QName BAZ = QName.create(FOO, "baz");
+    static final QName INPUT = QName.create(FOO, "input");
+    static final QName OUTPUT = QName.create(FOO, "output");
+    static final Absolute BAZ_TYPE = Absolute.of(FOO, BAZ);
+
+    private Actions() {
+        // Hidden on purpose
+    }
+}
index 368eda521029c84a0e6bf79b546fbebda007fb33..b9a874c21f5fe3e25f3a261d6da979018719fac3 100644 (file)
@@ -10,11 +10,9 @@ package org.opendaylight.mdsal.dom.broker;
 import static org.hamcrest.CoreMatchers.instanceOf;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
 import static org.mockito.ArgumentMatchers.any;
 import static org.mockito.Mockito.doCallRealMethod;
 import static org.mockito.Mockito.doNothing;
@@ -27,9 +25,9 @@ import static org.opendaylight.mdsal.dom.broker.TestUtils.getTestRpcImplementati
 import com.google.common.util.concurrent.Futures;
 import com.google.common.util.concurrent.ListenableFuture;
 import java.util.List;
+import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.RejectedExecutionException;
-import org.junit.BeforeClass;
 import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.mockito.junit.MockitoJUnitRunner;
@@ -58,63 +56,50 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifierWithPredicates;
 import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
-import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContextListener;
-import org.opendaylight.yangtools.yang.model.api.stmt.SchemaNodeIdentifier.Absolute;
-import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
 
 @RunWith(MockitoJUnitRunner.StrictStubs.class)
 public class DOMRpcRouterTest {
-    private static final QName FOO = QName.create("actions", "foo");
-    private static final QName BAR = QName.create(FOO, "bar");
-    private static final QName BAZ = QName.create(FOO, "baz");
-    private static final QName INPUT = QName.create(FOO, "input");
-    private static final QName OUTPUT = QName.create(FOO, "output");
-
-    private static final Absolute BAZ_TYPE = Absolute.of(FOO, BAZ);
     private static final YangInstanceIdentifier BAZ_PATH_BAD = YangInstanceIdentifier.create(
-        new NodeIdentifier(FOO), NodeIdentifierWithPredicates.of(FOO, BAR, "bad"));
+        new NodeIdentifier(Actions.FOO), NodeIdentifierWithPredicates.of(Actions.FOO, Actions.BAR, "bad"));
     private static final YangInstanceIdentifier BAZ_PATH_GOOD = YangInstanceIdentifier.create(
-        new NodeIdentifier(FOO), NodeIdentifierWithPredicates.of(FOO, BAR, "good"));
+        new NodeIdentifier(Actions.FOO), NodeIdentifierWithPredicates.of(Actions.FOO, Actions.BAR, "good"));
 
     private static final DOMActionImplementation IMPL =
         (type, path, input) -> Futures.immediateFuture(new SimpleDOMActionResult(
-            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(OUTPUT)).build()));
-
-    private static EffectiveModelContext ACTIONS_CONTEXT;
-
-    @BeforeClass
-    public static void beforeClass() {
-        ACTIONS_CONTEXT = YangParserTestUtils.parseYangResource("/actions.yang");
-    }
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(Actions.OUTPUT)).build()));
 
     @Test
     public void registerRpcImplementation() {
-        try (DOMRpcRouter rpcRouter = new DOMRpcRouter()) {
-            DOMRpcRoutingTable routingTable = rpcRouter.routingTable();
-            assertFalse(routingTable.getOperations().containsKey(TestModel.TEST_QNAME));
-
-            rpcRouter.getRpcProviderService().registerRpcImplementation(getTestRpcImplementation(),
-                DOMRpcIdentifier.create(TestModel.TEST_QNAME, null));
-            routingTable = rpcRouter.routingTable();
-            assertTrue(routingTable.getOperations().containsKey(TestModel.TEST_QNAME));
-
-            rpcRouter.getRpcProviderService().registerRpcImplementation(getTestRpcImplementation(),
-                DOMRpcIdentifier.create(TestModel.TEST2_QNAME, null));
-            routingTable = rpcRouter.routingTable();
-            assertTrue(routingTable.getOperations().containsKey(TestModel.TEST2_QNAME));
+        try (DOMRpcRouter rpcRouter = rpcsRouter()) {
+            assertOperationKeys(rpcRouter);
+
+            final Registration fooReg = rpcRouter.getRpcProviderService().registerRpcImplementation(
+                getTestRpcImplementation(), DOMRpcIdentifier.create(Rpcs.FOO, null));
+            assertOperationKeys(rpcRouter, Rpcs.FOO);
+
+            final Registration barReg = rpcRouter.getRpcProviderService().registerRpcImplementation(
+                getTestRpcImplementation(), DOMRpcIdentifier.create(Rpcs.BAR, null));
+            assertOperationKeys(rpcRouter, Rpcs.FOO, Rpcs.BAR);
+
+            fooReg.close();
+            assertOperationKeys(rpcRouter, Rpcs.BAR);
+            barReg.close();
+            assertOperationKeys(rpcRouter);
         }
     }
 
+    private static void assertOperationKeys(final DOMRpcRouter router, final QName... keys) {
+        assertEquals(Set.of(keys), router.routingTable().getOperations().keySet());
+    }
+
     @Test
     public void testFailedInvokeRpc() {
-        try (DOMRpcRouter rpcRouter = new DOMRpcRouter()) {
-            final ListenableFuture<?> future = rpcRouter.getRpcService().invokeRpc(TestModel.TEST_QNAME, null);
+        try (DOMRpcRouter rpcRouter = rpcsRouter()) {
+            final ListenableFuture<?> future = rpcRouter.getRpcService().invokeRpc(Rpcs.FOO, null);
             final Throwable cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
             assertThat(cause, instanceOf(DOMRpcImplementationNotAvailableException.class));
-            assertEquals("No implementation of RPC "
-                + "(urn:opendaylight:params:xml:ns:yang:controller:md:sal:dom:store:test?revision=2014-03-13)test "
-                + "available", cause.getMessage());
+            assertEquals("No implementation of RPC (rpcs)foo available", cause.getMessage());
         }
     }
 
@@ -133,7 +118,7 @@ public class DOMRpcRouterTest {
             assertEquals(List.of(reg), rpcRouter.listeners());
 
             final Registration implReg = rpcRouter.getRpcProviderService().registerRpcImplementation(
-                getTestRpcImplementation(), DOMRpcIdentifier.create(TestModel.TEST_QNAME, null));
+                getTestRpcImplementation(), DOMRpcIdentifier.create(Rpcs.FOO, null));
             verify(listener, timeout(1000)).onRpcAvailable(any());
 
             implReg.close();
@@ -165,13 +150,9 @@ public class DOMRpcRouterTest {
     @Test
     public void onGlobalContextUpdated() {
         try (DOMRpcRouter rpcRouter = new DOMRpcRouter()) {
-
             final DOMRpcRoutingTable routingTableOriginal = rpcRouter.routingTable();
-
             rpcRouter.onModelContextUpdated(TestModel.createTestContext());
-
-            final DOMRpcRoutingTable routingTableChanged = rpcRouter.routingTable();
-            assertNotEquals(routingTableOriginal, routingTableChanged);
+            assertNotEquals(routingTableOriginal, rpcRouter.routingTable());
         }
     }
 
@@ -187,21 +168,19 @@ public class DOMRpcRouterTest {
 
         final DOMRpcProviderService svc = rpcRouter.getRpcProviderService();
         assertThrows(RejectedExecutionException.class, () -> svc.registerRpcImplementation(getTestRpcImplementation(),
-            DOMRpcIdentifier.create(TestModel.TEST_QNAME, null)));
+            DOMRpcIdentifier.create(Rpcs.FOO, null)));
     }
 
     @Test
     public void testActionInstanceRouting() throws ExecutionException {
-        try (DOMRpcRouter rpcRouter = new DOMRpcRouter()) {
-            rpcRouter.onModelContextUpdated(ACTIONS_CONTEXT);
-
+        try (DOMRpcRouter rpcRouter = actionsRouter()) {
             final DOMActionProviderService actionProvider = rpcRouter.getActionProviderService();
             assertNotNull(actionProvider);
             final DOMActionService actionConsumer = rpcRouter.getActionService();
             assertNotNull(actionConsumer);
 
             try (ObjectRegistration<?> reg = actionProvider.registerActionImplementation(IMPL,
-                DOMActionInstance.of(BAZ_TYPE, LogicalDatastoreType.OPERATIONAL, BAZ_PATH_GOOD))) {
+                DOMActionInstance.of(Actions.BAZ_TYPE, LogicalDatastoreType.OPERATIONAL, BAZ_PATH_GOOD))) {
 
                 assertAvailable(actionConsumer, BAZ_PATH_GOOD);
                 assertUnavailable(actionConsumer, BAZ_PATH_BAD);
@@ -214,16 +193,15 @@ public class DOMRpcRouterTest {
 
     @Test
     public void testActionDatastoreRouting() throws ExecutionException {
-        try (DOMRpcRouter rpcRouter = new DOMRpcRouter()) {
-            rpcRouter.onModelContextUpdated(ACTIONS_CONTEXT);
-
+        try (DOMRpcRouter rpcRouter = actionsRouter()) {
             final DOMActionProviderService actionProvider = rpcRouter.getActionProviderService();
             assertNotNull(actionProvider);
             final DOMActionService actionConsumer = rpcRouter.getActionService();
             assertNotNull(actionConsumer);
 
             try (ObjectRegistration<?> reg = actionProvider.registerActionImplementation(IMPL,
-                DOMActionInstance.of(BAZ_TYPE, LogicalDatastoreType.OPERATIONAL, YangInstanceIdentifier.empty()))) {
+                DOMActionInstance.of(Actions.BAZ_TYPE, LogicalDatastoreType.OPERATIONAL,
+                    YangInstanceIdentifier.empty()))) {
 
                 assertAvailable(actionConsumer, BAZ_PATH_GOOD);
                 assertAvailable(actionConsumer, BAZ_PATH_BAD);
@@ -234,6 +212,18 @@ public class DOMRpcRouterTest {
         }
     }
 
+    private static DOMRpcRouter actionsRouter() {
+        final DOMRpcRouter router = new DOMRpcRouter();
+        router.onModelContextUpdated(Actions.CONTEXT);
+        return router;
+    }
+
+    private static DOMRpcRouter rpcsRouter() {
+        final DOMRpcRouter router = new DOMRpcRouter();
+        router.onModelContextUpdated(Rpcs.CONTEXT);
+        return router;
+    }
+
     private static void assertAvailable(final DOMActionService actionService, final YangInstanceIdentifier path) {
         final DOMActionResult result;
         try {
@@ -252,7 +242,8 @@ public class DOMRpcRouterTest {
 
     private static ListenableFuture<? extends DOMActionResult> invokeBaz(final DOMActionService actionService,
             final YangInstanceIdentifier path) {
-        return actionService.invokeAction(BAZ_TYPE, new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, path),
-            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(INPUT)).build());
+        return actionService.invokeAction(Actions.BAZ_TYPE,
+            new DOMDataTreeIdentifier(LogicalDatastoreType.OPERATIONAL, path),
+            Builders.containerBuilder().withNodeIdentifier(new NodeIdentifier(Actions.INPUT)).build());
     }
 }
diff --git a/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Rpcs.java b/dom/mdsal-dom-broker/src/test/java/org/opendaylight/mdsal/dom/broker/Rpcs.java
new file mode 100644 (file)
index 0000000..094ccec
--- /dev/null
@@ -0,0 +1,24 @@
+/*
+ * Copyright (c) 2021 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * 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.mdsal.dom.broker;
+
+import org.eclipse.jdt.annotation.NonNullByDefault;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
+import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
+
+@NonNullByDefault
+final class Rpcs {
+    static final EffectiveModelContext CONTEXT = YangParserTestUtils.parseYangResource("/rpcs.yang");
+    static final QName FOO = QName.create("rpcs", "foo");
+    static final QName BAR = QName.create(FOO, "bar");
+
+    private Rpcs() {
+        // Hidden on purpose
+    }
+}
diff --git a/dom/mdsal-dom-broker/src/test/resources/rpcs.yang b/dom/mdsal-dom-broker/src/test/resources/rpcs.yang
new file mode 100644 (file)
index 0000000..6943978
--- /dev/null
@@ -0,0 +1,8 @@
+module rpcs {
+  namespace rpcs;
+  prefix rpcs;
+
+  rpc foo;
+
+  rpc bar;
+}