Merge "BUG-614: migrate RuntimeGeneratedInvoker"
[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.controller.sal.core.spi.data.DOMStoreTransactionChain;
16 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
17 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
18 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
19
20 import com.google.common.base.Optional;
21 import com.google.common.util.concurrent.ListenableFuture;
22 import com.google.common.util.concurrent.MoreExecutors;
23
24 public class InMemoryDataStoreTest {
25
26     private SchemaContext schemaContext;
27     private InMemoryDOMDataStore domStore;
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     @Test
38     public void testTransactionIsolation() throws InterruptedException, ExecutionException {
39
40         assertNotNull(domStore);
41
42         DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
43         assertNotNull(readTx);
44
45         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
46         assertNotNull(writeTx);
47         /**
48          *
49          * Writes /test in writeTx
50          *
51          */
52         writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
53
54         /**
55          *
56          * Reads /test from writeTx Read should return container.
57          *
58          */
59         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
60         assertTrue(writeTxContainer.get().isPresent());
61
62         /**
63          *
64          * Reads /test from readTx Read should return Absent.
65          *
66          */
67         ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
68         assertFalse(readTxContainer.get().isPresent());
69     }
70
71     @Test
72     public void testTransactionCommit() throws InterruptedException, ExecutionException {
73
74         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
75         assertNotNull(writeTx);
76         /**
77          *
78          * Writes /test in writeTx
79          *
80          */
81         writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
82
83         /**
84          *
85          * Reads /test from writeTx Read should return container.
86          *
87          */
88         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
89         assertTrue(writeTxContainer.get().isPresent());
90
91         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
92
93         assertThreePhaseCommit(cohort);
94
95         Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
96                 .get();
97         assertTrue(afterCommitRead.isPresent());
98     }
99
100     @Test
101     public void testTransactionAbort() throws InterruptedException, ExecutionException {
102
103         DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
104         assertNotNull(writeTx);
105
106         assertTestContainerWrite(writeTx);
107
108         DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
109
110         assertTrue(cohort.canCommit().get().booleanValue());
111         cohort.preCommit().get();
112         cohort.abort().get();
113
114         Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
115                 .get();
116         assertFalse(afterCommitRead.isPresent());
117     }
118
119     @Test
120     public void testTransactionChain() throws InterruptedException, ExecutionException {
121         DOMStoreTransactionChain txChain = domStore.createTransactionChain();
122         assertNotNull(txChain);
123
124         /**
125          * We alocate new read-write transaction and write /test
126          *
127          *
128          */
129         DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
130         assertTestContainerWrite(firstTx);
131
132         /**
133          * First transaction is marked as ready, we are able to allocate chained
134          * transactions
135          */
136         DOMStoreThreePhaseCommitCohort firstWriteTxCohort = firstTx.ready();
137
138         /**
139          * We alocate chained transaction - read transaction, note first one is
140          * still not commited to datastore.
141          */
142         DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
143
144         /**
145          *
146          * We test if we are able to read data from tx, read should not fail
147          * since we are using chained transaction.
148          *
149          *
150          */
151         assertTestContainerExists(secondReadTx);
152
153         /**
154          *
155          * We alocate next transaction, which is still based on first one, but
156          * is read-write.
157          *
158          */
159         DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
160
161         /**
162          * We test existence of /test in third transaction container should
163          * still be visible from first one (which is still uncommmited).
164          *
165          *
166          */
167         assertTestContainerExists(thirdDeleteTx);
168
169         /**
170          * We delete node in third transaction
171          */
172         thirdDeleteTx.delete(TestModel.TEST_PATH);
173
174         /**
175          * third transaction is sealed.
176          */
177         DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
178
179         /**
180          * We commit first transaction
181          *
182          */
183         assertThreePhaseCommit(firstWriteTxCohort);
184
185         // Alocates store transacion
186         DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
187         /**
188          * We verify transaction is commited to store, container should exists
189          * in datastore.
190          */
191         assertTestContainerExists(storeReadTx);
192         /**
193          * We commit third transaction
194          *
195          */
196         assertThreePhaseCommit(thirdDeleteTxCohort);
197     }
198
199     @Test
200     @Ignore
201     public void testTransactionConflict() throws InterruptedException, ExecutionException {
202         DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
203         DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
204         assertTestContainerWrite(txOne);
205         assertTestContainerWrite(txTwo);
206
207         /**
208          * Commits transaction
209          */
210         assertThreePhaseCommit(txOne.ready());
211
212         /**
213          * Asserts that txTwo could not be commited
214          */
215         assertFalse(txTwo.ready().canCommit().get());
216     }
217
218     private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort)
219             throws InterruptedException, ExecutionException {
220         assertTrue(cohort.canCommit().get().booleanValue());
221         cohort.preCommit().get();
222         cohort.commit().get();
223     }
224
225     private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
226             throws InterruptedException, ExecutionException {
227         /**
228          *
229          * Writes /test in writeTx
230          *
231          */
232         writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
233
234         return assertTestContainerExists(writeTx);
235     }
236
237     /**
238      *
239      * Reads /test from readTx Read should return container.
240      *
241      */
242     private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(DOMStoreReadTransaction readTx)
243             throws InterruptedException, ExecutionException {
244
245         ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
246         assertTrue(writeTxContainer.get().isPresent());
247         return writeTxContainer.get();
248     }
249
250 }