2 * Copyright (c) 2016 Brocade Communications 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
8 package org.opendaylight.controller.cluster.datastore.compat;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertTrue;
12 import akka.actor.ActorRef;
13 import akka.dispatch.Dispatchers;
14 import akka.testkit.TestActorRef;
15 import org.junit.Test;
16 import org.opendaylight.controller.cluster.datastore.AbstractShardTest;
17 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
18 import org.opendaylight.controller.cluster.datastore.Shard;
19 import org.opendaylight.controller.cluster.datastore.ShardTestKit;
20 import org.opendaylight.controller.cluster.datastore.TransactionType;
21 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
22 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransaction;
23 import org.opendaylight.controller.cluster.datastore.messages.CanCommitTransactionReply;
24 import org.opendaylight.controller.cluster.datastore.messages.CommitTransaction;
25 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
26 import org.opendaylight.controller.cluster.datastore.messages.CreateTransactionReply;
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.modification.WriteModification;
30 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
31 import org.opendaylight.controller.protobuff.messages.cohort3pc.ThreePhaseCommitCohortMessages;
32 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
33 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
36 * Shard unit tests for backwards compatibility with pre-Boron versions.
38 * @author Thomas Pantelis
40 public class PreBoronShardTest extends AbstractShardTest {
43 public void testCreateTransaction(){
44 new ShardTestKit(getSystem()) {{
45 final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
47 waitUntilLeader(shard);
49 shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
51 shard.tell(new CreateTransaction("txn-1", TransactionType.READ_ONLY.ordinal(), null,
52 DataStoreVersions.LITHIUM_VERSION).toSerializable(), getRef());
54 ShardTransactionMessages.CreateTransactionReply reply =
55 expectMsgClass(ShardTransactionMessages.CreateTransactionReply.class);
57 final String path = CreateTransactionReply.fromSerializable(reply).getTransactionPath().toString();
58 assertTrue("Unexpected transaction path " + path,
59 path.contains("akka://test/user/testCreateTransaction/shard-txn-1"));
64 public void testBatchedModificationsWithCommitOnReady() throws Throwable {
65 new ShardTestKit(getSystem()) {{
66 final TestActorRef<Shard> shard = actorFactory.createTestActor(
67 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
68 "testBatchedModificationsWithCommitOnReady");
70 waitUntilLeader(shard);
72 final String transactionID = "tx";
74 BatchedModifications batched = new BatchedModifications(transactionID,
75 DataStoreVersions.LITHIUM_VERSION, "");
76 batched.addModification(new WriteModification(TestModel.TEST_PATH,
77 ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
78 batched.setReady(true);
79 batched.setDoCommitOnReady(true);
80 batched.setTotalMessagesSent(1);
82 shard.tell(batched, getRef());
83 expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
88 public void testImmediateCommitWithForwardedReadyTransaction() throws Throwable {
89 new ShardTestKit(getSystem()) {{
90 final TestActorRef<Shard> shard = actorFactory.createTestActor(
91 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
92 "testImmediateCommitWithForwardedReadyTransaction");
94 waitUntilLeader(shard);
96 final String transactionID = "tx";
98 shard.tell(prepareForwardedReadyTransaction(mockShardDataTreeCohort(), transactionID,
99 DataStoreVersions.LITHIUM_VERSION, true), getRef());
101 expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
106 public void testThreePhaseCommitOnForwardedReadyTransaction() throws Throwable {
107 new ShardTestKit(getSystem()) {{
108 final TestActorRef<Shard> shard = actorFactory.createTestActor(
109 newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
110 "testThreePhaseCommitOnForwardedReadyTransaction");
112 waitUntilLeader(shard);
114 final String transactionID = "tx";
116 shard.tell(prepareForwardedReadyTransaction(mockShardDataTreeCohort(), transactionID,
117 DataStoreVersions.LITHIUM_VERSION, false), getRef());
118 expectMsgClass(ReadyTransactionReply.class);
120 shard.tell(new CanCommitTransaction(transactionID, DataStoreVersions.LITHIUM_VERSION).toSerializable(), getRef());
121 CanCommitTransactionReply canCommitReply = CanCommitTransactionReply.fromSerializable(
122 expectMsgClass(ThreePhaseCommitCohortMessages.CanCommitTransactionReply.class));
123 assertEquals("Can commit", true, canCommitReply.getCanCommit());
125 shard.tell(new CommitTransaction(transactionID, DataStoreVersions.LITHIUM_VERSION).toSerializable(), getRef());
126 expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);