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.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 ContainerNode invokeRpcInput = makeRPCInput("foo");
54 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
56 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
57 .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
59 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
60 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
62 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
63 assertEquals(rpcOutput, result.getResult());
67 * This test method invokes and executes the remote action.
70 public void testInvokeAction() throws Exception {
71 final ContainerNode actionOutput = makeRPCOutput("bar");
72 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput, Collections.emptyList());
73 final ContainerNode invokeActionInput = makeRPCInput("foo");
74 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
75 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
76 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
77 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
78 TEST_DATA_TREE_ID, invokeActionInput);
79 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
80 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
81 assertEquals(actionOutput, result.getOutput().get());
86 * This test method invokes and executes the remote action.
89 public void testInvokeActionWithNullInput() throws Exception {
90 final ContainerNode actionOutput = makeRPCOutput("bar");
91 final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
93 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
94 doReturn(FluentFutures.immediateFluentFuture(actionResult)).when(domActionService2).invokeAction(
95 eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID), inputCaptor.capture());
97 ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
98 TEST_DATA_TREE_ID, actionOutput);
99 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
101 final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
102 assertEquals(actionOutput, result.getOutput().get());
106 * This test method invokes and executes the remote rpc.
109 public void testInvokeRpcWithNoOutput() throws Exception {
110 final ContainerNode rpcOutput = null;
111 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
113 final ContainerNode invokeRpcInput = makeRPCInput("foo");
114 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
116 doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
117 .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
119 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
120 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
122 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
123 assertNull(result.getResult());
127 * This test method invokes and executes the remote rpc.
129 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
130 @Test(expected = DOMRpcException.class)
131 public void testInvokeRpcWithRemoteFailedFuture() throws Throwable {
132 final ContainerNode invokeRpcInput = makeRPCInput("foo");
133 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
135 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
136 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
138 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
139 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
142 frontEndFuture.get(5, TimeUnit.SECONDS);
143 } catch (ExecutionException e) {
149 * This test method invokes and executes the remote rpc.
151 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
152 @Test(expected = DOMActionException.class)
153 public void testInvokeActionWithRemoteFailedFuture() throws Throwable {
154 final ContainerNode invokeActionInput = makeRPCInput("foo");
155 final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
157 when(domActionService2.invokeAction(eq(TEST_RPC_TYPE), eq(TEST_DATA_TREE_ID),
158 inputCaptor.capture())).thenReturn(FluentFutures.immediateFailedFluentFuture(
159 new RemoteDOMRpcException("Test Exception", null)));
161 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
162 TEST_DATA_TREE_ID, invokeActionInput);
163 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
166 frontEndFuture.get(5, TimeUnit.SECONDS);
167 } catch (ExecutionException e) {
173 * This test method invokes and tests exceptions when akka timeout occured
174 * Currently ignored since this test with current config takes around 15 seconds to complete.
177 @Test(expected = RemoteDOMRpcException.class)
178 public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
179 final ContainerNode invokeRpcInput = makeRPCInput("foo");
180 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
181 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
183 frontEndFuture.get(20, TimeUnit.SECONDS);
187 * This test method invokes remote rpc and lookup failed
188 * with runtime exception.
190 @Test(expected = DOMRpcException.class)
191 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
192 public void testInvokeRpcWithLookupException() throws Throwable {
193 final ContainerNode invokeRpcInput = makeRPCInput("foo");
195 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
196 any(ContainerNode.class));
198 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
199 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
202 frontEndFuture.get(5, TimeUnit.SECONDS);
203 } catch (ExecutionException e) {
209 * This test method invokes remote rpc and lookup failed
210 * with runtime exception.
212 @Test(expected = DOMActionException.class)
213 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
214 public void testInvokeActionWithLookupException() throws Throwable {
215 final ContainerNode invokeRpcInput = makeRPCInput("foo");
217 doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
218 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
220 final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
221 TEST_DATA_TREE_ID, invokeRpcInput);
222 assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
225 frontEndFuture.get(5, TimeUnit.SECONDS);
226 } catch (ExecutionException e) {