Bump yangtools to 13.0.0
[mdsal.git] / dom / mdsal-dom-spi / src / test / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadTransactionTest.java
index 1dce26f15e5124052312482e0f2ae0a61cae8c34..afde8a25c4ff565f03a7abd5d58d39faaf661b35 100644 (file)
@@ -7,25 +7,28 @@
  */
 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 java.lang.reflect.Field;
+import com.google.common.util.concurrent.Futures;
 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.api.schema.tree.DataTreeSnapshot;
+import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
+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);
@@ -35,14 +38,14 @@ public class SnapshotBackedReadTransactionTest {
 
     @Test
     public void basicTest() throws Exception {
-        final NormalizedNode testNode = mock(NormalizedNode.class);
-        final Optional<NormalizedNode> optional = Optional.of(testNode);
+        final var testNode = mock(ContainerNode.class);
+        final var optional = Optional.of(testNode);
         doReturn("testNode").when(testNode).toString();
-        doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.empty());
-        assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.empty()).get());
+        doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.of());
+        assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of()).get());
 
-        assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get());
-        final Field stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
+        assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.of()).get());
+        final var stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
         stableSnapshotField.setAccessible(true);
 
         DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
@@ -52,42 +55,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.of());
+        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.of());
+        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.of());
+        final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
+        assertThat(cause, instanceOf(ReadFailedException.class));
+        assertEquals("Read failed", cause.getMessage());
+        assertSame(thrown, cause.getCause());
     }
 }