Merge "BUG-1690: catch wildcard InstanceIdentifiers"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardManagerTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.ActorSystem;
5 import akka.actor.Props;
6 import akka.testkit.JavaTestKit;
7 import akka.testkit.TestActorRef;
8 import junit.framework.Assert;
9 import org.junit.AfterClass;
10 import org.junit.BeforeClass;
11 import org.junit.Test;
12 import org.opendaylight.controller.cluster.datastore.messages.FindLocalShard;
13 import org.opendaylight.controller.cluster.datastore.messages.FindPrimary;
14 import org.opendaylight.controller.cluster.datastore.messages.LocalShardFound;
15 import org.opendaylight.controller.cluster.datastore.messages.LocalShardNotFound;
16 import org.opendaylight.controller.cluster.datastore.messages.PrimaryFound;
17 import org.opendaylight.controller.cluster.datastore.messages.PrimaryNotFound;
18 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
19 import org.opendaylight.controller.cluster.datastore.utils.MockClusterWrapper;
20 import org.opendaylight.controller.cluster.datastore.utils.MockConfiguration;
21 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
22 import scala.concurrent.duration.Duration;
23
24 import static junit.framework.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
27 public class ShardManagerTest {
28     private static ActorSystem system;
29
30     @BeforeClass
31     public static void setUp() {
32         system = ActorSystem.create("test");
33     }
34
35     @AfterClass
36     public static void tearDown() {
37         JavaTestKit.shutdownActorSystem(system);
38         system = null;
39     }
40
41     @Test
42     public void testOnReceiveFindPrimaryForNonExistentShard() throws Exception {
43
44         new JavaTestKit(system) {{
45             final Props props = ShardManager
46                 .props("config", new MockClusterWrapper(),
47                     new MockConfiguration(), new DatastoreContext());
48             final TestActorRef<ShardManager> subject =
49                 TestActorRef.create(system, props);
50
51             new Within(duration("10 seconds")) {
52                 @Override
53                 protected void run() {
54
55                     subject.tell(new FindPrimary("inventory").toSerializable(), getRef());
56
57                     expectMsgEquals(Duration.Zero(),
58                         new PrimaryNotFound("inventory").toSerializable());
59
60                     expectNoMsg();
61                 }
62             };
63         }};
64     }
65
66     @Test
67     public void testOnReceiveFindPrimaryForExistentShard() throws Exception {
68
69         new JavaTestKit(system) {{
70             final Props props = ShardManager
71                 .props("config", new MockClusterWrapper(),
72                     new MockConfiguration(), new DatastoreContext());
73             final TestActorRef<ShardManager> subject =
74                 TestActorRef.create(system, props);
75
76             subject.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
77
78             new Within(duration("10 seconds")) {
79                 @Override
80                 protected void run() {
81
82                     subject.tell(new FindPrimary(Shard.DEFAULT_NAME).toSerializable(), getRef());
83
84                     expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
85
86                     expectNoMsg();
87                 }
88             };
89         }};
90     }
91
92     @Test
93     public void testOnReceiveFindLocalShardForNonExistentShard() throws Exception {
94
95         new JavaTestKit(system) {{
96             final Props props = ShardManager
97                 .props("config", new MockClusterWrapper(),
98                     new MockConfiguration(), new DatastoreContext());
99             final TestActorRef<ShardManager> subject =
100                 TestActorRef.create(system, props);
101
102             new Within(duration("10 seconds")) {
103                 @Override
104                 protected void run() {
105
106                     subject.tell(new FindLocalShard("inventory"), getRef());
107
108                     final String out = new ExpectMsg<String>(duration("10 seconds"), "find local") {
109                         @Override
110                         protected String match(Object in) {
111                             if (in instanceof LocalShardNotFound) {
112                                 return ((LocalShardNotFound) in).getShardName();
113                             } else {
114                                 throw noMatch();
115                             }
116                         }
117                     }.get(); // this extracts the received message
118
119                     assertEquals("inventory", out);
120
121                     expectNoMsg();
122                 }
123             };
124         }};
125     }
126
127     @Test
128     public void testOnReceiveFindLocalShardForExistentShard() throws Exception {
129
130         final MockClusterWrapper mockClusterWrapper = new MockClusterWrapper();
131
132         new JavaTestKit(system) {{
133             final Props props = ShardManager
134                 .props("config", mockClusterWrapper,
135                     new MockConfiguration(), new DatastoreContext());
136             final TestActorRef<ShardManager> subject =
137                 TestActorRef.create(system, props);
138
139             subject.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
140
141             new Within(duration("10 seconds")) {
142                 @Override
143                 protected void run() {
144
145                     subject.tell(new FindLocalShard(Shard.DEFAULT_NAME), getRef());
146
147                     final ActorRef out = new ExpectMsg<ActorRef>(duration("10 seconds"), "find local") {
148                         @Override
149                         protected ActorRef match(Object in) {
150                             if (in instanceof LocalShardFound) {
151                                 return ((LocalShardFound) in).getPath();
152                             } else {
153                                 throw noMatch();
154                             }
155                         }
156                     }.get(); // this extracts the received message
157
158                     assertTrue(out.path().toString(), out.path().toString().contains("member-1-shard-default-config"));
159
160
161                     expectNoMsg();
162                 }
163             };
164         }};
165     }
166
167     @Test
168     public void testOnReceiveMemberUp() throws Exception {
169
170         new JavaTestKit(system) {{
171             final Props props = ShardManager
172                 .props("config", new MockClusterWrapper(),
173                     new MockConfiguration(), new DatastoreContext());
174             final TestActorRef<ShardManager> subject =
175                 TestActorRef.create(system, props);
176
177             // the run() method needs to finish within 3 seconds
178             new Within(duration("10 seconds")) {
179                 @Override
180                 protected void run() {
181
182                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
183
184                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
185
186                     final String out = new ExpectMsg<String>(duration("1 seconds"), "primary found") {
187                         // do not put code outside this method, will run afterwards
188                         @Override
189                         protected String match(Object in) {
190                             if (in.getClass().equals(PrimaryFound.SERIALIZABLE_CLASS)) {
191                                 PrimaryFound f = PrimaryFound.fromSerializable(in);
192                                 return f.getPrimaryPath();
193                             } else {
194                                 throw noMatch();
195                             }
196                         }
197                     }.get(); // this extracts the received message
198
199                     Assert.assertTrue(out, out.contains("member-2-shard-astronauts-config"));
200
201                     expectNoMsg();
202                 }
203             };
204         }};
205     }
206
207     @Test
208     public void testOnReceiveMemberDown() throws Exception {
209
210         new JavaTestKit(system) {{
211             final Props props = ShardManager
212                 .props("config", new MockClusterWrapper(),
213                     new MockConfiguration(), new DatastoreContext());
214             final TestActorRef<ShardManager> subject =
215                 TestActorRef.create(system, props);
216
217             // the run() method needs to finish within 3 seconds
218             new Within(duration("10 seconds")) {
219                 @Override
220                 protected void run() {
221
222                     MockClusterWrapper.sendMemberUp(subject, "member-2", getRef().path().toString());
223
224                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
225
226                     expectMsgClass(duration("1 seconds"), PrimaryFound.SERIALIZABLE_CLASS);
227
228                     MockClusterWrapper.sendMemberRemoved(subject, "member-2", getRef().path().toString());
229
230                     subject.tell(new FindPrimary("astronauts").toSerializable(), getRef());
231
232                     expectMsgClass(duration("1 seconds"), PrimaryNotFound.SERIALIZABLE_CLASS);
233
234                     expectNoMsg();
235                 }
236             };
237         }};
238     }
239
240
241 }