2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.remote.rpc;
11 import static org.junit.Assert.assertEquals;
13 import java.util.Arrays;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicReference;
17 import org.junit.Test;
18 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
19 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
20 import org.opendaylight.controller.xml.codec.XmlUtils;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.common.RpcResult;
23 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
24 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
25 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
26 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import akka.testkit.JavaTestKit;
31 import com.google.common.util.concurrent.ListenableFuture;
34 * Unit tests for RemoteRpcImplementation.
36 * @author Thomas Pantelis
38 public class RemoteRpcImplementationTest extends AbstractRpcTest {
41 public void testInvokeRpc() throws Exception {
42 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
44 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
45 probeReg1.getRef(), schemaContext);
47 final CompositeNode input = makeRPCInput("foo");
48 final CompositeNode output = makeRPCOutput("bar");
49 final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
51 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
53 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
55 assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
57 assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
58 assertEquals("getInput", input, invokeRpcMsg.get().getInput());
60 if(assertError.get() != null) {
61 throw assertError.get();
67 public void testInvokeRpcWithIdentifier() throws Exception {
68 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
70 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
71 probeReg1.getRef(), schemaContext);
73 QName instanceQName = new QName(new URI("ns"), "instance");
74 YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName);
76 CompositeNode input = makeRPCInput("foo");
77 CompositeNode output = makeRPCOutput("bar");
78 final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
80 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(
81 TEST_RPC, identifier, input);
83 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
85 assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
87 assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
88 assertEquals("getInput", input, invokeRpcMsg.get().getInput());
89 assertEquals("getRoute", identifier, invokeRpcMsg.get().getIdentifier());
91 if(assertError.get() != null) {
92 throw assertError.get();
98 public void testInvokeRpcWithRpcErrorsException() throws Exception {
99 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
101 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
102 probeReg1.getRef(), schemaContext);
104 final CompositeNode input = makeRPCInput("foo");
106 setupInvokeRpcErrorReply(assertError, new RpcErrorsException(
107 "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag",
108 "error", "appTag", "info", null))));
110 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
112 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
114 assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag",
115 "error", "appTag", "info", null);
117 if(assertError.get() != null) {
118 throw assertError.get();
124 public void testInvokeRpcWithOtherException() throws Exception {
125 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
127 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
128 probeReg1.getRef(), schemaContext);
130 final CompositeNode input = makeRPCInput("foo");
132 setupInvokeRpcErrorReply(assertError, new TestException());
134 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
136 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
138 assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed",
139 TestException.MESSAGE, null, null, TestException.MESSAGE);
141 if(assertError.get() != null) {
142 throw assertError.get();
147 private AtomicReference<InvokeRpc> setupInvokeRpcReply(
148 final AtomicReference<AssertionError> assertError, final CompositeNode output) {
149 return setupInvokeRpcReply(assertError, output, null);
152 private AtomicReference<InvokeRpc> setupInvokeRpcErrorReply(
153 final AtomicReference<AssertionError> assertError, final Exception error) {
154 return setupInvokeRpcReply(assertError, null, error);
157 private AtomicReference<InvokeRpc> setupInvokeRpcReply(
158 final AtomicReference<AssertionError> assertError, final CompositeNode output,
159 final Exception error) {
160 final AtomicReference<InvokeRpc> invokeRpcMsg = new AtomicReference<>();
166 invokeRpcMsg.set(probeReg1.expectMsgClass(
167 JavaTestKit.duration("5 seconds"), InvokeRpc.class));
170 probeReg1.reply(new RpcResponse(XmlUtils.outputCompositeNodeToXml(
171 output, schemaContext)));
173 probeReg1.reply(new akka.actor.Status.Failure(error));
176 } catch(AssertionError e) {