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