8c3ec82a54eb7c50d195675cffc122b6ed13d546
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / BasicIntegrationTest.java
1 /*
2  * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8
9 package org.opendaylight.controller.cluster.datastore;
10
11 import akka.actor.ActorPath;
12 import akka.actor.ActorRef;
13 import akka.actor.ActorSelection;
14 import akka.actor.Props;
15 import akka.testkit.JavaTestKit;
16 import junit.framework.Assert;
17 import org.junit.Test;
18 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
28 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
33
34 public class BasicIntegrationTest extends AbstractActorTest {
35
36     @Test
37     public void integrationTest() {
38         // This test will
39         // - create a Shard
40         // - initiate a transaction
41         // - write something
42         // - read the transaction for commit
43         // - commit the transaction
44
45
46         new JavaTestKit(getSystem()) {{
47             final Props props = Shard.props("config");
48             final ActorRef shard = getSystem().actorOf(props);
49
50             new Within(duration("5 seconds")) {
51                 protected void run() {
52
53                     shard.tell(
54                         new UpdateSchemaContext(TestModel.createTestContext()),
55                         getRef());
56
57                     shard.tell(new CreateTransactionChain(), getRef());
58
59                     final ActorSelection transactionChain =
60                         new ExpectMsg<ActorSelection>("match hint") {
61                             protected ActorSelection match(Object in) {
62                                 if (in instanceof CreateTransactionChainReply) {
63                                     ActorPath transactionChainPath =
64                                         ((CreateTransactionChainReply) in)
65                                             .getTransactionChainPath();
66                                     return getSystem()
67                                         .actorSelection(transactionChainPath);
68                                 } else {
69                                     throw noMatch();
70                                 }
71                             }
72                         }.get(); // this extracts the received message
73
74                     Assert.assertNotNull(transactionChain);
75
76                     transactionChain.tell(new CreateTransaction(), getRef());
77
78                     final ActorSelection transaction =
79                         new ExpectMsg<ActorSelection>("match hint") {
80                             protected ActorSelection match(Object in) {
81                                 if (in instanceof CreateTransactionReply) {
82                                     ActorPath transactionPath =
83                                         ((CreateTransactionReply) in)
84                                             .getTransactionPath();
85                                     return getSystem()
86                                         .actorSelection(transactionPath);
87                                 } else {
88                                     throw noMatch();
89                                 }
90                             }
91                         }.get(); // this extracts the received message
92
93                     Assert.assertNotNull(transaction);
94
95                     transaction.tell(new WriteData(TestModel.TEST_PATH,
96                         ImmutableNodes.containerNode(TestModel.TEST_QNAME)),
97                         getRef());
98
99                     Boolean writeDone = new ExpectMsg<Boolean>("match hint") {
100                         protected Boolean match(Object in) {
101                             if (in instanceof WriteDataReply) {
102                                 return true;
103                             } else {
104                                 throw noMatch();
105                             }
106                         }
107                     }.get(); // this extracts the received message
108
109                     Assert.assertTrue(writeDone);
110
111                     transaction.tell(new ReadyTransaction(), getRef());
112
113                     final ActorSelection cohort =
114                         new ExpectMsg<ActorSelection>("match hint") {
115                             protected ActorSelection match(Object in) {
116                                 if (in instanceof ReadyTransactionReply) {
117                                     ActorPath cohortPath =
118                                         ((ReadyTransactionReply) in)
119                                             .getCohortPath();
120                                     return getSystem()
121                                         .actorSelection(cohortPath);
122                                 } else {
123                                     throw noMatch();
124                                 }
125                             }
126                         }.get(); // this extracts the received message
127
128                     Assert.assertNotNull(cohort);
129
130                     cohort.tell(new PreCommitTransaction(), getRef());
131
132                     Boolean preCommitDone =
133                         new ExpectMsg<Boolean>("match hint") {
134                             protected Boolean match(Object in) {
135                                 if (in instanceof PreCommitTransactionReply) {
136                                     return true;
137                                 } else {
138                                     throw noMatch();
139                                 }
140                             }
141                         }.get(); // this extracts the received message
142
143                     Assert.assertTrue(preCommitDone);
144
145                     cohort.tell(new CommitTransaction(), getRef());
146
147                     final Boolean commitDone =
148                         new ExpectMsg<Boolean>("match hint") {
149                             protected Boolean match(Object in) {
150                                 if (in instanceof CommitTransactionReply) {
151                                     return true;
152                                 } else {
153                                     throw noMatch();
154                                 }
155                             }
156                         }.get(); // this extracts the received message
157
158                     Assert.assertTrue(commitDone);
159
160                 }
161
162
163             };
164         }};
165
166
167     }
168 }

©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.