Remove l2switch sample
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / utils / ActorContextTest.java
1 package org.opendaylight.controller.cluster.datastore.utils;
2
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;
10 import org.junit.Test;
11 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
12 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
13 import org.opendaylight.controller.cluster.datastore.Configuration;
14 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
16 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
17 import scala.concurrent.Await;
18 import scala.concurrent.Future;
19 import scala.concurrent.duration.Duration;
20 import java.util.concurrent.TimeUnit;
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertTrue;
23 import static org.mockito.Mockito.mock;
24
25 public class ActorContextTest extends AbstractActorTest{
26
27     private static class MockShardManager extends UntypedActor {
28
29         private final boolean found;
30         private final ActorRef actorRef;
31
32         private MockShardManager(boolean found, ActorRef actorRef){
33
34             this.found = found;
35             this.actorRef = actorRef;
36         }
37
38         @Override public void onReceive(Object message) throws Exception {
39             if(found){
40                 getSender().tell(new LocalShardFound(actorRef), getSelf());
41             } else {
42                 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
43             }
44         }
45
46         private static Props props(final boolean found, final ActorRef actorRef){
47             return Props.create(new MockShardManagerCreator(found, actorRef) );
48         }
49
50         @SuppressWarnings("serial")
51         private static class MockShardManagerCreator implements Creator<MockShardManager> {
52             final boolean found;
53             final ActorRef actorRef;
54
55             MockShardManagerCreator(boolean found, ActorRef actorRef) {
56                 this.found = found;
57                 this.actorRef = actorRef;
58             }
59
60             @Override
61             public MockShardManager create() throws Exception {
62                 return new MockShardManager(found, actorRef);
63             }
64         }
65     }
66
67     @Test
68     public void testFindLocalShardWithShardFound(){
69         new JavaTestKit(getSystem()) {{
70
71             new Within(duration("1 seconds")) {
72                 @Override
73                 protected void run() {
74
75                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
76
77                     ActorRef shardManagerActorRef = getSystem()
78                         .actorOf(MockShardManager.props(true, shardActorRef));
79
80                     ActorContext actorContext =
81                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
82                             mock(Configuration.class));
83
84                     Optional<ActorRef> out = actorContext.findLocalShard("default");
85
86                     assertEquals(shardActorRef, out.get());
87
88
89                     expectNoMsg();
90                 }
91             };
92         }};
93
94     }
95
96     @Test
97     public void testFindLocalShardWithShardNotFound(){
98         new JavaTestKit(getSystem()) {{
99             ActorRef shardManagerActorRef = getSystem()
100                     .actorOf(MockShardManager.props(false, null));
101
102             ActorContext actorContext =
103                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
104                             mock(Configuration.class));
105
106             Optional<ActorRef> out = actorContext.findLocalShard("default");
107             assertTrue(!out.isPresent());
108         }};
109
110     }
111
112     @Test
113     public void testExecuteRemoteOperation() {
114         new JavaTestKit(getSystem()) {{
115             ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
116
117             ActorRef shardManagerActorRef = getSystem()
118                     .actorOf(MockShardManager.props(true, shardActorRef));
119
120             ActorContext actorContext =
121                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
122                             mock(Configuration.class));
123
124             ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
125
126             Object out = actorContext.executeOperation(actor, "hello");
127
128             assertEquals("hello", out);
129         }};
130     }
131
132     @Test
133     public void testExecuteRemoteOperationAsync() {
134         new JavaTestKit(getSystem()) {{
135             ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
136
137             ActorRef shardManagerActorRef = getSystem()
138                     .actorOf(MockShardManager.props(true, shardActorRef));
139
140             ActorContext actorContext =
141                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
142                             mock(Configuration.class));
143
144             ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
145
146             Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
147
148             try {
149                 Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
150                 assertEquals("Result", "hello", result);
151             } catch(Exception e) {
152                 throw new AssertionError(e);
153             }
154         }};
155     }
156
157     @Test
158     public void testIsLocalPath() {
159         MockClusterWrapper clusterWrapper = new MockClusterWrapper();
160         ActorContext actorContext =
161                 new ActorContext(getSystem(), null, clusterWrapper, mock(Configuration.class));
162
163         clusterWrapper.setSelfAddress("");
164         assertEquals(false, actorContext.isLocalPath(null));
165         assertEquals(false, actorContext.isLocalPath(""));
166
167         clusterWrapper.setSelfAddress(null);
168         assertEquals(false, actorContext.isLocalPath(""));
169
170         clusterWrapper.setSelfAddress("akka://test/user/$b");
171         assertEquals(false, actorContext.isLocalPath("akka://test/user/$a"));
172
173         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550/");
174         assertEquals(true, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
175
176         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550");
177         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
178
179         clusterWrapper.setSelfAddress("akka.tcp://system@128.0.0.1:2550/");
180         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
181
182         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2551/");
183         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
184     }
185 }