- @Test
- public void testInvokeRpc() throws Exception {
- final AtomicReference<AssertionError> assertError = new AtomicReference<>();
- try {
- RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
- probeReg1.getRef(), schemaContext, getConfig());
-
- final CompositeNode input = makeRPCInput("foo");
- final CompositeNode output = makeRPCOutput("bar");
- final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
-
- ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
-
- RpcResult<CompositeNode> 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
- public void testInvokeRpcWithIdentifier() throws Exception {
- final AtomicReference<AssertionError> 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<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
-
- ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(
- TEST_RPC, identifier, input);
-
- RpcResult<CompositeNode> 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();
- }
- }
- }
-
- @Test
- public void testInvokeRpcWithRpcErrorsException() throws Exception {
- final AtomicReference<AssertionError> 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<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
-
- RpcResult<CompositeNode> 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();
- }
- }
- }
-
- @Test
- public void testInvokeRpcWithOtherException() throws Exception {
- final AtomicReference<AssertionError> assertError = new AtomicReference<>();
- try {
- RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
- probeReg1.getRef(), schemaContext, getConfig());
-
- final CompositeNode input = makeRPCInput("foo");
-
- setupInvokeRpcErrorReply(assertError, new TestException());
-
- ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
-
- RpcResult<CompositeNode> 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();
- }
- }
- }
-
- private AtomicReference<InvokeRpc> setupInvokeRpcReply(
- final AtomicReference<AssertionError> assertError, final CompositeNode output) {
- return setupInvokeRpcReply(assertError, output, null);
- }
-
- private AtomicReference<InvokeRpc> setupInvokeRpcErrorReply(
- final AtomicReference<AssertionError> assertError, final Exception error) {
- return setupInvokeRpcReply(assertError, null, error);
- }
-
- private AtomicReference<InvokeRpc> setupInvokeRpcReply(
- final AtomicReference<AssertionError> assertError, final CompositeNode output,
- final Exception error) {
- final AtomicReference<InvokeRpc> 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;
- }