BUG-2184 Fix subtree filtering for identity-ref leaves
[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
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
103             new Within(duration("1 seconds")) {
104                 @Override
105                 protected void run() {
106
107                     ActorRef shardManagerActorRef = getSystem()
108                         .actorOf(MockShardManager.props(false, null));
109
110                     ActorContext actorContext =
111                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
112                             mock(Configuration.class));
113
114                     Optional<ActorRef> out = actorContext.findLocalShard("default");
115                     assertTrue(!out.isPresent());
116                     expectNoMsg();
117                 }
118             };
119         }};
120
121     }
122
123     @Test
124     public void testExecuteRemoteOperation() {
125         new JavaTestKit(getSystem()) {{
126
127             new Within(duration("3 seconds")) {
128                 @Override
129                 protected void run() {
130
131                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
132
133                     ActorRef shardManagerActorRef = getSystem()
134                         .actorOf(MockShardManager.props(true, shardActorRef));
135
136                     ActorContext actorContext =
137                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
138                             mock(Configuration.class));
139
140                     ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
141
142                     Object out = actorContext.executeOperation(actor, "hello");
143
144                     assertEquals("hello", out);
145
146                     expectNoMsg();
147                 }
148             };
149         }};
150     }
151
152     @Test
153     public void testExecuteRemoteOperationAsync() {
154         new JavaTestKit(getSystem()) {{
155
156             new Within(duration("3 seconds")) {
157                 @Override
158                 protected void run() {
159
160                     ActorRef shardActorRef = getSystem().actorOf(Props.create(EchoActor.class));
161
162                     ActorRef shardManagerActorRef = getSystem()
163                         .actorOf(MockShardManager.props(true, shardActorRef));
164
165                     ActorContext actorContext =
166                         new ActorContext(getSystem(), shardManagerActorRef , mock(ClusterWrapper.class),
167                             mock(Configuration.class));
168
169                     ActorSelection actor = actorContext.actorSelection(shardActorRef.path());
170
171                     Future<Object> future = actorContext.executeOperationAsync(actor, "hello");
172
173                     try {
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);
178                     }
179
180                     expectNoMsg();
181                 }
182             };
183         }};
184     }
185 }