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=5b3394eebd223dacf65df2616e65b8f98e9b5444;hp=6c3a57b3448e23ac485aa04518c788f2fad390ce;hb=a2b838f96589b502578fa4e15cef2769f886a378;hpb=35f74293edf98402e2b622e060185f7874d10857 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 6c3a57b344..5b3394eebd 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 @@ -9,177 +9,159 @@ package org.opendaylight.controller.remote.rpc; import static org.junit.Assert.assertEquals; -import java.net.URI; -import java.util.Arrays; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicReference; - -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.RpcResult; -import org.opendaylight.yangtools.yang.common.RpcResultBuilder; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity; -import org.opendaylight.yangtools.yang.common.RpcError.ErrorType; -import org.opendaylight.yangtools.yang.data.api.CompositeNode; -import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; - -import akka.testkit.JavaTestKit; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.when; import com.google.common.util.concurrent.ListenableFuture; - -/*** +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import org.junit.Ignore; +import org.junit.Test; +import org.mockito.ArgumentCaptor; +import org.opendaylight.mdsal.dom.api.DOMRpcException; +import org.opendaylight.mdsal.dom.api.DOMRpcResult; +import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult; +import org.opendaylight.yangtools.util.concurrent.FluentFutures; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; +import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.model.api.SchemaPath; + +/** * Unit tests for RemoteRpcImplementation. * * @author Thomas Pantelis */ public class RemoteRpcImplementationTest extends AbstractRpcTest { + /** + * This test method invokes and executes the remote rpc. + */ @Test public void testInvokeRpc() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext); + final ContainerNode rpcOutput = makeRPCOutput("bar"); + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); - final CompositeNode input = makeRPCInput("foo"); - final CompositeNode output = makeRPCOutput("bar"); - final AtomicReference invokeRpcMsg = setupInvokeRpcReply(assertError, output); + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + FluentFutures.immediateFluentFuture(rpcResult)); - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); + final ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); - 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(); - } - } + final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS); + assertEquals(rpcOutput, result.getResult()); } + /** + * This test method invokes and executes the remote rpc. + */ @Test - public void testInvokeRpcWithIdentifier() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext); + public void testInvokeRpcWithNullInput() throws Exception { + final ContainerNode rpcOutput = makeRPCOutput("bar"); + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); - QName instanceQName = new QName(new URI("ns"), "instance"); - YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); - CompositeNode input = makeRPCInput("foo"); - CompositeNode output = makeRPCOutput("bar"); - final AtomicReference invokeRpcMsg = setupInvokeRpcReply(assertError, output); + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + FluentFutures.immediateFluentFuture(rpcResult)); - ListenableFuture> future = rpcImpl.invokeRpc( - TEST_RPC, identifier, input); + ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); - 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(); - } - } + final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS); + assertEquals(rpcOutput, result.getResult()); } + /** + * 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); - - final CompositeNode input = makeRPCInput("foo"); + public void testInvokeRpcWithNoOutput() throws Exception { + final ContainerNode rpcOutput = null; + final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput); - setupInvokeRpcErrorReply(assertError, new RpcErrorsException( - "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag", - "error", "appTag", "info", null)))); + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + FluentFutures.immediateFluentFuture(rpcResult)); - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); + final ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); - assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag", - "error", "appTag", "info", null); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } - } + final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS); + assertNull(result.getResult()); } - @Test - public void testInvokeRpcWithOtherException() throws Exception { - final AtomicReference assertError = new AtomicReference<>(); - try { - RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation( - probeReg1.getRef(), schemaContext); - - final CompositeNode input = makeRPCInput("foo"); - - setupInvokeRpcErrorReply(assertError, new TestException()); + /** + * This test method invokes and executes the remote rpc. + */ + @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"}) + @Test(expected = DOMRpcException.class) + public void testInvokeRpcWithRemoteFailedFuture() throws Throwable { + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + @SuppressWarnings({"unchecked", "rawtypes"}) + final ArgumentCaptor> inputCaptor = + (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class); - ListenableFuture> future = rpcImpl.invokeRpc(TEST_RPC, input); + when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn( + FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null))); - RpcResult rpcResult = future.get(5, TimeUnit.SECONDS); + final ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); - assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed", - TestException.MESSAGE, null, null, TestException.MESSAGE); - } finally { - if(assertError.get() != null) { - throw assertError.get(); - } + try { + frontEndFuture.get(5, TimeUnit.SECONDS); + } catch (ExecutionException e) { + throw e.getCause(); } } - 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"); + final ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + + frontEndFuture.get(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) + @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"}) + public void testInvokeRpcWithLookupException() throws Throwable { + final NormalizedNode invokeRpcInput = makeRPCInput("foo"); + + doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class), + any(NormalizedNode.class)); - 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; + final ListenableFuture frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput); + assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture); + + try { + frontEndFuture.get(5, TimeUnit.SECONDS); + } catch (ExecutionException e) { + throw e.getCause(); + } } }