*/
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.Matchers.any;
+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.base.Optional;
+import com.google.common.util.concurrent.Futures;
+import java.util.Optional;
+import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.mdsal.common.api.ReadFailedException;
import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
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.DataTreeModification;
-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.DataTreeModification;
+import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
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);
@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).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
- assertTrue(snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get());
- assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get());
+ doReturn(Optional.of(testNode)).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.of());
+ assertTrue(snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.of()).get());
+ assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.of()).get());
}
- @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
- @Test(expected = ReadFailedException.class)
- public void readTestWithNullException() throws Throwable {
- doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
- try {
- snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
- fail("Expected ReadFailedException");
- } catch (Exception e) {
- throw e.getCause();
- }
+ @Test
+ public void readTestWithNullException() {
+ doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.of());
+
+ final var future = snapshotBackedReadWriteTransaction.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:IllegalCatch"})
- @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 (Exception 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.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:IllegalCatch"})
- @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 (Exception 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.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());
}
}