7b361a9b24d317ab47e46584e369727a58b3572e
[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 TRANSACTION_READY_PROTOTYPE =  mock(TransactionReadyPrototype.class);
33     private SnapshotBackedReadWriteTransaction snapshotBackedReadWriteTransaction;
34
35     @Before
36     public void setUp() throws Exception {
37         doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
38         snapshotBackedReadWriteTransaction = new SnapshotBackedReadWriteTransaction(new Object(), false,
39                 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
40     }
41
42     @Test
43     public void basicTest() throws Exception {
44         final NormalizedNode<?, ?> testNode = mock(NormalizedNode.class);
45         final Optional<NormalizedNode> optional = Optional.of(testNode);
46         doReturn("testNode").when(testNode).toString();
47         doReturn(optional).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
48         assertTrue((Boolean) snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get());
49         assertEquals(optional, snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get());
50     }
51
52     @Test(expected = ReadFailedException.class)
53     public void readTestWithNullException() throws Throwable {
54         doReturn(null).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
55         try {
56             snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
57             fail("Expected ReadFailedException");
58         } catch (Exception e) {
59             throw e.getCause();
60         }
61     }
62
63     @Test(expected = ReadFailedException.class)
64     public void readNodeTestWithException() throws Throwable {
65         doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
66         try {
67             snapshotBackedReadWriteTransaction.read(YangInstanceIdentifier.EMPTY).get();
68             fail("Expected ReadFailedException");
69         } catch (Exception e) {
70             throw e.getCause();
71         }
72     }
73
74     @Test(expected = ReadFailedException.class)
75     public void existsTestWithException() throws Throwable {
76         doThrow(new NullPointerException("no Node")).when(DATA_TREE_MODIFICATION).readNode(any());
77         try {
78             snapshotBackedReadWriteTransaction.exists(YangInstanceIdentifier.EMPTY).get();
79             fail("Expected ReadFailedException");
80         } catch (Exception e) {
81             throw e.getCause();
82         }
83     }
84 }