From 4b21f0e68572b4209c1e572a241cf1ef3c699327 Mon Sep 17 00:00:00 2001 From: Tom Pantelis Date: Tue, 15 Dec 2015 12:46:48 -0500 Subject: [PATCH] Add unit test for ReadyLocalTransaction NPE This is a follow-up to https://git.opendaylight.org/gerrit/#/c/31448/ to add a unit test for the scenario that caused a null modification instance passed to ReadyLocalTransaction from LocalThreePhaseCommitCohort. Basically the LocalThreePhaseCommitCohort was created with a non-null operationError which was later erroneously set to null. Change-Id: I2891b5653b5d425079dbb99ce65e52d3ebbc6a27 Signed-off-by: Tom Pantelis --- .../AbstractTransactionProxyTest.java | 7 +++ .../datastore/TransactionProxyTest.java | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java index 2e0874dc17..0e1a3b7304 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/AbstractTransactionProxyTest.java @@ -58,6 +58,7 @@ import org.opendaylight.controller.cluster.datastore.messages.DataExistsReply; import org.opendaylight.controller.cluster.datastore.messages.PrimaryShardInfo; import org.opendaylight.controller.cluster.datastore.messages.ReadData; import org.opendaylight.controller.cluster.datastore.messages.ReadDataReply; +import org.opendaylight.controller.cluster.datastore.messages.ReadyLocalTransaction; import org.opendaylight.controller.cluster.datastore.messages.ReadyTransactionReply; import org.opendaylight.controller.cluster.datastore.modification.AbstractModification; import org.opendaylight.controller.cluster.datastore.modification.Modification; @@ -319,6 +320,12 @@ public abstract class AbstractTransactionProxyTest { eq(actorSelection(actorRef)), isA(BatchedModifications.class)); } + protected void expectReadyLocalTransaction(ActorRef actorRef, boolean doCommitOnReady) { + doReturn(doCommitOnReady ? Futures.successful(new CommitTransactionReply().toSerializable()) : + readyTxReply(actorRef.path().toString())).when(mockActorContext).executeOperationAsync( + eq(actorSelection(actorRef)), isA(ReadyLocalTransaction.class), any(Timeout.class)); + } + protected CreateTransactionReply createTransactionReply(ActorRef actorRef, int transactionVersion){ return CreateTransactionReply.newBuilder() .setTransactionActorPath(actorRef.path().toString()) diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java index a5625637f4..8a17ddf8c3 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/TransactionProxyTest.java @@ -16,6 +16,7 @@ import static org.mockito.Matchers.anyString; import static org.mockito.Matchers.eq; import static org.mockito.Matchers.isA; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; import static org.mockito.Mockito.verify; @@ -645,6 +646,54 @@ public class TransactionProxyTest extends AbstractTransactionProxyTest { verifyCohortFutures((DebugThreePhaseCommitCohort)ready, new CommitTransactionReply().toSerializable()); } + @Test + public void testReadyWithLocalTransaction() throws Exception { + ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class)); + + doReturn(getSystem().actorSelection(shardActorRef.path())). + when(mockActorContext).actorSelection(shardActorRef.path().toString()); + + doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, createDataTree()))). + when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); + + TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY); + + expectReadyLocalTransaction(shardActorRef, true); + + NormalizedNode nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + transactionProxy.write(TestModel.TEST_PATH, nodeToWrite); + + DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready(); + assertTrue(ready instanceof SingleCommitCohortProxy); + verifyCohortFutures((SingleCommitCohortProxy)ready, new CommitTransactionReply().toSerializable()); + } + + @Test + public void testReadyWithLocalTransactionWithFailure() throws Exception { + ActorRef shardActorRef = getSystem().actorOf(Props.create(DoNothingActor.class)); + + doReturn(getSystem().actorSelection(shardActorRef.path())). + when(mockActorContext).actorSelection(shardActorRef.path().toString()); + + Optional mockDataTree = createDataTree(); + DataTreeModification mockModification = mockDataTree.get().takeSnapshot().newModification(); + doThrow(new RuntimeException("mock")).when(mockModification).ready(); + + doReturn(Futures.successful(newPrimaryShardInfo(shardActorRef, mockDataTree))). + when(mockActorContext).findPrimaryShardAsync(eq(DefaultShardStrategy.DEFAULT_SHARD)); + + TransactionProxy transactionProxy = new TransactionProxy(mockComponentFactory, WRITE_ONLY); + + expectReadyLocalTransaction(shardActorRef, true); + + NormalizedNode nodeToWrite = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + transactionProxy.write(TestModel.TEST_PATH, nodeToWrite); + + DOMStoreThreePhaseCommitCohort ready = transactionProxy.ready(); + assertTrue(ready instanceof SingleCommitCohortProxy); + verifyCohortFutures((SingleCommitCohortProxy)ready, RuntimeException.class); + } + private void testWriteOnlyTxWithFindPrimaryShardFailure(Exception toThrow) throws Exception { doReturn(Futures.failed(toThrow)).when(mockActorContext).findPrimaryShardAsync(anyString()); -- 2.36.6