Merge "Adding more unit tests for remote rpc connector and Integrating routing table"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / RpcBrokerTest.java
1 /*
2  * Copyright (c) 2014 Cisco 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
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;
38
39 import java.net.URI;
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;
45
46 import static org.mockito.Mockito.mock;
47 import static org.mockito.Mockito.when;
48
49 public class RpcBrokerTest {
50
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;
58
59
60   @BeforeClass
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"));
64   }
65
66   @AfterClass
67   public static void teardown() {
68     JavaTestKit.shutdownActorSystem(node1);
69     JavaTestKit.shutdownActorSystem(node2);
70     node1 = null;
71     node2 = null;
72   }
73
74   @Before
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));
82
83   }
84   @Test
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);
89
90
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>>()));
95
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 :");
101           } else {
102             throw noMatch();
103           }
104         }
105       }.get(); // this extracts the received message
106
107       Assert.assertTrue(getMsg);
108
109     }};
110   }
111
112
113   /**
114    * This test method invokes and executes the remote rpc
115    */
116
117   @Test
118   public void testInvokeRpc() throws URISyntaxException {
119     new JavaTestKit(node1) {{
120       QName rpc = new QName(new URI("noactor1"), "noactor1");
121       // invoke rpc
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());
125
126       probeReg1.expectMsgClass(RpcRegistry.Messages.FindRouters.class);
127       List<Pair<ActorRef, Long>> routerList = new ArrayList<Pair<ActorRef, Long>>();
128
129       routerList.add(new Pair<ActorRef, Long>(rpcBroker2, 200L));
130
131       probeReg1.reply(new RpcRegistry.Messages.FindRoutersReply(routerList));
132
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);
138
139       //verify response msg
140       Boolean getMsg = new ExpectMsg<Boolean>("RpcResponse") {
141         protected Boolean match(Object in) {
142           if (in instanceof RpcResponse) {
143             return true;
144           } else {
145             throw noMatch();
146           }
147         }
148       }.get(); // this extracts the received message
149
150       Assert.assertTrue(getMsg);
151     }};
152   }
153 }