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 akka.testkit.JavaTestKit;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.junit.Test;
14 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
15 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
16 import org.opendaylight.controller.xml.codec.XmlUtils;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import java.util.Arrays;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
30 import static org.junit.Assert.assertEquals;
33 * Unit tests for RemoteRpcImplementation.
35 * @author Thomas Pantelis
37 public class RemoteRpcImplementationTest extends AbstractRpcTest {
40 public void testInvokeRpc() throws Exception {
41 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
43 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
44 probeReg1.getRef(), schemaContext, getConfig());
46 final CompositeNode input = makeRPCInput("foo");
47 final CompositeNode output = makeRPCOutput("bar");
48 final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
50 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
52 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
54 assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
56 assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
57 assertEquals("getInput", input, invokeRpcMsg.get().getInput());
59 if(assertError.get() != null) {
60 throw assertError.get();
66 public void testInvokeRpcWithIdentifier() throws Exception {
67 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
69 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
70 probeReg1.getRef(), schemaContext, getConfig());
72 QName instanceQName = new QName(new URI("ns"), "instance");
73 YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName);
75 CompositeNode input = makeRPCInput("foo");
76 CompositeNode output = makeRPCOutput("bar");
77 final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
79 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(
80 TEST_RPC, identifier, input);
82 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
84 assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
86 assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
87 assertEquals("getInput", input, invokeRpcMsg.get().getInput());
88 assertEquals("getRoute", identifier, invokeRpcMsg.get().getIdentifier());
90 if(assertError.get() != null) {
91 throw assertError.get();
97 public void testInvokeRpcWithRpcErrorsException() throws Exception {
98 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
100 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
101 probeReg1.getRef(), schemaContext, getConfig());
103 final CompositeNode input = makeRPCInput("foo");
105 setupInvokeRpcErrorReply(assertError, new RpcErrorsException(
106 "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag",
107 "error", "appTag", "info", null))));
109 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
111 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
113 assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag",
114 "error", "appTag", "info", null);
116 if(assertError.get() != null) {
117 throw assertError.get();
123 public void testInvokeRpcWithOtherException() throws Exception {
124 final AtomicReference<AssertionError> assertError = new AtomicReference<>();
126 RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
127 probeReg1.getRef(), schemaContext, getConfig());
129 final CompositeNode input = makeRPCInput("foo");
131 setupInvokeRpcErrorReply(assertError, new TestException());
133 ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
135 RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
137 assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed",
138 TestException.MESSAGE, null, null, TestException.MESSAGE);
140 if(assertError.get() != null) {
141 throw assertError.get();
146 private AtomicReference<InvokeRpc> setupInvokeRpcReply(
147 final AtomicReference<AssertionError> assertError, final CompositeNode output) {
148 return setupInvokeRpcReply(assertError, output, null);
151 private AtomicReference<InvokeRpc> setupInvokeRpcErrorReply(
152 final AtomicReference<AssertionError> assertError, final Exception error) {
153 return setupInvokeRpcReply(assertError, null, error);
156 private AtomicReference<InvokeRpc> setupInvokeRpcReply(
157 final AtomicReference<AssertionError> assertError, final CompositeNode output,
158 final Exception error) {
159 final AtomicReference<InvokeRpc> invokeRpcMsg = new AtomicReference<>();
165 invokeRpcMsg.set(probeReg1.expectMsgClass(
166 JavaTestKit.duration("5 seconds"), InvokeRpc.class));
169 probeReg1.reply(new RpcResponse(XmlUtils.outputCompositeNodeToXml(
170 output, schemaContext)));
172 probeReg1.reply(new akka.actor.Status.Failure(error));
175 } catch(AssertionError e) {
185 private RemoteRpcProviderConfig getConfig(){
186 return new RemoteRpcProviderConfig.Builder("unit-test").build();