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.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;
19 import com.google.common.base.Optional;
20 import com.google.common.util.concurrent.ListenableFuture;
21 import com.google.common.util.concurrent.MoreExecutors;
23 public class InMemoryDataStoreTest {
25 private SchemaContext schemaContext;
26 private InMemoryDOMDataStore domStore;
30 public void setupStore() {
31 domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.sameThreadExecutor());
32 schemaContext = TestModel.createTestContext();
33 domStore.onGlobalContextUpdated(schemaContext);
39 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
41 assertNotNull(domStore);
44 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
45 assertNotNull(readTx);
47 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
48 assertNotNull(writeTx);
51 * Writes /test in writeTx
54 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
58 * Reads /test from writeTx
59 * Read should return container.
62 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
63 assertTrue(writeTxContainer.get().isPresent());
67 * Reads /test from readTx
68 * Read should return Absent.
71 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
72 assertFalse(readTxContainer.get().isPresent());
76 public void testTransactionCommit() throws InterruptedException, ExecutionException {
78 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
79 assertNotNull(writeTx);
82 * Writes /test in writeTx
85 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
89 * Reads /test from writeTx
90 * Read should return container.
93 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
94 assertTrue(writeTxContainer.get().isPresent());
96 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
98 assertThreePhaseCommit(cohort);
100 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
101 assertTrue(afterCommitRead.isPresent());
105 public void testTransactionAbort() throws InterruptedException, ExecutionException {
107 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
108 assertNotNull(writeTx);
110 assertTestContainerWrite(writeTx);
112 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
114 assertTrue(cohort.canCommit().get().booleanValue());
115 cohort.preCommit().get();
116 cohort.abort().get();
118 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
119 assertFalse(afterCommitRead.isPresent());
124 public void testTransactionConflict() throws InterruptedException, ExecutionException {
125 DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
126 DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
127 assertTestContainerWrite(txOne);
128 assertTestContainerWrite(txTwo);
131 * Commits transaction
133 assertThreePhaseCommit(txOne.ready());
136 * Asserts that txTwo could not be commited
138 assertFalse(txTwo.ready().canCommit().get());
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();
150 private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx) throws InterruptedException, ExecutionException {
153 * Writes /test in writeTx
156 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
160 * Reads /test from writeTx
161 * Read should return container.
164 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
165 assertTrue(writeTxContainer.get().isPresent());
166 return writeTxContainer.get();