sal-distributed-datastore: use lambdas
[controller.git] / opendaylight / md-sal / sal-distributed-datastore / src / test / java / org / opendaylight / controller / cluster / datastore / TransactionChainProxyTest.java
index 66b8c0ec8df3dfd353d39ba5887f04bce65b8000..9fd0849bf16199682fbc3c3bc5ea631ba5884751 100644 (file)
@@ -20,6 +20,7 @@ import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.opendaylight.controller.cluster.datastore.TransactionType.READ_WRITE;
 import static org.opendaylight.controller.cluster.datastore.TransactionType.WRITE_ONLY;
+
 import akka.actor.ActorRef;
 import akka.util.Timeout;
 import java.util.concurrent.CountDownLatch;
@@ -28,6 +29,7 @@ import java.util.concurrent.atomic.AtomicReference;
 import java.util.function.Function;
 import org.junit.Assert;
 import org.junit.Test;
+import org.opendaylight.controller.cluster.access.concepts.LocalHistoryIdentifier;
 import org.opendaylight.controller.cluster.datastore.messages.BatchedModifications;
 import org.opendaylight.controller.cluster.datastore.modification.WriteModification;
 import org.opendaylight.controller.cluster.datastore.shardstrategy.DefaultShardStrategy;
@@ -41,52 +43,50 @@ import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
 import scala.concurrent.Promise;
 
 public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
+    private LocalHistoryIdentifier historyId;
+
+    @Override
+    public void setUp() {
+        super.setUp();
+        historyId = MockIdentifiers.historyIdentifier(TransactionChainProxyTest.class, memberName);
+    }
 
     @SuppressWarnings("resource")
     @Test
-    public void testNewReadOnlyTransaction() throws Exception {
+    public void testNewReadOnlyTransaction() {
 
-        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory).newReadOnlyTransaction();
+        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadOnlyTransaction();
         Assert.assertTrue(dst instanceof DOMStoreReadTransaction);
 
     }
 
     @SuppressWarnings("resource")
     @Test
-    public void testNewReadWriteTransaction() throws Exception {
-        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory).newReadWriteTransaction();
+    public void testNewReadWriteTransaction() {
+        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newReadWriteTransaction();
         Assert.assertTrue(dst instanceof DOMStoreReadWriteTransaction);
 
     }
 
     @SuppressWarnings("resource")
     @Test
-    public void testNewWriteOnlyTransaction() throws Exception {
-        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory).newWriteOnlyTransaction();
+    public void testNewWriteOnlyTransaction() {
+        DOMStoreTransaction dst = new TransactionChainProxy(mockComponentFactory, historyId).newWriteOnlyTransaction();
         Assert.assertTrue(dst instanceof DOMStoreWriteTransaction);
 
     }
 
+    @SuppressWarnings("unchecked")
     @Test
-    public void testClose() throws Exception {
-        new TransactionChainProxy(mockComponentFactory).close();
-
-        verify(mockActorContext, times(1)).broadcast(any(Function.class));
-    }
-
-    @Test
-    public void testTransactionChainsHaveUniqueId() {
-        try (TransactionChainProxy one = new TransactionChainProxy(mockComponentFactory)) {
-            try (TransactionChainProxy two = new TransactionChainProxy(mockComponentFactory)) {
+    public void testClose() {
+        new TransactionChainProxy(mockComponentFactory, historyId).close();
 
-                Assert.assertNotEquals(one.getTransactionChainId(), two.getTransactionChainId());
-            }
-        }
+        verify(mockActorContext, times(1)).broadcast(any(Function.class), any(Class.class));
     }
 
     @Test
     public void testRateLimitingUsedInReadWriteTxCreation() {
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             txChainProxy.newReadWriteTransaction();
 
@@ -96,7 +96,7 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
 
     @Test
     public void testRateLimitingUsedInWriteOnlyTxCreation() {
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             txChainProxy.newWriteOnlyTransaction();
 
@@ -106,7 +106,7 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
 
     @Test
     public void testRateLimitingNotUsedInReadOnlyTxCreation() {
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             txChainProxy.newReadOnlyTransaction();
 
@@ -119,16 +119,17 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
      * initiated until the first one completes its read future.
      */
     @Test
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void testChainedWriteOnlyTransactions() throws Exception {
         dataStoreContextBuilder.writeOnlyTransactionOptimizationsEnabled(true);
 
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             ActorRef txActorRef1 = setupActorContextWithoutInitialCreateTransaction(getSystem());
 
             Promise<Object> batchedReplyPromise1 = akka.dispatch.Futures.promise();
             doReturn(batchedReplyPromise1.future()).when(mockActorContext).executeOperationAsync(
-                    eq(actorSelection(txActorRef1)), isA(BatchedModifications.class));
+                    eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
 
             DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction();
 
@@ -151,18 +152,15 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
 
             final AtomicReference<Exception> caughtEx = new AtomicReference<>();
             final CountDownLatch write2Complete = new CountDownLatch(1);
-            new Thread() {
-                @Override
-                public void run() {
-                    try {
-                        writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
-                    } catch (Exception e) {
-                        caughtEx.set(e);
-                    } finally {
-                        write2Complete.countDown();
-                    }
+            new Thread(() -> {
+                try {
+                    writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
+                } catch (Exception e) {
+                    caughtEx.set(e);
+                } finally {
+                    write2Complete.countDown();
                 }
-            }.start();
+            }).start();
 
             assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
 
@@ -189,8 +187,9 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
      * initiated until the first one completes its read future.
      */
     @Test
+    @SuppressWarnings("checkstyle:IllegalCatch")
     public void testChainedReadWriteTransactions() throws Exception {
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             ActorRef txActorRef1 = setupActorContextWithInitialCreateTransaction(getSystem(), READ_WRITE);
 
@@ -198,7 +197,7 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
 
             Promise<Object> readyReplyPromise1 = akka.dispatch.Futures.promise();
             doReturn(readyReplyPromise1.future()).when(mockActorContext).executeOperationAsync(
-                    eq(actorSelection(txActorRef1)), isA(BatchedModifications.class));
+                    eq(actorSelection(txActorRef1)), isA(BatchedModifications.class), any(Timeout.class));
 
             DOMStoreWriteTransaction writeTx1 = txChainProxy.newReadWriteTransaction();
 
@@ -222,18 +221,15 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
 
             final AtomicReference<Exception> caughtEx = new AtomicReference<>();
             final CountDownLatch write2Complete = new CountDownLatch(1);
-            new Thread() {
-                @Override
-                public void run() {
-                    try {
-                        writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
-                    } catch (Exception e) {
-                        caughtEx.set(e);
-                    } finally {
-                        write2Complete.countDown();
-                    }
+            new Thread(() -> {
+                try {
+                    writeTx2.write(TestModel.OUTER_LIST_PATH, writeNode2);
+                } catch (Exception e) {
+                    caughtEx.set(e);
+                } finally {
+                    write2Complete.countDown();
                 }
-            }.start();
+            }).start();
 
             assertEquals("Tx 2 write should've completed", true, write2Complete.await(5, TimeUnit.SECONDS));
 
@@ -258,20 +254,18 @@ public class TransactionChainProxyTest extends AbstractTransactionProxyTest {
     }
 
     @Test(expected = IllegalStateException.class)
-    public void testChainedWriteTransactionsWithPreviousTxNotReady() throws Exception {
+    public void testChainedWriteTransactionsWithPreviousTxNotReady() {
         ActorRef actorRef = setupActorContextWithInitialCreateTransaction(getSystem(), WRITE_ONLY);
 
         expectBatchedModifications(actorRef, 1);
 
-        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory)) {
+        try (TransactionChainProxy txChainProxy = new TransactionChainProxy(mockComponentFactory, historyId)) {
 
             DOMStoreWriteTransaction writeTx1 = txChainProxy.newWriteOnlyTransaction();
 
             NormalizedNode<?, ?> writeNode1 = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
             writeTx1.write(TestModel.TEST_PATH, writeNode1);
 
-            NormalizedNode<?, ?> writeNode2 = ImmutableNodes.containerNode(TestModel.OUTER_LIST_QNAME);
-
             txChainProxy.newWriteOnlyTransaction();
         }
     }