Ensure that modifications are tracked by ShardTransaction
[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.testkit.JavaTestKit;
6 import com.google.common.util.concurrent.ListeningExecutorService;
7 import com.google.common.util.concurrent.MoreExecutors;
8 import org.junit.Test;
9 import org.opendaylight.controller.cluster.datastore.messages.CloseTransaction;
10 import org.opendaylight.controller.cluster.datastore.messages.CloseTransactionReply;
11 import org.opendaylight.controller.cluster.datastore.messages.DeleteData;
12 import org.opendaylight.controller.cluster.datastore.messages.DeleteDataReply;
13 import org.opendaylight.controller.cluster.datastore.messages.MergeData;
14 import org.opendaylight.controller.cluster.datastore.messages.MergeDataReply;
15 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
16 import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply;
17 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
18 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
19 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
20 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
21 import org.opendaylight.controller.cluster.datastore.modification.CompositeModification;
22 import org.opendaylight.controller.cluster.datastore.modification.DeleteModification;
23 import org.opendaylight.controller.cluster.datastore.modification.MergeModification;
24 import org.opendaylight.controller.cluster.datastore.modification.Modification;
25 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
26 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
27 import org.opendaylight.controller.md.sal.dom.store.impl.InMemoryDOMDataStore;
28 import org.opendaylight.yangtools.yang.data.api.InstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
30
31 import static org.junit.Assert.assertEquals;
32 import static org.junit.Assert.assertTrue;
33
34 public class ShardTransactionTest extends AbstractActorTest {
35   private static ListeningExecutorService storeExecutor = MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
36
37   private static final InMemoryDOMDataStore store = new InMemoryDOMDataStore("OPER", storeExecutor);
38
39   static {
40     store.onGlobalContextUpdated(TestModel.createTestContext());
41   }
42
43   @Test
44   public void testOnReceiveReadData() throws Exception {
45     new JavaTestKit(getSystem()) {{
46       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
47       final ActorRef subject = getSystem().actorOf(props, "testReadData");
48
49       new Within(duration("1 seconds")) {
50         protected void run() {
51
52           subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
53
54           final String out = new ExpectMsg<String>("match hint") {
55             // do not put code outside this method, will run afterwards
56             protected String match(Object in) {
57               if (in instanceof ReadDataReply) {
58                 if (((ReadDataReply) in).getNormalizedNode() != null) {
59                   return "match";
60                 }
61                 return null;
62               } else {
63                 throw noMatch();
64               }
65             }
66           }.get(); // this extracts the received message
67
68           assertEquals("match", out);
69
70           expectNoMsg();
71         }
72
73
74       };
75     }};
76   }
77
78   private void assertModification(final ActorRef subject, final Class<? extends Modification> modificationType){
79     new JavaTestKit(getSystem()) {{
80       new Within(duration("1 seconds")) {
81         protected void run() {
82           subject.tell(new ShardTransaction.GetCompositedModification(), getRef());
83
84           final CompositeModification compositeModification = new ExpectMsg<CompositeModification>("match hint") {
85             // do not put code outside this method, will run afterwards
86             protected CompositeModification match(Object in) {
87               if (in instanceof ShardTransaction.GetCompositeModificationReply) {
88                 return ((ShardTransaction.GetCompositeModificationReply) in).getModification();
89               } else {
90                 throw noMatch();
91               }
92             }
93           }.get(); // this extracts the received message
94
95           assertTrue(compositeModification.getModifications().size() == 1);
96           assertEquals(modificationType, compositeModification.getModifications().get(0).getClass());
97
98         }
99       };
100     }};
101   }
102
103   @Test
104   public void testOnReceiveWriteData() throws Exception {
105     new JavaTestKit(getSystem()) {{
106       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
107       final ActorRef subject = getSystem().actorOf(props, "testWriteData");
108
109       new Within(duration("1 seconds")) {
110         protected void run() {
111
112           subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
113
114           final String out = new ExpectMsg<String>("match hint") {
115             // do not put code outside this method, will run afterwards
116             protected String match(Object in) {
117               if (in instanceof WriteDataReply) {
118                 return "match";
119               } else {
120                 throw noMatch();
121               }
122             }
123           }.get(); // this extracts the received message
124
125           assertEquals("match", out);
126
127           assertModification(subject, WriteModification.class);
128           expectNoMsg();
129         }
130
131
132       };
133     }};
134   }
135
136   @Test
137   public void testOnReceiveMergeData() throws Exception {
138     new JavaTestKit(getSystem()) {{
139       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
140       final ActorRef subject = getSystem().actorOf(props, "testMergeData");
141
142       new Within(duration("1 seconds")) {
143         protected void run() {
144
145           subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
146
147           final String out = new ExpectMsg<String>("match hint") {
148             // do not put code outside this method, will run afterwards
149             protected String match(Object in) {
150               if (in instanceof MergeDataReply) {
151                 return "match";
152               } else {
153                 throw noMatch();
154               }
155             }
156           }.get(); // this extracts the received message
157
158           assertEquals("match", out);
159
160           assertModification(subject, MergeModification.class);
161
162           expectNoMsg();
163         }
164
165
166       };
167     }};
168   }
169
170   @Test
171   public void testOnReceiveDeleteData() throws Exception {
172     new JavaTestKit(getSystem()) {{
173       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
174       final ActorRef subject = getSystem().actorOf(props, "testDeleteData");
175
176       new Within(duration("1 seconds")) {
177         protected void run() {
178
179           subject.tell(new DeleteData(TestModel.TEST_PATH), 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 instanceof DeleteDataReply) {
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, DeleteModification.class);
195           expectNoMsg();
196         }
197
198
199       };
200     }};
201   }
202
203
204   @Test
205   public void testOnReceiveReadyTransaction() throws Exception {
206     new JavaTestKit(getSystem()) {{
207       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
208       final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");
209
210       new Within(duration("1 seconds")) {
211         protected void run() {
212
213           subject.tell(new ReadyTransaction(), getRef());
214
215           final String out = new ExpectMsg<String>("match hint") {
216             // do not put code outside this method, will run afterwards
217             protected String match(Object in) {
218               if (in instanceof ReadyTransactionReply) {
219                 return "match";
220               } else {
221                 throw noMatch();
222               }
223             }
224           }.get(); // this extracts the received message
225
226           assertEquals("match", out);
227
228           expectNoMsg();
229         }
230
231
232       };
233     }};
234
235   }
236
237   @Test
238   public void testOnReceiveCloseTransaction() throws Exception {
239     new JavaTestKit(getSystem()) {{
240       final Props props = ShardTransaction.props(store.newReadWriteTransaction());
241       final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");
242
243       new Within(duration("1 seconds")) {
244         protected void run() {
245
246           subject.tell(new CloseTransaction(), getRef());
247
248           final String out = new ExpectMsg<String>("match hint") {
249             // do not put code outside this method, will run afterwards
250             protected String match(Object in) {
251               if (in instanceof CloseTransactionReply) {
252                 return "match";
253               } else {
254                 throw noMatch();
255               }
256             }
257           }.get(); // this extracts the received message
258
259           assertEquals("match", out);
260
261           expectNoMsg();
262         }
263
264
265       };
266     }};
267
268   }
269
270
271 }