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.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
43 import java.net.URISyntaxException;
44 import java.util.ArrayList;
45 import java.util.Collection;
46 import java.util.HashSet;
48 import java.util.concurrent.Future;
50 import static org.mockito.Mockito.mock;
51 import static org.mockito.Mockito.when;
53 public class RpcBrokerTest {
55 static ActorSystem system;
59 public static void setup() {
60 system = ActorSystem.create();
64 public static void teardown() {
65 JavaTestKit.shutdownActorSystem(system);
70 public void testInvokeRpcError() throws URISyntaxException {
71 new JavaTestKit(system) {{
72 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
73 Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
74 SchemaContext schemaContext = mock(SchemaContext.class);
75 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
76 QName rpc = new QName(new URI("noactor1"), "noactor1");
77 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "no child"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
78 InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
79 rpcBroker.tell(invokeMsg, getRef());
81 Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
82 protected Boolean match(Object in) {
83 if (in instanceof ErrorResponse) {
84 ErrorResponse reply = (ErrorResponse)in;
85 return reply.getException().getMessage().contains("No remote actor found for rpc execution of :");
90 }.get(); // this extracts the received message
92 Assert.assertTrue(getMsg);
97 * This test method invokes and executes the remote rpc
101 public void testInvokeRpc() throws URISyntaxException {
102 new JavaTestKit(system) {{
103 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
104 Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
105 SchemaContext schemaContext = mock(SchemaContext.class);
106 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
107 ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor1");
109 QName rpc = new QName(new URI("actor1"), "actor1");
110 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, null);
111 final String route = rpcBrokerRemote.path().toString();
112 AddRpc rpcMsg = new AddRpc(routeId, route);
113 rpcRegistry.tell(rpcMsg, getRef());
114 expectMsgEquals(duration("2 second"), "Success");
117 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
118 CompositeNode invokeRpcResult = mock(CompositeNode.class);
119 Collection<RpcError> errors = new ArrayList<>();
120 RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
121 Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
122 when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
123 InvokeRpc invokeMsg = new InvokeRpc(rpc, input);
124 rpcBroker.tell(invokeMsg, getRef());
126 //verify response msg
127 Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
128 protected Boolean match(Object in) {
129 if (in instanceof RpcResponse) {
135 }.get(); // this extracts the received message
137 Assert.assertTrue(getMsg);
142 public void testInvokeRoutedRpcError() throws URISyntaxException {
143 new JavaTestKit(system) {{
144 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
145 Broker.ProviderSession brokerSession = Mockito.mock(Broker.ProviderSession.class);
146 SchemaContext schemaContext = mock(SchemaContext.class);
147 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
148 QName rpc = new QName(new URI("actor1"), "actor1");
149 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
150 InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(rpc)), input);
151 rpcBroker.tell(invokeMsg, getRef());
153 Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
154 protected Boolean match(Object in) {
155 if (in instanceof ErrorResponse) {
156 ErrorResponse reply = (ErrorResponse)in;
157 return "No remote actor found for rpc execution.".equals(reply.getException().getMessage());
162 }.get(); // this extracts the received message
164 Assert.assertTrue(getMsg);
169 * This test method invokes and executes the remote routed rpc
173 public void testInvokeRoutedRpc() throws URISyntaxException {
174 new JavaTestKit(system) {{
175 ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(mock(ClusterWrapper.class)));
176 Broker.ProviderSession brokerSession = mock(Broker.ProviderSession.class);
177 SchemaContext schemaContext = mock(SchemaContext.class);
178 ActorRef rpcBroker = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext));
179 ActorRef rpcBrokerRemote = system.actorOf(RpcBroker.props(brokerSession, rpcRegistry, schemaContext), "actor2");
180 // Add Routed RPC in table
181 QName rpc = new QName(new URI("actor2"), "actor2");
182 YangInstanceIdentifier identifier = YangInstanceIdentifier.create(new YangInstanceIdentifier.NodeIdentifier(rpc));
183 RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, rpc, identifier);
184 final String route = rpcBrokerRemote.path().toString();
185 Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
186 routeIds.add(routeId);
188 AddRoutedRpc rpcMsg = new AddRoutedRpc(routeIds, route);
189 rpcRegistry.tell(rpcMsg, getRef());
190 expectMsgEquals(duration("2 second"), "Success");
193 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
194 CompositeNode invokeRpcResult = mock(CompositeNode.class);
195 Collection<RpcError> errors = new ArrayList<>();
196 RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
197 Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
198 when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
199 InvokeRoutedRpc invokeMsg = new InvokeRoutedRpc(rpc, identifier, input);
200 rpcBroker.tell(invokeMsg, getRef());
202 //verify response msg
203 Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
204 protected Boolean match(Object in) {
205 if (in instanceof RpcResponse) {
211 }.get(); // this extracts the received message
213 Assert.assertTrue(getMsg);