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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.