Unit tests for ClientBackedTransactionChain class 18/54018/10
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Wed, 29 Mar 2017 08:53:23 +0000 (10:53 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 6 Apr 2017 16:33:09 +0000 (16:33 +0000)
Change-Id: I97953cfdc32619c31295cfed2584b7466d48aa5d
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHandle.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/AbstractClientHistory.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionChainTest.java [new file with mode: 0644]

index b87819c34337a0435b02a3f98209f73a29aef7fe..b64cf21776afbf1f2378a7498640a4d568eda93c 100644 (file)
@@ -53,7 +53,7 @@ public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> e
     }
 
     @Override
-    public final TransactionIdentifier getIdentifier() {
+    public TransactionIdentifier getIdentifier() {
         return transactionId;
     }
 
@@ -62,7 +62,7 @@ public abstract class AbstractClientHandle<T extends AbstractProxyTransaction> 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;
index c15c7c85f2e1edf52fb37614ea9da9816119eaec..f2e72f18ec25044c62bc6e018243e9f871f1bb44 100644 (file)
@@ -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) {
index 24a58759673f134b6c24f0f020a7f2f0a09caa2d..bf107514809355dbcb2cff65384b083178fe3781 100644 (file)
@@ -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 (file)
index 0000000..dab2786
--- /dev/null
@@ -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