Adjust for RPCService methods changing
[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.model.api.SchemaPath;
37
38 /**
39  * Unit tests for RemoteRpcImplementation.
40  *
41  * @author Thomas Pantelis
42  */
43 public class RemoteOpsImplementationTest extends AbstractOpsTest {
44
45     /**
46      * This test method invokes and executes the remote rpc.
47      */
48     @Test
49     public void testInvokeRpc() throws Exception {
50         final ContainerNode rpcOutput = makeRPCOutput("bar");
51         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
52
53         final ContainerNode invokeRpcInput = makeRPCInput("foo");
54         final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
55
56         doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
57             .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
58
59         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
60         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
61
62         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
63         assertEquals(rpcOutput, result.getResult());
64     }
65
66     /**
67      * This test method invokes and executes the remote action.
68      */
69     @Test
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());
82
83     }
84
85     /**
86      * This test method invokes and executes the remote action.
87      */
88     @Test
89     public void testInvokeActionWithNullInput() throws Exception {
90         final ContainerNode actionOutput = makeRPCOutput("bar");
91         final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
92
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());
96
97         ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
98                 TEST_DATA_TREE_ID, actionOutput);
99         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
100
101         final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
102         assertEquals(actionOutput, result.getOutput().get());
103     }
104
105     /**
106      * This test method invokes and executes the remote rpc.
107      */
108     @Test
109     public void testInvokeRpcWithNoOutput() throws Exception {
110         final ContainerNode rpcOutput = null;
111         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
112
113         final ContainerNode invokeRpcInput = makeRPCInput("foo");
114         final ArgumentCaptor<ContainerNode> inputCaptor = ArgumentCaptor.forClass(ContainerNode.class);
115
116         doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
117             .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
118
119         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
120         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
121
122         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
123         assertNull(result.getResult());
124     }
125
126     /**
127      * This test method invokes and executes the remote rpc.
128      */
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);
134
135         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
136                 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
137
138         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
139         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
140
141         try {
142             frontEndFuture.get(5, TimeUnit.SECONDS);
143         } catch (ExecutionException e) {
144             throw e.getCause();
145         }
146     }
147
148     /**
149      * This test method invokes and executes the remote rpc.
150      */
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);
156
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)));
160
161         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
162                 TEST_DATA_TREE_ID, invokeActionInput);
163         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
164
165         try {
166             frontEndFuture.get(5, TimeUnit.SECONDS);
167         } catch (ExecutionException e) {
168             throw e.getCause();
169         }
170     }
171
172     /**
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.
175      */
176     @Ignore
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);
182
183         frontEndFuture.get(20, TimeUnit.SECONDS);
184     }
185
186     /**
187      * This test method invokes remote rpc and lookup failed
188      * with runtime exception.
189      */
190     @Test(expected = DOMRpcException.class)
191     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
192     public void testInvokeRpcWithLookupException() throws Throwable {
193         final ContainerNode invokeRpcInput = makeRPCInput("foo");
194
195         doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
196             any(ContainerNode.class));
197
198         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
199         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
200
201         try {
202             frontEndFuture.get(5, TimeUnit.SECONDS);
203         } catch (ExecutionException e) {
204             throw e.getCause();
205         }
206     }
207
208     /**
209      * This test method invokes remote rpc and lookup failed
210      * with runtime exception.
211      */
212     @Test(expected = DOMActionException.class)
213     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
214     public void testInvokeActionWithLookupException() throws Throwable {
215         final ContainerNode invokeRpcInput = makeRPCInput("foo");
216
217         doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
218                 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
219
220         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
221                 TEST_DATA_TREE_ID, invokeRpcInput);
222         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
223
224         try {
225             frontEndFuture.get(5, TimeUnit.SECONDS);
226         } catch (ExecutionException e) {
227             throw e.getCause();
228         }
229     }
230 }