2 * Copyright (c) 2016 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.mdsal.dom.spi.store;
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;
22 import com.google.common.base.MoreObjects;
23 import java.util.Optional;
24 import org.junit.Assert;
25 import org.junit.Before;
26 import org.junit.Test;
27 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
28 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
29 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
31 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
33 public class SnapshotBackedWriteTransactionTest {
35 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
36 private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
37 private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
38 mock(TransactionReadyPrototype.class);
39 private static final DOMStoreThreePhaseCommitCohort DOM_STORE_THREE_PHASE_COMMIT_COHORT =
40 mock(DOMStoreThreePhaseCommitCohort.class);
41 private static final NormalizedNode NORMALIZED_NODE = mock(NormalizedNode.class);
42 private static final Optional<NormalizedNode> NORMALIZED_NODE_OPTIONAL = Optional.of(NORMALIZED_NODE);
43 private static SnapshotBackedWriteTransaction<Object> snapshotBackedWriteTransaction;
46 public void setUp() throws Exception {
47 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
48 doNothing().when(DATA_TREE_MODIFICATION).ready();
49 doNothing().when(DATA_TREE_MODIFICATION).write(any(), any());
50 doNothing().when(DATA_TREE_MODIFICATION).merge(any(), any());
51 doNothing().when(DATA_TREE_MODIFICATION).delete(any());
52 doNothing().when(TRANSACTION_READY_PROTOTYPE).transactionAborted(any());
53 doReturn("testDataTreeModification").when(DATA_TREE_MODIFICATION).toString();
54 doReturn("testNormalizedNode").when(NORMALIZED_NODE).toString();
55 doReturn(DOM_STORE_THREE_PHASE_COMMIT_COHORT)
56 .when(TRANSACTION_READY_PROTOTYPE)
57 .transactionReady(any(),any(), any());
58 doReturn(NORMALIZED_NODE_OPTIONAL).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.empty());
59 snapshotBackedWriteTransaction = new SnapshotBackedWriteTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
60 TRANSACTION_READY_PROTOTYPE);
64 public void basicTest() throws Exception {
65 snapshotBackedWriteTransaction.write(YangInstanceIdentifier.empty(), NORMALIZED_NODE);
66 verify(DATA_TREE_MODIFICATION).write(any(), any());
68 snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.empty(), NORMALIZED_NODE);
69 verify(DATA_TREE_MODIFICATION).merge(any(), any());
71 snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.empty());
72 verify(DATA_TREE_MODIFICATION).delete(any());
74 assertEquals(NORMALIZED_NODE_OPTIONAL,
75 snapshotBackedWriteTransaction.readSnapshotNode(YangInstanceIdentifier.empty()));
76 verify(DATA_TREE_MODIFICATION).readNode(any());
78 assertTrue(snapshotBackedWriteTransaction.addToStringAttributes(
79 MoreObjects.toStringHelper(this).omitNullValues()).toString().contains("ready"));
80 snapshotBackedWriteTransaction.close();
84 public void readyTest() throws Exception {
85 SnapshotBackedWriteTransaction<Object> tx = new SnapshotBackedWriteTransaction<>(new Object(), false,
86 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
87 Assert.assertNotNull(tx.ready());
88 verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), eq(null));
93 public void readyWithException() {
94 Exception thrown = new RuntimeException();
95 doThrow(thrown).when(DATA_TREE_MODIFICATION).ready();
96 Assert.assertNotNull(snapshotBackedWriteTransaction.ready());
97 verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), same(thrown));
100 @Test(expected = IllegalArgumentException.class)
101 public void writeWithException() throws Exception {
103 throw new TestException();
104 }).when(DATA_TREE_MODIFICATION).write(any(), any());
105 snapshotBackedWriteTransaction.write(YangInstanceIdentifier.empty(), NORMALIZED_NODE);
108 @Test(expected = IllegalArgumentException.class)
109 public void mergeWithException() throws Exception {
111 throw new TestException();
112 }).when(DATA_TREE_MODIFICATION).merge(any(), any());
113 snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.empty(), NORMALIZED_NODE);
116 @Test(expected = IllegalArgumentException.class)
117 public void deleteWithException() throws Exception {
119 throw new TestException();
120 }).when(DATA_TREE_MODIFICATION).delete(any());
121 snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.empty());
124 private static final class TestException extends Exception {
125 private static final long serialVersionUID = 1L;