From: Robert Varga Date: Sat, 13 Nov 2021 11:52:29 +0000 (+0100) Subject: Clean up modification tests X-Git-Tag: v4.0.7~16 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=69d944b7b97e0e2c6a062ecb1227b7b30474cc18;ds=sidebyside Clean up modification tests Make sure we close the read transaction and improve assertions with Optional. Change-Id: Ia465f64c5845e3d5e215759cc32048e06478104b Signed-off-by: Robert Varga --- diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/AbstractModificationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/AbstractModificationTest.java index 17d05f22b7..f2527b13ab 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/AbstractModificationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/AbstractModificationTest.java @@ -7,24 +7,27 @@ */ package org.opendaylight.controller.cluster.datastore.modification; -import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.MoreExecutors; import java.util.Optional; +import org.eclipse.jdt.annotation.NonNull; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; -import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction; import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort; import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction; import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; +import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode; import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; +import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext; public abstract class AbstractModificationTest { private static EffectiveModelContext TEST_SCHEMA_CONTEXT; + static final @NonNull ContainerNode TEST_CONTAINER = ImmutableNodes.containerNode(TestModel.TEST_QNAME); + protected InMemoryDOMDataStore store; @BeforeClass @@ -50,9 +53,8 @@ public abstract class AbstractModificationTest { } protected Optional readData(final YangInstanceIdentifier path) throws Exception { - // FIXME: close the transaction - DOMStoreReadTransaction transaction = store.newReadOnlyTransaction(); - ListenableFuture> future = transaction.read(path); - return future.get(); + try (var transaction = store.newReadOnlyTransaction()) { + return transaction.read(path).get(); + } } } diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModificationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModificationTest.java index c483d4d411..c8b2c48ebb 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModificationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModificationTest.java @@ -5,36 +5,29 @@ * 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.datastore.modification; import static org.junit.Assert.assertEquals; import java.util.Optional; import org.apache.commons.lang.SerializationUtils; -import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction; import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier; -import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode; -import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; public class DeleteModificationTest extends AbstractModificationTest { - @Test public void testApply() throws Exception { // Write something into the datastore DOMStoreReadWriteTransaction writeTransaction = store.newReadWriteTransaction(); - WriteModification writeModification = new WriteModification(TestModel.TEST_PATH, - ImmutableNodes.containerNode(TestModel.TEST_QNAME)); + WriteModification writeModification = new WriteModification(TestModel.TEST_PATH, TEST_CONTAINER); writeModification.apply(writeTransaction); commitTransaction(writeTransaction); // Check if it's in the datastore - Optional data = readData(TestModel.TEST_PATH); - Assert.assertTrue(data.isPresent()); + assertEquals(Optional.of(TEST_CONTAINER), readData(TestModel.TEST_PATH)); // Delete stuff from the datastore DOMStoreWriteTransaction deleteTransaction = store.newWriteOnlyTransaction(); @@ -42,8 +35,7 @@ public class DeleteModificationTest extends AbstractModificationTest { deleteModification.apply(deleteTransaction); commitTransaction(deleteTransaction); - data = readData(TestModel.TEST_PATH); - Assert.assertFalse(data.isPresent()); + assertEquals(Optional.empty(), readData(TestModel.TEST_PATH)); } @Test diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MergeModificationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MergeModificationTest.java index 5a9c88d16b..f5ada9ffe3 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MergeModificationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MergeModificationTest.java @@ -5,14 +5,12 @@ * 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.datastore.modification; import static org.junit.Assert.assertEquals; import java.util.Optional; import org.apache.commons.lang.SerializationUtils; -import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; @@ -22,7 +20,6 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; public class MergeModificationTest extends AbstractModificationTest { - @Test public void testApply() throws Exception { //TODO : Need to write a better test for this @@ -35,9 +32,7 @@ public class MergeModificationTest extends AbstractModificationTest { commitTransaction(writeTransaction); //Check if it's in the datastore - Optional data = readData(TestModel.TEST_PATH); - Assert.assertTrue(data.isPresent()); - + assertEquals(Optional.of(TEST_CONTAINER), readData(TestModel.TEST_PATH)); } @Test diff --git a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/WriteModificationTest.java b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/WriteModificationTest.java index 7a68b542f4..416b9ae0ef 100644 --- a/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/WriteModificationTest.java +++ b/opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/WriteModificationTest.java @@ -5,14 +5,12 @@ * 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.datastore.modification; import static org.junit.Assert.assertEquals; import java.util.Optional; import org.apache.commons.lang.SerializationUtils; -import org.junit.Assert; import org.junit.Test; import org.opendaylight.controller.md.cluster.datastore.model.TestModel; import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction; @@ -22,19 +20,16 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes; import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder; public class WriteModificationTest extends AbstractModificationTest { - @Test public void testApply() throws Exception { //Write something into the datastore DOMStoreReadWriteTransaction writeTransaction = store.newReadWriteTransaction(); - WriteModification writeModification = new WriteModification(TestModel.TEST_PATH, - ImmutableNodes.containerNode(TestModel.TEST_QNAME)); + WriteModification writeModification = new WriteModification(TestModel.TEST_PATH, TEST_CONTAINER); writeModification.apply(writeTransaction); commitTransaction(writeTransaction); //Check if it's in the datastore - Optional data = readData(TestModel.TEST_PATH); - Assert.assertTrue(data.isPresent()); + assertEquals(Optional.of(TEST_CONTAINER), readData(TestModel.TEST_PATH)); } @Test