47ce52bf9c21625a9100070747b2da19c5ef1d08
[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 package org.opendaylight.controller.remote.rpc;
9
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;
18
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;
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 NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
54         @SuppressWarnings({"unchecked", "rawtypes"})
55         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
56                 ArgumentCaptor.forClass(NormalizedNode.class);
57
58         doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
59             .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
60
61         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
62         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
63
64         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
65         assertEquals(rpcOutput, result.getResult());
66     }
67
68     /**
69      * This test method invokes and executes the remote action.
70      */
71     @Test
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());
86
87     }
88
89     /**
90      * This test method invokes and executes the remote rpc.
91      */
92     @Test
93     public void testInvokeRpcWithNullInput() throws Exception {
94         final ContainerNode rpcOutput = makeRPCOutput("bar");
95         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
96
97         @SuppressWarnings({"unchecked", "rawtypes"})
98         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
99                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
100
101         doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
102             .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
103
104         ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
105         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
106
107         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
108         assertEquals(rpcOutput, result.getResult());
109     }
110
111     /**
112      * This test method invokes and executes the remote action.
113      */
114     @Test
115     public void testInvokeActionWithNullInput() throws Exception {
116         final ContainerNode actionOutput = makeRPCOutput("bar");
117         final DOMActionResult actionResult = new SimpleDOMActionResult(actionOutput);
118
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());
124
125         ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
126                 TEST_DATA_TREE_ID, actionOutput);
127         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
128
129         final DOMActionResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
130         assertEquals(actionOutput, result.getOutput().get());
131     }
132
133     /**
134      * This test method invokes and executes the remote rpc.
135      */
136     @Test
137     public void testInvokeRpcWithNoOutput() throws Exception {
138         final ContainerNode rpcOutput = null;
139         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
140
141         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
142         @SuppressWarnings({"unchecked", "rawtypes"})
143         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
144                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
145
146         doReturn(FluentFutures.immediateFluentFuture(rpcResult)).when(domRpcService2)
147             .invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture());
148
149         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
150         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
151
152         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
153         assertNull(result.getResult());
154     }
155
156     /**
157      * This test method invokes and executes the remote rpc.
158      */
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);
166
167         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
168                 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
169
170         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
171         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
172
173         try {
174             frontEndFuture.get(5, TimeUnit.SECONDS);
175         } catch (ExecutionException e) {
176             throw e.getCause();
177         }
178     }
179
180     /**
181      * This test method invokes and executes the remote rpc.
182      */
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);
190
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)));
194
195         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
196                 TEST_DATA_TREE_ID, invokeActionInput);
197         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
198
199         try {
200             frontEndFuture.get(5, TimeUnit.SECONDS);
201         } catch (ExecutionException e) {
202             throw e.getCause();
203         }
204     }
205
206     /**
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.
209      */
210     @Ignore
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);
216
217         frontEndFuture.get(20, TimeUnit.SECONDS);
218     }
219
220     /**
221      * This test method invokes remote rpc and lookup failed
222      * with runtime exception.
223      */
224     @Test(expected = DOMRpcException.class)
225     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
226     public void testInvokeRpcWithLookupException() throws Throwable {
227         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
228
229         doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
230             any(NormalizedNode.class));
231
232         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
233         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
234
235         try {
236             frontEndFuture.get(5, TimeUnit.SECONDS);
237         } catch (ExecutionException e) {
238             throw e.getCause();
239         }
240     }
241
242     /**
243      * This test method invokes remote rpc and lookup failed
244      * with runtime exception.
245      */
246     @Test(expected = DOMActionException.class)
247     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
248     public void testInvokeActionWithLookupException() throws Throwable {
249         final ContainerNode invokeRpcInput = makeRPCInput("foo");
250
251         doThrow(new RuntimeException("test")).when(domActionService2).invokeAction(any(SchemaPath.class),
252                 any(DOMDataTreeIdentifier.class), any(ContainerNode.class));
253
254         final ListenableFuture<DOMActionResult> frontEndFuture = remoteActionImpl1.invokeAction(TEST_RPC_TYPE,
255                 TEST_DATA_TREE_ID, invokeRpcInput);
256         assertTrue(frontEndFuture instanceof RemoteDOMActionFuture);
257
258         try {
259             frontEndFuture.get(5, TimeUnit.SECONDS);
260         } catch (ExecutionException e) {
261             throw e.getCause();
262         }
263     }
264 }