Merge "Fixed for bug 1197"
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / ShardTransactionTest.java
1 package org.opendaylight.controller.cluster.datastore;
2
3 import akka.actor.ActorRef;
4 import akka.actor.Props;
5 import akka.actor.Terminated;
6 import akka.testkit.JavaTestKit;
7 import com.google.common.util.concurrent.ListeningExecutorService;
8 import com.google.common.util.concurrent.MoreExecutors;
9 import org.junit.Test;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
11 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
12 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
13 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
14 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
15 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
16 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
21 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
22 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
23 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
24 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
25 import org.opendaylight.controller.cluster.datastore.modification.Modification;
26 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
27 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
28 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
32
33 import java.util.Collections;
34
35 import static org.junit.Assert.assertEquals;
36 import static org.junit.Assert.assertTrue;
37
38 public class ShardTransactionTest extends AbstractActorTest {
39     private static ListeningExecutorService storeExecutor =
40         MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
41
42     private static final InMemoryDOMDataStore store =
43         new InMemoryDOMDataStore("OPER", storeExecutor);
44
45     private static final SchemaContext testSchemaContext = TestModel.createTestContext();
46
47     static {
48         store.onGlobalContextUpdated(testSchemaContext);
49     }
50
51     @Test
52     public void testOnReceiveReadData() throws Exception {
53         new JavaTestKit(getSystem()) {{
54             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
55             final Props props =
56                 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
57             final ActorRef subject = getSystem().actorOf(props, "testReadData");
58
59             new Within(duration("1 seconds")) {
60                 protected void run() {
61
62                     subject.tell(
63                         new ReadData(YangInstanceIdentifier.builder().build()).toSerializable(),
64                         getRef());
65
66                     final String out = new ExpectMsg<String>("match hint") {
67                         // do not put code outside this method, will run afterwards
68                         protected String match(Object in) {
69                             if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
70                               if (ReadDataReply.fromSerializable(testSchemaContext,YangInstanceIdentifier.builder().build(), in)
71                                   .getNormalizedNode()!= null) {
72                                     return "match";
73                                 }
74                                 return null;
75                             } else {
76                                 throw noMatch();
77                             }
78                         }
79                     }.get(); // this extracts the received message
80
81                     assertEquals("match", out);
82
83                     expectNoMsg();
84                 }
85
86
87             };
88         }};
89     }
90
91     @Test
92     public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
93         new JavaTestKit(getSystem()) {{
94             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
95             final Props props =
96                 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
97             final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
98
99             new Within(duration("1 seconds")) {
100                 protected void run() {
101
102                     subject.tell(
103                         new ReadData(TestModel.TEST_PATH).toSerializable(),
104                         getRef());
105
106                     final String out = new ExpectMsg<String>("match hint") {
107                         // do not put code outside this method, will run afterwards
108                         protected String match(Object in) {
109                             if (in.getClass().equals(ReadDataReply.SERIALIZABLE_CLASS)) {
110                                 if (ReadDataReply.fromSerializable(testSchemaContext,TestModel.TEST_PATH, in)
111                                     .getNormalizedNode()
112                                     == null) {
113                                     return "match";
114                                 }
115                                 return null;
116                             } else {
117                                 throw noMatch();
118                             }
119                         }
120                     }.get(); // this extracts the received message
121
122                     assertEquals("match", out);
123
124                     expectNoMsg();
125                 }
126
127
128             };
129         }};
130     }
131
132     private void assertModification(final ActorRef subject,
133         final Class<? extends Modification> modificationType) {
134         new JavaTestKit(getSystem()) {{
135             new Within(duration("1 seconds")) {
136                 protected void run() {
137                     subject
138                         .tell(new ShardTransaction.GetCompositedModification(),
139                             getRef());
140
141                     final CompositeModification compositeModification =
142                         new ExpectMsg<CompositeModification>("match hint") {
143                             // do not put code outside this method, will run afterwards
144                             protected CompositeModification match(Object in) {
145                                 if (in instanceof ShardTransaction.GetCompositeModificationReply) {
146                                     return ((ShardTransaction.GetCompositeModificationReply) in)
147                                         .getModification();
148                                 } else {
149                                     throw noMatch();
150                                 }
151                             }
152                         }.get(); // this extracts the received message
153
154                     assertTrue(
155                         compositeModification.getModifications().size() == 1);
156                     assertEquals(modificationType,
157                         compositeModification.getModifications().get(0)
158                             .getClass());
159
160                 }
161             };
162         }};
163     }
164
165     @Test
166     public void testOnReceiveWriteData() throws Exception {
167         new JavaTestKit(getSystem()) {{
168             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
169             final Props props =
170                 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
171             final ActorRef subject =
172                 getSystem().actorOf(props, "testWriteData");
173
174             new Within(duration("1 seconds")) {
175                 protected void run() {
176
177                     subject.tell(new WriteData(TestModel.TEST_PATH,
178                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
179                         getRef());
180
181                     final String out = new ExpectMsg<String>("match hint") {
182                         // do not put code outside this method, will run afterwards
183                         protected String match(Object in) {
184                             if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
185                                 return "match";
186                             } else {
187                                 throw noMatch();
188                             }
189                         }
190                     }.get(); // this extracts the received message
191
192                     assertEquals("match", out);
193
194                     assertModification(subject, WriteModification.class);
195                     expectNoMsg();
196                 }
197
198
199             };
200         }};
201     }
202
203     @Test
204     public void testOnReceiveMergeData() throws Exception {
205         new JavaTestKit(getSystem()) {{
206             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
207             final Props props =
208                 ShardTransaction.props(store.newReadWriteTransaction(), shard, testSchemaContext);
209             final ActorRef subject =
210                 getSystem().actorOf(props, "testMergeData");
211
212             new Within(duration("1 seconds")) {
213                 protected void run() {
214
215                     subject.tell(new MergeData(TestModel.TEST_PATH,
216                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), testSchemaContext).toSerializable(),
217                         getRef());
218
219                     final String out = new ExpectMsg<String>(duration("500 milliseconds"), "match hint") {
220                         // do not put code outside this method, will run afterwards
221                         protected String match(Object in) {
222                             if (in.getClass().equals(MergeDataReply.SERIALIZABLE_CLASS)) {
223                                 return "match";
224                             } else {
225                                 throw noMatch();
226                             }
227                         }
228                     }.get(); // this extracts the received message
229
230                     assertEquals("match", out);
231
232                     assertModification(subject, MergeModification.class);
233
234                     expectNoMsg();
235                 }
236
237
238             };
239         }};
240     }
241
242     @Test
243     public void testOnReceiveDeleteData() throws Exception {
244         new JavaTestKit(getSystem()) {{
245             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
246             final Props props =
247                 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
248             final ActorRef subject =
249                 getSystem().actorOf(props, "testDeleteData");
250
251             new Within(duration("1 seconds")) {
252                 protected void run() {
253
254                     subject.tell(new DeleteData(TestModel.TEST_PATH).toSerializable(), getRef());
255
256                     final String out = new ExpectMsg<String>("match hint") {
257                         // do not put code outside this method, will run afterwards
258                         protected String match(Object in) {
259                             if (in.getClass().equals(DeleteDataReply.SERIALIZABLE_CLASS)) {
260                                 return "match";
261                             } else {
262                                 throw noMatch();
263                             }
264                         }
265                     }.get(); // this extracts the received message
266
267                     assertEquals("match", out);
268
269                     assertModification(subject, DeleteModification.class);
270                     expectNoMsg();
271                 }
272
273
274             };
275         }};
276     }
277
278
279     @Test
280     public void testOnReceiveReadyTransaction() throws Exception {
281         new JavaTestKit(getSystem()) {{
282             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
283             final Props props =
284                 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
285             final ActorRef subject =
286                 getSystem().actorOf(props, "testReadyTransaction");
287
288             new Within(duration("1 seconds")) {
289                 protected void run() {
290
291                     subject.tell(new ReadyTransaction().toSerializable(), getRef());
292
293                     final String out = new ExpectMsg<String>("match hint") {
294                         // do not put code outside this method, will run afterwards
295                         protected String match(Object in) {
296                             if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
297                                 return "match";
298                             } else {
299                                 throw noMatch();
300                             }
301                         }
302                     }.get(); // this extracts the received message
303
304                     assertEquals("match", out);
305
306                     expectNoMsg();
307                 }
308
309
310             };
311         }};
312
313     }
314
315     @Test
316     public void testOnReceiveCloseTransaction() throws Exception {
317         new JavaTestKit(getSystem()) {{
318             final ActorRef shard = getSystem().actorOf(Shard.props("config", Collections.EMPTY_MAP));
319             final Props props =
320                 ShardTransaction.props(store.newReadWriteTransaction(), shard, TestModel.createTestContext());
321             final ActorRef subject =
322                 getSystem().actorOf(props, "testCloseTransaction");
323
324             watch(subject);
325
326             new Within(duration("2 seconds")) {
327                 protected void run() {
328
329                     subject.tell(new CloseTransaction().toSerializable(), getRef());
330
331                     final String out = new ExpectMsg<String>("match hint") {
332                         // do not put code outside this method, will run afterwards
333                         protected String match(Object in) {
334                             if (in.getClass().equals(CloseTransactionReply.SERIALIZABLE_CLASS)) {
335                                 return "match";
336                             } else {
337                                 throw noMatch();
338                             }
339                         }
340                     }.get(); // this extracts the received message
341
342                     assertEquals("match", out);
343
344                     final String termination = new ExpectMsg<String>("match hint") {
345                         // do not put code outside this method, will run afterwards
346                         protected String match(Object in) {
347                             if (in instanceof Terminated) {
348                                 return "match";
349                             } else {
350                                 throw noMatch();
351                             }
352                         }
353                     }.get(); // this extracts the received message
354
355
356                     expectNoMsg();
357                 }
358
359
360             };
361         }};
362
363     }
364 }