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%2FRemoteRpcImplementationTest.java;h=b3ed7ffb9e0cdda0125650714edaf200b8433a8c;hp=49451dd0db99114141c289750cf37f7b12e036c1;hb=9216287a4d1fc310f81f1956685f4e6deb7eefa8;hpb=b5b204bafd8ee18692fc023cb2eae6e123369340 diff --git a/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RemoteRpcImplementationTest.java b/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RemoteRpcImplementationTest.java index 49451dd0db..b3ed7ffb9e 100644 --- a/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RemoteRpcImplementationTest.java +++ b/opendaylight/md-sal/sal-remoterpc-connector/src/test/java/org/opendaylight/controller/remote/rpc/RemoteRpcImplementationTest.java @@ -8,26 +8,34 @@ package org.opendaylight.controller.remote.rpc; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.when; + +import akka.actor.ActorRef; +import akka.actor.Status; +import akka.japi.Pair; import akka.testkit.JavaTestKit; -import com.google.common.util.concurrent.ListenableFuture; -import org.junit.Test; -import org.opendaylight.controller.remote.rpc.messages.InvokeRpc; -import org.opendaylight.controller.remote.rpc.messages.RpcResponse; -import org.opendaylight.controller.xml.codec.XmlUtils; -import org.opendaylight.yangtools.yang.common.QName; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; -import org.opendaylight.yangtools.yang.common.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; - -import java.net.URI; +import com.google.common.util.concurrent.CheckedFuture; +import com.google.common.util.concurrent.Futures; import java.util.Arrays; +import java.util.Collections; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -import static org.junit.Assert.assertEquals; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.opendaylight.controller.cluster.datastore.node.utils.serialization.NormalizedNodeSerializer; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcException; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcImplementationNotAvailableException; +import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult; +import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.controller.remote.rpc.registry.RpcRegistry; +import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.FindRouters; +import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; /*** * Unit tests for RemoteRpcImplementation. @@ -36,153 +44,202 @@ import static org.junit.Assert.assertEquals; */ public class RemoteRpcImplementationTest extends AbstractRpcTest { - @Test - public void testInvokeRpc() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext, getConfig()); - - final CompositeNode input = makeRPCInput("foo"); - final CompositeNode output = makeRPCOutput("bar"); - final AtomicReference invokeRpcMsg = setupInvokeRpcReply(assertError, output); - - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); - assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0)); - - assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc()); - assertEquals("getInput", input, invokeRpcMsg.get().getInput()); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } - } + @Test(expected = DOMRpcImplementationNotAvailableException.class) + public void testInvokeRpcWithNoRemoteActor() throws Exception { + final ContainerNode input = makeRPCInput("foo"); + final CheckedFuture failedFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, input); + rpcRegistry1Probe.expectMsgClass(JavaTestKit.duration("5 seconds"), RpcRegistry.Messages.FindRouters.class); + rpcRegistry1Probe + .reply(new RpcRegistry.Messages.FindRoutersReply(Collections.>emptyList())); + failedFuture.checkedGet(5, TimeUnit.SECONDS); } - @Test - public void testInvokeRpcWithIdentifier() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext, getConfig()); - - QName instanceQName = new QName(new URI("ns"), "instance"); - YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName); - - CompositeNode input = makeRPCInput("foo"); - CompositeNode output = makeRPCOutput("bar"); - final AtomicReference invokeRpcMsg = setupInvokeRpcReply(assertError, output); - - ListenableFuture> future = rpcImpl.invokeRpc( - TEST_RPC, identifier, input); - - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); - - assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0)); - - assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc()); - assertEquals("getInput", input, invokeRpcMsg.get().getInput()); - assertEquals("getRoute", identifier, invokeRpcMsg.get().getIdentifier()); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } - } - } + /** + * This test method invokes and executes the remote rpc + */ @Test - public void testInvokeRpcWithRpcErrorsException() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext, getConfig()); - - final CompositeNode input = makeRPCInput("foo"); - - setupInvokeRpcErrorReply(assertError, new RpcErrorsException( - "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag", - "error", "appTag", "info", null)))); - - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); - - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); - - assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag", - "error", "appTag", "info", null); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } - } + public void testInvokeRpc() throws Exception { + final ContainerNode rpcOutput = makeRPCOutput("bar"); + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); + + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + Futures.immediateCheckedFuture(rpcResult)); + + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + + rpcRegistry1Probe.reply(new RpcRegistry.Messages.FindRoutersReply(Arrays.asList(new Pair( + rpcBroker2, 200L)))); + + final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS); + assertEquals(rpcOutput, result.getResult()); } + /** + * This test method invokes and executes the remote rpc + */ @Test - public void testInvokeRpcWithOtherException() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext, getConfig()); - - final CompositeNode input = makeRPCInput("foo"); + public void testInvokeRpcWithNullInput() throws Exception { + final ContainerNode rpcOutput = makeRPCOutput("bar"); + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); + + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + Futures.immediateCheckedFuture(rpcResult)); + + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + + rpcRegistry1Probe.reply(new RpcRegistry.Messages.FindRoutersReply(Arrays.asList(new Pair( + rpcBroker2, 200L)))); + + final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS); + assertEquals(rpcOutput, result.getResult()); + } - setupInvokeRpcErrorReply(assertError, new TestException()); - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); + /** + * This test method invokes and executes the remote rpc + */ + @Test + public void testInvokeRpcWithNoOutput() throws Exception { + final ContainerNode rpcOutput = null; + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); + + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + Futures.immediateCheckedFuture(rpcResult)); + + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + + rpcRegistry1Probe.reply(new RpcRegistry.Messages.FindRoutersReply(Arrays.asList(new Pair( + rpcBroker2, 200L)))); + + final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS); + assertNull(result.getResult()); + } - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); - assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed", - TestException.MESSAGE, null, null, TestException.MESSAGE); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } - } + /** + * This test method invokes and executes the remote rpc + */ + @Test(expected = DOMRpcException.class) + public void testInvokeRpcWithRemoteFailedFuture() throws Exception { + final ContainerNode rpcOutput = null; + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); + + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + Futures.immediateFailedCheckedFuture(new DOMRpcException( + "Test Exception") {})); + + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + + rpcRegistry1Probe.reply(new RpcRegistry.Messages.FindRoutersReply(Arrays.asList(new Pair( + rpcBroker2, 200L)))); + frontEndFuture.checkedGet(5, TimeUnit.SECONDS); } - private AtomicReference setupInvokeRpcReply( - final AtomicReference assertError, final CompositeNode output) { - return setupInvokeRpcReply(assertError, output, null); + /** + * This test method invokes and tests exceptions when akka timeout occured + * + * Currently ignored since this test with current config takes around 15 seconds + * to complete. + * + */ + @Ignore + @Test(expected = RemoteDOMRpcException.class) + public void testInvokeRpcWithAkkaTimeoutException() throws Exception { + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + + frontEndFuture.checkedGet(20, TimeUnit.SECONDS); } - private AtomicReference setupInvokeRpcErrorReply( - final AtomicReference assertError, final Exception error) { - return setupInvokeRpcReply(assertError, null, error); + /** + * This test method invokes remote rpc and lookup failed + * with runtime exception. + */ + @Test(expected = DOMRpcException.class) + public void testInvokeRpcWithLookupException() throws Exception { + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); + final CheckedFuture frontEndFuture = + remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + final FindRouters findRouters = rpcRegistry1Probe.expectMsgClass(RpcRegistry.Messages.FindRouters.class); + final RouteIdentifier routeIdentifier = findRouters.getRouteIdentifier(); + assertEquals("getType", TEST_RPC, routeIdentifier.getType()); + assertEquals("getRoute", TEST_PATH, routeIdentifier.getRoute()); + rpcRegistry1Probe.reply( new Status.Failure(new RuntimeException("test"))); + frontEndFuture.checkedGet(5, TimeUnit.SECONDS); } - private AtomicReference setupInvokeRpcReply( - final AtomicReference assertError, final CompositeNode output, - final Exception error) { - final AtomicReference invokeRpcMsg = new AtomicReference<>(); - - new Thread() { - @Override - public void run() { - try { - invokeRpcMsg.set(probeReg1.expectMsgClass( - JavaTestKit.duration("5 seconds"), InvokeRpc.class)); - - if(output != null) { - probeReg1.reply(new RpcResponse(XmlUtils.outputCompositeNodeToXml( - output, schemaContext))); - } else { - probeReg1.reply(new akka.actor.Status.Failure(error)); - } - - } catch(AssertionError e) { - assertError.set(e); - } - } - - }.start(); - - return invokeRpcMsg; + /** + * This test method invokes and executes the remote rpc + */ + @Test(expected = DOMRpcImplementationNotAvailableException.class) + public void testInvokeRpcWithLoopException() throws Exception { + final NormalizedNode invokeRpcInput = RemoteRpcInput.from(NormalizedNodeSerializer.serialize(makeRPCInput("foo"))); + final CheckedFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + + frontEndFuture.checkedGet(5, TimeUnit.SECONDS); } - private RemoteRpcProviderConfig getConfig(){ + + private RemoteRpcProviderConfig getConfig() { return new RemoteRpcProviderConfig.Builder("unit-test").build(); } }