From 4b86f6bdff477342da2c6062399c158ed4a5228d Mon Sep 17 00:00:00 2001 From: "matus.kubica" Date: Wed, 5 Apr 2017 17:16:46 +0200 Subject: [PATCH] RpcRegistrar unit test Change-Id: I90403cb3c5fb98854c9e7dcd80ba0ce6e5f944f4 Signed-off-by: matus.kubica Signed-off-by: Ivan Hrasko --- .../remote/rpc/registry/RpcRegistry.java | 7 +- .../remote/rpc/RpcRegistrarTest.java | 139 ++++++++++++++++++ 2 files changed, 144 insertions(+), 2 deletions(-) create mode 100644 opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RpcRegistrarTest.java diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java index 805e47a0dd..40600d091e 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/main/java/org/opendaylight/controller/remote/rpc/registry/RpcRegistry.java @@ -10,6 +10,7 @@ package org.opendaylight.controller.remote.rpc.registry; 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; @@ -109,7 +110,8 @@ public class RpcRegistry extends BucketStoreActor { private final Set rpcs; private final ActorRef router; - RemoteRpcEndpoint(final ActorRef router, final Collection rpcs) { + @VisibleForTesting + public RemoteRpcEndpoint(final ActorRef router, final Collection rpcs) { this.router = Preconditions.checkNotNull(router); this.rpcs = ImmutableSet.copyOf(rpcs); } @@ -161,7 +163,8 @@ public class RpcRegistry extends BucketStoreActor { public static final class UpdateRemoteEndpoints { private final Map> endpoints; - UpdateRemoteEndpoints(final Map> endpoints) { + @VisibleForTesting + public UpdateRemoteEndpoints(final Map> 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 index 0000000000..aecadc4eb1 --- /dev/null +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RpcRegistrarTest.java @@ -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 oldReg; + @Mock + private DOMRpcImplementationRegistration newReg; + + private ActorSystem system; + private TestActorRef 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> 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> 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 -- 2.36.6