2 * Copyright (c) 2014 Cisco 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;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSystem;
14 import akka.testkit.JavaTestKit;
15 import com.google.common.util.concurrent.Futures;
16 import junit.framework.Assert;
17 import org.junit.AfterClass;
18 import org.junit.BeforeClass;
19 import org.junit.Test;
20 import org.mockito.Mockito;
21 import org.opendaylight.controller.remote.rpc.messages.AddRoutedRpc;
22 import org.opendaylight.controller.remote.rpc.messages.AddRpc;
23 import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
24 import org.opendaylight.controller.remote.rpc.messages.InvokeRoutedRpc;
25 import org.opendaylight.controller.remote.rpc.messages.InvokeRpc;
26 import org.opendaylight.controller.remote.rpc.messages.RpcResponse;
27 import org.opendaylight.controller.remote.rpc.registry.ClusterWrapper;
28 import org.opendaylight.controller.remote.rpc.registry.RpcRegistry;
29 import org.opendaylight.controller.sal.common.util.Rpcs;
30 import org.opendaylight.controller.sal.connector.api.RpcRouter;
31 import org.opendaylight.controller.sal.core.api.Broker;
32 import org.opendaylight.yangtools.yang.common.QName;
33 import org.opendaylight.yangtools.yang.common.RpcError;
34 import org.opendaylight.yangtools.yang.common.RpcResult;
35 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
36 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
37 import org.opendaylight.yangtools.yang.data.api.Node;
38 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
39 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 import java.net.URISyntaxException;
43 import java.util.ArrayList;
44 import java.util.Collection;
45 import java.util.HashSet;
47 import java.util.concurrent.Future;
49 import static org.mockito.Mockito.mock;
50 import static org.mockito.Mockito.when;
52 public class RpcBrokerTest {
54 static ActorSystem system;
58 public static void setup() {
59 system = ActorSystem.create();
63 public static void teardown() {
64 JavaTestKit.shutdownActorSystem(system);
69 public void testInvokeRpcError() throws URISyntaxException {
70 new JavaTestKit(system) {{
71 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
72 Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
73 SchemaContext schemaContext = mock(SchemaContext.class);
74 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
75 QName rpc = new QName(new URI("actor1"), "actor1");
76 InvokeRpc invokeMsg = new InvokeRpc(rpc, null);
77 rpcBroker.tell(invokeMsg, getRef());
79 Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
80 protected Boolean match(Object in) {
81 if (in instanceof ErrorResponse) {
82 ErrorResponse reply = (ErrorResponse)in;
83 return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
88 }.get(); // this extracts the received message
90 Assert.assertTrue(getMsg);
95 * This test method invokes and executes the remote rpc
99 public void testInvokeRpc() throws URISyntaxException {
100 new JavaTestKit(system) {{
101 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
102 Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
103 SchemaContext schemaContext = mock(SchemaContext.class);
104 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
105 ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor1");
107 QName rpc = new QName(new URI("actor1"), "actor1");
108 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
109 final String route = rpcBrokerRemote.path().toString();
110 AddRpc rpcMsg = new AddRpc(routeId, route);
111 rpcRegistry.tell(rpcMsg, getRef());
112 expectMsgEquals(duration("2 second"), "Success");
115 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
116 CompositeNode invokeRpcResult = mock(CompositeNode.class);
117 Collection<RpcError> errors = new ArrayList<>();
118 RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
119 Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
120 when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
121 InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
122 rpcBroker.tell(invokeMsg, getRef());
124 //verify response msg
125 Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
126 protected Boolean match(Object in) {
127 if (in instanceof RpcResponse) {
133 }.get(); // this extracts the received message
135 Assert.assertTrue(getMsg);
140 public void testInvokeRoutedRpcError() throws URISyntaxException {
141 new JavaTestKit(system) {{
142 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
143 Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
144 SchemaContext schemaContext = mock(SchemaContext.class);
145 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
146 QName rpc = new QName(new URI("actor1"), "actor1");
147 InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, null);
148 rpcBroker.tell(invokeMsg, getRef());
150 Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
151 protected Boolean match(Object in) {
152 if (in instanceof ErrorResponse) {
153 ErrorResponse reply = (ErrorResponse)in;
154 return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
159 }.get(); // this extracts the received message
161 Assert.assertTrue(getMsg);
166 * This test method invokes and executes the remote routed rpc
170 public void testInvokeRoutedRpc() throws URISyntaxException {
171 new JavaTestKit(system) {{
172 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
173 Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
174 SchemaContext schemaContext = mock(SchemaContext.class);
175 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
176 ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor2");
177 // Add Routed RPC in table
178 QName rpc = new QName(new URI("actor2"), "actor2");
179 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
180 final String route = rpcBrokerRemote.path().toString();
181 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
182 routeIds.add(routeId);
184 AddRoutedRpc rpcMsg = new AddRoutedRpc(routeIds, route);
185 rpcRegistry.tell(rpcMsg, getRef());
186 expectMsgEquals(duration("2 second"), "Success");
189 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
190 CompositeNode invokeRpcResult = mock(CompositeNode.class);
191 Collection<RpcError> errors = new ArrayList<>();
192 RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
193 Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
194 when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
195 InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, input);
196 rpcBroker.tell(invokeMsg, getRef());
198 //verify response msg
199 Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
200 protected Boolean match(Object in) {
201 if (in instanceof RpcResponse) {
207 }.get(); // this extracts the received message
209 Assert.assertTrue(getMsg);