From: Ivan Hrasko Date: Tue, 28 Mar 2017 15:21:21 +0000 (+0200) Subject: Unit test for ClientBackedTransaction derived classes X-Git-Tag: release/carbon~107 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=3887530459d39d3bbf37f1cd127dd23ad6e1a1ea Unit test for ClientBackedTransaction derived classes Change-Id: I2967a0e224fc783ffac73a994def666e86a423a6 Signed-off-by: Ivan Hrasko --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientSnapshot.java b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientSnapshot.java index 482d1a5b03..ac9a7091ae 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientSnapshot.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/main/java/org/opendaylight/controller/cluster/databroker/actors/dds/ClientSnapshot.java @@ -36,11 +36,11 @@ public class ClientSnapshot extends AbstractClientHandle exists(final YangInstanceIdentifier path) { + public CheckedFuture exists(final YangInstanceIdentifier path) { return ensureSnapshotProxy(path).exists(path); } - public final CheckedFuture>, ReadFailedException> read( + public CheckedFuture>, 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 index 0000000000..92cd99d825 --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadTransactionTest.java @@ -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 { + 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>, ReadFailedException> result = object().read( + YangInstanceIdentifier.EMPTY); + final Optional> resultData = result.get(); + Assert.assertTrue(resultData.isPresent()); + Assert.assertEquals(data, resultData.get()); + } + + @Test + public void testExists() throws Exception { + final CheckedFuture 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 index 0000000000..a1af473ebf --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedReadWriteTransactionTest.java @@ -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 { + 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>, ReadFailedException> result = object().read( + YangInstanceIdentifier.EMPTY); + final Optional> 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 index 0000000000..bff76b3f5e --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedTransactionTest.java @@ -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> { + 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 index 0000000000..26b875011c --- /dev/null +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/databroker/ClientBackedWriteTransactionTest.java @@ -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 { + 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