Merge "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.ActorSystem;
6 import akka.actor.Props;
7 import akka.actor.UntypedActor;
8 import akka.japi.Creator;
9 import akka.testkit.JavaTestKit;
10 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;
21
22 import java.util.concurrent.TimeUnit;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Mockito.mock;
27
28 public class ActorContextTest extends AbstractActorTest{
29
30     private static class MockShardManager extends UntypedActor {
31
32         private final boolean found;
33         private final ActorRef actorRef;
34
35         private MockShardManager(boolean found, ActorRef actorRef){
36
37             this.found = found;
38             this.actorRef = actorRef;
39         }
40
41         @Override public void onReceive(Object message) throws Exception {
42             if(found){
43                 getSender().tell(new LocalShardFound(actorRef), getSelf());
44             } else {
45                 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
46             }
47         }
48
49         private static Props props(final boolean found, final ActorRef actorRef){
50             return Props.create(new MockShardManagerCreator(found, actorRef) );
51         }
52
53         @SuppressWarnings("serial")
54         private static class MockShardManagerCreator implements Creator<MockShardManager> {
55             final boolean found;
56             final ActorRef actorRef;
57
58             MockShardManagerCreator(boolean found, ActorRef actorRef) {
59                 this.found = found;
60                 this.actorRef = actorRef;
61             }
62
63             @Override
64             public MockShardManager create() throws Exception {
65                 return new MockShardManager(found, actorRef);
66             }
67         }
68     }
69
70     @Test
71     public void testFindLocalShardWithShardFound(){
72         new JavaTestKit(getSystem()) {{
73
74             new Within(duration("1 seconds")) {
75                 @Override
76                 protected void run() {
77
78                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
79
80                     ActorRef shardManagerActorRef = getSystem()
81                         .actorOf(MockShardManager.props(true, shardActorRef));
82
83                     ActorContext actorContext =
84                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
85                             mock(Configuration.class));
86
87                     Optional<ActorRef> out = actorContext.findLocalShard("default");
88
89                     assertEquals(shardActorRef, out.get());
90
91
92                     expectNoMsg();
93                 }
94             };
95         }};
96
97     }
98
99     @Test
100     public void testFindLocalShardWithShardNotFound(){
101         new JavaTestKit(getSystem()) {{
102             ActorRef shardManagerActorRef = getSystem()
103                     .actorOf(MockShardManager.props(false, null));
104
105             ActorContext actorContext =
106                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
107                             mock(Configuration.class));
108
109             Optional<ActorRef> out = actorContext.findLocalShard("default");
110             assertTrue(!out.isPresent());
111         }};
112
113     }
114
115     @Test
116     public void testExecuteRemoteOperation() {
117         new JavaTestKit(getSystem()) {{
118             ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
119
120             ActorRef shardManagerActorRef = getSystem()
121                     .actorOf(MockShardManager.props(true, shardActorRef));
122
123             ActorContext actorContext =
124                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
125                             mock(Configuration.class));
126
127             ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
128
129             Object out = actorContext.executeOperation(actor, "hello");
130
131             assertEquals("hello", out);
132         }};
133     }
134
135     @Test
136     public void testExecuteRemoteOperationAsync() {
137         new JavaTestKit(getSystem()) {{
138             ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
139
140             ActorRef shardManagerActorRef = getSystem()
141                     .actorOf(MockShardManager.props(true, shardActorRef));
142
143             ActorContext actorContext =
144                     new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
145                             mock(Configuration.class));
146
147             ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
148
149             Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
150
151             try {
152                 Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
153                 assertEquals("Result", "hello", result);
154             } catch(Exception e) {
155                 throw new AssertionError(e);
156             }
157         }};
158     }
159
160     @Test
161     public void testIsLocalPath() {
162         MockClusterWrapper clusterWrapper = new MockClusterWrapper();
163         ActorContext actorContext =
164                 new ActorContext(getSystem(), null, clusterWrapper, mock(Configuration.class));
165
166         clusterWrapper.setSelfAddress("");
167         assertEquals(false, actorContext.isLocalPath(null));
168         assertEquals(false, actorContext.isLocalPath(""));
169
170         clusterWrapper.setSelfAddress(null);
171         assertEquals(false, actorContext.isLocalPath(""));
172
173         clusterWrapper.setSelfAddress("akka://test/user/$b");
174         assertEquals(false, actorContext.isLocalPath("akka://test/user/$a"));
175
176         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550/");
177         assertEquals(true, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
178
179         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2550");
180         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
181
182         clusterWrapper.setSelfAddress("akka.tcp://system@128.0.0.1:2550/");
183         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
184
185         clusterWrapper.setSelfAddress("akka.tcp://system@127.0.0.1:2551/");
186         assertEquals(false, actorContext.isLocalPath("akka.tcp://system@127.0.0.1:2550/"));
187     }
188
189     @Test
190     public void testResolvePathForRemoteActor() {
191         ActorContext actorContext =
192                 new ActorContext(mock(ActorSystem.class), mock(ActorRef.class), mock(
193                         ClusterWrapper.class),
194                         mock(Configuration.class));
195
196         String actual = actorContext.resolvePath(
197                 "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard",
198                 "akka://system/user/shardmanager/shard/transaction");
199
200         String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction";
201
202         assertEquals(expected, actual);
203     }
204
205     @Test
206     public void testResolvePathForLocalActor() {
207         ActorContext actorContext =
208                 new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
209                         mock(Configuration.class));
210
211         String actual = actorContext.resolvePath(
212                 "akka://system/user/shardmanager/shard",
213                 "akka://system/user/shardmanager/shard/transaction");
214
215         String expected = "akka://system/user/shardmanager/shard/transaction";
216
217         assertEquals(expected, actual);
218     }
219
220     @Test
221     public void testResolvePathForRemoteActorWithProperRemoteAddress() {
222         ActorContext actorContext =
223                 new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
224                         mock(Configuration.class));
225
226         String actual = actorContext.resolvePath(
227                 "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard",
228                 "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard/transaction");
229
230         String expected = "akka.tcp://system@7.0.0.1:2550/user/shardmanager/shard/transaction";
231
232         assertEquals(expected, actual);
233     }
234
235 }