2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.cluster.datastore;
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;
36 import java.util.Collections;
38 public class BasicIntegrationTest extends AbstractActorTest {
41 public void integrationTest() throws Exception{
42 // System.setProperty("shard.persistent", "true");
45 // - initiate a transaction
47 // - read the transaction for commit
48 // - commit the transaction
51 new JavaTestKit(getSystem()) {{
52 final Props props = Shard.props("config", Collections.EMPTY_MAP);
53 final ActorRef shard = getSystem().actorOf(props);
55 new Within(duration("5 seconds")) {
56 protected void run() {
60 new UpdateSchemaContext(TestModel.createTestContext()),
64 // Wait for Shard to become a Leader
67 } catch (InterruptedException e) {
70 // 1. Create a TransactionChain
71 shard.tell(new CreateTransactionChain().toSerializable(), getRef());
73 final ActorSelection transactionChain =
74 new ExpectMsg<ActorSelection>("CreateTransactionChainReply") {
75 protected ActorSelection match(Object in) {
76 if (in.getClass().equals(CreateTransactionChainReply.SERIALIZABLE_CLASS)) {
77 ActorPath transactionChainPath =
78 CreateTransactionChainReply.fromSerializable(getSystem(),in)
79 .getTransactionChainPath();
81 .actorSelection(transactionChainPath);
86 }.get(); // this extracts the received message
88 Assert.assertNotNull(transactionChain);
90 System.out.println("Successfully created transaction chain");
92 // 2. Create a Transaction on the TransactionChain
93 transactionChain.tell(new CreateTransaction("txn-1").toSerializable(), getRef());
95 final ActorSelection transaction =
96 new ExpectMsg<ActorSelection>("CreateTransactionReply") {
97 protected ActorSelection match(Object in) {
98 if (CreateTransactionReply.SERIALIZABLE_CLASS.equals(in.getClass())) {
99 CreateTransactionReply reply = CreateTransactionReply.fromSerializable(in);
101 .actorSelection(reply
102 .getTransactionPath());
107 }.get(); // this extracts the received message
109 Assert.assertNotNull(transaction);
111 System.out.println("Successfully created transaction");
113 // 3. Write some data
114 transaction.tell(new WriteData(TestModel.TEST_PATH,
115 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
118 Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
119 protected Boolean match(Object in) {
120 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
126 }.get(); // this extracts the received message
128 Assert.assertTrue(writeDone);
130 System.out.println("Successfully wrote data");
132 // 4. Ready the transaction for commit
134 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
136 final ActorSelection cohort =
137 new ExpectMsg<ActorSelection>("ReadyTransactionReply") {
138 protected ActorSelection match(Object in) {
139 if (in.getClass().equals(ReadyTransactionReply.SERIALIZABLE_CLASS)) {
140 ActorPath cohortPath =
141 ReadyTransactionReply.fromSerializable(getSystem(),in)
144 .actorSelection(cohortPath);
149 }.get(); // this extracts the received message
151 Assert.assertNotNull(cohort);
153 System.out.println("Successfully readied the transaction");
155 // 5. PreCommit the transaction
157 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
159 Boolean preCommitDone =
160 new ExpectMsg<Boolean>("PreCommitTransactionReply") {
161 protected Boolean match(Object in) {
162 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
168 }.get(); // this extracts the received message
170 Assert.assertTrue(preCommitDone);
172 System.out.println("Successfully pre-committed the transaction");
174 // 6. Commit the transaction
175 cohort.tell(new CommitTransaction().toSerializable(), getRef());
177 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
179 System.out.println("TODO : Check Successfully committed the transaction");
186 private ActorRef watchActor(ActorSelection actor) {
187 Future<ActorRef> future = actor
188 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
191 ActorRef actorRef = Await.result(future,
192 FiniteDuration.apply(100, "milliseconds"));
197 } catch (Exception e) {
198 throw new RuntimeException(e);