0fe9dfe5e6720c791da24b93c3d06f97430fd85c
[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.Matchers.any;
15 import static org.mockito.Matchers.eq;
16 import static org.mockito.Mockito.doThrow;
17 import static org.mockito.Mockito.when;
18
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;
31
32 /**
33  * Unit tests for RemoteRpcImplementation.
34  *
35  * @author Thomas Pantelis
36  */
37 public class RemoteRpcImplementationTest extends AbstractRpcTest {
38
39     /**
40      * This test method invokes and executes the remote rpc.
41      */
42     @Test
43     public void testInvokeRpc() throws Exception {
44         final ContainerNode rpcOutput = makeRPCOutput("bar");
45         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
46
47         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
48         @SuppressWarnings({"unchecked", "rawtypes"})
49         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
50                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
51
52         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
53                 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
54
55         final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
56                 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
57         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
58
59         final DOMRpcResult result = frontEndFuture.checkedGet(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                 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
77
78         final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
79                 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, null);
80         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
81
82         final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
83         assertEquals(rpcOutput, result.getResult());
84     }
85
86     /**
87      * This test method invokes and executes the remote rpc.
88      */
89     @Test
90     public void testInvokeRpcWithNoOutput() throws Exception {
91         final ContainerNode rpcOutput = null;
92         final DOMRpcResult rpcResult = new DefaultDOMRpcResult(rpcOutput);
93
94         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
95         @SuppressWarnings({"unchecked", "rawtypes"})
96         final ArgumentCaptor<NormalizedNode<?, ?>> inputCaptor =
97                 (ArgumentCaptor) ArgumentCaptor.forClass(NormalizedNode.class);
98
99         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
100                 Futures.<DOMRpcResult, DOMRpcException>immediateCheckedFuture(rpcResult));
101
102         final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
103                 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
104         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
105
106         final DOMRpcResult result = frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
107         assertNull(result.getResult());
108     }
109
110     /**
111      * This test method invokes and executes the remote rpc.
112      */
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);
119
120         when(domRpcService2.invokeRpc(eq(TEST_RPC_TYPE), inputCaptor.capture())).thenReturn(
121                 Futures.<DOMRpcResult, DOMRpcException>immediateFailedCheckedFuture(new RemoteDOMRpcException(
122                         "Test Exception", null)));
123
124         final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
125                 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
126         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
127         frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
128     }
129
130     /**
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.
133      */
134     @Ignore
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);
141
142         frontEndFuture.checkedGet(20, TimeUnit.SECONDS);
143     }
144
145     /**
146      * This test method invokes remote rpc and lookup failed
147      * with runtime exception.
148      */
149     @Test(expected = DOMRpcException.class)
150     public void testInvokeRpcWithLookupException() throws Exception {
151         final NormalizedNode<?, ?> invokeRpcInput = makeRPCInput("foo");
152
153         doThrow(new RuntimeException("test")).when(domRpcService2).invokeRpc(any(SchemaPath.class),
154             any(NormalizedNode.class));
155
156         final CheckedFuture<DOMRpcResult, DOMRpcException> frontEndFuture =
157                 remoteRpcImpl1.invokeRpc(TEST_RPC_ID, invokeRpcInput);
158         assertTrue(frontEndFuture instanceof RemoteDOMRpcFuture);
159
160         frontEndFuture.checkedGet(5, TimeUnit.SECONDS);
161     }
162 }