Clean up InMemoryDataStoreTest
[mdsal.git] / dom / mdsal-dom-inmemory-datastore / src / test / java / org / opendaylight / mdsal / dom / store / inmemory / InMemoryDataStoreTest.java
index d4c64204c5453744f425852607b0bd2292df8d93..ac943862277d2708762df6a8114c047d3014ba18 100644 (file)
@@ -7,20 +7,28 @@
  */
 package org.opendaylight.mdsal.dom.store.inmemory;
 
+import static org.hamcrest.CoreMatchers.containsString;
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
 
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.MoreExecutors;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
-import org.junit.Assert;
+import org.junit.AfterClass;
 import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Ignore;
 import org.junit.Test;
-import org.mockito.Mockito;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
@@ -33,29 +41,35 @@ import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 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.tree.DataTreeModification;
-import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
+import org.opendaylight.yangtools.yang.data.impl.schema.Builders;
 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
-import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
-import org.opendaylight.yangtools.yang.model.api.SchemaContext;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
+import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
 
 public class InMemoryDataStoreTest {
+    private static EffectiveModelContext SCHEMA_CONTEXT;
 
-    private SchemaContext schemaContext;
     private InMemoryDOMDataStore domStore;
 
+    @BeforeClass
+    public static void beforeClass() {
+        SCHEMA_CONTEXT = TestModel.createTestContext();
+    }
+
+    @AfterClass
+    public static void afterClass() {
+        SCHEMA_CONTEXT = null;
+    }
+
     @Before
     public void setupStore() {
         domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.newDirectExecutorService());
-        schemaContext = TestModel.createTestContext();
-        domStore.onGlobalContextUpdated(schemaContext);
+        domStore.onModelContextUpdated(SCHEMA_CONTEXT);
     }
 
     @Test
-    public void testTransactionIsolation() throws InterruptedException, ExecutionException {
-
-        assertNotNull(domStore);
-
+    public void testTransactionIsolation() throws Exception {
         DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
         assertNotNull(readTx);
 
@@ -65,25 +79,25 @@ public class InMemoryDataStoreTest {
         /**
          * Writes /test in writeTx.
          */
-        NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
+        NormalizedNode testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
         writeTx.write(TestModel.TEST_PATH, testNode);
 
         /**
          * Reads /test from writeTx Read should return container.
          */
-        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+        ListenableFuture<Optional<NormalizedNode>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
         assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
         assertEquals("read: data", testNode, writeTxContainer.get().get());
 
         /**
          * Reads /test from readTx Read should return Absent.
          */
-        ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
+        ListenableFuture<Optional<NormalizedNode>> readTxContainer = readTx.read(TestModel.TEST_PATH);
         assertEquals("read: isPresent", false, readTxContainer.get().isPresent());
     }
 
     @Test
-    public void testTransactionCommit() throws InterruptedException, ExecutionException {
+    public void testTransactionCommit() throws Exception {
 
         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
         assertNotNull(writeTx);
@@ -91,13 +105,13 @@ public class InMemoryDataStoreTest {
         /**
          * Writes /test in writeTx.
          */
-        NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
+        NormalizedNode testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
         writeTx.write(TestModel.TEST_PATH, testNode);
 
         /**
          * Reads /test from writeTx Read should return container.
          */
-        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+        ListenableFuture<Optional<NormalizedNode>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
         assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
         assertEquals("read: data", testNode, writeTxContainer.get().get());
 
@@ -105,8 +119,7 @@ public class InMemoryDataStoreTest {
 
         assertThreePhaseCommit(cohort);
 
-        Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
-                .get();
+        Optional<NormalizedNode> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
         assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
         assertEquals("After commit read: data", testNode, afterCommitRead.get());
     }
@@ -123,8 +136,7 @@ public class InMemoryDataStoreTest {
 
         assertThreePhaseCommit(writeTx.ready());
 
-        Optional<NormalizedNode<?, ?>> afterCommitRead =
-                domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
+        Optional<NormalizedNode> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
         assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
 
         // Delete /test and verify
@@ -145,7 +157,7 @@ public class InMemoryDataStoreTest {
         DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
         assertNotNull(writeTx);
 
-        ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
+        ContainerNode containerNode = Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
                 .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
                         .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
@@ -155,7 +167,7 @@ public class InMemoryDataStoreTest {
 
         assertThreePhaseCommit(writeTx.ready());
 
-        Optional<NormalizedNode<?, ?>> afterCommitRead =
+        Optional<NormalizedNode> afterCommitRead =
                 domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
         assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
         assertEquals("After commit read: data", containerNode, afterCommitRead.get());
@@ -165,13 +177,13 @@ public class InMemoryDataStoreTest {
         writeTx = domStore.newWriteOnlyTransaction();
         assertNotNull(writeTx);
 
-        containerNode = ImmutableContainerNodeBuilder.create()
-                .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
-                .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
-                        .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
-                                                            TestModel.ID_QNAME, 1))
-                        .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
-                                                            TestModel.ID_QNAME, 2)).build()).build();
+        containerNode = Builders.containerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
+            .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
+                .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 1))
+                .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME, TestModel.ID_QNAME, 2))
+                .build())
+            .build();
 
         writeTx.merge(TestModel.TEST_PATH, containerNode);
 
@@ -182,14 +194,13 @@ public class InMemoryDataStoreTest {
         assertEquals("After commit read: data", containerNode, afterCommitRead.get());
     }
 
-
     @Test
     public void testExistsForExistingData() throws Exception {
 
         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
         assertNotNull(writeTx);
 
-        ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
+        ContainerNode containerNode = Builders.containerBuilder()
             .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
             .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
                 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
@@ -210,8 +221,7 @@ public class InMemoryDataStoreTest {
         DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
         assertNotNull(readTx);
 
-        exists =
-            readTx.exists(TestModel.TEST_PATH);
+        exists = readTx.exists(TestModel.TEST_PATH);
 
         assertEquals(Boolean.TRUE, exists.get());
     }
@@ -222,140 +232,123 @@ public class InMemoryDataStoreTest {
         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
         assertNotNull(writeTx);
 
-        ListenableFuture<Boolean> exists = writeTx.exists(TestModel.TEST_PATH);
+        var exists = writeTx.exists(TestModel.TEST_PATH);
 
         assertEquals(Boolean.FALSE, exists.get());
 
-        DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+        var readTx = domStore.newReadOnlyTransaction();
         assertNotNull(readTx);
 
-        exists =
-            readTx.exists(TestModel.TEST_PATH);
+        exists = readTx.exists(TestModel.TEST_PATH);
 
         assertEquals(Boolean.FALSE, exists.get());
     }
 
-    @Test(expected = ReadFailedException.class)
-    @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:AvoidHidingCauseException"})
-    public void testExistsThrowsReadFailedException() throws Throwable {
-
-        DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+    @Test
+    public void testExistsThrowsReadFailedException() {
+        var readTx = domStore.newReadOnlyTransaction();
         assertNotNull(readTx);
 
         readTx.close();
 
-        try {
-            readTx.exists(TestModel.TEST_PATH).get();
-        } catch (ExecutionException e) {
-            throw e.getCause();
-        }
-    }
+        final var future = readTx.exists(TestModel.TEST_PATH);
 
+        final var ex = assertThrows(ExecutionException.class, future::get).getCause();
+        assertThat(ex, instanceOf(ReadFailedException.class));
+    }
 
-    @SuppressWarnings("checkstyle:IllegalThrows")
-    @Test(expected = ReadFailedException.class)
-    public void testReadWithReadOnlyTransactionClosed() throws Throwable {
 
+    @Test
+    public void testReadWithReadOnlyTransactionClosed() {
         DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
         assertNotNull(readTx);
 
         readTx.close();
 
-        doReadAndThrowEx(readTx);
+        assertReadThrows(readTx);
     }
 
-    @SuppressWarnings("checkstyle:IllegalThrows")
-    @Test(expected = ReadFailedException.class)
-    public void testReadWithReadOnlyTransactionFailure() throws Throwable {
-
-        DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
-        Mockito.doThrow(new RuntimeException("mock ex")).when(mockSnapshot)
-        .readNode(Mockito.any(YangInstanceIdentifier.class));
+    @Test
+    public void testReadWithReadOnlyTransactionFailure() {
+        DataTreeSnapshot mockSnapshot = mock(DataTreeSnapshot.class);
+        doThrow(new RuntimeException("mock ex")).when(mockSnapshot)
+            .readNode(any(YangInstanceIdentifier.class));
 
         DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadTransaction("1", true, mockSnapshot);
 
-        doReadAndThrowEx(readTx);
+        assertReadThrows(readTx);
     }
 
-    @SuppressWarnings("checkstyle:IllegalThrows")
-    @Test(expected = ReadFailedException.class)
-    public void testReadWithReadWriteTransactionClosed() throws Throwable {
+    @Test
+    public void testReadWithReadWriteTransactionClosed() {
 
         DOMStoreReadTransaction readTx = domStore.newReadWriteTransaction();
         assertNotNull(readTx);
 
         readTx.close();
 
-        doReadAndThrowEx(readTx);
+        assertReadThrows(readTx);
     }
 
-    @SuppressWarnings("checkstyle:IllegalThrows")
-    @Test(expected = ReadFailedException.class)
-    public void testReadWithReadWriteTransactionFailure() throws Throwable {
-
-        DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
-        DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
-        Mockito.doThrow(new RuntimeException("mock ex")).when(mockModification)
-        .readNode(Mockito.any(YangInstanceIdentifier.class));
-        Mockito.doReturn(mockModification).when(mockSnapshot).newModification();
+    @Test
+    public void testReadWithReadWriteTransactionFailure() {
+        DataTreeSnapshot mockSnapshot = mock(DataTreeSnapshot.class);
+        DataTreeModification mockModification = mock(DataTreeModification.class);
+        doThrow(new RuntimeException("mock ex")).when(mockModification)
+            .readNode(any(YangInstanceIdentifier.class));
+        doReturn(mockModification).when(mockSnapshot).newModification();
         @SuppressWarnings("unchecked")
-        TransactionReadyPrototype<String> mockReady = Mockito.mock(TransactionReadyPrototype.class);
+        TransactionReadyPrototype<String> mockReady = mock(TransactionReadyPrototype.class);
         DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadWriteTransaction(
                 "1", false, mockSnapshot, mockReady);
 
-        doReadAndThrowEx(readTx);
+        assertReadThrows(readTx);
     }
 
-    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    private static void doReadAndThrowEx(final DOMStoreReadTransaction readTx) throws Throwable {
-        try {
-            readTx.read(TestModel.TEST_PATH).get();
-        } catch (ExecutionException e) {
-            throw e.getCause();
-        }
+    private static void assertReadThrows(final DOMStoreReadTransaction readTx) {
+        final var future = readTx.read(TestModel.TEST_PATH);
+        final var cause = assertThrows(ExecutionException.class, future::get).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
     }
 
-    @Test(expected = IllegalStateException.class)
-    public void testWriteWithTransactionReady() throws Exception {
-
-        DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
-
+    @Test
+    public void testWriteWithTransactionReady() {
+        var writeTx = domStore.newWriteOnlyTransaction();
         writeTx.ready();
 
         // Should throw ex
-        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+        assertThrows(IllegalStateException.class,
+            () -> writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME)));
     }
 
-    @Test(expected = IllegalStateException.class)
-    public void testReadyWithTransactionAlreadyReady() throws Exception {
-
-        DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
+    @Test
+    public void testReadyWithTransactionAlreadyReady() {
+        var writeTx = domStore.newWriteOnlyTransaction();
 
         writeTx.ready();
 
         // Should throw ex
-        writeTx.ready();
+        assertThrows(IllegalStateException.class, writeTx::ready);
     }
 
     @Test
-    public void testReadyWithMissingMandatoryData() throws InterruptedException {
+    public void testReadyWithMissingMandatoryData() throws Exception {
         DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
-        NormalizedNode<?, ?> testNode = ImmutableContainerNodeBuilder.create()
+        NormalizedNode testNode = Builders.containerBuilder()
                 .withNodeIdentifier(new NodeIdentifier(TestModel.MANDATORY_DATA_TEST_QNAME))
                 .addChild(ImmutableNodes.leafNode(TestModel.OPTIONAL_QNAME, "data"))
                 .build();
         writeTx.write(TestModel.MANDATORY_DATA_TEST_PATH, testNode);
         DOMStoreThreePhaseCommitCohort ready = writeTx.ready();
-        try {
-            ready.canCommit().get();
-            Assert.fail("Expected exception on canCommit");
-        } catch (ExecutionException e) {
-            // nop
-        }
+
+        final var future = ready.canCommit();
+        final var cause = assertThrows(ExecutionException.class, future::get).getCause();
+        assertThat(cause, instanceOf(IllegalArgumentException.class));
+        assertThat(cause.getMessage(), containsString("mandatory-data-test is missing mandatory descendant"));
     }
 
     @Test
-    public void testTransactionAbort() throws InterruptedException, ExecutionException {
+    public void testTransactionAbort() throws Exception {
 
         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
         assertNotNull(writeTx);
@@ -364,17 +357,15 @@ public class InMemoryDataStoreTest {
 
         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
 
-        assertTrue(cohort.canCommit().get().booleanValue());
+        assertTrue(cohort.canCommit().get());
         cohort.preCommit().get();
         cohort.abort().get();
 
-        Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
-                .get();
-        assertFalse(afterCommitRead.isPresent());
+        assertEquals(Optional.empty(), domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get());
     }
 
     @Test
-    public void testTransactionChain() throws InterruptedException, ExecutionException {
+    public void testTransactionChain() throws Exception {
         DOMStoreTransactionChain txChain = domStore.createTransactionChain();
         assertNotNull(txChain);
 
@@ -446,7 +437,7 @@ public class InMemoryDataStoreTest {
 
     @Test
     @Ignore
-    public void testTransactionConflict() throws InterruptedException, ExecutionException {
+    public void testTransactionConflict() throws Exception {
         DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
         DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
         assertTestContainerWrite(txOne);
@@ -463,15 +454,14 @@ public class InMemoryDataStoreTest {
         assertFalse(txTwo.ready().canCommit().get());
     }
 
-    private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort)
-            throws InterruptedException, ExecutionException {
-        assertTrue(cohort.canCommit().get().booleanValue());
+    private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort) throws Exception {
+        assertTrue(cohort.canCommit().get());
         cohort.preCommit().get();
         cohort.commit().get();
     }
 
-    private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
-            throws InterruptedException, ExecutionException {
+    private static Optional<NormalizedNode> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
+            throws Exception {
         /**
          *
          * Writes /test in writeTx
@@ -485,12 +475,11 @@ public class InMemoryDataStoreTest {
     /**
      * Reads /test from readTx Read should return container.
      */
-    private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(final DOMStoreReadTransaction readTx)
-            throws InterruptedException, ExecutionException {
+    private static Optional<NormalizedNode> assertTestContainerExists(final DOMStoreReadTransaction readTx)
+            throws Exception {
 
-        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
+        ListenableFuture<Optional<NormalizedNode>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
         assertTrue(writeTxContainer.get().isPresent());
         return writeTxContainer.get();
     }
-
 }