Merge "Fix allowable Unix ports range 1024 - 65535"
[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 akka.testkit.JavaTestKit;
12 import com.google.common.util.concurrent.ListenableFuture;
13 import org.junit.Test;
14 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
15 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
16 import org.opendaylight.controller.xml.codec.XmlUtils;
17 import org.opendaylight.yangtools.yang.common.QName;
18 import org.opendaylight.yangtools.yang.common.RpcError.ErrorSeverity;
19 import org.opendaylight.yangtools.yang.common.RpcError.ErrorType;
20 import org.opendaylight.yangtools.yang.common.RpcResult;
21 import org.opendaylight.yangtools.yang.common.RpcResultBuilder;
22 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24
25 import java.net.URI;
26 import java.util.Arrays;
27 import java.util.concurrent.TimeUnit;
28 import java.util.concurrent.atomic.AtomicReference;
29
30 import static org.junit.Assert.assertEquals;
31
32 /***
33  * Unit tests for RemoteRpcImplementation.
34  *
35  * @author Thomas Pantelis
36  */
37 public class RemoteRpcImplementationTest extends AbstractRpcTest {
38
39     @Test
40     public void testInvokeRpc() throws Exception {
41         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
42         try {
43             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
44                     probeReg1.getRef(), schemaContext, getConfig());
45
46             final CompositeNode input = makeRPCInput("foo");
47             final CompositeNode output = makeRPCOutput("bar");
48             final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
49
50             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
51
52             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
53
54             assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
55
56             assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
57             assertEquals("getInput", input, invokeRpcMsg.get().getInput());
58         } finally {
59             if(assertError.get() != null) {
60                 throw assertError.get();
61             }
62         }
63     }
64
65     @Test
66     public void testInvokeRpcWithIdentifier() throws Exception {
67         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
68         try {
69             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
70                     probeReg1.getRef(), schemaContext, getConfig());
71
72             QName instanceQName = new QName(new URI("ns"), "instance");
73             YangInstanceIdentifier identifier = YangInstanceIdentifier.of(instanceQName);
74
75             CompositeNode input = makeRPCInput("foo");
76             CompositeNode output = makeRPCOutput("bar");
77             final AtomicReference<InvokeRpc> invokeRpcMsg = setupInvokeRpcReply(assertError, output);
78
79             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(
80                     TEST_RPC, identifier, input);
81
82             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
83
84             assertSuccessfulRpcResult(rpcResult, (CompositeNode)output.getValue().get(0));
85
86             assertEquals("getRpc", TEST_RPC, invokeRpcMsg.get().getRpc());
87             assertEquals("getInput", input, invokeRpcMsg.get().getInput());
88             assertEquals("getRoute", identifier, invokeRpcMsg.get().getIdentifier());
89         } finally {
90             if(assertError.get() != null) {
91                 throw assertError.get();
92             }
93         }
94     }
95
96     @Test
97     public void testInvokeRpcWithRpcErrorsException() throws Exception {
98         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
99         try {
100             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
101                     probeReg1.getRef(), schemaContext, getConfig());
102
103             final CompositeNode input = makeRPCInput("foo");
104
105             setupInvokeRpcErrorReply(assertError, new RpcErrorsException(
106                     "mock", Arrays.asList(RpcResultBuilder.newError(ErrorType.RPC, "tag",
107                             "error", "appTag", "info", null))));
108
109             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
110
111             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
112
113             assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "tag",
114                     "error", "appTag", "info", null);
115         } finally {
116             if(assertError.get() != null) {
117                 throw assertError.get();
118             }
119         }
120     }
121
122     @Test
123     public void testInvokeRpcWithOtherException() throws Exception {
124         final AtomicReference<AssertionError> assertError = new AtomicReference<>();
125         try {
126             RemoteRpcImplementation rpcImpl = new RemoteRpcImplementation(
127                     probeReg1.getRef(), schemaContext, getConfig());
128
129             final CompositeNode input = makeRPCInput("foo");
130
131             setupInvokeRpcErrorReply(assertError, new TestException());
132
133             ListenableFuture<RpcResult<CompositeNode>> future = rpcImpl.invokeRpc(TEST_RPC, input);
134
135             RpcResult<CompositeNode> rpcResult = future.get(5, TimeUnit.SECONDS);
136
137             assertFailedRpcResult(rpcResult, ErrorSeverity.ERROR, ErrorType.RPC, "operation-failed",
138                     TestException.MESSAGE, null, null, TestException.MESSAGE);
139         } finally {
140             if(assertError.get() != null) {
141                 throw assertError.get();
142             }
143         }
144     }
145
146     private AtomicReference<InvokeRpc> setupInvokeRpcReply(
147             final AtomicReference<AssertionError> assertError, final CompositeNode output) {
148         return setupInvokeRpcReply(assertError, output, null);
149     }
150
151     private AtomicReference<InvokeRpc> setupInvokeRpcErrorReply(
152             final AtomicReference<AssertionError> assertError, final Exception error) {
153         return setupInvokeRpcReply(assertError, null, error);
154     }
155
156     private AtomicReference<InvokeRpc> setupInvokeRpcReply(
157             final AtomicReference<AssertionError> assertError, final CompositeNode output,
158             final Exception error) {
159         final AtomicReference<InvokeRpc> invokeRpcMsg = new AtomicReference<>();
160
161         new Thread() {
162             @Override
163             public void run() {
164                 try {
165                     invokeRpcMsg.set(probeReg1.expectMsgClass(
166                             JavaTestKit.duration("5 seconds"), InvokeRpc.class));
167
168                     if(output != null) {
169                         probeReg1.reply(new RpcResponse(XmlUtils.outputCompositeNodeToXml(
170                                 output, schemaContext)));
171                     } else {
172                         probeReg1.reply(new akka.actor.Status.Failure(error));
173                     }
174
175                 } catch(AssertionError e) {
176                     assertError.set(e);
177                 }
178             }
179
180         }.start();
181
182         return invokeRpcMsg;
183     }
184
185     private RemoteRpcProviderConfig getConfig(){
186         return new RemoteRpcProviderConfig.Builder("unit-test").build();
187     }
188 }