Bug 508: Improved error reporting for failed canCommit phase.
[controller.git] / opendaylight / md-sal / sal-dom-broker / src / test / java / org / opendaylight / controller / md / sal / dom / store / impl / InMemoryDataStoreTest.java
1 package org.opendaylight.controller.md.sal.dom.store.impl;
2
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.util.concurrent.ExecutionException;
8
9 import org.junit.Before;
10 import org.junit.Ignore;
11 import org.junit.Test;
12 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
14 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
15 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
16 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
17 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
22
23 public class InMemoryDataStoreTest {
24
25     private SchemaContext schemaContext;
26     private InMemoryDOMDataStore domStore;
27
28
29     @Before
30     public void setupStore() {
31         domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
32         schemaContext = TestModel.createTestContext();
33         domStore.onGlobalContextUpdated(schemaContext);
34
35     }
36
37
38     @Test
39     public void testTransactionIsolation() throws InterruptedException, ExecutionException {
40
41         assertNotNull(domStore);
42
43
44         DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
45         assertNotNull(readTx);
46
47         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
48         assertNotNull(writeTx);
49         /**
50          *
51          * Writes /test in writeTx
52          *
53          */
54         writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
55
56         /**
57          *
58          * Reads /test from writeTx
59          * Read should return container.
60          *
61          */
62         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
63         assertTrue(writeTxContainer.get().isPresent());
64
65         /**
66         *
67         * Reads /test from readTx
68         * Read should return Absent.
69         *
70         */
71         ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
72         assertFalse(readTxContainer.get().isPresent());
73     }
74
75     @Test
76     public void testTransactionCommit() throws InterruptedException, ExecutionException {
77
78         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
79         assertNotNull(writeTx);
80         /**
81          *
82          * Writes /test in writeTx
83          *
84          */
85         writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
86
87         /**
88          *
89          * Reads /test from writeTx
90          * Read should return container.
91          *
92          */
93         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
94         assertTrue(writeTxContainer.get().isPresent());
95
96         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
97
98         assertThreePhaseCommit(cohort);
99
100         Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
101         assertTrue(afterCommitRead.isPresent());
102     }
103
104     @Test
105     public void testTransactionAbort() throws InterruptedException, ExecutionException {
106
107         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
108         assertNotNull(writeTx);
109
110         assertTestContainerWrite(writeTx);
111
112         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
113
114         assertTrue(cohort.canCommit().get().booleanValue());
115         cohort.preCommit().get();
116         cohort.abort().get();
117
118         Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
119         assertFalse(afterCommitRead.isPresent());
120     }
121
122     @Test
123     @Ignore
124     public void testTransactionConflict() throws InterruptedException, ExecutionException {
125         DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
126         DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
127         assertTestContainerWrite(txOne);
128         assertTestContainerWrite(txTwo);
129
130         /**
131          * Commits transaction
132          */
133         assertThreePhaseCommit(txOne.ready());
134
135         /**
136          * Asserts that txTwo could not be commited
137          */
138         assertFalse(txTwo.ready().canCommit().get());
139     }
140
141
142
143     private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort) throws InterruptedException, ExecutionException {
144         assertTrue(cohort.canCommit().get().booleanValue());
145         cohort.preCommit().get();
146         cohort.commit().get();
147     }
148
149
150     private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx) throws InterruptedException, ExecutionException {
151         /**
152         *
153         * Writes /test in writeTx
154         *
155         */
156        writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
157
158        /**
159         *
160         * Reads /test from writeTx
161         * Read should return container.
162         *
163         */
164        ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
165        assertTrue(writeTxContainer.get().isPresent());
166        return writeTxContainer.get();
167     }
168
169 }