Move AbstractDOMDataBroker to mdsal-dom-spi
[mdsal.git] / dom / mdsal-dom-broker / src / test / java / org / opendaylight / mdsal / dom / broker / DOMTransactionChainTest.java
index 44f746c761110f776d1e3e92f9e1a04229935ae3..c3f3d6a4c2c5ba52cca8f9766ecf22ba6f585ac2 100644 (file)
@@ -7,78 +7,79 @@
  */
 package org.opendaylight.mdsal.dom.broker;
 
+import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.CONFIGURATION;
 import static org.opendaylight.mdsal.common.api.LogicalDatastoreType.OPERATIONAL;
 
-import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableMap;
 import com.google.common.util.concurrent.ListenableFuture;
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.common.util.concurrent.MoreExecutors;
+import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import java.util.concurrent.Executors;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.TimeoutException;
 import org.junit.Before;
 import org.junit.Test;
+import org.opendaylight.mdsal.common.api.CommitInfo;
 import org.opendaylight.mdsal.common.api.LogicalDatastoreType;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeReadTransaction;
 import org.opendaylight.mdsal.dom.api.DOMDataTreeWriteTransaction;
 import org.opendaylight.mdsal.dom.api.DOMTransactionChain;
-import org.opendaylight.mdsal.dom.broker.util.TestModel;
+import org.opendaylight.mdsal.dom.spi.AbstractDOMDataBroker;
 import org.opendaylight.mdsal.dom.spi.store.DOMStore;
 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
+import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
 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.SchemaContext;
+import org.opendaylight.yangtools.yang.data.spi.node.ImmutableNodes;
 
-public class DOMTransactionChainTest {
+public class DOMTransactionChainTest extends AbstractDatastoreTest {
 
-    private SchemaContext schemaContext;
     private AbstractDOMDataBroker domBroker;
 
     @Before
-    public void setupStore() throws Exception{
-        final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER", MoreExecutors.newDirectExecutorService());
-        final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG", MoreExecutors.newDirectExecutorService());
-        schemaContext = TestModel.createTestContext();
-
-        operStore.onGlobalContextUpdated(schemaContext);
-        configStore.onGlobalContextUpdated(schemaContext);
-
-        final ImmutableMap<LogicalDatastoreType, DOMStore> stores = ImmutableMap.<LogicalDatastoreType, DOMStore> builder() //
-                .put(CONFIGURATION, configStore) //
-                .put(OPERATIONAL, operStore) //
+    public void setupStore() {
+        final InMemoryDOMDataStore operStore = new InMemoryDOMDataStore("OPER",
+                MoreExecutors.newDirectExecutorService());
+        final InMemoryDOMDataStore configStore = new InMemoryDOMDataStore("CFG",
+                MoreExecutors.newDirectExecutorService());
+
+        operStore.onModelContextUpdated(SCHEMA_CONTEXT);
+        configStore.onModelContextUpdated(SCHEMA_CONTEXT);
+
+        final ImmutableMap<LogicalDatastoreType, DOMStore> stores =
+                ImmutableMap.<LogicalDatastoreType, DOMStore>builder()
+                .put(CONFIGURATION, configStore)
+                .put(OPERATIONAL, operStore)
                 .build();
 
-        final ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newSingleThreadExecutor());
+        final ListeningExecutorService executor = MoreExecutors.listeningDecorator(
+                Executors.newSingleThreadExecutor());
         domBroker = new SerializedDOMDataBroker(stores, executor);
     }
 
     @Test
     public void testTransactionChainNoConflict() throws InterruptedException, ExecutionException, TimeoutException {
-        final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
-        final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
+        final DOMTransactionChain txChain = domBroker.createTransactionChain();
         assertNotNull(txChain);
 
         /**
-         * We alocate new read-write transaction and write /test
-         *
-         *
+         * We allocate new read-write transaction and write /test.
          */
         final DOMDataTreeWriteTransaction firstTx = allocateAndWrite(txChain);
 
         /**
          * First transaction is marked as ready, we are able to allocate chained
-         * transactions
+         * transactions.
          */
-        final ListenableFuture<Void> firstWriteTxFuture = firstTx.submit();
+        final ListenableFuture<? extends CommitInfo> firstWriteTxFuture = firstTx.commit();
 
         /**
-         * We alocate chained transaction - read transaction.
+         * We allocate chained transaction - read transaction.
          */
         final DOMDataTreeReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
 
@@ -92,8 +93,7 @@ public class DOMTransactionChainTest {
         assertTestContainerExists(secondReadTx);
 
         /**
-         *
-         * We alocate next transaction, which is still based on first one, but
+         * We allocate next transaction, which is still based on first one, but
          * is read-write.
          *
          */
@@ -106,56 +106,48 @@ public class DOMTransactionChainTest {
         assertCommitSuccessful(firstWriteTxFuture);
 
         /**
-         *
          * Allocates transaction from data store.
          *
          */
         final DOMDataTreeReadTransaction storeReadTx = domBroker.newReadOnlyTransaction();
 
         /**
-         * We verify transaction is commited to store, container should exists
+         * We verify transaction is committed to store, container should exist
          * in datastore.
          */
         assertTestContainerExists(storeReadTx);
 
         /**
-         * third transaction is sealed and commited
+         * third transaction is sealed and committed.
          */
-        final ListenableFuture<Void> thirdDeleteTxFuture = thirdDeleteTx.submit();
-        assertCommitSuccessful(thirdDeleteTxFuture);
+        assertCommitSuccessful(thirdDeleteTx.commit());
 
         /**
          * We close transaction chain.
          */
         txChain.close();
 
-        listener.getSuccessFuture().get(1000, TimeUnit.MILLISECONDS);
+        txChain.future().get(1000, TimeUnit.MILLISECONDS);
     }
 
     @Test
-    public void testTransactionChainNotSealed() throws InterruptedException, ExecutionException, TimeoutException {
-        final BlockingTransactionChainListener listener = new BlockingTransactionChainListener();
-        final DOMTransactionChain txChain = domBroker.createTransactionChain(listener);
+    public void testTransactionChainNotSealed() {
+        final var txChain = domBroker.createTransactionChain();
         assertNotNull(txChain);
 
         /**
-         * We alocate new read-write transaction and write /test
-         *
-         *
+         * We allocate new read-write transaction and write /test
          */
         allocateAndWrite(txChain);
 
         /**
-         * We alocate chained transaction - read transaction, note first one is
-         * still not commited to datastore, so this allocation should fail with
+         * We allocate chained transaction - read transaction, note first one is
+         * still not committed to datastore, so this allocation should fail with
          * IllegalStateException.
          */
-        try {
-            txChain.newReadOnlyTransaction();
-            fail("Allocation of secondReadTx should fail with IllegalStateException");
-        } catch (final Exception e) {
-            assertTrue(e instanceof IllegalStateException);
-        }
+        // actual backing tx allocation happens on put
+        final var ex = assertThrows(IllegalStateException.class, () -> allocateAndWrite(txChain));
+        assertEquals("Previous transaction OPER-0 is not ready yet", ex.getMessage());
     }
 
     private static DOMDataTreeWriteTransaction allocateAndDelete(final DOMTransactionChain txChain)
@@ -168,27 +160,27 @@ public class DOMTransactionChainTest {
         return tx;
     }
 
-    private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain)
-            throws InterruptedException, ExecutionException {
-        final DOMDataTreeWriteTransaction tx = txChain.newWriteOnlyTransaction();
+    private static DOMDataTreeWriteTransaction allocateAndWrite(final DOMTransactionChain txChain) {
+        final var tx = txChain.newWriteOnlyTransaction();
         writeTestContainer(tx);
         return tx;
     }
 
-    private static void assertCommitSuccessful(final ListenableFuture<Void> future)
+    private static void assertCommitSuccessful(final ListenableFuture<? extends CommitInfo> firstWriteTxFuture)
             throws InterruptedException, ExecutionException {
-        future.get();
+        firstWriteTxFuture.get();
     }
 
-    private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx) throws InterruptedException,
-            ExecutionException {
-        final ListenableFuture<Optional<NormalizedNode<?, ?>>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
-        final Optional<NormalizedNode<?, ?>> readedData = readFuture.get();
+    private static void assertTestContainerExists(final DOMDataTreeReadTransaction readTx)
+            throws InterruptedException, ExecutionException {
+        final ListenableFuture<Optional<NormalizedNode>> readFuture = readTx.read(OPERATIONAL, TestModel.TEST_PATH);
+        final Optional<NormalizedNode> readedData = readFuture.get();
         assertTrue(readedData.isPresent());
     }
 
-    private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) throws InterruptedException,
-            ExecutionException {
-        tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
+    private static void writeTestContainer(final DOMDataTreeWriteTransaction tx) {
+        tx.put(OPERATIONAL, TestModel.TEST_PATH, ImmutableNodes.newContainerBuilder()
+            .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
+            .build());
     }
 }