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.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.when;
19 import com.google.common.util.concurrent.CheckedFuture;
20 import com.google.common.util.concurrent.Futures;
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.controller.md.sal.dom.api.DOMRpcException;
26 import org.opendaylight.controller.md.sal.dom.api.DOMRpcResult;
27 import org.opendaylight.controller.md.sal.dom.spi.DefaultDOMRpcResult;
28 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
33 * Unit tests for RemoteRpcImplementation.
35 * @author Thomas Pantelis
37 public class RemoteRpcImplementationTest extends AbstractRpcTest {
40 * This test method invokes and executes the remote rpc.
43 public void testInvokeRpc() throws Exception {
44 final ContainerNode rpcOutput = makeRPCOutput("bar");
45 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
47 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
48 @SuppressWarnings({"unchecked", "rawtypes"})
49 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
50 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
52 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
53 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
55 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
56 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
57 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
59 final DOMRpcResult result = frontEndFuture.checkedGet(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 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
78 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
79 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
80 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
82 final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
83 assertEquals(rpcOutput, result.getResult());
87 * This test method invokes and executes the remote rpc.
90 public void testInvokeRpcWithNoOutput() throws Exception {
91 final ContainerNode rpcOutput = null;
92 final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
94 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
95 @SuppressWarnings({"unchecked", "rawtypes"})
96 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
97 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
99 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
100 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
102 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
103 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
104 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
106 final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
107 assertNull(result.getResult());
111 * This test method invokes and executes the remote rpc.
113 @Test(expected = DOMRpcException.class)
114 public void testInvokeRpcWithRemoteFailedFuture() throws Exception {
115 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
116 @SuppressWarnings({"unchecked", "rawtypes"})
117 final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
118 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
120 when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
121 Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new RemoteDOMRpcException(
122 "Test Exception", null)));
124 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
125 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
126 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
127 frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
131 * This test method invokes and tests exceptions when akka timeout occured
132 * Currently ignored since this test with current config takes around 15 seconds to complete.
135 @Test(expected = RemoteDOMRpcException.class)
136 public void testInvokeRpcWithAkkaTimeoutException() throws Exception {
137 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
138 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
139 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
140 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
142 frontEndFuture.checkedGet(20, TimeUnit.SECONDS);
146 * This test method invokes remote rpc and lookup failed
147 * with runtime exception.
149 @Test(expected = DOMRpcException.class)
150 public void testInvokeRpcWithLookupException() throws Exception {
151 final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
153 doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
154 any(NormalizedNode.class));
156 final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
157 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
158 assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
160 frontEndFuture.checkedGet(5, TimeUnit.SECONDS);