1 package org.opendaylight.controller.cluster.datastore.utils;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import akka.actor.UntypedActor;
7 import akka.japi.Creator;
8 import akka.testkit.JavaTestKit;
10 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
11 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
12 import org.opendaylight.controller.cluster.datastore.Configuration;
13 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
14 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
17 import static org.junit.Assert.assertEquals;
18 import static org.junit.Assert.assertNull;
19 import static org.mockito.Mockito.mock;
21 public class ActorContextTest extends AbstractActorTest{
23 public void testResolvePathForRemoteActor(){
24 ActorContext actorContext =
25 new ActorContext(mock(ActorSystem.class), mock(ActorRef.class),mock(
26 ClusterWrapper.class),
27 mock(Configuration.class));
29 String actual = actorContext.resolvePath(
30 "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard",
31 "akka://system/user/shardmanager/shard/transaction");
33 String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction";
35 assertEquals(expected, actual);
39 public void testResolvePathForLocalActor(){
40 ActorContext actorContext =
41 new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
42 mock(Configuration.class));
44 String actual = actorContext.resolvePath(
45 "akka://system/user/shardmanager/shard",
46 "akka://system/user/shardmanager/shard/transaction");
48 String expected = "akka://system/user/shardmanager/shard/transaction";
50 assertEquals(expected, actual);
52 System.out.println(actorContext
53 .actorFor("akka://system/user/shardmanager/shard/transaction"));
57 private static class MockShardManager extends UntypedActor {
59 private final boolean found;
60 private final ActorRef actorRef;
62 private MockShardManager(boolean found, ActorRef actorRef){
65 this.actorRef = actorRef;
68 @Override public void onReceive(Object message) throws Exception {
70 getSender().tell(new LocalShardFound(actorRef), getSelf());
72 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
76 private static Props props(final boolean found, final ActorRef actorRef){
77 return Props.create(new Creator<MockShardManager>() {
79 @Override public MockShardManager create()
81 return new MockShardManager(found,
89 public void testExecuteLocalShardOperationWithShardFound(){
90 new JavaTestKit(getSystem()) {{
92 new Within(duration("1 seconds")) {
93 protected void run() {
95 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
97 ActorRef shardManagerActorRef = getSystem()
98 .actorOf(MockShardManager.props(true, shardActorRef));
100 ActorContext actorContext =
101 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
102 mock(Configuration.class));
104 Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
106 assertEquals("hello", out);
117 public void testExecuteLocalShardOperationWithShardNotFound(){
118 new JavaTestKit(getSystem()) {{
120 new Within(duration("1 seconds")) {
121 protected void run() {
123 ActorRef shardManagerActorRef = getSystem()
124 .actorOf(MockShardManager.props(false, null));
126 ActorContext actorContext =
127 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
128 mock(Configuration.class));
130 Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
144 public void testFindLocalShardWithShardFound(){
145 new JavaTestKit(getSystem()) {{
147 new Within(duration("1 seconds")) {
148 protected void run() {
150 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
152 ActorRef shardManagerActorRef = getSystem()
153 .actorOf(MockShardManager.props(true, shardActorRef));
155 ActorContext actorContext =
156 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
157 mock(Configuration.class));
159 Object out = actorContext.findLocalShard("default");
161 assertEquals(shardActorRef, out);
172 public void testFindLocalShardWithShardNotFound(){
173 new JavaTestKit(getSystem()) {{
175 new Within(duration("1 seconds")) {
176 protected void run() {
178 ActorRef shardManagerActorRef = getSystem()
179 .actorOf(MockShardManager.props(false, null));
181 ActorContext actorContext =
182 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
183 mock(Configuration.class));
185 Object out = actorContext.findLocalShard("default");