From e46f5ca6e44904d9dd0b69fb8a36ebf69a293496 Mon Sep 17 00:00:00 2001 From: Ivan Hrasko Date: Wed, 29 Mar 2017 10:53:23 +0200 Subject: [PATCH] Unit tests for ClientBackedTransactionChain class Change-Id: I97953cfdc32619c31295cfed2584b7466d48aa5d Signed-off-by: Ivan Hrasko --- .../actors/dds/AbstractClientHandle.java | 4 +- .../actors/dds/AbstractClientHistory.java | 4 +- .../databroker/ClientBackedDataStoreTest.java | 11 +-- .../ClientBackedTransactionChainTest.java | 89 +++++++++++++++++++ 4 files changed, 95 insertions(+), 13 deletions(-) create mode 100644 opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionChainTest.java diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java index b87819c343..b64cf21776 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java @@ -53,7 +53,7 @@ public abstract class AbstractClientHandle e } @Override - public final TransactionIdentifier getIdentifier() { + public TransactionIdentifier getIdentifier() { return transactionId; } @@ -62,7 +62,7 @@ public abstract class AbstractClientHandle e * * @return True if this transaction became closed during this call */ - public final boolean abort() { + public boolean abort() { if (commonAbort()) { parent.onTransactionAbort(this); return true; diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistory.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistory.java index c15c7c85f2..f2e72f18ec 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistory.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistory.java @@ -98,7 +98,7 @@ public abstract class AbstractClientHistory extends LocalAbortable implements Id } @Override - public final LocalHistoryIdentifier getIdentifier() { + public LocalHistoryIdentifier getIdentifier() { return identifier; } @@ -212,7 +212,7 @@ public abstract class AbstractClientHistory extends LocalAbortable implements Id * @throws TransactionChainClosedException if this history is closed * @throws IllegalStateException if a previous dependent transaction has not been closed */ - public final ClientSnapshot takeSnapshot() { + public ClientSnapshot takeSnapshot() { checkNotClosed(); synchronized (this) { diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java index 24a5875967..bf10751480 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java @@ -7,7 +7,6 @@ */ package org.opendaylight.controller.cluster.databroker; -import java.lang.reflect.Field; import org.junit.Assert; import org.junit.Before; import org.junit.Test; @@ -20,7 +19,6 @@ import org.opendaylight.controller.cluster.access.concepts.FrontendType; import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; import org.opendaylight.controller.cluster.access.concepts.MemberName; import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; -import org.opendaylight.controller.cluster.databroker.actors.dds.AbstractClientHandle; import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory; import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot; import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction; @@ -69,13 +67,8 @@ public class ClientBackedDataStoreTest { Mockito.when(actorContext.getSchemaContext()).thenReturn(schemaContext); Mockito.when(actorContext.getDatastoreContext()).thenReturn(DatastoreContext.newBuilder().build()); - - final Field transactionIdField = AbstractClientHandle.class.getDeclaredField("transactionId"); - transactionIdField.setAccessible(true); - - // set transaction ids to mocked objects - transactionIdField.set(clientTransaction, TRANSACTION_IDENTIFIER); - transactionIdField.set(clientSnapshot, TRANSACTION_IDENTIFIER); + Mockito.when(clientTransaction.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER); + Mockito.when(clientSnapshot.getIdentifier()).thenReturn(TRANSACTION_IDENTIFIER); Mockito.when(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER); Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction); diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionChainTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionChainTest.java new file mode 100644 index 0000000000..dab278635f --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionChainTest.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2017 Pantheon Technologies s.r.o. and others. All rights reserved. + * + * This program and the accompanying materials are made available under the + * terms of the Eclipse Public License v1.0 which accompanies this distribution, + * and is available at http://www.eclipse.org/legal/epl-v10.html + */ +package org.opendaylight.controller.cluster.databroker; + +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; +import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier; +import org.opendaylight.controller.cluster.access.concepts.FrontendIdentifier; +import org.opendaylight.controller.cluster.access.concepts.FrontendType; +import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier; +import org.opendaylight.controller.cluster.access.concepts.MemberName; +import org.opendaylight.controller.cluster.access.concepts.TransactionIdentifier; +import org.opendaylight.controller.cluster.databroker.actors.dds.ClientLocalHistory; +import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot; +import org.opendaylight.controller.cluster.databroker.actors.dds.ClientTransaction; + +public class ClientBackedTransactionChainTest { + private ClientBackedTransactionChain chain; + + @Mock + private ClientLocalHistory history; + @Mock + private ClientSnapshot snapshot; + @Mock + private ClientTransaction transaction; + + @Before + public void setUp() throws Exception { + MockitoAnnotations.initMocks(this); + + final FrontendIdentifier frontendId = FrontendIdentifier.create( + MemberName.forName("member"), FrontendType.forName("frontend")); + final ClientIdentifier clientId = ClientIdentifier.create(frontendId, 0); + final LocalHistoryIdentifier historyId = new LocalHistoryIdentifier(clientId, 0); + final TransactionIdentifier transactionId = new TransactionIdentifier(historyId, 0); + + Mockito.when(history.getIdentifier()).thenReturn(historyId); + Mockito.when(transaction.getIdentifier()).thenReturn(transactionId); + Mockito.when(snapshot.getIdentifier()).thenReturn(transactionId); + Mockito.when(history.takeSnapshot()).thenReturn(snapshot); + Mockito.when(history.createTransaction()).thenReturn(transaction); + + chain = new ClientBackedTransactionChain(history); + } + + @Test + public void testNewReadOnlyTransaction() throws Exception { + Assert.assertNotNull(chain.newReadOnlyTransaction()); + Mockito.verify(history).takeSnapshot(); + } + + @Test + public void testNewReadWriteTransaction() throws Exception { + Assert.assertNotNull(chain.newReadWriteTransaction()); + Mockito.verify(history).createTransaction(); + } + + @Test + public void testNewWriteOnlyTransaction() throws Exception { + Assert.assertNotNull(chain.newWriteOnlyTransaction()); + Mockito.verify(history).createTransaction(); + } + + @Test + public void testClose() throws Exception { + chain.newReadOnlyTransaction(); + chain.close(); + Mockito.verify(snapshot).abort(); + Mockito.verify(history).close(); + } + + @Test + public void testSnapshotClosed() throws Exception { + chain.snapshotClosed(snapshot); + // snap is removed, so cannot be aborted + chain.close(); + Mockito.verify(snapshot, Mockito.never()).abort(); + Mockito.verify(history).close(); + } +} \ No newline at end of file -- 2.36.6