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.japi.Pair;
15 import akka.testkit.JavaTestKit;
16 import com.google.common.util.concurrent.Futures;
17 import com.typesafe.config.ConfigFactory;
18 import junit.framework.Assert;
19 import org.junit.AfterClass;
20 import org.junit.Before;
21 import org.junit.BeforeClass;
22 import org.junit.Test;
23 import org.mockito.Mockito;
24 import org.opendaylight.controller.remote.rpc.messages.ErrorResponse;
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.RpcRegistry;
28 import org.opendaylight.controller.sal.common.util.Rpcs;
29 import org.opendaylight.controller.sal.core.api.Broker;
30 import org.opendaylight.yangtools.yang.common.QName;
31 import org.opendaylight.yangtools.yang.common.RpcError;
32 import org.opendaylight.yangtools.yang.common.RpcResult;
33 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
34 import org.opendaylight.yangtools.yang.data.api.ModifyAction;
35 import org.opendaylight.yangtools.yang.data.api.Node;
36 import org.opendaylight.yangtools.yang.data.impl.ImmutableCompositeNode;
37 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
40 import java.net.URISyntaxException;
41 import java.util.ArrayList;
42 import java.util.Collection;
43 import java.util.List;
44 import java.util.concurrent.Future;
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
49 public class RpcBrokerTest {
51 static ActorSystem node1;
52 static ActorSystem node2;
53 private ActorRef rpcBroker1;
54 private JavaTestKit probeReg1;
55 private ActorRef rpcBroker2;
56 private JavaTestKit probeReg2;
57 private Broker.ProviderSession brokerSession;
61 public static void setup() throws InterruptedException {
62 node1 = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("memberA"));
63 node2 = ActorSystem.create("opendaylight-rpc", ConfigFactory.load().getConfig("memberB"));
67 public static void teardown() {
68 JavaTestKit.shutdownActorSystem(node1);
69 JavaTestKit.shutdownActorSystem(node2);
75 public void createActor() {
76 brokerSession = Mockito.mock(Broker.ProviderSession.class);
77 SchemaContext schemaContext = mock(SchemaContext.class);
78 probeReg1 = new JavaTestKit(node1);
79 rpcBroker1 = node1.actorOf(RpcBroker.props(brokerSession, probeReg1.getRef(), schemaContext));
80 probeReg2 = new JavaTestKit(node2);
81 rpcBroker2 = node2.actorOf(RpcBroker.props(brokerSession, probeReg2.getRef(), schemaContext));
85 public void testInvokeRpcError() throws Exception {
86 new JavaTestKit(node1) {{
87 QName rpc = new QName(new URI("noactor1"), "noactor1");
88 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "no child"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
91 InvokeRpc invokeMsg = new InvokeRpc(rpc, null, input);
92 rpcBroker1.tell(invokeMsg, getRef());
93 probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
94 probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(new ArrayList<Pair<ActorRef, Long>>()));
96 Boolean getMsg = new ExpectMsg<Boolean>("ErrorResponse") {
97 protected Boolean match(Object in) {
98 if (in instanceof ErrorResponse) {
99 ErrorResponse reply = (ErrorResponse)in;
100 return reply.getException().getMessage().contains("No remote actor found for rpc execution of :");
105 }.get(); // this extracts the received message
107 Assert.assertTrue(getMsg);
114 * This test method invokes and executes the remote rpc
118 public void testInvokeRpc() throws URISyntaxException {
119 new JavaTestKit(node1) {{
120 QName rpc = new QName(new URI("noactor1"), "noactor1");
122 CompositeNode input = new ImmutableCompositeNode(QName.create("ns", "2013-12-09", "child1"), new ArrayList<Node<?>>(), ModifyAction.REPLACE);
123 InvokeRpc invokeMsg = new InvokeRpc(rpc, null, input);
124 rpcBroker1.tell(invokeMsg, getRef());
126 probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
127 List<Pair<ActorRef, Long>> routerList = new ArrayList<Pair<ActorRef, Long>>();
129 routerList.add(new Pair<ActorRef, Long>(rpcBroker2, 200L));
131 probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(routerList));
133 CompositeNode invokeRpcResult = mock(CompositeNode.class);
134 Collection<RpcError> errors = new ArrayList<>();
135 RpcResult<CompositeNode> result = Rpcs.getRpcResult(true, invokeRpcResult, errors);
136 Future<RpcResult<CompositeNode>> rpcResult = Futures.immediateFuture(result);
137 when(brokerSession.rpc(rpc, input)).thenReturn(rpcResult);
139 //verify response msg
140 Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
141 protected Boolean match(Object in) {
142 if (in instanceof RpcResponse) {
148 }.get(); // this extracts the received message
150 Assert.assertTrue(getMsg);