41e11f524b55b4a95ceb5a933f3b9411ce352d87
[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.ArgumentMatchers.any;
13 import static org.mockito.ArgumentMatchers.eq;
14 import static org.mockito.ArgumentMatchers.same;
15 import static org.mockito.Mockito.doAnswer;
16 import static org.mockito.Mockito.doNothing;
17 import static org.mockito.Mockito.doReturn;
18 import static org.mockito.Mockito.doThrow;
19 import static org.mockito.Mockito.mock;
20 import static org.mockito.Mockito.verify;
21
22 import com.google.common.base.MoreObjects;
23 import java.io.Serial;
24 import java.util.Optional;
25 import org.junit.Assert;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
29 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
30 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
31 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeModification;
32 import org.opendaylight.yangtools.yang.data.tree.api.DataTreeSnapshot;
33
34 public class SnapshotBackedWriteTransactionTest {
35
36     private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
37     private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
38     private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
39             mock(TransactionReadyPrototype.class);
40     private static final DOMStoreThreePhaseCommitCohort DOM_STORE_THREE_PHASE_COMMIT_COHORT =
41             mock(DOMStoreThreePhaseCommitCohort.class);
42     private static final NormalizedNode NORMALIZED_NODE = mock(NormalizedNode.class);
43     private static final Optional<NormalizedNode> NORMALIZED_NODE_OPTIONAL = Optional.of(NORMALIZED_NODE);
44     private static SnapshotBackedWriteTransaction<Object> snapshotBackedWriteTransaction;
45
46     @Before
47     public void setUp() throws Exception {
48         doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
49         doNothing().when(DATA_TREE_MODIFICATION).ready();
50         doNothing().when(DATA_TREE_MODIFICATION).write(any(), any());
51         doNothing().when(DATA_TREE_MODIFICATION).merge(any(), any());
52         doNothing().when(DATA_TREE_MODIFICATION).delete(any());
53         doNothing().when(TRANSACTION_READY_PROTOTYPE).transactionAborted(any());
54         doReturn("testDataTreeModification").when(DATA_TREE_MODIFICATION).toString();
55         doReturn("testNormalizedNode").when(NORMALIZED_NODE).toString();
56         doReturn(DOM_STORE_THREE_PHASE_COMMIT_COHORT)
57                 .when(TRANSACTION_READY_PROTOTYPE)
58                 .transactionReady(any(),any(), any());
59         doReturn(NORMALIZED_NODE_OPTIONAL).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.of());
60         snapshotBackedWriteTransaction = new SnapshotBackedWriteTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
61                 TRANSACTION_READY_PROTOTYPE);
62     }
63
64     @Test
65     public void basicTest() throws Exception {
66         snapshotBackedWriteTransaction.write(YangInstanceIdentifier.of(), NORMALIZED_NODE);
67         verify(DATA_TREE_MODIFICATION).write(any(), any());
68
69         snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.of(), NORMALIZED_NODE);
70         verify(DATA_TREE_MODIFICATION).merge(any(), any());
71
72         snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.of());
73         verify(DATA_TREE_MODIFICATION).delete(any());
74
75         assertEquals(NORMALIZED_NODE_OPTIONAL,
76                 snapshotBackedWriteTransaction.readSnapshotNode(YangInstanceIdentifier.of()));
77         verify(DATA_TREE_MODIFICATION).readNode(any());
78
79         assertTrue(snapshotBackedWriteTransaction.addToStringAttributes(
80                 MoreObjects.toStringHelper(this).omitNullValues()).toString().contains("ready"));
81         snapshotBackedWriteTransaction.close();
82     }
83
84     @Test
85     public void readyTest() throws Exception {
86         SnapshotBackedWriteTransaction<Object> tx = new SnapshotBackedWriteTransaction<>(new Object(), false,
87                 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
88         Assert.assertNotNull(tx.ready());
89         verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), eq(null));
90         tx.close();
91     }
92
93     @Test
94     public void readyWithException() {
95         Exception thrown = new RuntimeException();
96         doThrow(thrown).when(DATA_TREE_MODIFICATION).ready();
97         Assert.assertNotNull(snapshotBackedWriteTransaction.ready());
98         verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), same(thrown));
99     }
100
101     @Test(expected = IllegalArgumentException.class)
102     public void writeWithException() throws Exception {
103         doAnswer(inv -> {
104             throw new TestException();
105         }).when(DATA_TREE_MODIFICATION).write(any(), any());
106         snapshotBackedWriteTransaction.write(YangInstanceIdentifier.of(), NORMALIZED_NODE);
107     }
108
109     @Test(expected = IllegalArgumentException.class)
110     public void mergeWithException() throws Exception {
111         doAnswer(inv -> {
112             throw new TestException();
113         }).when(DATA_TREE_MODIFICATION).merge(any(), any());
114         snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.of(), NORMALIZED_NODE);
115     }
116
117     @Test(expected = IllegalArgumentException.class)
118     public void deleteWithException() throws Exception {
119         doAnswer(inv -> {
120             throw new TestException();
121         }).when(DATA_TREE_MODIFICATION).delete(any());
122         snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.of());
123     }
124
125     private static final class TestException extends Exception {
126         @Serial
127         private static final long serialVersionUID = 1L;
128     }
129 }