9116f24c92971b3f0491b6de52d07eff01d84645
[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 ActorRef shard = getSystem().actorOf(Shard.props("config"));
47       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
48       final ActorRef subject = getSystem().actorOf(props, "testReadData");
49
50       new Within(duration("1 seconds")) {
51         protected void run() {
52
53           subject.tell(new ReadData(InstanceIdentifier.builder().build()), getRef());
54
55           final String out = new ExpectMsg<String>("match hint") {
56             // do not put code outside this method, will run afterwards
57             protected String match(Object in) {
58               if (in instanceof ReadDataReply) {
59                 if (((ReadDataReply) in).getNormalizedNode() != null) {
60                   return "match";
61                 }
62                 return null;
63               } else {
64                 throw noMatch();
65               }
66             }
67           }.get(); // this extracts the received message
68
69           assertEquals("match", out);
70
71           expectNoMsg();
72         }
73
74
75       };
76     }};
77   }
78
79   private void assertModification(final ActorRef subject, final Class<? extends Modification> modificationType){
80     new JavaTestKit(getSystem()) {{
81       new Within(duration("1 seconds")) {
82         protected void run() {
83           subject.tell(new ShardTransaction.GetCompositedModification(), getRef());
84
85           final CompositeModification compositeModification = new ExpectMsg<CompositeModification>("match hint") {
86             // do not put code outside this method, will run afterwards
87             protected CompositeModification match(Object in) {
88               if (in instanceof ShardTransaction.GetCompositeModificationReply) {
89                 return ((ShardTransaction.GetCompositeModificationReply) in).getModification();
90               } else {
91                 throw noMatch();
92               }
93             }
94           }.get(); // this extracts the received message
95
96           assertTrue(compositeModification.getModifications().size() == 1);
97           assertEquals(modificationType, compositeModification.getModifications().get(0).getClass());
98
99         }
100       };
101     }};
102   }
103
104   @Test
105   public void testOnReceiveWriteData() throws Exception {
106     new JavaTestKit(getSystem()) {{
107       final ActorRef shard = getSystem().actorOf(Shard.props("config"));
108       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
109       final ActorRef subject = getSystem().actorOf(props, "testWriteData");
110
111       new Within(duration("1 seconds")) {
112         protected void run() {
113
114           subject.tell(new WriteData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
115
116           final String out = new ExpectMsg<String>("match hint") {
117             // do not put code outside this method, will run afterwards
118             protected String match(Object in) {
119               if (in instanceof WriteDataReply) {
120                 return "match";
121               } else {
122                 throw noMatch();
123               }
124             }
125           }.get(); // this extracts the received message
126
127           assertEquals("match", out);
128
129           assertModification(subject, WriteModification.class);
130           expectNoMsg();
131         }
132
133
134       };
135     }};
136   }
137
138   @Test
139   public void testOnReceiveMergeData() throws Exception {
140     new JavaTestKit(getSystem()) {{
141       final ActorRef shard = getSystem().actorOf(Shard.props("config"));
142       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
143       final ActorRef subject = getSystem().actorOf(props, "testMergeData");
144
145       new Within(duration("1 seconds")) {
146         protected void run() {
147
148           subject.tell(new MergeData(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)), getRef());
149
150           final String out = new ExpectMsg<String>("match hint") {
151             // do not put code outside this method, will run afterwards
152             protected String match(Object in) {
153               if (in instanceof MergeDataReply) {
154                 return "match";
155               } else {
156                 throw noMatch();
157               }
158             }
159           }.get(); // this extracts the received message
160
161           assertEquals("match", out);
162
163           assertModification(subject, MergeModification.class);
164
165           expectNoMsg();
166         }
167
168
169       };
170     }};
171   }
172
173   @Test
174   public void testOnReceiveDeleteData() throws Exception {
175     new JavaTestKit(getSystem()) {{
176       final ActorRef shard = getSystem().actorOf(Shard.props("config"));
177       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
178       final ActorRef subject = getSystem().actorOf(props, "testDeleteData");
179
180       new Within(duration("1 seconds")) {
181         protected void run() {
182
183           subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
184
185           final String out = new ExpectMsg<String>("match hint") {
186             // do not put code outside this method, will run afterwards
187             protected String match(Object in) {
188               if (in instanceof DeleteDataReply) {
189                 return "match";
190               } else {
191                 throw noMatch();
192               }
193             }
194           }.get(); // this extracts the received message
195
196           assertEquals("match", out);
197
198           assertModification(subject, DeleteModification.class);
199           expectNoMsg();
200         }
201
202
203       };
204     }};
205   }
206
207
208   @Test
209   public void testOnReceiveReadyTransaction() throws Exception {
210     new JavaTestKit(getSystem()) {{
211       final ActorRef shard = getSystem().actorOf(Shard.props("config"));
212       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
213       final ActorRef subject = getSystem().actorOf(props, "testReadyTransaction");
214
215       new Within(duration("1 seconds")) {
216         protected void run() {
217
218           subject.tell(new ReadyTransaction(), getRef());
219
220           final String out = new ExpectMsg<String>("match hint") {
221             // do not put code outside this method, will run afterwards
222             protected String match(Object in) {
223               if (in instanceof ReadyTransactionReply) {
224                 return "match";
225               } else {
226                 throw noMatch();
227               }
228             }
229           }.get(); // this extracts the received message
230
231           assertEquals("match", out);
232
233           expectNoMsg();
234         }
235
236
237       };
238     }};
239
240   }
241
242   @Test
243   public void testOnReceiveCloseTransaction() throws Exception {
244     new JavaTestKit(getSystem()) {{
245       final ActorRef shard = getSystem().actorOf(Shard.props("config"));
246       final Props props = ShardTransaction.props(store.newReadWriteTransaction(), shard);
247       final ActorRef subject = getSystem().actorOf(props, "testCloseTransaction");
248
249       new Within(duration("1 seconds")) {
250         protected void run() {
251
252           subject.tell(new CloseTransaction(), getRef());
253
254           final String out = new ExpectMsg<String>("match hint") {
255             // do not put code outside this method, will run afterwards
256             protected String match(Object in) {
257               if (in instanceof CloseTransactionReply) {
258                 return "match";
259               } else {
260                 throw noMatch();
261               }
262             }
263           }.get(); // this extracts the received message
264
265           assertEquals("match", out);
266
267           expectNoMsg();
268         }
269
270
271       };
272     }};
273
274   }
275
276
277 }

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.