Clean up modification tests 71/98471/1
authorRobert Varga <robert.varga@pantheon.tech>
Sat, 13 Nov 2021 11:52:29 +0000 (12:52 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Sat, 13 Nov 2021 11:52:29 +0000 (12:52 +0100)
Make sure we close the read transaction and improve assertions with
Optional.

Change-Id: Ia465f64c5845e3d5e215759cc32048e06478104b
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/AbstractModificationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/DeleteModificationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/MergeModificationTest.java
opendaylight/md-sal/sal-distributed-datastore/src/test/java/org/opendaylight/controller/cluster/datastore/modification/WriteModificationTest.java

index 17d05f22b797a71c74291637eb8b5bea39daaff2..f2527b13abb013b475f8eeb2286ec809a9debc18 100644 (file)
@@ -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<NormalizedNode> readData(final YangInstanceIdentifier path) throws Exception {
-        // FIXME: close the transaction
-        DOMStoreReadTransaction transaction = store.newReadOnlyTransaction();
-        ListenableFuture<Optional<NormalizedNode>> future = transaction.read(path);
-        return future.get();
+        try (var transaction = store.newReadOnlyTransaction()) {
+            return transaction.read(path).get();
+        }
     }
 }
index c483d4d411f45b154dc20b159990703ce0186bd0..c8b2c48ebb2366f689f74e735d4a431fe798af89 100644 (file)
@@ -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<NormalizedNode> 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
index 5a9c88d16b80c6d95a1cf84fa866942125741113..f5ada9ffe37f79f444c41ccc4a0eb2063b8d5858 100644 (file)
@@ -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<NormalizedNode> data = readData(TestModel.TEST_PATH);
-        Assert.assertTrue(data.isPresent());
-
+        assertEquals(Optional.of(TEST_CONTAINER), readData(TestModel.TEST_PATH));
     }
 
     @Test
index 7a68b542f4ae804bc70360720553ddfb43d95c88..416b9ae0ef95cc9063149ad6e8de23e1e1226e84 100644 (file)
@@ -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<NormalizedNode> data = readData(TestModel.TEST_PATH);
-        Assert.assertTrue(data.isPresent());
+        assertEquals(Optional.of(TEST_CONTAINER), readData(TestModel.TEST_PATH));
     }
 
     @Test