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.mockito.Matchers.any;
11 import static org.mockito.Matchers.argThat;
12 import static org.mockito.Matchers.eq;
13 import static org.mockito.Matchers.isA;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.verify;
16 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
17 import akka.actor.ActorRef;
18 import akka.actor.ActorSystem;
19 import akka.actor.Props;
20 import akka.dispatch.Futures;
21 import akka.util.Timeout;
22 import org.junit.Test;
23 import org.mockito.ArgumentMatcher;
24 import org.opendaylight.controller.cluster.datastore.AbstractTransactionProxyTest;
25 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
26 import org.opendaylight.controller.cluster.datastore.TransactionProxy;
27 import org.opendaylight.controller.cluster.datastore.TransactionType;
28 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
29 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
30 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
31 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
32 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
33 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages.CreateTransactionReply;
36 * TransactionProxy unit tests for backwards compatibility with pre-Boron versions.
38 * @author Thomas Pantelis
40 public class PreBoronTransactionProxyTest extends AbstractTransactionProxyTest {
42 private CreateTransaction eqLegacyCreateTransaction(final TransactionType type) {
43 ArgumentMatcher<CreateTransaction> matcher = new ArgumentMatcher<CreateTransaction>() {
45 public boolean matches(Object argument) {
46 if(ShardTransactionMessages.CreateTransaction.class.equals(argument.getClass())) {
47 CreateTransaction obj = CreateTransaction.fromSerializable(argument);
48 return obj.getTransactionId().startsWith(memberName) &&
49 obj.getTransactionType() == type.ordinal();
56 return argThat(matcher);
59 private CreateTransactionReply legacyCreateTransactionReply(ActorRef actorRef, int transactionVersion){
60 return CreateTransactionReply.newBuilder()
61 .setTransactionActorPath(actorRef.path().toString())
62 .setTransactionId("txn-1")
63 .setMessageVersion(transactionVersion)
67 private ActorRef setupPreBoronActorContextWithInitialCreateTransaction(ActorSystem actorSystem,
68 TransactionType type) {
69 ActorRef shardActorRef = setupActorContextWithoutInitialCreateTransaction(actorSystem,
70 DefaultShardStrategy.DEFAULT_SHARD, DataStoreVersions.LITHIUM_VERSION);
73 if(type == TransactionType.WRITE_ONLY) {
74 txActorRef = shardActorRef;
76 txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
77 doReturn(actorSystem.actorSelection(txActorRef.path())).
78 when(mockActorContext).actorSelection(txActorRef.path().toString());
80 doReturn(Futures.successful(legacyCreateTransactionReply(txActorRef, DataStoreVersions.LITHIUM_VERSION)))
81 .when(mockActorContext).executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
82 eqLegacyCreateTransaction(type), any(Timeout.class));
90 public void testClose() throws Exception{
91 ActorRef actorRef = setupPreBoronActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
93 doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
94 eq(actorSelection(actorRef)), eqSerializedReadData());
96 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
98 transactionProxy.read(TestModel.TEST_PATH);
100 transactionProxy.close();
102 verify(mockActorContext).sendOperationAsync(
103 eq(actorSelection(actorRef)), isA(ShardTransactionMessages.CloseTransaction.class));