1 package org.opendaylight.controller.cluster.datastore;
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;
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;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertTrue;
35 public class ShardTransactionTest extends AbstractActorTest {
36 private static ListeningExecutorService storeExecutor =
37 MoreExecutors.listeningDecorator(MoreExecutors.sameThreadExecutor());
39 private static final InMemoryDOMDataStore store =
40 new InMemoryDOMDataStore("OPER", storeExecutor);
43 store.onGlobalContextUpdated(TestModel.createTestContext());
47 public void testOnReceiveReadData() throws Exception {
48 new JavaTestKit(getSystem()) {{
49 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
51 ShardTransaction.props(store.newReadWriteTransaction(), shard);
52 final ActorRef subject = getSystem().actorOf(props, "testReadData");
54 new Within(duration("1 seconds")) {
55 protected void run() {
58 new ReadData(InstanceIdentifier.builder().build()),
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()
74 }.get(); // this extracts the received message
76 assertEquals("match", out);
87 public void testOnReceiveReadDataWhenDataNotFound() throws Exception {
88 new JavaTestKit(getSystem()) {{
89 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
91 ShardTransaction.props(store.newReadWriteTransaction(), shard);
92 final ActorRef subject = getSystem().actorOf(props, "testReadDataWhenDataNotFound");
94 new Within(duration("1 seconds")) {
95 protected void run() {
98 new ReadData(TestModel.TEST_PATH),
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()
114 }.get(); // this extracts the received message
116 assertEquals("match", out);
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() {
132 .tell(new ShardTransaction.GetCompositedModification(),
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)
146 }.get(); // this extracts the received message
149 compositeModification.getModifications().size() == 1);
150 assertEquals(modificationType,
151 compositeModification.getModifications().get(0)
160 public void testOnReceiveWriteData() throws Exception {
161 new JavaTestKit(getSystem()) {{
162 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
164 ShardTransaction.props(store.newReadWriteTransaction(), shard);
165 final ActorRef subject =
166 getSystem().actorOf(props, "testWriteData");
168 new Within(duration("1 seconds")) {
169 protected void run() {
171 subject.tell(new WriteData(TestModel.TEST_PATH,
172 ImmutableNodes.containerNode(TestModel.TEST_QNAME)),
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) {
184 }.get(); // this extracts the received message
186 assertEquals("match", out);
188 assertModification(subject, WriteModification.class);
198 public void testOnReceiveMergeData() throws Exception {
199 new JavaTestKit(getSystem()) {{
200 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
202 ShardTransaction.props(store.newReadWriteTransaction(), shard);
203 final ActorRef subject =
204 getSystem().actorOf(props, "testMergeData");
206 new Within(duration("1 seconds")) {
207 protected void run() {
209 subject.tell(new MergeData(TestModel.TEST_PATH,
210 ImmutableNodes.containerNode(TestModel.TEST_QNAME)),
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) {
222 }.get(); // this extracts the received message
224 assertEquals("match", out);
226 assertModification(subject, MergeModification.class);
237 public void testOnReceiveDeleteData() throws Exception {
238 new JavaTestKit(getSystem()) {{
239 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
241 ShardTransaction.props(store.newReadWriteTransaction(), shard);
242 final ActorRef subject =
243 getSystem().actorOf(props, "testDeleteData");
245 new Within(duration("1 seconds")) {
246 protected void run() {
248 subject.tell(new DeleteData(TestModel.TEST_PATH), getRef());
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) {
259 }.get(); // this extracts the received message
261 assertEquals("match", out);
263 assertModification(subject, DeleteModification.class);
274 public void testOnReceiveReadyTransaction() throws Exception {
275 new JavaTestKit(getSystem()) {{
276 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
278 ShardTransaction.props(store.newReadWriteTransaction(), shard);
279 final ActorRef subject =
280 getSystem().actorOf(props, "testReadyTransaction");
282 new Within(duration("1 seconds")) {
283 protected void run() {
285 subject.tell(new ReadyTransaction(), getRef());
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) {
296 }.get(); // this extracts the received message
298 assertEquals("match", out);
310 public void testOnReceiveCloseTransaction() throws Exception {
311 new JavaTestKit(getSystem()) {{
312 final ActorRef shard = getSystem().actorOf(Shard.props("config"));
314 ShardTransaction.props(store.newReadWriteTransaction(), shard);
315 final ActorRef subject =
316 getSystem().actorOf(props, "testCloseTransaction");
320 new Within(duration("2 seconds")) {
321 protected void run() {
323 subject.tell(new CloseTransaction(), getRef());
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) {
334 }.get(); // this extracts the received message
336 assertEquals("match", out);
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) {
347 }.get(); // this extracts the received message