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;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.mockito.ArgumentMatchers.any;
15 import static org.mockito.ArgumentMatchers.eq;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.when;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import java.util.Collections;
22 import java.util.concurrent.ExecutionException;
23 import java.util.concurrent.TimeUnit;
24 import org.junit.Ignore;
25 import org.junit.Test;
26 import org.mockito.ArgumentCaptor;
27 import org.opendaylight.mdsal.dom.api.DOMActionException;
28 import org.opendaylight.mdsal.dom.api.DOMActionResult;
29 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
30 import org.opendaylight.mdsal.dom.api.DOMRpcException;
31 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
32 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
33 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
34 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
35 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
40 * Unit tests for RemoteRpcImplementation.
42 * @author Thomas Pantelis
44 public class RemoteOpsImplementationTest extends AbstractOpsTest {
47 * This test method invokes and executes the remote rpc.
50 public void testInvokeRpc() throws Exception {
51 final ContainerNode rpcOutput = makeRPCOutput("bar");
52 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
54 // Answer<FluentFuture<DOMRpcResult>> answer = FluentFutures.immediateFluentFuture(rpcResult);
56 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
57 @SuppressWarnings({"unchecked", "rawtypes"})
58 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
59 ArgumentCaptor.forClass(NormalizedNode.class);
61 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
62 FluentFutures.immediateFluentFuture(rpcResult));
64 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
65 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
67 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
68 assertEquals(rpcOutput, result.getResult());
72 * This test method invokes and executes the remote action.
75 public void testInvokeAction() throws Exception {
76 final ContainerNode actionOutput = makeRPCOutput("bar");
77 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput, Collections.emptyList());
78 final NormalizedNode<?, ?> invokeActionInput = makeRPCInput("foo");
79 @SuppressWarnings({"unchecked", "rawtypes"})
80 final ArgumentCaptor<ContainerNode> inputCaptor =
81 ArgumentCaptor.forClass(ContainerNode.class);
82 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
83 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
84 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
85 TEST_DATA_TREE_ID, (ContainerNode) invokeActionInput);
86 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
87 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
88 assertEquals(actionOutput, result.getOutput().get());
93 * This test method invokes and executes the remote rpc.
96 public void testInvokeRpcWithNullInput() throws Exception {
97 final ContainerNode rpcOutput = makeRPCOutput("bar");
98 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
100 @SuppressWarnings({"unchecked", "rawtypes"})
101 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
102 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
104 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
105 FluentFutures.immediateFluentFuture(rpcResult));
107 ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
108 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
110 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
111 assertEquals(rpcOutput, result.getResult());
115 * This test method invokes and executes the remote action.
118 public void testInvokeActionWithNullInput() throws Exception {
119 final ContainerNode actionOutput = makeRPCOutput("bar");
120 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
122 @SuppressWarnings({"unchecked", "rawtypes"})
123 final ArgumentCaptor<ContainerNode> inputCaptor =
124 ArgumentCaptor.forClass(ContainerNode.class);
125 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
126 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
128 ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
129 TEST_DATA_TREE_ID, actionOutput);
130 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
132 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
133 assertEquals(actionOutput, result.getOutput().get());
137 * This test method invokes and executes the remote rpc.
140 public void testInvokeRpcWithNoOutput() throws Exception {
141 final ContainerNode rpcOutput = null;
142 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
144 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
145 @SuppressWarnings({"unchecked", "rawtypes"})
146 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
147 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
149 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
150 FluentFutures.immediateFluentFuture(rpcResult));
152 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
153 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
155 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
156 assertNull(result.getResult());
160 * This test method invokes and executes the remote rpc.
162 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
163 @Test(expected = DOMRpcException.class)
164 public void testInvokeRpcWithRemoteFailedFuture() throws Throwable {
165 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
166 @SuppressWarnings({"unchecked", "rawtypes"})
167 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
168 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
170 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
171 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
173 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
174 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
177 frontEndFuture.get(5, TimeUnit.SECONDS);
178 } catch (ExecutionException e) {
184 * This test method invokes and executes the remote rpc.
186 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
187 @Test(expected = DOMActionException.class)
188 public void testInvokeActionWithRemoteFailedFuture() throws Throwable {
189 final ContainerNode invokeActionInput = makeRPCInput("foo");
190 @SuppressWarnings({"unchecked", "rawtypes"})
191 final ArgumentCaptor<ContainerNode> inputCaptor =
192 ArgumentCaptor.forClass(ContainerNode.class);
194 when(domActionService2.invokeAction(eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID),
195 inputCaptor.capture())).thenReturn(FluentFutures.immediateFailedFluentFuture(
196 new RemoteDOMRpcException("Test Exception", null)));
198 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
199 TEST_DATA_TREE_ID, invokeActionInput);
200 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
203 frontEndFuture.get(5, TimeUnit.SECONDS);
204 } catch (ExecutionException e) {
210 * This test method invokes and tests exceptions when akka timeout occured
211 * Currently ignored since this test with current config takes around 15 seconds to complete.
214 @Test(expected = RemoteDOMRpcException.class)
215 public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
216 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
217 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
218 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
220 frontEndFuture.get(20, TimeUnit.SECONDS);
224 * This test method invokes remote rpc and lookup failed
225 * with runtime exception.
227 @Test(expected = DOMRpcException.class)
228 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
229 public void testInvokeRpcWithLookupException() throws Throwable {
230 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
232 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
233 any(NormalizedNode.class));
235 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
236 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
239 frontEndFuture.get(5, TimeUnit.SECONDS);
240 } catch (ExecutionException e) {
246 * This test method invokes remote rpc and lookup failed
247 * with runtime exception.
249 @Test(expected = DOMActionException.class)
250 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
251 public void testInvokeActionWithLookupException() throws Throwable {
252 final ContainerNode invokeRpcInput = makeRPCInput("foo");
254 doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
255 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
257 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
258 TEST_DATA_TREE_ID, invokeRpcInput);
259 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
262 frontEndFuture.get(5, TimeUnit.SECONDS);
263 } catch (ExecutionException e) {