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.mockito.Matchers.any;
12 import static org.mockito.Matchers.argThat;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Matchers.isA;
15 import static org.mockito.Mockito.doReturn;
16 import static org.mockito.Mockito.verify;
17 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
18 import akka.actor.ActorRef;
19 import akka.actor.ActorSystem;
20 import akka.actor.Props;
21 import akka.dispatch.Futures;
22 import akka.util.Timeout;
23 import com.google.common.base.Optional;
24 import java.util.concurrent.TimeUnit;
25 import org.junit.Test;
26 import org.mockito.ArgumentMatcher;
27 import org.opendaylight.controller.cluster.datastore.AbstractTransactionProxyTest;
28 import org.opendaylight.controller.cluster.datastore.DataStoreVersions;
29 import org.opendaylight.controller.cluster.datastore.TransactionProxy;
30 import org.opendaylight.controller.cluster.datastore.TransactionType;
31 import org.opendaylight.controller.cluster.datastore.messages.CreateTransaction;
32 import org.opendaylight.controller.cluster.datastore.messages.DataExists;
33 import org.opendaylight.controller.cluster.datastore.messages.ReadData;
34 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
35 import org.opendaylight.controller.cluster.raft.utils.DoNothingActor;
36 import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
37 import org.opendaylight.controller.protobuff.messages.transaction.ShardTransactionMessages;
38 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
39 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
40 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
43 * TransactionProxy unit tests for backwards compatibility with pre-Boron versions.
45 * @author Thomas Pantelis
47 @SuppressWarnings("resource")
48 public class PreBoronTransactionProxyTest extends AbstractTransactionProxyTest {
50 private CreateTransaction eqLegacyCreateTransaction(final TransactionType type) {
51 ArgumentMatcher<CreateTransaction> matcher = new ArgumentMatcher<CreateTransaction>() {
53 public boolean matches(Object argument) {
54 if(ShardTransactionMessages.CreateTransaction.class.equals(argument.getClass())) {
55 CreateTransaction obj = CreateTransaction.fromSerializable(argument);
56 return obj.getTransactionId().startsWith(memberName) &&
57 obj.getTransactionType() == type.ordinal();
64 return argThat(matcher);
67 private static ShardTransactionMessages.CreateTransactionReply legacyCreateTransactionReply(ActorRef actorRef,
68 int transactionVersion){
69 return ShardTransactionMessages.CreateTransactionReply.newBuilder()
70 .setTransactionActorPath(actorRef.path().toString())
71 .setTransactionId("txn-1")
72 .setMessageVersion(transactionVersion)
76 private static ReadData eqLegacySerializedReadData(final YangInstanceIdentifier path) {
77 ArgumentMatcher<ReadData> matcher = new ArgumentMatcher<ReadData>() {
79 public boolean matches(Object argument) {
80 return ShardTransactionMessages.ReadData.class.equals(argument.getClass()) &&
81 ReadData.fromSerializable(argument).getPath().equals(path);
85 return argThat(matcher);
88 private static DataExists eqLegacySerializedDataExists() {
89 ArgumentMatcher<DataExists> matcher = new ArgumentMatcher<DataExists>() {
91 public boolean matches(Object argument) {
92 return ShardTransactionMessages.DataExists.class.equals(argument.getClass()) &&
93 DataExists.fromSerializable(argument).getPath().equals(TestModel.TEST_PATH);
97 return argThat(matcher);
100 private ActorRef setupPreBoronActorContextWithInitialCreateTransaction(ActorSystem actorSystem,
101 TransactionType type) {
102 ActorRef shardActorRef = setupActorContextWithoutInitialCreateTransaction(actorSystem,
103 DefaultShardStrategy.DEFAULT_SHARD, DataStoreVersions.LITHIUM_VERSION);
106 if(type == TransactionType.WRITE_ONLY) {
107 txActorRef = shardActorRef;
109 txActorRef = actorSystem.actorOf(Props.create(DoNothingActor.class));
110 doReturn(actorSystem.actorSelection(txActorRef.path())).
111 when(mockActorContext).actorSelection(txActorRef.path().toString());
113 doReturn(Futures.successful(legacyCreateTransactionReply(txActorRef, DataStoreVersions.LITHIUM_VERSION)))
114 .when(mockActorContext).executeOperationAsync(eq(actorSystem.actorSelection(shardActorRef.path())),
115 eqLegacyCreateTransaction(type), any(Timeout.class));
122 public void testClose() throws Exception{
123 ActorRef actorRef = setupPreBoronActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
125 expectBatchedModifications(actorRef, 1);
127 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
129 transactionProxy.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
131 transactionProxy.close();
133 verify(mockActorContext).sendOperationAsync(
134 eq(actorSelection(actorRef)), isA(ShardTransactionMessages.CloseTransaction.class));
138 public void testRead() throws Exception{
139 ActorRef actorRef = setupPreBoronActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
141 NormalizedNode<?, ?> expectedNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
142 doReturn(readDataReply(expectedNode)).when(mockActorContext).executeOperationAsync(
143 eq(actorSelection(actorRef)), eqLegacySerializedReadData(TestModel.TEST_PATH), any(Timeout.class));
145 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
147 Optional<NormalizedNode<?, ?>> readOptional = transactionProxy.read(
148 TestModel.TEST_PATH).get(5, TimeUnit.SECONDS);
150 assertEquals("NormalizedNode isPresent", true, readOptional.isPresent());
151 assertEquals("Response NormalizedNode", expectedNode, readOptional.get());
155 public void testExists() throws Exception{
156 ActorRef actorRef = setupPreBoronActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
158 doReturn(dataExistsReply(true)).when(mockActorContext).executeOperationAsync(
159 eq(actorSelection(actorRef)), eqLegacySerializedDataExists(), any(Timeout.class));
161 TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, READ_WRITE);
163 Boolean exists = transactionProxy.exists(TestModel.TEST_PATH).checkedGet(5, TimeUnit.SECONDS);
164 assertEquals("Exists response", true, exists);