RpcRegistrar unit test 68/54368/10
authormatus.kubica <matus.kubica@pantheon.tech>
Wed, 5 Apr 2017 15:16:46 +0000 (17:16 +0200)
committerMatúš Kubica <Matus.Kubica@pantheon.tech>
Fri, 21 Apr 2017 05:33:09 +0000 (05:33 +0000)
Change-Id: I90403cb3c5fb98854c9e7dcd80ba0ce6e5f944f4
Signed-off-by: matus.kubica <matus.kubica@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java
opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RpcRegistrarTest.java [new file with mode: 0644]

index 805e47a0dd05e7b52082f705f523b8870cd5bb02..40600d091e16345a61c9dbb9ecfe9096d89b6040 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.remote.rpc.registry;
 import akka.actor.ActorRef;
 import akka.actor.Address;
 import akka.actor.Props;
 import akka.actor.ActorRef;
 import akka.actor.Address;
 import akka.actor.Props;
+import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.base.Preconditions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
@@ -109,7 +110,8 @@ public class RpcRegistry extends BucketStoreActor<RoutingTable> {
         private final Set<DOMRpcIdentifier> rpcs;
         private final ActorRef router;
 
         private final Set<DOMRpcIdentifier> rpcs;
         private final ActorRef router;
 
-        RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
+        @VisibleForTesting
+        public RemoteRpcEndpoint(final ActorRef router, final Collection<DOMRpcIdentifier> rpcs) {
             this.router = Preconditions.checkNotNull(router);
             this.rpcs = ImmutableSet.copyOf(rpcs);
         }
             this.router = Preconditions.checkNotNull(router);
             this.rpcs = ImmutableSet.copyOf(rpcs);
         }
@@ -161,7 +163,8 @@ public class RpcRegistry extends BucketStoreActor<RoutingTable> {
         public static final class UpdateRemoteEndpoints {
             private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
 
         public static final class UpdateRemoteEndpoints {
             private final Map<Address, Optional<RemoteRpcEndpoint>> endpoints;
 
-            UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
+            @VisibleForTesting
+            public UpdateRemoteEndpoints(final Map<Address, Optional<RemoteRpcEndpoint>> endpoints) {
                 this.endpoints = ImmutableMap.copyOf(endpoints);
             }
 
                 this.endpoints = ImmutableMap.copyOf(endpoints);
             }
 
diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RpcRegistrarTest.java b/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RpcRegistrarTest.java
new file mode 100644 (file)
index 0000000..aecadc4
--- /dev/null
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2017 Pantheon Technologies 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.controller.remote.rpc;
+
+import akka.actor.ActorRef;
+import akka.actor.ActorSystem;
+import akka.actor.Address;
+import akka.actor.Props;
+import akka.testkit.JavaTestKit;
+import akka.testkit.TestActorRef;
+import com.google.common.collect.ImmutableMap;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Optional;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.mockito.InOrder;
+import org.mockito.Mock;
+import org.mockito.Mockito;
+import org.mockito.MockitoAnnotations;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcIdentifier;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationRegistration;
+import org.opendaylight.controller.md.sal.dom.api.DOMRpcProviderService;
+import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.UpdateRemoteEndpoints;
+import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.RemoteRpcEndpoint;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+
+public class RpcRegistrarTest {
+    @Mock
+    private DOMRpcProviderService service;
+    @Mock
+    private DOMRpcImplementationRegistration<RemoteRpcImplementation> oldReg;
+    @Mock
+    private DOMRpcImplementationRegistration<RemoteRpcImplementation> newReg;
+
+    private ActorSystem system;
+    private TestActorRef<RpcRegistrar> testActorRef;
+    private Address endpointAddress;
+    private RemoteRpcEndpoint firstEndpoint;
+    private RemoteRpcEndpoint secondEndpoint;
+    private RpcRegistrar rpcRegistrar;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+        system = ActorSystem.create("test");
+
+        final JavaTestKit testKit = new JavaTestKit(system);
+        final RemoteRpcProviderConfig config = new RemoteRpcProviderConfig.Builder("system").build();
+        final Props props = RpcRegistrar.props(config, service);
+        testActorRef = new TestActorRef<>(system, props, testKit.getRef(), "actorRef");
+        endpointAddress = new Address("http", "local");
+
+        final DOMRpcIdentifier firstEndpointId = DOMRpcIdentifier.create(
+                SchemaPath.create(true, QName.create("first:identifier", "foo")));
+        final DOMRpcIdentifier secondEndpointId = DOMRpcIdentifier.create(
+                SchemaPath.create(true, QName.create("second:identifier", "bar")));
+
+        final JavaTestKit senderKit = new JavaTestKit(system);
+        firstEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(firstEndpointId));
+        secondEndpoint = new RemoteRpcEndpoint(senderKit.getRef(), Collections.singletonList(secondEndpointId));
+
+        Mockito.doReturn(oldReg).when(service).registerRpcImplementation(
+                Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
+
+        Mockito.doReturn(newReg).when(service).registerRpcImplementation(
+                Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
+
+        rpcRegistrar = testActorRef.underlyingActor();
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        JavaTestKit.shutdownActorSystem(system, null, Boolean.TRUE);
+    }
+
+    @Test
+    public void testPostStop() throws Exception {
+        testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
+                ActorRef.noSender());
+        testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
+                ActorRef.noSender());
+
+        rpcRegistrar.postStop();
+
+        Mockito.verify(oldReg).close();
+        Mockito.verify(newReg).close();
+    }
+
+    @Test
+    public void testHandleReceiveAddEndpoint() throws Exception {
+        final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
+                endpointAddress, Optional.of(firstEndpoint));
+        testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
+
+        Mockito.verify(service).registerRpcImplementation(
+                Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
+        Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
+    }
+
+    @Test
+    public void testHandleReceiveRemoveEndpoint() throws Exception {
+        final Map<Address, Optional<RemoteRpcEndpoint>> endpoints = ImmutableMap.of(
+                endpointAddress, Optional.empty());
+        testActorRef.tell(new UpdateRemoteEndpoints(endpoints), ActorRef.noSender());
+        Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
+    }
+
+    @Test
+    public void testHandleReceiveUpdateEndpoint() throws Exception {
+        final InOrder inOrder = Mockito.inOrder(service, oldReg, newReg);
+
+        testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(firstEndpoint))),
+                ActorRef.noSender());
+
+        // first registration
+        inOrder.verify(service).registerRpcImplementation(
+                Mockito.any(RemoteRpcImplementation.class), Mockito.eq(firstEndpoint.getRpcs()));
+
+        testActorRef.tell(new UpdateRemoteEndpoints(ImmutableMap.of(endpointAddress, Optional.of(secondEndpoint))),
+                ActorRef.noSender());
+
+        // second registration
+        inOrder.verify(service).registerRpcImplementation(
+                Mockito.any(RemoteRpcImplementation.class), Mockito.eq(secondEndpoint.getRpcs()));
+
+        // verify first registration is closed
+        inOrder.verify(oldReg).close();
+
+        Mockito.verifyNoMoreInteractions(service, oldReg, newReg);
+    }
+}
\ No newline at end of file