Bump versions to 13.0.4-SNAPSHOT
[mdsal.git] / dom / mdsal-dom-spi / src / test / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadTransactionTest.java
1 /*
2  * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.mdsal.dom.spi.store;
9
10 import static org.hamcrest.CoreMatchers.instanceOf;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15 import static org.junit.Assert.assertSame;
16 import static org.junit.Assert.assertThrows;
17 import static org.junit.Assert.assertTrue;
18 import static org.mockito.ArgumentMatchers.any;
19 import static org.mockito.Mockito.doReturn;
20 import static org.mockito.Mockito.doThrow;
21 import static org.mockito.Mockito.mock;
22
23 import com.google.common.util.concurrent.Futures;
24 import java.util.Optional;
25 import java.util.concurrent.ExecutionException;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.common.api.ReadFailedException;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
30 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
31
32 public class SnapshotBackedReadTransactionTest {
33
34     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
35
36     private SnapshotBackedReadTransaction<Object> snapshotBackedReadTransaction =
37             new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT, null);
38
39     @Test
40     public void basicTest() throws Exception {
41         final var testNode = mock(ContainerNode.class);
42         final var optional = Optional.of(testNode);
43         doReturn("testNode").when(testNode).toString();
44         doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.of());
45         assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of()).get());
46
47         assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.of()).get());
48         final var stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
49         stableSnapshotField.setAccessible(true);
50
51         DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
52         assertNotNull(stableSnapshot);
53         snapshotBackedReadTransaction.close();
54         stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
55         assertNull(stableSnapshot);
56     }
57
58     @Test
59     public void readTestWithException() {
60         snapshotBackedReadTransaction.close();
61         final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.of());
62         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
63         assertThat(cause, instanceOf(ReadFailedException.class));
64         assertEquals("Transaction is closed", cause.getMessage());
65     }
66
67     @Test
68     public void readNodeTestWithException() {
69         final var thrown = new NullPointerException("no Node");
70         doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
71         snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
72                 null);
73
74         final var future = snapshotBackedReadTransaction.read(YangInstanceIdentifier.of());
75         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
76         assertThat(cause, instanceOf(ReadFailedException.class));
77         assertEquals("Read failed", cause.getMessage());
78         assertSame(thrown, cause.getCause());
79     }
80
81     @Test
82     public void existsTestWithException() {
83         final var thrown = new NullPointerException("no Node");
84         doThrow(thrown).when(DATA_TREE_SNAPSHOT).readNode(any());
85
86         final var future = snapshotBackedReadTransaction.exists(YangInstanceIdentifier.of());
87         final var cause = assertThrows(ExecutionException.class, () -> Futures.getDone(future)).getCause();
88         assertThat(cause, instanceOf(ReadFailedException.class));
89         assertEquals("Read failed", cause.getMessage());
90         assertSame(thrown, cause.getCause());
91     }
92 }