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.Matchers.any;
13 import static org.mockito.Matchers.eq;
14 import static org.mockito.Matchers.same;
15 import static org.mockito.Mockito.doNothing;
16 import static org.mockito.Mockito.doReturn;
17 import static org.mockito.Mockito.doThrow;
18 import static org.mockito.Mockito.mock;
19 import static org.mockito.Mockito.verify;
21 import com.google.common.base.MoreObjects;
22 import java.util.Optional;
23 import org.junit.Assert;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
27 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
28 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
29 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
30 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
32 public class SnapshotBackedWriteTransactionTest {
34 private static final DataTreeSnapshot DATA_TREE_SNAPSHOT = mock(DataTreeSnapshot.class);
35 private static final DataTreeModification DATA_TREE_MODIFICATION = mock(DataTreeModification.class);
36 private static final TransactionReadyPrototype<Object> TRANSACTION_READY_PROTOTYPE =
37 mock(TransactionReadyPrototype.class);
38 private static final DOMStoreThreePhaseCommitCohort DOM_STORE_THREE_PHASE_COMMIT_COHORT =
39 mock(DOMStoreThreePhaseCommitCohort.class);
40 private static final NormalizedNode<?, ?> NORMALIZED_NODE = mock(NormalizedNode.class);
41 private static final Optional<NormalizedNode<?, ?>> NORMALIZED_NODE_OPTIONAL = Optional.of(NORMALIZED_NODE);
42 private static SnapshotBackedWriteTransaction<Object> snapshotBackedWriteTransaction;
45 public void setUp() throws Exception {
46 doReturn(DATA_TREE_MODIFICATION).when(DATA_TREE_SNAPSHOT).newModification();
47 doNothing().when(DATA_TREE_MODIFICATION).ready();
48 doNothing().when(DATA_TREE_MODIFICATION).write(any(), any());
49 doNothing().when(DATA_TREE_MODIFICATION).merge(any(), any());
50 doNothing().when(DATA_TREE_MODIFICATION).delete(any());
51 doNothing().when(TRANSACTION_READY_PROTOTYPE).transactionAborted(any());
52 doReturn("testDataTreeModification").when(DATA_TREE_MODIFICATION).toString();
53 doReturn("testNormalizedNode").when(NORMALIZED_NODE).toString();
54 doReturn(DOM_STORE_THREE_PHASE_COMMIT_COHORT)
55 .when(TRANSACTION_READY_PROTOTYPE)
56 .transactionReady(any(),any(), any());
57 doReturn(NORMALIZED_NODE_OPTIONAL).when(DATA_TREE_MODIFICATION).readNode(YangInstanceIdentifier.EMPTY);
58 snapshotBackedWriteTransaction = new SnapshotBackedWriteTransaction<>(new Object(), false, DATA_TREE_SNAPSHOT,
59 TRANSACTION_READY_PROTOTYPE);
63 public void basicTest() throws Exception {
64 snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
65 verify(DATA_TREE_MODIFICATION).write(any(), any());
67 snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
68 verify(DATA_TREE_MODIFICATION).merge(any(), any());
70 snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
71 verify(DATA_TREE_MODIFICATION).delete(any());
73 assertEquals(NORMALIZED_NODE_OPTIONAL,
74 snapshotBackedWriteTransaction.readSnapshotNode(YangInstanceIdentifier.EMPTY));
75 verify(DATA_TREE_MODIFICATION).readNode(any());
77 assertTrue(snapshotBackedWriteTransaction.addToStringAttributes(
78 MoreObjects.toStringHelper(this).omitNullValues()).toString().contains("ready"));
79 snapshotBackedWriteTransaction.close();
83 public void readyTest() throws Exception {
84 SnapshotBackedWriteTransaction<Object> tx = new SnapshotBackedWriteTransaction<>(new Object(), false,
85 DATA_TREE_SNAPSHOT, TRANSACTION_READY_PROTOTYPE);
86 Assert.assertNotNull(tx.ready());
87 verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), eq(null));
92 public void readyWithException() {
93 Exception thrown = new RuntimeException();
94 doThrow(thrown).when(DATA_TREE_MODIFICATION).ready();
95 Assert.assertNotNull(snapshotBackedWriteTransaction.ready());
96 verify(TRANSACTION_READY_PROTOTYPE).transactionReady(any(), any(), same(thrown));
99 @Test(expected = IllegalArgumentException.class)
100 public void writeWithException() throws Exception {
101 doThrow(TestException.class).when(DATA_TREE_MODIFICATION).write(any(), any());
102 snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
105 @Test(expected = IllegalArgumentException.class)
106 public void mergeWithException() throws Exception {
107 doThrow(TestException.class).when(DATA_TREE_MODIFICATION).merge(any(), any());
108 snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
111 @Test(expected = IllegalArgumentException.class)
112 public void deleteWithException() throws Exception {
113 doThrow(TestException.class).when(DATA_TREE_MODIFICATION).delete(any());
114 snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
117 private static final class TestException extends Exception {
118 private static final long serialVersionUID = 1L;