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