Bump mdsal to 4.0.0
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RemoteRpcImplementationTest.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.doThrow;
17 import static org.mockito.Mockito.when;
18
19 import com.google.common.util.concurrent.ListenableFuture;
20 import java.util.concurrent.ExecutionException;
21 import java.util.concurrent.TimeUnit;
22 import org.junit.Ignore;
23 import org.junit.Test;
24 import org.mockito.ArgumentCaptor;
25 import org.opendaylight.mdsal.dom.api.DOMRpcException;
26 import org.opendaylight.mdsal.dom.api.DOMRpcResult;
27 import org.opendaylight.mdsal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.yangtools.util.concurrent.FluentFutures;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
32
33 /**
34  * Unit tests for RemoteRpcImplementation.
35  *
36  * @author Thomas Pantelis
37  */
38 public class RemoteRpcImplementationTest extends AbstractRpcTest {
39
40     /**
41      * This test method invokes and executes the remote rpc.
42      */
43     @Test
44     public void testInvokeRpc() throws Exception {
45         final ContainerNode rpcOutput = makeRPCOutput("bar");
46         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
47
48         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
49         @SuppressWarnings({"unchecked", "rawtypes"})
50         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
51                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
52
53         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
54                 FluentFutures.immediateFluentFuture(rpcResult));
55
56         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
57         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
58
59         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
60         assertEquals(rpcOutput, result.getResult());
61     }
62
63     /**
64      * This test method invokes and executes the remote rpc.
65      */
66     @Test
67     public void testInvokeRpcWithNullInput() throws Exception {
68         final ContainerNode rpcOutput = makeRPCOutput("bar");
69         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
70
71         @SuppressWarnings({"unchecked", "rawtypes"})
72         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
73                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
74
75         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
76                 FluentFutures.immediateFluentFuture(rpcResult));
77
78         ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
79         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
80
81         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
82         assertEquals(rpcOutput, result.getResult());
83     }
84
85     /**
86      * This test method invokes and executes the remote rpc.
87      */
88     @Test
89     public void testInvokeRpcWithNoOutput() throws Exception {
90         final ContainerNode rpcOutput = null;
91         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
92
93         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
94         @SuppressWarnings({"unchecked", "rawtypes"})
95         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
96                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
97
98         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
99                 FluentFutures.immediateFluentFuture(rpcResult));
100
101         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
102         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
103
104         final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
105         assertNull(result.getResult());
106     }
107
108     /**
109      * This test method invokes and executes the remote rpc.
110      */
111     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
112     @Test(expected = DOMRpcException.class)
113     public void testInvokeRpcWithRemoteFailedFuture() throws Throwable {
114         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
115         @SuppressWarnings({"unchecked", "rawtypes"})
116         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
117                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
118
119         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
120                 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
121
122         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
123         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
124
125         try {
126             frontEndFuture.get(5, TimeUnit.SECONDS);
127         } catch (ExecutionException e) {
128             throw e.getCause();
129         }
130     }
131
132     /**
133      * This test method invokes and tests exceptions when akka timeout occured
134      * Currently ignored since this test with current config takes around 15 seconds to complete.
135      */
136     @Ignore
137     @Test(expected = RemoteDOMRpcException.class)
138     public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
139         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
140         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
141         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
142
143         frontEndFuture.get(20, TimeUnit.SECONDS);
144     }
145
146     /**
147      * This test method invokes remote rpc and lookup failed
148      * with runtime exception.
149      */
150     @Test(expected = DOMRpcException.class)
151     @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
152     public void testInvokeRpcWithLookupException() throws Throwable {
153         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
154
155         doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
156             any(NormalizedNode.class));
157
158         final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
159         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
160
161         try {
162             frontEndFuture.get(5, TimeUnit.SECONDS);
163         } catch (ExecutionException e) {
164             throw e.getCause();
165         }
166     }
167 }