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.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;
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;
30 public class SnapshotBackedWriteTransactionTest {
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;
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);
59 public void basicTest() throws Exception {
60 snapshotBackedWriteTransaction.write(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
61 verify(DATA_TREE_MODIFICATION).write(any(), any());
63 snapshotBackedWriteTransaction.merge(YangInstanceIdentifier.EMPTY, NORMALIZED_NODE);
64 verify(DATA_TREE_MODIFICATION).merge(any(), any());
66 snapshotBackedWriteTransaction.delete(YangInstanceIdentifier.EMPTY);
67 verify(DATA_TREE_MODIFICATION).delete(any());
69 assertEquals(NORMALIZED_NODE_OPTIONAL,
70 snapshotBackedWriteTransaction.readSnapshotNode(YangInstanceIdentifier.EMPTY));
71 verify(DATA_TREE_MODIFICATION).readNode(any());
73 assertTrue(snapshotBackedWriteTransaction.addToStringAttributes(
74 MoreObjects.toStringHelper(this).omitNullValues()).toString().contains("ready"));
75 snapshotBackedWriteTransaction.close();
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());
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);
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);
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);
105 private static final class TestException extends Exception {
106 private static final long serialVersionUID = 1L;