X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=blobdiff_plain;f=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2FRpcRegistrarTest.java;fp=opendaylight%2Fmd-sal%2Fsal-remoterpc-connector%2Fsrc%2Ftest%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fremote%2Frpc%2FRpcRegistrarTest.java;h=aecadc4eb114fbb4ecff478084f220076a97e071;hp=0000000000000000000000000000000000000000;hb=b6fcc46c9b753a8c542456ea2b5d6cc20c266d7c;hpb=37f4afe3c6b199d277b619bfc0056c5a96f8c3e1 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