Fix odlparent-3.0.0 checkstyle issues
[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<NormalizedNode<?, ?>> NORMALIZED_NODE_OPTIONAL = Optional.of(NORMALIZED_NODE);
40     private static SnapshotBackedWriteTransaction<Object> 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<Object> tx = new SnapshotBackedWriteTransaction<>(new Object(), false,
81                 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
82         Assert.assertNotNull(tx.ready());
83         verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any());
84         tx.close();
85     }
86
87     @Test(expected = IllegalArgumentException.class)
88     public void writeWithException() throws Exception {
89         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).write(any(), any());
90         snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
91     }
92
93     @Test(expected = IllegalArgumentException.class)
94     public void mergeWithException() throws Exception {
95         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).merge(any(), any());
96         snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
97     }
98
99     @Test(expected = IllegalArgumentException.class)
100     public void deleteWithException() throws Exception {
101         doThrow(TestException.class).when(DATA_TREE_MODIFICATION).delete(any());
102         snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
103     }
104
105     private static final class TestException extends Exception {
106         private static final long serialVersionUID = 1L;
107     }
108 }