31fbf03329cfd0486ce02e05bc9b1ff5ac1a47f0
[mdsal.git] / dom / mdsal-dom-spi / src / test / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedReadWriteTransactionTest.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.assertTrue;
12 import static org.junit.Assert.fail;
13 import static org.mockito.Matchers.any;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.mock;
17
18 import com.google.common.base.Optional;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.opendaylight.mdsal.common.api.ReadFailedException;
22 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
26 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
27
28 public class SnapshotBackedReadWriteTransactionTest {
29
30     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
31     private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
32     private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
33             mock(TransactionReadyPrototype.class);
34     private SnapshotBackedReadWriteTransaction<Object> snapshotBackedReadWriteTransaction;
35
36     @Before
37     public void setUp() throws Exception {
38         doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
39         snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction<>(new Object(), false,
40                 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
41     }
42
43     @Test
44     public void basicTest() throws Exception {
45         final NormalizedNode<?, ?> testNode = mock(NormalizedNode.class);
46         final Optional<NormalizedNode<?, ?>> optional = Optional.of(testNode);
47         doReturn("testNode").when(testNode).toString();
48         doReturn(optional).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
49         assertTrue(snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get());
50         assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get());
51     }
52
53     @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
54     @Test(expected = ReadFailedException.class)
55     public void readTestWithNullException() throws Throwable {
56         doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
57         try {
58             snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
59             fail("Expected ReadFailedException");
60         } catch (Exception e) {
61             throw e.getCause();
62         }
63     }
64
65     @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
66     @Test(expected = ReadFailedException.class)
67     public void readNodeTestWithException() throws Throwable {
68         doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
69         try {
70             snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
71             fail("Expected ReadFailedException");
72         } catch (Exception e) {
73             throw e.getCause();
74         }
75     }
76
77     @SuppressWarnings({"checkstyle:IllegalThrows", "checkstyle:IllegalCatch"})
78     @Test(expected = ReadFailedException.class)
79     public void existsTestWithException() throws Throwable {
80         doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
81         try {
82             snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get();
83             fail("Expected ReadFailedException");
84         } catch (Exception e) {
85             throw e.getCause();
86         }
87     }
88 }