2 * Copyright (c) 2014 Brocade Communications Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.remote.rpc;
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;
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;
34 * Unit tests for RemoteRpcImplementation.
36 * @author Thomas Pantelis
38 public class RemoteRpcImplementationTest extends AbstractRpcTest {
41 * This test method invokes and executes the remote rpc.
44 public void testInvokeRpc() throws Exception {
45 final ContainerNode rpcOutput = makeRPCOutput("bar");
46 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
48 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
49 @SuppressWarnings({"unchecked", "rawtypes"})
50 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
51 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
53 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
54 FluentFutures.immediateFluentFuture(rpcResult));
56 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
57 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
59 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
60 assertEquals(rpcOutput, result.getResult());
64 * This test method invokes and executes the remote rpc.
67 public void testInvokeRpcWithNullInput() throws Exception {
68 final ContainerNode rpcOutput = makeRPCOutput("bar");
69 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
71 @SuppressWarnings({"unchecked", "rawtypes"})
72 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
73 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
75 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
76 FluentFutures.immediateFluentFuture(rpcResult));
78 ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
79 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
81 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
82 assertEquals(rpcOutput, result.getResult());
86 * This test method invokes and executes the remote rpc.
89 public void testInvokeRpcWithNoOutput() throws Exception {
90 final ContainerNode rpcOutput = null;
91 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
93 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
94 @SuppressWarnings({"unchecked", "rawtypes"})
95 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
96 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
98 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
99 FluentFutures.immediateFluentFuture(rpcResult));
101 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
102 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
104 final DOMRpcResult result = frontEndFuture.get(5, TimeUnit.SECONDS);
105 assertNull(result.getResult());
109 * This test method invokes and executes the remote rpc.
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);
119 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
120 FluentFutures.immediateFailedFluentFuture(new RemoteDOMRpcException("Test Exception", null)));
122 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
123 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
126 frontEndFuture.get(5, TimeUnit.SECONDS);
127 } catch (ExecutionException e) {
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.
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);
143 frontEndFuture.get(20, TimeUnit.SECONDS);
147 * This test method invokes remote rpc and lookup failed
148 * with runtime exception.
150 @Test(expected = DOMRpcException.class)
151 @SuppressWarnings({"checkstyle:AvoidHidingCauseException", "checkstyle:IllegalThrows"})
152 public void testInvokeRpcWithLookupException() throws Throwable {
153 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
155 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
156 any(NormalizedNode.class));
158 final ListenableFuture<DOMRpcResult> frontEndFuture = remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
159 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
162 frontEndFuture.get(5, TimeUnit.SECONDS);
163 } catch (ExecutionException e) {