Bug 5947: Increasing code coverage - mdsal-dom-spi
[mdsal.git] / dom / mdsal-dom-spi / src / test / java / org / opendaylight / mdsal / dom / spi / store / SnapshotBackedWriteTransactionTest.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.mockito.Matchers.any;
13 import static org.mockito.Mockito.doNothing;
14 import static org.mockito.Mockito.doReturn;
15 import static org.mockito.Mockito.doThrow;
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18
19 import com.google.common.base.MoreObjects;
20 import com.google.common.base.Optional;
21 import org.junit.Assert;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
25 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
28 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
29
30 public class SnapshotBackedWriteTransactionTest {
31
32     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
33     private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
34     private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
35             mock(TransactionReadyPrototype.class);
36     private static final DOMStoreThreePhaseCommitCohort DOM_STORE_THREE_PHASE_COMMIT_COHORT =
37             mock(DOMStoreThreePhaseCommitCohort.class);
38     private static final NormalizedNode NORMALIZED_NODE = mock(NormalizedNode.class);
39     private static final Optional NORMALIZED_NODE_OPTIONAL = Optional.of(NORMALIZED_NODE);
40     private static SnapshotBackedWriteTransaction snapshotBackedWriteTransaction;
41
42     @Before
43     public void setUp() throws Exception {
44         doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
45         doNothing().when(DATA_TREE_MODIFICATION).ready();
46         doNothing().when(DATA_TREE_MODIFICATION).write(any(), any());
47         doNothing().when(DATA_TREE_MODIFICATION).merge(any(), any());
48         doNothing().when(DATA_TREE_MODIFICATION).delete(any());
49         doNothing().when(TRANSACTION_READY_PROTOTYPE).transactionAborted(any());
50         doReturn("testDataTreeModification").when(DATA_TREE_MODIFICATION).toString();
51         doReturn("testNormalizedNode").when(NORMALIZED_NODE).toString();
52         doReturn(DOM_STORE_THREE_PHASE_COMMIT_COHORT).when(TRANSACTION_READY_PROTOTYPE).transactionReady(any(),any());
53         doReturn(NORMALIZED_NODE_OPTIONAL).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
54         snapshotBackedWriteTransaction = new SnapshotBackedWriteTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
55                 TRANSACTION_READY_PROTOTYPE);
56     }
57
58     @Test
59     public void basicTest() throws Exception {
60         snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
61         verify(DATA_TREE_MODIFICATION).write(any(), any());
62
63         snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
64         verify(DATA_TREE_MODIFICATION).merge(any(), any());
65
66         snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
67         verify(DATA_TREE_MODIFICATION).delete(any());
68
69         assertEquals(NORMALIZED_NODE_OPTIONAL,
70                 snapshotBackedWriteTransaction.readSnapshotNode(YangInstanceIdentifier.EMPTY));
71         verify(DATA_TREE_MODIFICATION).readNode(any());
72
73         assertTrue(snapshotBackedWriteTransaction.addToStringAttributes(
74                 MoreObjects.toStringHelper(this).omitNullValues()).toString().contains("ready"));
75         snapshotBackedWriteTransaction.close();
76     }
77
78     @Test
79     public void readyTest() throws Exception {
80         SnapshotBackedWriteTransaction snapshotBackedWriteTransaction =
81                 new SnapshotBackedWriteTransaction<>(
82                         new Object(), false, DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
83         Assert.assertNotNull(snapshotBackedWriteTransaction.ready());
84         verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any());
85         snapshotBackedWriteTransaction.close();
86     }
87
88     @Test(expected = IllegalArgumentException.class)
89     public void writeWithException() throws Exception {
90         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).write(any(), any());
91         snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
92     }
93
94     @Test(expected = IllegalArgumentException.class)
95     public void mergeWithException() throws Exception {
96         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).merge(any(), any());
97         snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
98     }
99
100     @Test(expected = IllegalArgumentException.class)
101     public void deleteWithException() throws Exception {
102         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).delete(any());
103         snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
104     }
105
106     private static final class TestException extends Exception {}
107 }