BUG 1623 - Clustering : Parsing Error thrown on startup
[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 java.util.concurrent.TimeUnit;
4 import akka.actor.ActorRef;
5 import akka.actor.ActorSelection;
6 import akka.actor.ActorSystem;
7 import akka.actor.Props;
8 import akka.actor.UntypedActor;
9 import akka.japi.Creator;
10 import akka.testkit.JavaTestKit;
11
12 import org.junit.Test;
13 import org.opendaylight.controller.cluster.datastore.AbstractActorTest;
14 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
15 import org.opendaylight.controller.cluster.datastore.Configuration;
16 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
17 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
18 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
19
20 import scala.concurrent.Await;
21 import scala.concurrent.Future;
22 import scala.concurrent.duration.Duration;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNull;
25 import static org.mockito.Mockito.mock;
26
27 public class ActorContextTest extends AbstractActorTest{
28     @Test
29     public void testResolvePathForRemoteActor(){
30         ActorContext actorContext =
31             new ActorContext(mock(ActorSystem.class), mock(ActorRef.class),mock(
32                 ClusterWrapper.class),
33                 mock(Configuration.class));
34
35         String actual = actorContext.resolvePath(
36             "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard",
37             "akka://system/user/shardmanager/shard/transaction");
38
39         String expected = "akka.tcp://system@127.0.0.1:2550/user/shardmanager/shard/transaction";
40
41         assertEquals(expected, actual);
42     }
43
44     @Test
45     public void testResolvePathForLocalActor(){
46         ActorContext actorContext =
47             new ActorContext(getSystem(), mock(ActorRef.class), mock(ClusterWrapper.class),
48                 mock(Configuration.class));
49
50         String actual = actorContext.resolvePath(
51             "akka://system/user/shardmanager/shard",
52             "akka://system/user/shardmanager/shard/transaction");
53
54         String expected = "akka://system/user/shardmanager/shard/transaction";
55
56         assertEquals(expected, actual);
57
58         System.out.println(actorContext
59             .actorFor("akka://system/user/shardmanager/shard/transaction"));
60     }
61
62
63     private static class MockShardManager extends UntypedActor {
64
65         private final boolean found;
66         private final ActorRef actorRef;
67
68         private MockShardManager(boolean found, ActorRef actorRef){
69
70             this.found = found;
71             this.actorRef = actorRef;
72         }
73
74         @Override public void onReceive(Object message) throws Exception {
75             if(found){
76                 getSender().tell(new LocalShardFound(actorRef), getSelf());
77             } else {
78                 getSender().tell(new LocalShardNotFound(((FindLocalShard) message).getShardName()), getSelf());
79             }
80         }
81
82         private static Props props(final boolean found, final ActorRef actorRef){
83             return Props.create(new MockShardManagerCreator(found, actorRef) );
84         }
85
86         @SuppressWarnings("serial")
87         private static class MockShardManagerCreator implements Creator<MockShardManager> {
88             final boolean found;
89             final ActorRef actorRef;
90
91             MockShardManagerCreator(boolean found, ActorRef actorRef) {
92                 this.found = found;
93                 this.actorRef = actorRef;
94             }
95
96             @Override
97             public MockShardManager create() throws Exception {
98                 return new MockShardManager(found, actorRef);
99             }
100         }
101     }
102
103     @Test
104     public void testExecuteLocalShardOperationWithShardFound(){
105         new JavaTestKit(getSystem()) {{
106
107             new Within(duration("1 seconds")) {
108                 @Override
109                 protected void run() {
110
111                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
112
113                     ActorRef shardManagerActorRef = getSystem()
114                         .actorOf(MockShardManager.props(true, shardActorRef));
115
116                     ActorContext actorContext =
117                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
118                             mock(Configuration.class));
119
120                     Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
121
122                     assertEquals("hello", out);
123
124
125                     expectNoMsg();
126                 }
127             };
128         }};
129
130     }
131
132     @Test
133     public void testExecuteLocalShardOperationWithShardNotFound(){
134         new JavaTestKit(getSystem()) {{
135
136             new Within(duration("1 seconds")) {
137                 @Override
138                 protected void run() {
139
140                     ActorRef shardManagerActorRef = getSystem()
141                         .actorOf(MockShardManager.props(false, null));
142
143                     ActorContext actorContext =
144                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
145                             mock(Configuration.class));
146
147                     Object out = actorContext.executeLocalShardOperation("default", "hello", duration("1 seconds"));
148
149                     assertNull(out);
150
151
152                     expectNoMsg();
153                 }
154             };
155         }};
156
157     }
158
159
160     @Test
161     public void testFindLocalShardWithShardFound(){
162         new JavaTestKit(getSystem()) {{
163
164             new Within(duration("1 seconds")) {
165                 @Override
166                 protected void run() {
167
168                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
169
170                     ActorRef shardManagerActorRef = getSystem()
171                         .actorOf(MockShardManager.props(true, shardActorRef));
172
173                     ActorContext actorContext =
174                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
175                             mock(Configuration.class));
176
177                     Object out = actorContext.findLocalShard("default");
178
179                     assertEquals(shardActorRef, out);
180
181
182                     expectNoMsg();
183                 }
184             };
185         }};
186
187     }
188
189     @Test
190     public void testFindLocalShardWithShardNotFound(){
191         new JavaTestKit(getSystem()) {{
192
193             new Within(duration("1 seconds")) {
194                 @Override
195                 protected void run() {
196
197                     ActorRef shardManagerActorRef = getSystem()
198                         .actorOf(MockShardManager.props(false, null));
199
200                     ActorContext actorContext =
201                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
202                             mock(Configuration.class));
203
204                     Object out = actorContext.findLocalShard("default");
205
206                     assertNull(out);
207
208
209                     expectNoMsg();
210                 }
211             };
212         }};
213
214     }
215
216     @Test
217     public void testExecuteRemoteOperation() {
218         new JavaTestKit(getSystem()) {{
219
220             new Within(duration("3 seconds")) {
221                 @Override
222                 protected void run() {
223
224                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
225
226                     ActorRef shardManagerActorRef = getSystem()
227                         .actorOf(MockShardManager.props(true, shardActorRef));
228
229                     ActorContext actorContext =
230                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
231                             mock(Configuration.class));
232
233                     ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
234
235                     Object out = actorContext.executeRemoteOperation(actor, "hello", duration("3 seconds"));
236
237                     assertEquals("hello", out);
238
239                     expectNoMsg();
240                 }
241             };
242         }};
243     }
244
245     @Test
246     public void testExecuteRemoteOperationAsync() {
247         new JavaTestKit(getSystem()) {{
248
249             new Within(duration("3 seconds")) {
250                 @Override
251                 protected void run() {
252
253                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
254
255                     ActorRef shardManagerActorRef = getSystem()
256                         .actorOf(MockShardManager.props(true, shardActorRef));
257
258                     ActorContext actorContext =
259                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
260                             mock(Configuration.class));
261
262                     ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
263
264                     Future<Object> future = actorContext.executeRemoteOperationAsync(actor, "hello",
265                             Duration.create(3, TimeUnit.SECONDS));
266
267                     try {
268                         Object result = Await.result(future, Duration.create(3, TimeUnit.SECONDS));
269                         assertEquals("Result", "hello", result);
270                     } catch(Exception e) {
271                         throw new AssertionError(e);
272                     }
273
274                     expectNoMsg();
275                 }
276             };
277         }};
278     }
279 }