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 17d05f2..f2527b1 100644 (file)
@@ -7,24 +7,27 @@
  */
 package org.opendaylight.controller.cluster.datastore.modification;
 
  */
 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 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.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.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.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;
 
 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
     protected InMemoryDOMDataStore store;
 
     @BeforeClass
@@ -50,9 +53,8 @@ public abstract class AbstractModificationTest {
     }
 
     protected Optional<NormalizedNode> readData(final YangInstanceIdentifier path) throws Exception {
     }
 
     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 c483d4d..c8b2c48 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
  */
  * 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;
 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.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 {
 
 public class DeleteModificationTest extends AbstractModificationTest {
-
     @Test
     public void testApply() throws Exception {
         // Write something into the datastore
         DOMStoreReadWriteTransaction writeTransaction = store.newReadWriteTransaction();
     @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
         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();
 
         // Delete stuff from the datastore
         DOMStoreWriteTransaction deleteTransaction = store.newWriteOnlyTransaction();
@@ -42,8 +35,7 @@ public class DeleteModificationTest extends AbstractModificationTest {
         deleteModification.apply(deleteTransaction);
         commitTransaction(deleteTransaction);
 
         deleteModification.apply(deleteTransaction);
         commitTransaction(deleteTransaction);
 
-        data = readData(TestModel.TEST_PATH);
-        Assert.assertFalse(data.isPresent());
+        assertEquals(Optional.empty(), readData(TestModel.TEST_PATH));
     }
 
     @Test
     }
 
     @Test
index 5a9c88d..f5ada9f 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
  */
  * 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;
 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.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 {
 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
     @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
         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
     }
 
     @Test
index 7a68b54..416b9ae 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
  */
  * 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;
 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.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 {
 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();
     @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
         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
     }
 
     @Test