Fix raw reference
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / compat / PreBoronShardTest.java
1 /*
2  * Copyright (c) 2016 Brocade Communications 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 package org.opendaylight.controller.cluster.datastore.compat;
9
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;
34
35 /**
36  * Shard unit tests for backwards compatibility with pre-Boron versions.
37  *
38  * @author Thomas Pantelis
39  */
40 public class PreBoronShardTest extends AbstractShardTest {
41
42     @Test
43     public void testCreateTransaction(){
44         new ShardTestKit(getSystem()) {{
45             final ActorRef shard = actorFactory.createActor(newShardProps(), "testCreateTransaction");
46
47             waitUntilLeader(shard);
48
49             shard.tell(new UpdateSchemaContext(TestModel.createTestContext()), getRef());
50
51             shard.tell(new CreateTransaction("txn-1", TransactionType.READ_ONLY.ordinal(), null,
52                     DataStoreVersions.LITHIUM_VERSION).toSerializable(), getRef());
53
54             ShardTransactionMessages.CreateTransactionReply reply =
55                     expectMsgClass(ShardTransactionMessages.CreateTransactionReply.class);
56
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"));
60         }};
61     }
62
63     @Test
64     public void testBatchedModificationsWithCommitOnReady() throws Throwable {
65         new ShardTestKit(getSystem()) {{
66             final TestActorRef<Shard> shard = actorFactory.createTestActor(
67                     newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
68                     "testBatchedModificationsWithCommitOnReady");
69
70             waitUntilLeader(shard);
71
72             final String transactionID = "tx";
73
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);
81
82             shard.tell(batched, getRef());
83             expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
84         }};
85     }
86
87     @Test
88     public void testImmediateCommitWithForwardedReadyTransaction() throws Throwable {
89         new ShardTestKit(getSystem()) {{
90             final TestActorRef<Shard> shard = actorFactory.createTestActor(
91                     newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
92                     "testImmediateCommitWithForwardedReadyTransaction");
93
94             waitUntilLeader(shard);
95
96             final String transactionID = "tx";
97
98             shard.tell(prepareForwardedReadyTransaction(mockShardDataTreeCohort(), transactionID,
99                     DataStoreVersions.LITHIUM_VERSION, true), getRef());
100
101             expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
102         }};
103     }
104
105     @Test
106     public void testThreePhaseCommitOnForwardedReadyTransaction() throws Throwable {
107         new ShardTestKit(getSystem()) {{
108             final TestActorRef<Shard> shard = actorFactory.createTestActor(
109                     newShardProps().withDispatcher(Dispatchers.DefaultDispatcherId()),
110                     "testThreePhaseCommitOnForwardedReadyTransaction");
111
112             waitUntilLeader(shard);
113
114             final String transactionID = "tx";
115
116             shard.tell(prepareForwardedReadyTransaction(mockShardDataTreeCohort(), transactionID,
117                     DataStoreVersions.LITHIUM_VERSION, false), getRef());
118             expectMsgClass(ReadyTransactionReply.class);
119
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());
124
125             shard.tell(new CommitTransaction(transactionID, DataStoreVersions.LITHIUM_VERSION).toSerializable(), getRef());
126             expectMsgClass(ThreePhaseCommitCohortMessages.CommitTransactionReply.class);
127         }};
128     }
129 }