Make CompositeModification serializable using protocol buffers
[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.actor.Terminated;
16 import akka.testkit.JavaTestKit;
17 import junit.framework.Assert;
18 import org.junit.Test;
19 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CommitTransactionReply;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
23 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
24 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
25 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
28 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
29 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
30 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
31 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
32 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
34 import scala.concurrent.Await;
35 import scala.concurrent.Future;
36 import scala.concurrent.duration.FiniteDuration;
37
38 public class BasicIntegrationTest extends AbstractActorTest {
39
40     @Test
41     public void integrationTest() throws Exception{
42         // System.setProperty("shard.persistent", "true");
43         // This test will
44         // - create a Shard
45         // - initiate a transaction
46         // - write something
47         // - read the transaction for commit
48         // - commit the transaction
49
50
51         new JavaTestKit(getSystem()) {{
52             final Props props = Shard.props("config");
53             final ActorRef shard = getSystem().actorOf(props);
54
55             new Within(duration("5 seconds")) {
56                 protected void run() {
57
58                     shard.tell(
59                         new UpdateSchemaContext(TestModel.createTestContext()),
60                         getRef());
61
62                     shard.tell(new CreateTransactionChain(), getRef());
63
64                     final ActorSelection transactionChain =
65                         new ExpectMsg<ActorSelection>("CreateTransactionChainReply") {
66                             protected ActorSelection match(Object in) {
67                                 if (in instanceof CreateTransactionChainReply) {
68                                     ActorPath transactionChainPath =
69                                         ((CreateTransactionChainReply) in)
70                                             .getTransactionChainPath();
71                                     return getSystem()
72                                         .actorSelection(transactionChainPath);
73                                 } else {
74                                     throw noMatch();
75                                 }
76                             }
77                         }.get(); // this extracts the received message
78
79                     Assert.assertNotNull(transactionChain);
80
81                     transactionChain.tell(new CreateTransaction("txn-1"), getRef());
82
83                     final ActorSelection transaction =
84                         new ExpectMsg<ActorSelection>("CreateTransactionReply") {
85                             protected ActorSelection match(Object in) {
86                                 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
87                                     CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
88                                     return getSystem()
89                                         .actorSelection(reply
90                                             .getTransactionPath());
91                                 } else {
92                                     throw noMatch();
93                                 }
94                             }
95                         }.get(); // this extracts the received message
96
97                     Assert.assertNotNull(transaction);
98
99                     // Add a watch on the transaction actor so that we are notified when it dies
100                     final ActorRef transactionActorRef = watchActor(transaction);
101
102                     transaction.tell(new WriteData(TestModel.TEST_PATH,
103                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
104                         getRef());
105
106                     Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
107                         protected Boolean match(Object in) {
108                             if (in instanceof WriteDataReply) {
109                                 return true;
110                             } else {
111                                 throw noMatch();
112                             }
113                         }
114                     }.get(); // this extracts the received message
115
116                     Assert.assertTrue(writeDone);
117
118                     transaction.tell(new ReadyTransaction(), getRef());
119
120                     final ActorSelection cohort =
121                         new ExpectMsg<ActorSelection>("ReadyTransactionReply") {
122                             protected ActorSelection match(Object in) {
123                                 if (in instanceof ReadyTransactionReply) {
124                                     ActorPath cohortPath =
125                                         ((ReadyTransactionReply) in)
126                                             .getCohortPath();
127                                     return getSystem()
128                                         .actorSelection(cohortPath);
129                                 } else {
130                                     throw noMatch();
131                                 }
132                             }
133                         }.get(); // this extracts the received message
134
135                     Assert.assertNotNull(cohort);
136
137                     // Add a watch on the transaction actor so that we are notified when it dies
138                     final ActorRef cohorActorRef = watchActor(cohort);
139
140                     cohort.tell(new PreCommitTransaction(), getRef());
141
142                     Boolean preCommitDone =
143                         new ExpectMsg<Boolean>("PreCommitTransactionReply") {
144                             protected Boolean match(Object in) {
145                                 if (in instanceof PreCommitTransactionReply) {
146                                     return true;
147                                 } else {
148                                     throw noMatch();
149                                 }
150                             }
151                         }.get(); // this extracts the received message
152
153                     Assert.assertTrue(preCommitDone);
154
155                     // FIXME : When we commit on the cohort it "kills" the Transaction.
156                     // This in turn kills the child of Transaction as well.
157                     // The order in which we receive the terminated event for both
158                     // these actors is not fixed which may cause this test to fail
159                     cohort.tell(new CommitTransaction(), getRef());
160
161                     final Boolean terminatedCohort =
162                         new ExpectMsg<Boolean>("Terminated Cohort") {
163                             protected Boolean match(Object in) {
164                                 if (in instanceof Terminated) {
165                                     return cohorActorRef.equals(((Terminated) in).actor());
166                                 } else {
167                                     throw noMatch();
168                                 }
169                             }
170                         }.get(); // this extracts the received message
171
172                     Assert.assertTrue(terminatedCohort);
173
174
175                     final Boolean terminatedTransaction =
176                         new ExpectMsg<Boolean>("Terminated Transaction") {
177                             protected Boolean match(Object in) {
178                                 if (in instanceof Terminated) {
179                                     return transactionActorRef.equals(((Terminated) in).actor());
180                                 } else {
181                                     throw noMatch();
182                                 }
183                             }
184                         }.get(); // this extracts the received message
185
186                     Assert.assertTrue(terminatedTransaction);
187
188                     final Boolean commitDone =
189                         new ExpectMsg<Boolean>("CommitTransactionReply") {
190                             protected Boolean match(Object in) {
191                                 if (in instanceof CommitTransactionReply) {
192                                     return true;
193                                 } else {
194                                     throw noMatch();
195                                 }
196                             }
197                         }.get(); // this extracts the received message
198
199                     Assert.assertTrue(commitDone);
200
201                 }
202
203
204             };
205         }
206
207             private ActorRef watchActor(ActorSelection actor) {
208                 Future<ActorRef> future = actor
209                     .resolveOne(FiniteDuration.apply(100, "milliseconds"));
210
211                 try {
212                     ActorRef actorRef = Await.result(future,
213                         FiniteDuration.apply(100, "milliseconds"));
214
215                     watch(actorRef);
216
217                     return actorRef;
218                 } catch (Exception e) {
219                     throw new RuntimeException(e);
220                 }
221
222             }
223         };
224
225
226     }
227 }