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.common.QName;
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.stmt.SchemaNodeIdentifier.Absolute;
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 final NormalizedNode invokeRpcInput = makeRPCInput("foo");
55 @SuppressWarnings({"unchecked", "rawtypes"})
56 final ArgumentCaptor<NormalizedNode> inputCaptor =
57 ArgumentCaptor.forClass(NormalizedNode.class);
59 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
60 .invokeRpc(eq(TEST_RPC), inputCaptor.capture());
62 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
63 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
65 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
66 assertEquals(rpcOutput, result.getResult());
70 * This test method invokes and executes the remote action.
73 public void testInvokeAction() throws Exception {
74 final ContainerNode actionOutput = makeRPCOutput("bar");
75 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput, Collections.emptyList());
76 final NormalizedNode invokeActionInput = makeRPCInput("foo");
77 @SuppressWarnings({"unchecked", "rawtypes"})
78 final ArgumentCaptor<ContainerNode> inputCaptor =
79 ArgumentCaptor.forClass(ContainerNode.class);
80 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
81 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
82 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
83 TEST_DATA_TREE_ID, (ContainerNode) invokeActionInput);
84 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
85 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
86 assertEquals(actionOutput, result.getOutput().get());
91 * This test method invokes and executes the remote rpc.
94 public void testInvokeRpcWithNullInput() throws Exception {
95 final ContainerNode rpcOutput = makeRPCOutput("bar");
96 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
98 @SuppressWarnings({"unchecked", "rawtypes"})
99 final ArgumentCaptor<NormalizedNode> inputCaptor =
100 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
102 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
103 .invokeRpc(eq(TEST_RPC), inputCaptor.capture());
105 ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
106 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
108 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
109 assertEquals(rpcOutput, result.getResult());
113 * This test method invokes and executes the remote action.
116 public void testInvokeActionWithNullInput() throws Exception {
117 final ContainerNode actionOutput = makeRPCOutput("bar");
118 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
120 @SuppressWarnings({"unchecked", "rawtypes"})
121 final ArgumentCaptor<ContainerNode> inputCaptor =
122 ArgumentCaptor.forClass(ContainerNode.class);
123 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
124 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
126 ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
127 TEST_DATA_TREE_ID, actionOutput);
128 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
130 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
131 assertEquals(actionOutput, result.getOutput().get());
135 * This test method invokes and executes the remote rpc.
138 public void testInvokeRpcWithNoOutput() throws Exception {
139 final ContainerNode rpcOutput = null;
140 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
142 final NormalizedNode invokeRpcInput = makeRPCInput("foo");
143 @SuppressWarnings({"unchecked", "rawtypes"})
144 final ArgumentCaptor<NormalizedNode> inputCaptor =
145 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
147 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
148 .invokeRpc(eq(TEST_RPC), inputCaptor.capture());
150 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
151 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
153 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
154 assertNull(result.getResult());
158 * This test method invokes and executes the remote rpc.
160 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
161 @Test(expected = DOMRpcException.class)
162 public void testInvokeRpcWithRemoteFailedFuture() throws Throwable {
163 final NormalizedNode invokeRpcInput = makeRPCInput("foo");
164 @SuppressWarnings({"unchecked", "rawtypes"})
165 final ArgumentCaptor<NormalizedNode> inputCaptor =
166 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
168 when(domRpcService2.invokeRpc(eq(TEST_RPC), inputCaptor.capture())).thenReturn(
169 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
171 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
172 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
175 frontEndFuture.get(5, TimeUnit.SECONDS);
176 } catch (ExecutionException e) {
182 * This test method invokes and executes the remote rpc.
184 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
185 @Test(expected = DOMActionException.class)
186 public void testInvokeActionWithRemoteFailedFuture() throws Throwable {
187 final ContainerNode invokeActionInput = makeRPCInput("foo");
188 @SuppressWarnings({"unchecked", "rawtypes"})
189 final ArgumentCaptor<ContainerNode> inputCaptor =
190 ArgumentCaptor.forClass(ContainerNode.class);
192 when(domActionService2.invokeAction(eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID),
193 inputCaptor.capture())).thenReturn(FluentFutures.immediateFailedFluentFuture(
194 new RemoteDOMRpcException("Test Exception", null)));
196 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
197 TEST_DATA_TREE_ID, invokeActionInput);
198 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
201 frontEndFuture.get(5, TimeUnit.SECONDS);
202 } catch (ExecutionException e) {
208 * This test method invokes and tests exceptions when akka timeout occured
209 * Currently ignored since this test with current config takes around 15 seconds to complete.
212 @Test(expected = RemoteDOMRpcException.class)
213 public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
214 final NormalizedNode invokeRpcInput = makeRPCInput("foo");
215 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
216 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
218 frontEndFuture.get(20, TimeUnit.SECONDS);
222 * This test method invokes remote rpc and lookup failed
223 * with runtime exception.
225 @Test(expected = DOMRpcException.class)
226 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
227 public void testInvokeRpcWithLookupException() throws Throwable {
228 final NormalizedNode invokeRpcInput = makeRPCInput("foo");
230 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(QName.class),
231 any(NormalizedNode.class));
233 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
234 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
237 frontEndFuture.get(5, TimeUnit.SECONDS);
238 } catch (ExecutionException e) {
244 * This test method invokes remote rpc and lookup failed
245 * with runtime exception.
247 @Test(expected = DOMActionException.class)
248 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
249 public void testInvokeActionWithLookupException() throws Throwable {
250 final ContainerNode invokeRpcInput = makeRPCInput("foo");
252 doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(Absolute.class),
253 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
255 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
256 TEST_DATA_TREE_ID, invokeRpcInput);
257 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
260 frontEndFuture.get(5, TimeUnit.SECONDS);
261 } catch (ExecutionException e) {