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