Unit test for ClientBackedTransaction derived classes 88/53988/21
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 28 Mar 2017 15:21:21 +0000 (17:21 +0200)
committerTom Pantelis <tompantelis@gmail.com>
Thu, 6 Apr 2017 16:33:38 +0000 (16:33 +0000)
Change-Id: I2967a0e224fc783ffac73a994def666e86a423a6
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/ClientSnapshot.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadTransactionTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadWriteTransactionTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionTest.java [new file with mode: 0644]
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedWriteTransactionTest.java [new file with mode: 0644]

index 482d1a5..ac9a709 100644 (file)
@@ -36,11 +36,11 @@ public class ClientSnapshot extends AbstractClientHandle<AbstractProxyTransactio
         return ensureProxy(path, this::createProxy);
     }
 
-    public final CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
+    public CheckedFuture<Boolean, ReadFailedException> exists(final YangInstanceIdentifier path) {
         return ensureSnapshotProxy(path).exists(path);
     }
 
-    public final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
+    public CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> read(
             final YangInstanceIdentifier path) {
         return ensureSnapshotProxy(path).read(path);
     }
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadTransactionTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadTransactionTest.java
new file mode 100644 (file)
index 0000000..92cd99d
--- /dev/null
@@ -0,0 +1,70 @@
+/*
+ * 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 com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+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.client.ClientActorContext;
+import org.opendaylight.controller.cluster.databroker.actors.dds.ClientSnapshot;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class ClientBackedReadTransactionTest extends ClientBackedTransactionTest<ClientBackedReadTransaction> {
+    private ClientBackedReadTransaction object;
+
+    @Mock
+    private NormalizedNode data;
+    @Mock
+    private ClientActorContext clientContext;
+    @Mock
+    private ClientSnapshot delegate;
+
+    @Override
+    ClientBackedReadTransaction object() throws Exception {
+        return object;
+    }
+
+    @Before
+    @SuppressWarnings("unchecked")
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        Mockito.doReturn(CLIENT_ID).when(clientContext).getIdentifier();
+        Mockito.doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
+
+        Mockito.doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(delegate)
+                .exists(YangInstanceIdentifier.EMPTY);
+        Mockito.doReturn(Futures.immediateCheckedFuture(Optional.of(data))).when(delegate)
+                .read(YangInstanceIdentifier.EMPTY);
+
+        object = new ClientBackedReadTransaction(delegate, null);
+    }
+
+    @Test
+    public void testRead() throws Exception {
+        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> result = object().read(
+                YangInstanceIdentifier.EMPTY);
+        final Optional<NormalizedNode<?, ?>> resultData = result.get();
+        Assert.assertTrue(resultData.isPresent());
+        Assert.assertEquals(data, resultData.get());
+    }
+
+    @Test
+    public void testExists() throws Exception {
+        final CheckedFuture<Boolean, ReadFailedException> result = object().exists(YangInstanceIdentifier.EMPTY);
+        Assert.assertTrue(result.get());
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadWriteTransactionTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadWriteTransactionTest.java
new file mode 100644 (file)
index 0000000..a1af473
--- /dev/null
@@ -0,0 +1,69 @@
+/*
+ * 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 com.google.common.base.Optional;
+import com.google.common.util.concurrent.CheckedFuture;
+import com.google.common.util.concurrent.Futures;
+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.databroker.actors.dds.ClientTransaction;
+import org.opendaylight.controller.md.sal.common.api.data.ReadFailedException;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class ClientBackedReadWriteTransactionTest
+        extends ClientBackedTransactionTest<ClientBackedReadWriteTransaction> {
+    private ClientBackedReadWriteTransaction object;
+
+    @Mock
+    private ClientTransaction delegate;
+    @Mock
+    private NormalizedNode data;
+    @Mock
+    private DOMStoreThreePhaseCommitCohort readyCohort;
+
+    @Override
+    ClientBackedReadWriteTransaction object() throws Exception {
+        return object;
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        Mockito.doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
+        Mockito.doReturn(readyCohort).when(delegate).ready();
+
+        Mockito.doReturn(Futures.immediateCheckedFuture(Boolean.TRUE)).when(delegate)
+                .exists(YangInstanceIdentifier.EMPTY);
+        Mockito.doReturn(Futures.immediateCheckedFuture(Optional.of(data))).when(delegate)
+                .read(YangInstanceIdentifier.EMPTY);
+
+        object = new ClientBackedReadWriteTransaction(delegate);
+    }
+
+    @Test
+    public void testRead() throws Exception {
+        final CheckedFuture<Optional<NormalizedNode<?, ?>>, ReadFailedException> result = object().read(
+                YangInstanceIdentifier.EMPTY);
+        final Optional<NormalizedNode<?, ?>> resultData = result.get();
+        Assert.assertTrue(resultData.isPresent());
+        Assert.assertEquals(data, resultData.get());
+    }
+
+    @Test
+    public void testExists() throws Exception {
+        Assert.assertTrue(object().exists(YangInstanceIdentifier.EMPTY).get());
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionTest.java
new file mode 100644 (file)
index 0000000..bff76b3
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * 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.Test;
+import org.mockito.Mockito;
+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;
+
+public abstract class ClientBackedTransactionTest<T extends ClientBackedTransaction<?>> {
+    private static FrontendIdentifier FRONTEND_ID = FrontendIdentifier.create(
+            MemberName.forName("member"), FrontendType.forName("frontend"));
+    protected static final ClientIdentifier CLIENT_ID = ClientIdentifier.create(FRONTEND_ID, 0);
+
+    private static LocalHistoryIdentifier HISTORY_ID = new LocalHistoryIdentifier(CLIENT_ID, 0);
+    protected static final TransactionIdentifier TRANSACTION_ID = new TransactionIdentifier(HISTORY_ID, 0);
+
+    abstract T object() throws Exception;
+
+    @Test
+    public void testClose() throws Exception {
+        final AbstractClientHandle<?> delegate = object().delegate();
+        object().close();
+        Mockito.verify(delegate).abort();
+    }
+}
\ No newline at end of file
diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedWriteTransactionTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedWriteTransactionTest.java
new file mode 100644 (file)
index 0000000..26b8750
--- /dev/null
@@ -0,0 +1,72 @@
+/*
+ * 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.databroker.actors.dds.ClientTransaction;
+import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
+import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
+
+public class ClientBackedWriteTransactionTest extends ClientBackedTransactionTest<ClientBackedWriteTransaction> {
+    private ClientBackedWriteTransaction object;
+
+    @Mock
+    private ClientTransaction delegate;
+    @Mock
+    private NormalizedNode data;
+    @Mock
+    private YangInstanceIdentifier path;
+    @Mock
+    private DOMStoreThreePhaseCommitCohort readyCohort;
+
+    @Before
+    public void setUp() throws Exception {
+        MockitoAnnotations.initMocks(this);
+
+        Mockito.doReturn(TRANSACTION_ID).when(delegate).getIdentifier();
+        Mockito.doReturn(readyCohort).when(delegate).ready();
+
+        object = new ClientBackedWriteTransaction(delegate);
+    }
+
+    @Override
+    ClientBackedWriteTransaction object() throws Exception {
+        return object;
+    }
+
+    @Test
+    public void testWrite() throws Exception {
+        object().write(path, data);
+        Mockito.verify(delegate).write(path, data);
+    }
+
+    @Test
+    public void testMerge() throws Exception {
+        object().merge(path, data);
+        Mockito.verify(delegate).merge(path, data);
+    }
+
+    @Test
+    public void testDelete() throws Exception {
+        object().delete(path);
+        Mockito.verify(delegate).delete(path);
+    }
+
+    @Test
+    public void testReady() throws Exception {
+        final org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort result = object().ready();
+        Assert.assertNotNull(result);
+        Mockito.verify(delegate).ready();
+    }
+}
\ No newline at end of file

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.