Merge "Fixed for bug 1168 : Issue while update subnet"
[controller.git] / opendaylight / md-sal / sal-remoterpc-connector / src / test / java / org / opendaylight / controller / remote / rpc / registry / RpcRegistryTest.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.registry;
10
11 import akka.actor.ActorRef;
12 import akka.actor.ActorSystem;
13 import akka.testkit.JavaTestKit;
14 import junit.framework.Assert;
15 import org.junit.AfterClass;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.mockito.Mockito;
19 import org.opendaylight.controller.remote.rpc.RouteIdentifierImpl;
20 import org.opendaylight.controller.remote.rpc.messages.AddRoutedRpc;
21 import org.opendaylight.controller.remote.rpc.messages.AddRpc;
22 import org.opendaylight.controller.remote.rpc.messages.GetRoutedRpc;
23 import org.opendaylight.controller.remote.rpc.messages.GetRoutedRpcReply;
24 import org.opendaylight.controller.remote.rpc.messages.GetRpc;
25 import org.opendaylight.controller.remote.rpc.messages.GetRpcReply;
26 import org.opendaylight.controller.remote.rpc.messages.RemoveRoutedRpc;
27 import org.opendaylight.controller.remote.rpc.messages.RemoveRpc;
28 import org.opendaylight.controller.sal.connector.api.RpcRouter;
29 import org.opendaylight.yangtools.yang.common.QName;
30
31 import java.net.URI;
32 import java.net.URISyntaxException;
33 import java.util.HashSet;
34 import java.util.Set;
35
36 public class RpcRegistryTest {
37
38   static ActorSystem system;
39
40
41   @BeforeClass
42   public static void setup() {
43     system = ActorSystem.create();
44   }
45
46   @AfterClass
47   public static void teardown() {
48     JavaTestKit.shutdownActorSystem(system);
49     system = null;
50   }
51
52   /**
53    This test add, read and remove an entry in global rpc
54    */
55   @Test
56   public void testGlobalRpc() throws URISyntaxException {
57     new JavaTestKit(system) {{
58       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
59       QName type = new QName(new URI("actor1"), "actor1");
60       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
61       final String route = "actor1";
62
63       AddRpc rpcMsg = new AddRpc(routeId, route);
64       rpcRegistry.tell(rpcMsg, getRef());
65       expectMsgEquals(duration("2 second"), "Success");
66
67       GetRpc getRpc = new GetRpc(routeId);
68       rpcRegistry.tell(getRpc, getRef());
69
70       Boolean getMsg = new ExpectMsg<Boolean>("GetRpcReply") {
71         protected Boolean match(Object in) {
72           if (in instanceof GetRpcReply) {
73             GetRpcReply reply = (GetRpcReply)in;
74             return route.equals(reply.getRoutePath());
75           } else {
76             throw noMatch();
77           }
78         }
79       }.get(); // this extracts the received message
80
81       Assert.assertTrue(getMsg);
82
83       RemoveRpc removeMsg = new RemoveRpc(routeId);
84       rpcRegistry.tell(removeMsg, getRef());
85       expectMsgEquals(duration("2 second"), "Success");
86
87       rpcRegistry.tell(getRpc, getRef());
88
89       Boolean getNullMsg = new ExpectMsg<Boolean>("GetRpcReply") {
90         protected Boolean match(Object in) {
91           if (in instanceof GetRpcReply) {
92             GetRpcReply reply = (GetRpcReply)in;
93             return reply.getRoutePath() == null;
94           } else {
95             throw noMatch();
96           }
97         }
98       }.get();
99       Assert.assertTrue(getNullMsg);
100     }};
101
102   }
103
104   /**
105    This test add, read and remove an entry in routed rpc
106    */
107   @Test
108   public void testRoutedRpc() throws URISyntaxException {
109     new JavaTestKit(system) {{
110       ActorRef rpcRegistry = system.actorOf(RpcRegistry.props(Mockito.mock(ClusterWrapper.class)));
111       QName type = new QName(new URI("actor1"), "actor1");
112       RouteIdentifierImpl routeId = new RouteIdentifierImpl(null, type, null);
113       final String route = "actor1";
114
115       Set<RpcRouter.RouteIdentifier<?, ?, ?>> routeIds = new HashSet<>();
116       routeIds.add(routeId);
117
118       AddRoutedRpc rpcMsg = new AddRoutedRpc(routeIds, route);
119       rpcRegistry.tell(rpcMsg, getRef());
120       expectMsgEquals(duration("2 second"), "Success");
121
122       GetRoutedRpc getRpc = new GetRoutedRpc(routeId);
123       rpcRegistry.tell(getRpc, getRef());
124
125       Boolean getMsg = new ExpectMsg<Boolean>("GetRoutedRpcReply") {
126         protected Boolean match(Object in) {
127           if (in instanceof GetRoutedRpcReply) {
128             GetRoutedRpcReply reply = (GetRoutedRpcReply)in;
129             return route.equals(reply.getRoutePath());
130           } else {
131             throw noMatch();
132           }
133         }
134       }.get(); // this extracts the received message
135
136       Assert.assertTrue(getMsg);
137
138       RemoveRoutedRpc removeMsg = new RemoveRoutedRpc(routeIds, route);
139       rpcRegistry.tell(removeMsg, getRef());
140       expectMsgEquals(duration("2 second"), "Success");
141
142       rpcRegistry.tell(getRpc, getRef());
143
144       Boolean getNullMsg = new ExpectMsg<Boolean>("GetRoutedRpcReply") {
145         protected Boolean match(Object in) {
146           if (in instanceof GetRoutedRpcReply) {
147             GetRoutedRpcReply reply = (GetRoutedRpcReply)in;
148             return reply.getRoutePath() == null;
149           } else {
150             throw noMatch();
151           }
152         }
153       }.get();
154       Assert.assertTrue(getNullMsg);
155     }};
156
157   }
158
159 }