Adopt odlparent-10.0.0/yangtools-8.0.0-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.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertNull;
13 import static org.junit.Assert.assertTrue;
14 import static org.junit.Assert.fail;
15 import static org.mockito.ArgumentMatchers.any;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19
20 import java.lang.reflect.Field;
21 import java.util.Optional;
22 import org.junit.Test;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
26 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
27
28 @SuppressWarnings("checkstyle:IllegalCatch")
29 public class SnapshotBackedReadTransactionTest {
30
31     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
32
33     private SnapshotBackedReadTransaction<Object> snapshotBackedReadTransaction =
34             new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT, null);
35
36     @Test
37     public void basicTest() throws Exception {
38         final NormalizedNode testNode = mock(NormalizedNode.class);
39         final Optional<NormalizedNode> optional = Optional.of(testNode);
40         doReturn("testNode").when(testNode).toString();
41         doReturn(Optional.of(testNode)).when(DATA_TREE_SNAPSHOT).readNode(YangInstanceIdentifier.empty());
42         assertTrue(snapshotBackedReadTransaction.exists(YangInstanceIdentifier.empty()).get());
43
44         assertEquals(optional, snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get());
45         final Field stableSnapshotField = SnapshotBackedReadTransaction.class.getDeclaredField("stableSnapshot");
46         stableSnapshotField.setAccessible(true);
47
48         DataTreeSnapshot stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
49         assertNotNull(stableSnapshot);
50         snapshotBackedReadTransaction.close();
51         stableSnapshot = (DataTreeSnapshot) stableSnapshotField.get(snapshotBackedReadTransaction);
52         assertNull(stableSnapshot);
53     }
54
55     @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
56     @Test(expected = ReadFailedException.class)
57     public void readTestWithException() throws Throwable {
58         snapshotBackedReadTransaction.close();
59         try {
60             snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get();
61             fail("Expected ReadFailedException");
62         } catch (Exception e) {
63             throw e.getCause();
64         }
65     }
66
67     @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
68     @Test(expected = ReadFailedException.class)
69     public void readNodeTestWithException() throws Throwable {
70         doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
71         snapshotBackedReadTransaction = new SnapshotBackedReadTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
72                 null);
73         try {
74             snapshotBackedReadTransaction.read(YangInstanceIdentifier.empty()).get();
75             fail("Expected ReadFailedException");
76         } catch (Exception e) {
77             throw e.getCause();
78         }
79     }
80
81     @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
82     @Test(expected = ReadFailedException.class)
83     public void existsTestWithException() throws Throwable  {
84         doThrow(new NullPointerException("no Node")).when(DATA_TREE_SNAPSHOT).readNode(any());
85
86         try {
87             snapshotBackedReadTransaction.exists(YangInstanceIdentifier.empty()).get();
88             fail("Expected ReadFailedException");
89         } catch (Exception e) {
90             throw e.getCause();
91         }
92     }
93 }