Merge "BUG 1595 - Clustering : NPE on startup"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RpcBrokerTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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
12 import akka.actor.ActorRef;
13 import akka.japi.Pair;
14 import akka.testkit.JavaTestKit;
15
16 import com.google.common.collect.Lists;
17 import com.google.common.util.concurrent.Futures;
18 import static org.junit.Assert.assertEquals;
19 import org.junit.Test;
20 import org.mockito.ArgumentCaptor;
21 import org.opendaylight.controller.remote.rpc.messages.ExecuteRpc;
22 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
23 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
24 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
25 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry.Messages.FindRouters;
26 import org.opendaylight.controller.sal.connector.api.RpcRouter.RouteIdentifier;
27 import org.opendaylight.controller.xml.codec.XmlUtils;
28 import org.opendaylight.yangtools.yang.common.QName;
29 import org.opendaylight.yangtools.yang.common.RpcError;
30 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
31 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
34 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
35 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
36 import java.net.URI;
37 import java.net.URISyntaxException;
38 import java.util.Arrays;
39 import java.util.Collections;
40 import java.util.List;
41
42 import static org.mockito.Mockito.when;
43 import static org.mockito.Mockito.eq;
44 import static org.mockito.Mockito.any;
45
46 public class RpcBrokerTest extends AbstractRpcTest {
47
48     @Test
49     public void testInvokeRpcWithNoRemoteActor() throws Exception {
50         new JavaTestKit(node1) {{
51             CompositeNode input = makeRPCInput("foo");
52
53             InvokeRpc invokeMsg = new InvokeRpc(TEST_RPC, null, input);
54             rpcBroker1.tell(invokeMsg, getRef());
55
56             probeReg1.expectMsgClass(duration("5 seconds"), RpcRegistry.Messages.FindRouters.class);
57             probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(
58                     Collections.<Pair<ActorRef, Long>>emptyList()));
59
60             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
61                     akka.actor.Status.Failure.class);
62
63             assertEquals("failure.cause()", RpcErrorsException.class, failure.cause().getClass());
64         }};
65     }
66
67
68     /**
69      * This test method invokes and executes the remote rpc
70      */
71     //@Test
72     public void testInvokeRpc() throws URISyntaxException {
73         new JavaTestKit(node1) {{
74             QName instanceQName = new QName(new URI("ns"), "instance");
75
76             CompositeNode invokeRpcResult = makeRPCOutput("bar");
77             RpcResult<CompositeNode> rpcResult =
78                                RpcResultBuilder.<CompositeNode>success(invokeRpcResult).build();
79             ArgumentCaptor<CompositeNode> inputCaptor = new ArgumentCaptor<>();
80             when(brokerSession.rpc(eq(TEST_RPC), inputCaptor.capture()))
81                     .thenReturn(Futures.immediateFuture(rpcResult));
82
83             // invoke rpc
84             CompositeNode input = makeRPCInput("foo");
85             YangInstanceIdentifier instanceID = YangInstanceIdentifier.of(instanceQName);
86             InvokeRpc invokeMsg = new InvokeRpc(TEST_RPC, instanceID, input);
87             rpcBroker1.tell(invokeMsg, getRef());
88
89             FindRouters findRouters = probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
90             RouteIdentifier<?, ?, ?> routeIdentifier = findRouters.getRouteIdentifier();
91             assertEquals("getType", TEST_RPC, routeIdentifier.getType());
92             assertEquals("getRoute", instanceID, routeIdentifier.getRoute());
93
94             probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(
95                     Arrays.asList(new Pair<ActorRef, Long>(rpcBroker2, 200L))));
96
97             RpcResponse rpcResponse = expectMsgClass(duration("5 seconds"), RpcResponse.class);
98             assertCompositeNodeEquals((CompositeNode)invokeRpcResult.getValue().get(0),
99                     XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode()));
100             assertCompositeNodeEquals(input, inputCaptor.getValue());
101         }};
102     }
103
104     @Test
105     public void testInvokeRpcWithNoOutput() {
106         new JavaTestKit(node1) {{
107
108             RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>success().build();
109             when(brokerSession.rpc(eq(TEST_RPC), any(CompositeNode.class)))
110                     .thenReturn(Futures.immediateFuture(rpcResult));
111
112             InvokeRpc invokeMsg = new InvokeRpc(TEST_RPC, null, makeRPCInput("foo"));
113             rpcBroker1.tell(invokeMsg, getRef());
114
115             probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
116             probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(
117                     Arrays.asList(new Pair<ActorRef, Long>(rpcBroker2, 200L))));
118
119             RpcResponse rpcResponse = expectMsgClass(duration("5 seconds"), RpcResponse.class);
120
121             assertEquals("getResultCompositeNode", "", rpcResponse.getResultCompositeNode());
122         }};
123     }
124
125     @Test
126     public void testInvokeRpcWithExecuteFailure() {
127         new JavaTestKit(node1) {{
128
129             RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>failed()
130                     .withError(ErrorType.RPC, "tag", "error", "appTag", "info",
131                             new Exception("mock"))
132                     .build();
133             when(brokerSession.rpc(eq(TEST_RPC), any(CompositeNode.class)))
134                     .thenReturn(Futures.immediateFuture(rpcResult));
135
136             InvokeRpc invokeMsg = new InvokeRpc(TEST_RPC, null, makeRPCInput("foo"));
137             rpcBroker1.tell(invokeMsg, getRef());
138
139             probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
140             probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(
141                     Arrays.asList(new Pair<ActorRef, Long>(rpcBroker2, 200L))));
142
143             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
144                     akka.actor.Status.Failure.class);
145
146             assertEquals("failure.cause()", RpcErrorsException.class, failure.cause().getClass());
147
148             RpcErrorsException errorsEx = (RpcErrorsException)failure.cause();
149             List<RpcError> rpcErrors = Lists.newArrayList(errorsEx.getRpcErrors());
150             assertEquals("RpcErrors count", 1, rpcErrors.size());
151             assertRpcErrorEquals(rpcErrors.get(0), ErrorSeverity.ERROR, ErrorType.RPC, "tag",
152                     "error", "appTag", "info", "mock");
153         }};
154     }
155
156     @Test
157     public void testInvokeRpcWithFindRoutersFailure() {
158         new JavaTestKit(node1) {{
159
160             InvokeRpc invokeMsg = new InvokeRpc(TEST_RPC, null, makeRPCInput("foo"));
161             rpcBroker1.tell(invokeMsg, getRef());
162
163             probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
164             probeReg1.reply(new akka.actor.Status.Failure(new TestException()));
165
166             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
167                     akka.actor.Status.Failure.class);
168
169             assertEquals("failure.cause()", TestException.class, failure.cause().getClass());
170         }};
171     }
172
173     @Test
174     public void testExecuteRpc() {
175         new JavaTestKit(node1) {{
176
177             String xml = "<input xmlns=\"urn:test\"><input-data>foo</input-data></input>";
178
179             CompositeNode invokeRpcResult = makeRPCOutput("bar");
180             RpcResult<CompositeNode> rpcResult =
181                                RpcResultBuilder.<CompositeNode>success(invokeRpcResult).build();
182             ArgumentCaptor<CompositeNode> inputCaptor = new ArgumentCaptor<>();
183             when(brokerSession.rpc(eq(TEST_RPC), inputCaptor.capture()))
184                     .thenReturn(Futures.immediateFuture(rpcResult));
185
186             ExecuteRpc executeMsg = new ExecuteRpc(xml, TEST_RPC);
187
188             rpcBroker1.tell(executeMsg, getRef());
189
190             RpcResponse rpcResponse = expectMsgClass(duration("5 seconds"), RpcResponse.class);
191
192             assertCompositeNodeEquals((CompositeNode)invokeRpcResult.getValue().get(0),
193                     XmlUtils.xmlToCompositeNode(rpcResponse.getResultCompositeNode()));
194         }};
195     }
196
197     @Test
198     public void testExecuteRpcFailureWithRpcErrors() {
199         new JavaTestKit(node1) {{
200
201             String xml = "<input xmlns=\"urn:test\"><input-data>foo</input-data></input>";
202
203             RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>failed()
204                     .withError(ErrorType.RPC, "tag1", "error", "appTag1", "info1",
205                             new Exception("mock"))
206                     .withWarning(ErrorType.PROTOCOL, "tag2", "warning", "appTag2", "info2", null)
207                     .build();
208             when(brokerSession.rpc(eq(TEST_RPC), any(CompositeNode.class)))
209                     .thenReturn(Futures.immediateFuture(rpcResult));
210
211             ExecuteRpc executeMsg = new ExecuteRpc(xml, TEST_RPC);
212
213             rpcBroker1.tell(executeMsg, getRef());
214
215             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
216                     akka.actor.Status.Failure.class);
217
218             assertEquals("failure.cause()", RpcErrorsException.class, failure.cause().getClass());
219
220             RpcErrorsException errorsEx = (RpcErrorsException)failure.cause();
221             List<RpcError> rpcErrors = Lists.newArrayList(errorsEx.getRpcErrors());
222             assertEquals("RpcErrors count", 2, rpcErrors.size());
223             assertRpcErrorEquals(rpcErrors.get(0), ErrorSeverity.ERROR, ErrorType.RPC, "tag1",
224                     "error", "appTag1", "info1", "mock");
225             assertRpcErrorEquals(rpcErrors.get(1), ErrorSeverity.WARNING, ErrorType.PROTOCOL, "tag2",
226                     "warning", "appTag2", "info2", null);
227         }};
228     }
229
230     @Test
231     public void testExecuteRpcFailureWithNoRpcErrors() {
232         new JavaTestKit(node1) {{
233
234             String xml = "<input xmlns=\"urn:test\"><input-data>foo</input-data></input>";
235
236             RpcResult<CompositeNode> rpcResult = RpcResultBuilder.<CompositeNode>failed().build();
237             when(brokerSession.rpc(eq(TEST_RPC), any(CompositeNode.class)))
238                     .thenReturn(Futures.immediateFuture(rpcResult));
239
240             ExecuteRpc executeMsg = new ExecuteRpc(xml, TEST_RPC);
241
242             rpcBroker1.tell(executeMsg, getRef());
243
244             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
245                     akka.actor.Status.Failure.class);
246
247             assertEquals("failure.cause()", RpcErrorsException.class, failure.cause().getClass());
248
249             RpcErrorsException errorsEx = (RpcErrorsException)failure.cause();
250             List<RpcError> rpcErrors = Lists.newArrayList(errorsEx.getRpcErrors());
251             assertEquals("RpcErrors count", 1, rpcErrors.size());
252             assertRpcErrorEquals(rpcErrors.get(0), ErrorSeverity.ERROR, ErrorType.RPC,
253                     "operation-failed", "failed", null, null, null);
254         }};
255     }
256
257     @Test
258     public void testExecuteRpcFailureWithException() {
259         new JavaTestKit(node1) {{
260
261             String xml = "<input xmlns=\"urn:test\"><input-data>foo</input-data></input>";
262
263             when(brokerSession.rpc(eq(TEST_RPC), any(CompositeNode.class)))
264                     .thenReturn(Futures.<RpcResult<CompositeNode>>immediateFailedFuture(
265                             new TestException()));
266
267             ExecuteRpc executeMsg = new ExecuteRpc(xml, TEST_RPC);
268
269             rpcBroker1.tell(executeMsg, getRef());
270
271             akka.actor.Status.Failure failure = expectMsgClass(duration("5 seconds"),
272                     akka.actor.Status.Failure.class);
273
274             assertEquals("failure.cause()", TestException.class, failure.cause().getClass());
275         }};
276     }
277 }