6c3a57b3448e23ac485aa04518c788f2fad390ce
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementationTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.remote.rpc;
10
11 import static org.junit.Assert.assertEquals;
12 import java.net.URI;
13 import java.util.Arrays;
14 import java.util.concurrent.TimeUnit;
15 import java.util.concurrent.atomic.AtomicReference;
16
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;
28
29 import akka.testkit.JavaTestKit;
30
31 import com.google.common.util.concurrent.ListenableFuture;
32
33 /***
34  * Unit tests for RemoteRpcImplementation.
35  *
36  * @author Thomas Pantelis
37  */
38 public class RemoteRpcImplementationTest extends AbstractRpcTest {
39
40     @Test
41     public void testInvokeRpc() throws Exception {
42         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
43         try {
44             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
45                     probeReg1.getRef(), schemaContext);
46
47             final CompositeNode input = makeRPCInput("foo");
48             final CompositeNode output = makeRPCOutput("bar");
49             final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
50
51             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
52
53             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
54
55             assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
56
57             assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
58             assertEquals("getInput", input, invokeRpcMsg.get().getInput());
59         } finally {
60             if(assertError.get() != null) {
61                 throw assertError.get();
62             }
63         }
64     }
65
66     @Test
67     public void testInvokeRpcWithIdentifier() throws Exception {
68         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
69         try {
70             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
71                     probeReg1.getRef(), schemaContext);
72
73             QName instanceQName = new QName(new URI("ns"), "instance");
74             YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName);
75
76             CompositeNode input = makeRPCInput("foo");
77             CompositeNode output = makeRPCOutput("bar");
78             final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
79
80             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(
81                     TEST_RPC, identifier, input);
82
83             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
84
85             assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
86
87             assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
88             assertEquals("getInput", input, invokeRpcMsg.get().getInput());
89             assertEquals("getRoute", identifier, invokeRpcMsg.get().getIdentifier());
90         } finally {
91             if(assertError.get() != null) {
92                 throw assertError.get();
93             }
94         }
95     }
96
97     @Test
98     public void testInvokeRpcWithRpcErrorsException() throws Exception {
99         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
100         try {
101             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
102                     probeReg1.getRef(), schemaContext);
103
104             final CompositeNode input = makeRPCInput("foo");
105
106             setupInvokeRpcErrorReply(assertError, new RpcErrorsException(
107                     "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag",
108                             "error", "appTag", "info", null))));
109
110             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
111
112             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
113
114             assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag",
115                     "error", "appTag", "info", null);
116         } finally {
117             if(assertError.get() != null) {
118                 throw assertError.get();
119             }
120         }
121     }
122
123     @Test
124     public void testInvokeRpcWithOtherException() throws Exception {
125         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
126         try {
127             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
128                     probeReg1.getRef(), schemaContext);
129
130             final CompositeNode input = makeRPCInput("foo");
131
132             setupInvokeRpcErrorReply(assertError, new TestException());
133
134             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
135
136             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
137
138             assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed",
139                     TestException.MESSAGE, null, null, TestException.MESSAGE);
140         } finally {
141             if(assertError.get() != null) {
142                 throw assertError.get();
143             }
144         }
145     }
146
147     private AtomicReference<InvokeRpc> setupInvokeRpcReply(
148             final AtomicReference<AssertionError> assertError, final CompositeNode output) {
149         return setupInvokeRpcReply(assertError, output, null);
150     }
151
152     private AtomicReference<InvokeRpc> setupInvokeRpcErrorReply(
153             final AtomicReference<AssertionError> assertError, final Exception error) {
154         return setupInvokeRpcReply(assertError, null, error);
155     }
156
157     private AtomicReference<InvokeRpc> setupInvokeRpcReply(
158             final AtomicReference<AssertionError> assertError, final CompositeNode output,
159             final Exception error) {
160         final AtomicReference<InvokeRpc> invokeRpcMsg = new AtomicReference<>();
161
162         new Thread() {
163             @Override
164             public void run() {
165                 try {
166                     invokeRpcMsg.set(probeReg1.expectMsgClass(
167                             JavaTestKit.duration("5 seconds"), InvokeRpc.class));
168
169                     if(output != null) {
170                         probeReg1.reply(new RpcResponse(XmlUtils.outputCompositeNodeToXml(
171                                 output, schemaContext)));
172                     } else {
173                         probeReg1.reply(new akka.actor.Status.Failure(error));
174                     }
175
176                 } catch(AssertionError e) {
177                     assertError.set(e);
178                 }
179             }
180
181         }.start();
182
183         return invokeRpcMsg;
184     }
185 }