2 * Copyright (c) 2014 Cisco Systems, Inc. and others. All rights reserved.
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
8 package org.opendaylight.controller.md.sal.dom.store.impl;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertFalse;
12 import static org.junit.Assert.assertNotNull;
13 import static org.junit.Assert.assertTrue;
15 import com.google.common.base.Optional;
16 import com.google.common.util.concurrent.ListenableFuture;
17 import com.google.common.util.concurrent.MoreExecutors;
18 import java.util.concurrent.ExecutionException;
19 import org.junit.Before;
20 import org.junit.Ignore;
21 import org.junit.Test;
22 import org.mockito.Mockito;
23 import org.opendaylight.mdsal.common.api.ReadFailedException;
24 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadTransaction;
25 import org.opendaylight.mdsal.dom.spi.store.DOMStoreReadWriteTransaction;
26 import org.opendaylight.mdsal.dom.spi.store.DOMStoreThreePhaseCommitCohort;
27 import org.opendaylight.mdsal.dom.spi.store.DOMStoreTransactionChain;
28 import org.opendaylight.mdsal.dom.spi.store.DOMStoreWriteTransaction;
29 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedTransactions;
30 import org.opendaylight.mdsal.dom.spi.store.SnapshotBackedWriteTransaction.TransactionReadyPrototype;
31 import org.opendaylight.mdsal.dom.store.inmemory.InMemoryDOMDataStore;
32 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier;
33 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
34 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
35 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
36 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeModification;
37 import org.opendaylight.yangtools.yang.data.api.schema.tree.DataTreeSnapshot;
38 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
39 import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
40 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
42 public class InMemoryDataStoreTest {
44 private SchemaContext schemaContext;
45 private InMemoryDOMDataStore domStore;
48 public void setupStore() {
49 domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.newDirectExecutorService());
50 schemaContext = TestModel.createTestContext();
51 domStore.onGlobalContextUpdated(schemaContext);
55 public void testTransactionIsolation() throws InterruptedException, ExecutionException {
57 assertNotNull(domStore);
59 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
60 assertNotNull(readTx);
62 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
63 assertNotNull(writeTx);
66 * Writes /test in writeTx.
68 NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
69 writeTx.write(TestModel.TEST_PATH, testNode);
72 * Reads /test from writeTx Read should return container.
74 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
75 assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
76 assertEquals("read: data", testNode, writeTxContainer.get().get());
79 * Reads /test from readTx Read should return Absent.
81 ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
82 assertEquals("read: isPresent", false, readTxContainer.get().isPresent());
86 public void testTransactionCommit() throws InterruptedException, ExecutionException {
88 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
89 assertNotNull(writeTx);
92 * Writes /test in writeTx.
94 NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
95 writeTx.write(TestModel.TEST_PATH, testNode);
98 * Reads /test from writeTx Read should return container.
100 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
101 assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
102 assertEquals("read: data", testNode, writeTxContainer.get().get());
104 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
106 assertThreePhaseCommit(cohort);
108 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
110 assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
111 assertEquals("After commit read: data", testNode, afterCommitRead.get());
115 public void testDelete() throws Exception {
117 DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
118 assertNotNull(writeTx);
120 // Write /test and commit
122 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
124 assertThreePhaseCommit(writeTx.ready());
126 Optional<NormalizedNode<?, ?>> afterCommitRead =
127 domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
128 assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
130 // Delete /test and verify
132 writeTx = domStore.newWriteOnlyTransaction();
134 writeTx.delete(TestModel.TEST_PATH);
136 assertThreePhaseCommit(writeTx.ready());
138 afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
139 assertEquals("After commit read: isPresent", false, afterCommitRead.isPresent());
143 public void testMerge() throws Exception {
145 DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
146 assertNotNull(writeTx);
148 ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
149 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
150 .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
151 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
152 TestModel.ID_QNAME, 1)).build()).build();
154 writeTx.merge(TestModel.TEST_PATH, containerNode);
156 assertThreePhaseCommit(writeTx.ready());
158 Optional<NormalizedNode<?, ?>> afterCommitRead =
159 domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
160 assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
161 assertEquals("After commit read: data", containerNode, afterCommitRead.get());
163 // Merge a new list entry node
165 writeTx = domStore.newWriteOnlyTransaction();
166 assertNotNull(writeTx);
168 containerNode = ImmutableContainerNodeBuilder.create()
169 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
170 .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
171 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
172 TestModel.ID_QNAME, 1))
173 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
174 TestModel.ID_QNAME, 2)).build()).build();
176 writeTx.merge(TestModel.TEST_PATH, containerNode);
178 assertThreePhaseCommit(writeTx.ready());
180 afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
181 assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
182 assertEquals("After commit read: data", containerNode, afterCommitRead.get());
187 public void testExistsForExistingData() throws Exception {
189 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
190 assertNotNull(writeTx);
192 ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
193 .withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
194 .addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
195 .addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
196 TestModel.ID_QNAME, 1)).build()).build();
198 writeTx.merge(TestModel.TEST_PATH, containerNode);
200 ListenableFuture<Boolean> exists = writeTx.exists(TestModel.TEST_PATH);
202 assertEquals(Boolean.TRUE, exists.get());
204 DOMStoreThreePhaseCommitCohort ready = writeTx.ready();
206 ready.preCommit().get();
208 ready.commit().get();
210 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
211 assertNotNull(readTx);
214 readTx.exists(TestModel.TEST_PATH);
216 assertEquals(Boolean.TRUE, exists.get());
220 public void testExistsForNonExistingData() throws Exception {
222 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
223 assertNotNull(writeTx);
225 ListenableFuture<Boolean> exists = writeTx.exists(TestModel.TEST_PATH);
227 assertEquals(Boolean.FALSE, exists.get());
229 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
230 assertNotNull(readTx);
233 readTx.exists(TestModel.TEST_PATH);
235 assertEquals(Boolean.FALSE, exists.get());
238 @Test(expected = ReadFailedException.class)
239 public void testExistsThrowsReadFailedException() throws Exception {
241 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
242 assertNotNull(readTx);
246 readTx.exists(TestModel.TEST_PATH).checkedGet();
250 @SuppressWarnings("checkstyle:IllegalThrows")
251 @Test(expected = ReadFailedException.class)
252 public void testReadWithReadOnlyTransactionClosed() throws Throwable {
254 DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
255 assertNotNull(readTx);
259 doReadAndThrowEx(readTx);
262 @SuppressWarnings("checkstyle:IllegalThrows")
263 @Test(expected = ReadFailedException.class)
264 public void testReadWithReadOnlyTransactionFailure() throws Throwable {
266 DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
267 Mockito.doThrow(new RuntimeException("mock ex")).when(mockSnapshot)
268 .readNode(Mockito.any(YangInstanceIdentifier.class));
270 DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadTransaction("1", true, mockSnapshot);
272 doReadAndThrowEx(readTx);
275 @SuppressWarnings("checkstyle:IllegalThrows")
276 @Test(expected = ReadFailedException.class)
277 public void testReadWithReadWriteTransactionClosed() throws Throwable {
279 DOMStoreReadTransaction readTx = domStore.newReadWriteTransaction();
280 assertNotNull(readTx);
284 doReadAndThrowEx(readTx);
287 @SuppressWarnings("checkstyle:IllegalThrows")
288 @Test(expected = ReadFailedException.class)
289 public void testReadWithReadWriteTransactionFailure() throws Throwable {
291 DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
292 DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
293 Mockito.doThrow(new RuntimeException("mock ex")).when(mockModification)
294 .readNode(Mockito.any(YangInstanceIdentifier.class));
295 Mockito.doReturn(mockModification).when(mockSnapshot).newModification();
296 @SuppressWarnings("unchecked")
297 TransactionReadyPrototype<String> mockReady = Mockito.mock(TransactionReadyPrototype.class);
298 DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadWriteTransaction(
299 "1", false, mockSnapshot, mockReady);
301 doReadAndThrowEx(readTx);
304 @SuppressWarnings({ "checkstyle:IllegalThrows", "checkstyle:avoidHidingCauseException" })
305 private static void doReadAndThrowEx(final DOMStoreReadTransaction readTx) throws Throwable {
307 readTx.read(TestModel.TEST_PATH).get();
308 } catch (ExecutionException e) {
313 @Test(expected = IllegalStateException.class)
314 public void testWriteWithTransactionReady() throws Exception {
316 DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
321 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
324 @Test(expected = IllegalStateException.class)
325 public void testReadyWithTransactionAlreadyReady() throws Exception {
327 DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
336 public void testTransactionAbort() throws InterruptedException, ExecutionException {
338 DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
339 assertNotNull(writeTx);
341 assertTestContainerWrite(writeTx);
343 DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
345 assertTrue(cohort.canCommit().get().booleanValue());
346 cohort.preCommit().get();
347 cohort.abort().get();
349 Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
351 assertFalse(afterCommitRead.isPresent());
355 public void testTransactionChain() throws InterruptedException, ExecutionException {
356 DOMStoreTransactionChain txChain = domStore.createTransactionChain();
357 assertNotNull(txChain);
360 * We alocate new read-write transaction and write /test.
362 DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
363 assertTestContainerWrite(firstTx);
366 * First transaction is marked as ready, we are able to allocate chained
369 final DOMStoreThreePhaseCommitCohort firstWriteTxCohort = firstTx.ready();
372 * We alocate chained transaction - read transaction, note first one is
373 * still not commited to datastore.
375 DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
378 * We test if we are able to read data from tx, read should not fail
379 * since we are using chained transaction.
381 assertTestContainerExists(secondReadTx);
384 * We alocate next transaction, which is still based on first one, but
387 DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
390 * We test existence of /test in third transaction container should
391 * still be visible from first one (which is still uncommmited).
393 assertTestContainerExists(thirdDeleteTx);
396 * We delete node in third transaction.
398 thirdDeleteTx.delete(TestModel.TEST_PATH);
401 * third transaction is sealed.
403 DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
406 * We commit first transaction.
409 assertThreePhaseCommit(firstWriteTxCohort);
411 // Alocates store transacion
412 DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
414 * We verify transaction is commited to store, container should exists
417 assertTestContainerExists(storeReadTx);
419 * We commit third transaction
422 assertThreePhaseCommit(thirdDeleteTxCohort);
427 public void testTransactionConflict() throws InterruptedException, ExecutionException {
428 DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
429 DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
430 assertTestContainerWrite(txOne);
431 assertTestContainerWrite(txTwo);
434 * Commits transaction
436 assertThreePhaseCommit(txOne.ready());
439 * Asserts that txTwo could not be commited
441 assertFalse(txTwo.ready().canCommit().get());
444 private static void assertThreePhaseCommit(final DOMStoreThreePhaseCommitCohort cohort)
445 throws InterruptedException, ExecutionException {
446 assertTrue(cohort.canCommit().get().booleanValue());
447 cohort.preCommit().get();
448 cohort.commit().get();
451 private static Optional<NormalizedNode<?, ?>> assertTestContainerWrite(final DOMStoreReadWriteTransaction writeTx)
452 throws InterruptedException, ExecutionException {
455 * Writes /test in writeTx
458 writeTx.write(TestModel.TEST_PATH, ImmutableNodes.containerNode(TestModel.TEST_QNAME));
460 return assertTestContainerExists(writeTx);
464 * Reads /test from readTx Read should return container.
466 private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(final DOMStoreReadTransaction readTx)
467 throws InterruptedException, ExecutionException {
469 ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
470 assertTrue(writeTxContainer.get().isPresent());
471 return writeTxContainer.get();