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
8 package org.opendaylight.controller.remote.rpc;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.mockito.ArgumentMatchers.any;
14 import static org.mockito.ArgumentMatchers.eq;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.when;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.Collections;
21 import java.util.concurrent.ExecutionException;
22 import java.util.concurrent.TimeUnit;
23 import org.junit.Ignore;
24 import org.junit.Test;
25 import org.mockito.ArgumentCaptor;
26 import org.opendaylight.mdsal.dom.api.DOMActionException;
27 import org.opendaylight.mdsal.dom.api.DOMActionResult;
28 import org.opendaylight.mdsal.dom.api.DOMDataTreeIdentifier;
29 import org.opendaylight.mdsal.dom.api.DOMRpcException;
30 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
31 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
32 import org.opendaylight.mdsal.dom.spi.SimpleDOMActionResult;
33 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
39 * Unit tests for RemoteRpcImplementation.
41 * @author Thomas Pantelis
43 public class RemoteOpsImplementationTest extends AbstractOpsTest {
46 * This test method invokes and executes the remote rpc.
49 public void testInvokeRpc() throws Exception {
50 final ContainerNode rpcOutput = makeRPCOutput("bar");
51 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
53 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
54 @SuppressWarnings({"unchecked", "rawtypes"})
55 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
56 ArgumentCaptor.forClass(NormalizedNode.class);
58 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
59 .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
61 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
62 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
64 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
65 assertEquals(rpcOutput, result.getResult());
69 * This test method invokes and executes the remote action.
72 public void testInvokeAction() throws Exception {
73 final ContainerNode actionOutput = makeRPCOutput("bar");
74 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput, Collections.emptyList());
75 final NormalizedNode<?, ?> invokeActionInput = makeRPCInput("foo");
76 @SuppressWarnings({"unchecked", "rawtypes"})
77 final ArgumentCaptor<ContainerNode> inputCaptor =
78 ArgumentCaptor.forClass(ContainerNode.class);
79 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
80 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
81 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
82 TEST_DATA_TREE_ID, (ContainerNode) invokeActionInput);
83 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
84 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
85 assertEquals(actionOutput, result.getOutput().get());
90 * This test method invokes and executes the remote rpc.
93 public void testInvokeRpcWithNullInput() throws Exception {
94 final ContainerNode rpcOutput = makeRPCOutput("bar");
95 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
97 @SuppressWarnings({"unchecked", "rawtypes"})
98 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
99 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
101 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
102 .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
104 ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
105 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
107 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
108 assertEquals(rpcOutput, result.getResult());
112 * This test method invokes and executes the remote action.
115 public void testInvokeActionWithNullInput() throws Exception {
116 final ContainerNode actionOutput = makeRPCOutput("bar");
117 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
119 @SuppressWarnings({"unchecked", "rawtypes"})
120 final ArgumentCaptor<ContainerNode> inputCaptor =
121 ArgumentCaptor.forClass(ContainerNode.class);
122 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
123 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
125 ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
126 TEST_DATA_TREE_ID, actionOutput);
127 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
129 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
130 assertEquals(actionOutput, result.getOutput().get());
134 * This test method invokes and executes the remote rpc.
137 public void testInvokeRpcWithNoOutput() throws Exception {
138 final ContainerNode rpcOutput = null;
139 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
141 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
142 @SuppressWarnings({"unchecked", "rawtypes"})
143 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
144 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
146 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
147 .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
149 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
150 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
152 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
153 assertNull(result.getResult());
157 * This test method invokes and executes the remote rpc.
159 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
160 @Test(expected = DOMRpcException.class)
161 public void testInvokeRpcWithRemoteFailedFuture() throws Throwable {
162 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
163 @SuppressWarnings({"unchecked", "rawtypes"})
164 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
165 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
167 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
168 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
170 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
171 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
174 frontEndFuture.get(5, TimeUnit.SECONDS);
175 } catch (ExecutionException e) {
181 * This test method invokes and executes the remote rpc.
183 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
184 @Test(expected = DOMActionException.class)
185 public void testInvokeActionWithRemoteFailedFuture() throws Throwable {
186 final ContainerNode invokeActionInput = makeRPCInput("foo");
187 @SuppressWarnings({"unchecked", "rawtypes"})
188 final ArgumentCaptor<ContainerNode> inputCaptor =
189 ArgumentCaptor.forClass(ContainerNode.class);
191 when(domActionService2.invokeAction(eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID),
192 inputCaptor.capture())).thenReturn(FluentFutures.immediateFailedFluentFuture(
193 new RemoteDOMRpcException("Test Exception", null)));
195 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
196 TEST_DATA_TREE_ID, invokeActionInput);
197 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
200 frontEndFuture.get(5, TimeUnit.SECONDS);
201 } catch (ExecutionException e) {
207 * This test method invokes and tests exceptions when akka timeout occured
208 * Currently ignored since this test with current config takes around 15 seconds to complete.
211 @Test(expected = RemoteDOMRpcException.class)
212 public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
213 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
214 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
215 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
217 frontEndFuture.get(20, TimeUnit.SECONDS);
221 * This test method invokes remote rpc and lookup failed
222 * with runtime exception.
224 @Test(expected = DOMRpcException.class)
225 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
226 public void testInvokeRpcWithLookupException() throws Throwable {
227 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
229 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
230 any(NormalizedNode.class));
232 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
233 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
236 frontEndFuture.get(5, TimeUnit.SECONDS);
237 } catch (ExecutionException e) {
243 * This test method invokes remote rpc and lookup failed
244 * with runtime exception.
246 @Test(expected = DOMActionException.class)
247 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
248 public void testInvokeActionWithLookupException() throws Throwable {
249 final ContainerNode invokeRpcInput = makeRPCInput("foo");
251 doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
252 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
254 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
255 TEST_DATA_TREE_ID, invokeRpcInput);
256 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
259 frontEndFuture.get(5, TimeUnit.SECONDS);
260 } catch (ExecutionException e) {