9dbeb5dfe1041cf936650071e3908f8c531239e9
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / compat / PreBoronTransactionProxyTest.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.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;
34
35 /**
36  * TransactionProxy unit tests for backwards compatibility with pre-Boron versions.
37  *
38  * @author Thomas Pantelis
39  */
40 public class PreBoronTransactionProxyTest extends AbstractTransactionProxyTest {
41
42     private CreateTransaction eqLegacyCreateTransaction(final TransactionType type) {
43         ArgumentMatcher<CreateTransaction> matcher = new ArgumentMatcher<CreateTransaction>() {
44             @Override
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();
50                 }
51
52                 return false;
53             }
54         };
55
56         return argThat(matcher);
57     }
58
59     private CreateTransactionReply legacyCreateTransactionReply(ActorRef actorRef, int transactionVersion){
60         return CreateTransactionReply.newBuilder()
61             .setTransactionActorPath(actorRef.path().toString())
62             .setTransactionId("txn-1")
63             .setMessageVersion(transactionVersion)
64             .build();
65     }
66
67     private ActorRef setupPreBoronActorContextWithInitialCreateTransaction(ActorSystem actorSystem,
68             TransactionType type) {
69         ActorRef shardActorRef = setupActorContextWithoutInitialCreateTransaction(actorSystem,
70                 DefaultShardStrategy.DEFAULT_SHARD, DataStoreVersions.LITHIUM_VERSION);
71
72         ActorRef txActorRef;
73         if(type == TransactionType.WRITE_ONLY) {
74             txActorRef = shardActorRef;
75         } else {
76             txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
77             doReturn(actorSystem.actorSelection(txActorRef.path())).
78                 when(mockActorContext).actorSelection(txActorRef.path().toString());
79
80             doReturn(Futures.successful(legacyCreateTransactionReply(txActorRef, DataStoreVersions.LITHIUM_VERSION)))
81                 .when(mockActorContext).executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
82                         eqLegacyCreateTransaction(type), any(Timeout.class));
83         }
84
85         return txActorRef;
86
87     }
88
89     @Test
90     public void testClose() throws Exception{
91         ActorRef actorRef = setupPreBoronActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
92
93         doReturn(readDataReply(null)).when(mockActorContext).executeOperationAsync(
94                 eq(actorSelection(actorRef)), eqSerializedReadData());
95
96         TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
97
98         transactionProxy.read(TestModel.TEST_PATH);
99
100         transactionProxy.close();
101
102         verify(mockActorContext).sendOperationAsync(
103                 eq(actorSelection(actorRef)), isA(ShardTransactionMessages.CloseTransaction.class));
104     }
105 }