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