1 package org.opendaylight.controller.md.sal.dom.store.impl;
3 import static org.junit.Assert.assertFalse;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
7 import java.util.concurrent.ExecutionException;
9 import org.junit.Before;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadTransaction;
12 import org.opendaylight.controller.sal.core.spi.data.DOMStoreReadWriteTransaction;
13 import org.opendaylight.controller.sal.core.spi.data.DOMStoreThreePhaseCommitCohort;
14 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
15 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
16 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
18 import com.google.common.base.Optional;
19 import com.google.common.util.concurrent.ListenableFuture;
20 import com.google.common.util.concurrent.MoreExecutors;
22 public class InMemoryDataStoreTest {
24 private SchemaContext schemaContext;
25 private InMemoryDOMDataStore domStore;
29 public void setupStore() {
30 domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
31 schemaContext = TestModel.createTestContext();
32 domStore.onGlobalContextUpdated(schemaContext);
38 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
40 assertNotNull(domStore);
43 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
44 assertNotNull(readTx);
46 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
47 assertNotNull(writeTx);
50 * Writes /test in writeTx
53 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
57 * Reads /test from writeTx
58 * Read should return container.
61 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
62 assertTrue(writeTxContainer.get().isPresent());
66 * Reads /test from readTx
67 * Read should return Absent.
70 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
71 assertFalse(readTxContainer.get().isPresent());
75 public void testTransactionCommit() throws InterruptedException, ExecutionException {
77 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
78 assertNotNull(writeTx);
81 * Writes /test in writeTx
84 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
88 * Reads /test from writeTx
89 * Read should return container.
92 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
93 assertTrue(writeTxContainer.get().isPresent());
95 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
97 assertThreePhaseCommit(cohort);
99 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
100 assertTrue(afterCommitRead.isPresent());
104 public void testTransactionAbort() throws InterruptedException, ExecutionException {
106 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
107 assertNotNull(writeTx);
109 assertTestContainerWrite(writeTx);
111 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
113 assertTrue(cohort.canCommit().get().booleanValue());
114 cohort.preCommit().get();
115 cohort.abort().get();
117 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
118 assertFalse(afterCommitRead.isPresent());
122 public void testTransactionConflict() throws InterruptedException, ExecutionException {
123 DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
124 DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
125 assertTestContainerWrite(txOne);
126 assertTestContainerWrite(txTwo);
129 * Commits transaction
131 assertThreePhaseCommit(txOne.ready());
134 * Asserts that txTwo could not be commited
136 assertFalse(txTwo.ready().canCommit().get());
141 private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort) throws InterruptedException, ExecutionException {
142 assertTrue(cohort.canCommit().get().booleanValue());
143 cohort.preCommit().get();
144 cohort.commit().get();
148 private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx) throws InterruptedException, ExecutionException {
151 * Writes /test in writeTx
154 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
158 * Reads /test from writeTx
159 * Read should return container.
162 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
163 assertTrue(writeTxContainer.get().isPresent());
164 return writeTxContainer.get();