Unit tests for ClientBackedDataStore class 82/53982/4
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 28 Mar 2017 13:56:44 +0000 (15:56 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 30 Mar 2017 08:41:32 +0000 (08:41 +0000)
Change-Id: Ieba1004283905b82730b1ee23c2afeb4eb98f963
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStore.java
opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/datastore/AbstractDataStore.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedDataStoreTest.java [new file with mode: 0644]

index 0d576a8..940e5b2 100644 (file)
@@ -10,6 +10,7 @@ package org.opendaylight.controller.cluster.databroker;
 import akka.actor.ActorSystem;
 import com.google.common.annotations.VisibleForTesting;
 import org.opendaylight.controller.cluster.access.concepts.ClientIdentifier;
+import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
 import org.opendaylight.controller.cluster.datastore.AbstractDataStore;
 import org.opendaylight.controller.cluster.datastore.ClusterWrapper;
 import org.opendaylight.controller.cluster.datastore.DatastoreContextFactory;
@@ -33,8 +34,9 @@ public class ClientBackedDataStore extends AbstractDataStore {
     }
 
     @VisibleForTesting
-    ClientBackedDataStore(final ActorContext actorContext, final ClientIdentifier identifier) {
-        super(actorContext, identifier);
+    ClientBackedDataStore(final ActorContext actorContext, final ClientIdentifier identifier,
+                          final DataStoreClient clientActor) {
+        super(actorContext, identifier, clientActor);
     }
 
     @Override
index 01f2ceb..dce5368 100644 (file)
@@ -136,6 +136,16 @@ public abstract class AbstractDataStore implements DistributedDataStoreInterface
                 .duration().toMillis() * READY_WAIT_FACTOR;
     }
 
+    @VisibleForTesting
+    protected AbstractDataStore(final ActorContext actorContext, final ClientIdentifier identifier,
+                                final DataStoreClient clientActor) {
+        this.actorContext = Preconditions.checkNotNull(actorContext, "actorContext should not be null");
+        this.client = clientActor;
+        this.identifier = Preconditions.checkNotNull(identifier);
+        this.waitTillReadyTimeInMillis = actorContext.getDatastoreContext().getShardLeaderElectionTimeout()
+                .duration().toMillis() * READY_WAIT_FACTOR;
+    }
+
     protected final DataStoreClient getClient() {
         return client;
     }
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
new file mode 100644 (file)
index 0000000..24a5875
--- /dev/null
@@ -0,0 +1,125 @@
+/*
+ * 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 java.lang.reflect.Field;
+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.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;
+import org.opendaylight.controller.cluster.databroker.actors.dds.DataStoreClient;
+import org.opendaylight.controller.cluster.datastore.DatastoreContext;
+import org.opendaylight.controller.cluster.datastore.utils.ActorContext;
+import org.opendaylight.controller.md.cluster.datastore.model.TestModel;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreTransactionChain;
+import org.opendaylight.controller.sal.core.spi.data.DOMStoreWriteTransaction;
+import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+
+public class ClientBackedDataStoreTest {
+
+    private static final ClientIdentifier UNKNOWN_ID = ClientIdentifier.create(
+            FrontendIdentifier.create(MemberName.forName("local"), FrontendType.forName("unknown")), 0);
+
+    private static FrontendIdentifier FRONTEND_IDENTIFIER = FrontendIdentifier.create(
+            MemberName.forName("member"), FrontendType.forName("frontend"));
+    private static final ClientIdentifier CLIENT_IDENTIFIER = ClientIdentifier.create(FRONTEND_IDENTIFIER, 0);
+
+    private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_IDENTIFIER, 0);
+    private static final TransactionIdentifier TRANSACTION_IDENTIFIER = new TransactionIdentifier(HISTORY_ID, 0);
+
+    @Mock
+    private DataStoreClient clientActor;
+
+    @Mock
+    private ActorContext actorContext;
+
+    @Mock
+    private ClientLocalHistory clientLocalHistory;
+
+    @Mock
+    private ClientTransaction clientTransaction;
+
+    @Mock
+    private ClientSnapshot clientSnapshot;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        final SchemaContext schemaContext = TestModel.createTestContext();
+
+        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(clientActor.getIdentifier()).thenReturn(CLIENT_IDENTIFIER);
+        Mockito.when(clientActor.createTransaction()).thenReturn(clientTransaction);
+        Mockito.when(clientActor.createLocalHistory()).thenReturn(clientLocalHistory);
+        Mockito.when(clientActor.createSnapshot()).thenReturn(clientSnapshot);
+    }
+
+    @Test
+    public void testCreateTransactionChain() throws Exception {
+        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorContext, UNKNOWN_ID, clientActor)) {
+            final DOMStoreTransactionChain txChain = clientBackedDataStore.createTransactionChain();
+            Assert.assertNotNull(txChain);
+            Mockito.verify(clientActor, Mockito.times(1)).createLocalHistory();
+        }
+    }
+
+    @Test
+    public void testNewReadOnlyTransaction() throws Exception {
+        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorContext, UNKNOWN_ID, clientActor)) {
+            final DOMStoreReadTransaction tx = clientBackedDataStore.newReadOnlyTransaction();
+            Assert.assertNotNull(tx);
+            Mockito.verify(clientActor, Mockito.times(1)).createSnapshot();
+        }
+    }
+
+    @Test
+    public void testNewWriteOnlyTransaction() throws Exception {
+        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorContext, UNKNOWN_ID, clientActor)) {
+            final DOMStoreWriteTransaction tx = clientBackedDataStore.newWriteOnlyTransaction();
+            Assert.assertNotNull(tx);
+            Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
+        }
+    }
+
+    @Test
+    public void testNewReadWriteTransaction() throws Exception {
+        try (final ClientBackedDataStore clientBackedDataStore = new ClientBackedDataStore(
+                actorContext, UNKNOWN_ID, clientActor)) {
+            final DOMStoreReadWriteTransaction tx = clientBackedDataStore.newReadWriteTransaction();
+            Assert.assertNotNull(tx);
+            Mockito.verify(clientActor, Mockito.times(1)).createTransaction();
+        }
+    }
+}
\ No newline at end of file