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 public class BasicIntegrationTest extends AbstractActorTest {
39 public void integrationTest() throws Exception{
40 // System.setProperty("shard.persistent", "true");
43 // - initiate a transaction
45 // - read the transaction for commit
46 // - commit the transaction
49 new JavaTestKit(getSystem()) {{
50 final Props props = Shard.props("config");
51 final ActorRef shard = getSystem().actorOf(props);
53 new Within(duration("5 seconds")) {
54 protected void run() {
57 new UpdateSchemaContext(TestModel.createTestContext()),
60 shard.tell(new CreateTransactionChain().toSerializable(), getRef());
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();
70 .actorSelection(transactionChainPath);
75 }.get(); // this extracts the received message
77 Assert.assertNotNull(transactionChain);
79 transactionChain.tell(new CreateTransaction("txn-1").toSerializable(), getRef());
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);
88 .getTransactionPath());
93 }.get(); // this extracts the received message
95 Assert.assertNotNull(transaction);
97 // Add a watch on the transaction actor so that we are notified when it dies
98 final ActorRef transactionActorRef = watchActor(transaction);
100 transaction.tell(new WriteData(TestModel.TEST_PATH,
101 ImmutableNodes.containerNode(TestModel.TEST_QNAME), TestModel.createTestContext()).toSerializable(),
104 Boolean writeDone = new ExpectMsg<Boolean>("WriteDataReply") {
105 protected Boolean match(Object in) {
106 if (in.getClass().equals(WriteDataReply.SERIALIZABLE_CLASS)) {
112 }.get(); // this extracts the received message
114 Assert.assertTrue(writeDone);
116 transaction.tell(new ReadyTransaction().toSerializable(), getRef());
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)
126 .actorSelection(cohortPath);
131 }.get(); // this extracts the received message
133 Assert.assertNotNull(cohort);
135 // Add a watch on the transaction actor so that we are notified when it dies
136 final ActorRef cohorActorRef = watchActor(cohort);
138 cohort.tell(new PreCommitTransaction().toSerializable(), getRef());
140 Boolean preCommitDone =
141 new ExpectMsg<Boolean>("PreCommitTransactionReply") {
142 protected Boolean match(Object in) {
143 if (in.getClass().equals(PreCommitTransactionReply.SERIALIZABLE_CLASS)) {
149 }.get(); // this extracts the received message
151 Assert.assertTrue(preCommitDone);
153 cohort.tell(new CommitTransaction().toSerializable(), getRef());
155 // FIXME : Add assertions that the commit worked and that the cohort and transaction actors were terminated
163 private ActorRef watchActor(ActorSelection actor) {
164 Future<ActorRef> future = actor
165 .resolveOne(FiniteDuration.apply(100, "milliseconds"));
168 ActorRef actorRef = Await.result(future,
169 FiniteDuration.apply(100, "milliseconds"));
174 } catch (Exception e) {
175 throw new RuntimeException(e);