6a7681223eca790c0b78e40ecb2149f668ea3fa6
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RemoteOpsImplementationTest.java
1 /*
2  * Copyright (c) 2014 Brocade Communications 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 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;
19
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;
38
39 /**
40  * Unit tests for RemoteRpcImplementation.
41  *
42  * @author Thomas Pantelis
43  */
44 public class RemoteOpsImplementationTest extends AbstractOpsTest {
45
46     /**
47      * This test method invokes and executes the remote rpc.
48      */
49     @Test
50     public void testInvokeRpc() throws Exception {
51         final ContainerNode rpcOutput = makeRPCOutput("bar");
52         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
53
54 //        Answer<FluentFuture<DOMRpcResult>> answer = FluentFutures.immediateFluentFuture(rpcResult);
55
56         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
57         @SuppressWarnings({"unchecked", "rawtypes"})
58         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
59                 ArgumentCaptor.forClass(NormalizedNode.class);
60
61         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
62                 FluentFutures.immediateFluentFuture(rpcResult));
63
64         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
65         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
66
67         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
68         assertEquals(rpcOutput, result.getResult());
69     }
70
71     /**
72      * This test method invokes and executes the remote action.
73      */
74     @Test
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());
89
90     }
91
92     /**
93      * This test method invokes and executes the remote rpc.
94      */
95     @Test
96     public void testInvokeRpcWithNullInput() throws Exception {
97         final ContainerNode rpcOutput = makeRPCOutput("bar");
98         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
99
100         @SuppressWarnings({"unchecked", "rawtypes"})
101         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
102                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
103
104         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
105                 FluentFutures.immediateFluentFuture(rpcResult));
106
107         ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
108         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
109
110         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
111         assertEquals(rpcOutput, result.getResult());
112     }
113
114     /**
115      * This test method invokes and executes the remote action.
116      */
117     @Test
118     public void testInvokeActionWithNullInput() throws Exception {
119         final ContainerNode actionOutput = makeRPCOutput("bar");
120         final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
121
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());
127
128         ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
129                 TEST_DATA_TREE_ID, actionOutput);
130         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
131
132         final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
133         assertEquals(actionOutput, result.getOutput().get());
134     }
135
136     /**
137      * This test method invokes and executes the remote rpc.
138      */
139     @Test
140     public void testInvokeRpcWithNoOutput() throws Exception {
141         final ContainerNode rpcOutput = null;
142         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
143
144         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
145         @SuppressWarnings({"unchecked", "rawtypes"})
146         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
147                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
148
149         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
150                 FluentFutures.immediateFluentFuture(rpcResult));
151
152         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
153         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
154
155         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
156         assertNull(result.getResult());
157     }
158
159     /**
160      * This test method invokes and executes the remote rpc.
161      */
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);
169
170         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
171                 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
172
173         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
174         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
175
176         try {
177             frontEndFuture.get(5, TimeUnit.SECONDS);
178         } catch (ExecutionException e) {
179             throw e.getCause();
180         }
181     }
182
183     /**
184      * This test method invokes and executes the remote rpc.
185      */
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);
193
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)));
197
198         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
199                 TEST_DATA_TREE_ID, invokeActionInput);
200         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
201
202         try {
203             frontEndFuture.get(5, TimeUnit.SECONDS);
204         } catch (ExecutionException e) {
205             throw e.getCause();
206         }
207     }
208
209     /**
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.
212      */
213     @Ignore
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);
219
220         frontEndFuture.get(20, TimeUnit.SECONDS);
221     }
222
223     /**
224      * This test method invokes remote rpc and lookup failed
225      * with runtime exception.
226      */
227     @Test(expected = DOMRpcException.class)
228     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
229     public void testInvokeRpcWithLookupException() throws Throwable {
230         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
231
232         doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
233             any(NormalizedNode.class));
234
235         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
236         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
237
238         try {
239             frontEndFuture.get(5, TimeUnit.SECONDS);
240         } catch (ExecutionException e) {
241             throw e.getCause();
242         }
243     }
244
245     /**
246      * This test method invokes remote rpc and lookup failed
247      * with runtime exception.
248      */
249     @Test(expected = DOMActionException.class)
250     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
251     public void testInvokeActionWithLookupException() throws Throwable {
252         final ContainerNode invokeRpcInput = makeRPCInput("foo");
253
254         doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
255                 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
256
257         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
258                 TEST_DATA_TREE_ID, invokeRpcInput);
259         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
260
261         try {
262             frontEndFuture.get(5, TimeUnit.SECONDS);
263         } catch (ExecutionException e) {
264             throw e.getCause();
265         }
266     }
267 }