1 package org.opendaylight.controller.cluster.datastore.utils;
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSelection;
5 import akka.actor.Props;
6 import akka.actor.UntypedActor;
7 import akka.japi.Creator;
8 import akka.testkit.JavaTestKit;
9 import com.google.common.base.Optional;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
13 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
14 import org.opendaylight.controller.cluster.datastore.Configuration;
15 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
16 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
17 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
18 import scala.concurrent.Await;
19 import scala.concurrent.Future;
20 import scala.concurrent.duration.Duration;
22 import java.util.concurrent.TimeUnit;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
28 public class ActorContextTest extends AbstractActorTest{
30 private static class MockShardManager extends UntypedActor {
32 private final boolean found;
33 private final ActorRef actorRef;
35 private MockShardManager(boolean found, ActorRef actorRef){
38 this.actorRef = actorRef;
41 @Override public void onReceive(Object message) throws Exception {
43 getSender().tell(new LocalShardFound(actorRef), getSelf());
45 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
49 private static Props props(final boolean found, final ActorRef actorRef){
50 return Props.create(new MockShardManagerCreator(found, actorRef) );
53 @SuppressWarnings("serial")
54 private static class MockShardManagerCreator implements Creator<MockShardManager> {
56 final ActorRef actorRef;
58 MockShardManagerCreator(boolean found, ActorRef actorRef) {
60 this.actorRef = actorRef;
64 public MockShardManager create() throws Exception {
65 return new MockShardManager(found, actorRef);
71 public void testFindLocalShardWithShardFound(){
72 new JavaTestKit(getSystem()) {{
74 new Within(duration("1 seconds")) {
76 protected void run() {
78 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
80 ActorRef shardManagerActorRef = getSystem()
81 .actorOf(MockShardManager.props(true, shardActorRef));
83 ActorContext actorContext =
84 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
85 mock(Configuration.class));
87 Optional<ActorRef> out = actorContext.findLocalShard("default");
89 assertEquals(shardActorRef, out.get());
100 public void testFindLocalShardWithShardNotFound(){
101 new JavaTestKit(getSystem()) {{
103 new Within(duration("1 seconds")) {
105 protected void run() {
107 ActorRef shardManagerActorRef = getSystem()
108 .actorOf(MockShardManager.props(false, null));
110 ActorContext actorContext =
111 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
112 mock(Configuration.class));
114 Optional<ActorRef> out = actorContext.findLocalShard("default");
115 assertTrue(!out.isPresent());
124 public void testExecuteRemoteOperation() {
125 new JavaTestKit(getSystem()) {{
127 new Within(duration("3 seconds")) {
129 protected void run() {
131 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
133 ActorRef shardManagerActorRef = getSystem()
134 .actorOf(MockShardManager.props(true, shardActorRef));
136 ActorContext actorContext =
137 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
138 mock(Configuration.class));
140 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
142 Object out = actorContext.executeOperation(actor, "hello");
144 assertEquals("hello", out);
153 public void testExecuteRemoteOperationAsync() {
154 new JavaTestKit(getSystem()) {{
156 new Within(duration("3 seconds")) {
158 protected void run() {
160 ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
162 ActorRef shardManagerActorRef = getSystem()
163 .actorOf(MockShardManager.props(true, shardActorRef));
165 ActorContext actorContext =
166 new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
167 mock(Configuration.class));
169 ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
171 Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
174 Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
175 assertEquals("Result", "hello", result);
176 } catch(Exception e) {
177 throw new AssertionError(e);