Adding the wrongly removed module back in pom.xml
[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.CreateTransaction;
20 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChain;
21 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionChainReply;
22 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
23 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransaction;
24 import org.opendaylight.controller.cluster.datastore.messages.PreCommitTransactionReply;
25 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply;
27 import org.opendaylight.controller.cluster.datastore.messages.UpdateSchemaContext;
28 import org.opendaylight.controller.cluster.datastore.messages.WriteData;
29 import org.opendaylight.controller.cluster.datastore.messages.WriteDataReply;
30 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
32 import scala.concurrent.Await;
33 import scala.concurrent.Future;
34 import scala.concurrent.duration.FiniteDuration;
35
36 public class BasicIntegrationTest extends AbstractActorTest {
37
38     @Test
39     public void integrationTest() throws Exception{
40         // System.setProperty("shard.persistent", "true");
41         // This test will
42         // - create a Shard
43         // - initiate a transaction
44         // - write something
45         // - read the transaction for commit
46         // - commit the transaction
47
48
49         new JavaTestKit(getSystem()) {{
50             final Props props = Shard.props("config");
51             final ActorRef shard = getSystem().actorOf(props);
52
53             new Within(duration("5 seconds")) {
54                 protected void run() {
55
56                     shard.tell(
57                         new UpdateSchemaContext(TestModel.createTestContext()),
58                         getRef());
59
60                     shard.tell(new CreateTransactionChain().toSerializable(), getRef());
61
62                     final ActorSelection transactionChain =
63                         new ExpectMsg<ActorSelection>("CreateTransactionChainReply") {
64                             protected ActorSelection match(Object in) {
65                                 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
66                                     ActorPath transactionChainPath =
67                                         CreateTransactionChainReply.fromSerializable(getSystem(),in)
68                                             .getTransactionChainPath();
69                                     return getSystem()
70                                         .actorSelection(transactionChainPath);
71                                 } else {
72                                     throw noMatch();
73                                 }
74                             }
75                         }.get(); // this extracts the received message
76
77                     Assert.assertNotNull(transactionChain);
78
79                     transactionChain.tell(new CreateTransaction("txn-1").toSerializable(), getRef());
80
81                     final ActorSelection transaction =
82                         new ExpectMsg<ActorSelection>("CreateTransactionReply") {
83                             protected ActorSelection match(Object in) {
84                                 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
85                                     CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
86                                     return getSystem()
87                                         .actorSelection(reply
88                                             .getTransactionPath());
89                                 } else {
90                                     throw noMatch();
91                                 }
92                             }
93                         }.get(); // this extracts the received message
94
95                     Assert.assertNotNull(transaction);
96
97                     // Add a watch on the transaction actor so that we are notified when it dies
98                     final ActorRef transactionActorRef = watchActor(transaction);
99
100                     transaction.tell(new WriteData(TestModel.TEST_PATH,
101                         ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
102                         getRef());
103
104                     Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
105                         protected Boolean match(Object in) {
106                             if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
107                                 return true;
108                             } else {
109                                 throw noMatch();
110                             }
111                         }
112                     }.get(); // this extracts the received message
113
114                     Assert.assertTrue(writeDone);
115
116                     transaction.tell(new ReadyTransaction().toSerializable(), getRef());
117
118                     final ActorSelection cohort =
119                         new ExpectMsg<ActorSelection>("ReadyTransactionReply") {
120                             protected ActorSelection match(Object in) {
121                                 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
122                                     ActorPath cohortPath =
123                                         ReadyTransactionReply.fromSerializable(getSystem(),in)
124                                             .getCohortPath();
125                                     return getSystem()
126                                         .actorSelection(cohortPath);
127                                 } else {
128                                     throw noMatch();
129                                 }
130                             }
131                         }.get(); // this extracts the received message
132
133                     Assert.assertNotNull(cohort);
134
135                     // Add a watch on the transaction actor so that we are notified when it dies
136                     final ActorRef cohorActorRef = watchActor(cohort);
137
138                     cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
139
140                     Boolean preCommitDone =
141                         new ExpectMsg<Boolean>("PreCommitTransactionReply") {
142                             protected Boolean match(Object in) {
143                                 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
144                                     return true;
145                                 } else {
146                                     throw noMatch();
147                                 }
148                             }
149                         }.get(); // this extracts the received message
150
151                     Assert.assertTrue(preCommitDone);
152
153                     cohort.tell(new CommitTransaction().toSerializable(), getRef());
154
155                     // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
156
157                 }
158
159
160             };
161         }
162
163             private ActorRef watchActor(ActorSelection actor) {
164                 Future<ActorRef> future = actor
165                     .resolveOne(FiniteDuration.apply(100, "milliseconds"));
166
167                 try {
168                     ActorRef actorRef = Await.result(future,
169                         FiniteDuration.apply(100, "milliseconds"));
170
171                     watch(actorRef);
172
173                     return actorRef;
174                 } catch (Exception e) {
175                     throw new RuntimeException(e);
176                 }
177
178             }
179         };
180
181
182     }
183 }