Clean up SnapshotBackedRead(Write)TransactionTest 62/101262/2
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 23 May 2022 11:00:30 +0000 (13:00 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 23 May 2022 11:08:37 +0000 (13:08 +0200)
Use assertThrows() and other assertions, reducing the number of
CheckStyle violations and improving test converage.

JIRA: MDSAL-698
Change-Id: I6ce0e66c23f30bce65cd3b006064f75bfb215c80
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/store/SnapshotBackedReadTransactionTest.java
dom/mdsal-dom-spi/src/test/java/org/opendaylight/mdsal/dom/spi/store/SnapshotBackedReadWriteTransactionTest.java

index eb3e2fb73ad01b986ceaca2fbe9e6e9425201610..66ee6bd15919ae13566fef0bb8af02661c4d4d19 100644 (file)
@@ -7,25 +7,29 @@
  */
 package org.opendaylight.mdsal.dom.spi.store;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 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.Futures;
 import java.lang.reflect.Field;
 import java.util.Optional;
+import java.util.concurrent.ExecutionException;
 import org.junit.Test;
 import org.opendaylight.mdsal.common.api.ReadFailedException;
 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
 
-@SuppressWarnings("checkstyle:IllegalCatch")
 public class SnapshotBackedReadTransactionTest {
 
     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
@@ -52,42 +56,38 @@ public class SnapshotBackedReadTransactionTest {
         assertNull(stableSnapshot);
     }
 
-    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    @Test(expected = ReadFailedException.class)
-    public void readTestWithException() throws Throwable {
+    @Test
+    public void readTestWithException() {
         snapshotBackedReadTransaction.close();
-        try {
-            snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (Exception e) {
-            throw e.getCause();
-        }
+        final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Transaction is closed", cause.getMessage());
     }
 
-    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    @Test(expected = ReadFailedException.class)
-    public void readNodeTestWithException() throws Throwable {
-        doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
+    @Test
+    public void readNodeTestWithException() {
+        final var thrown = new NullPointerException("no Node");
+        doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
         snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
                 null);
-        try {
-            snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (Exception e) {
-            throw e.getCause();
-        }
+
+        final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Read failed", cause.getMessage());
+        assertSame(thrown, cause.getCause());
     }
 
-    @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    @Test(expected = ReadFailedException.class)
-    public void existsTestWithException() throws Throwable  {
-        doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
+    @Test
+    public void existsTestWithException() {
+        final var thrown = new NullPointerException("no Node");
+        doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
 
-        try {
-            snapshotBackedReadTransaction.exists(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (Exception e) {
-            throw e.getCause();
-        }
+        final var future = snapshotBackedReadTransaction.exists(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Read failed", cause.getMessage());
+        assertSame(thrown, cause.getCause());
     }
 }
index b568c45a75d5cbc2c76a6cd323fbda8c6f1b7c88..59006cfcf6857727b538f83748dc8e9c3daa8077 100644 (file)
@@ -7,14 +7,18 @@
  */
 package org.opendaylight.mdsal.dom.spi.store;
 
+import static org.hamcrest.CoreMatchers.instanceOf;
+import static org.hamcrest.MatcherAssert.assertThat;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
 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.Futures;
 import java.util.Optional;
 import java.util.concurrent.ExecutionException;
 import org.junit.Before;
@@ -35,7 +39,7 @@ public class SnapshotBackedReadWriteTransactionTest {
     private SnapshotBackedReadWriteTransaction<Object> snapshotBackedReadWriteTransaction;
 
     @Before
-    public void setUp() throws Exception {
+    public void setUp() {
         doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
         snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction<>(new Object(), false,
                 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
@@ -51,39 +55,37 @@ public class SnapshotBackedReadWriteTransactionTest {
         assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.empty()).get());
     }
 
-    @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
-    @Test(expected = ReadFailedException.class)
-    public void readTestWithNullException() throws Throwable {
+    @Test
+    public void readTestWithNullException() {
         doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.empty());
-        try {
-            snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (ExecutionException e) {
-            throw e.getCause();
-        }
+
+        final var future = snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Transaction is closed", cause.getMessage());
     }
 
-    @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException"})
-    @Test(expected = ReadFailedException.class)
-    public void readNodeTestWithException() throws Throwable {
-        doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
-        try {
-            snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (ExecutionException e) {
-            throw e.getCause();
-        }
+    @Test
+    public void readNodeTestWithException() {
+        final var thrown = new NullPointerException("no Node");
+        doThrow(thrown).when(DATA_TREE_MODIFICATION).readNode(any());
+
+        final var future = snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Read failed", cause.getMessage());
+        assertSame(thrown, cause.getCause());
     }
 
-    @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException"})
-    @Test(expected = ReadFailedException.class)
-    public void existsTestWithException() throws Throwable {
-        doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
-        try {
-            snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.empty()).get();
-            fail("Expected ReadFailedException");
-        } catch (ExecutionException e) {
-            throw e.getCause();
-        }
+    @Test
+    public void existsTestWithException() {
+        final var thrown = new NullPointerException("no Node");
+        doThrow(thrown).when(DATA_TREE_MODIFICATION).readNode(any());
+
+        final var future = snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.empty());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Read failed", cause.getMessage());
+        assertSame(thrown, cause.getCause());
     }
 }