import com.google.common.base.Optional;
import com.google.common.util.concurrent.ListenableFuture;
import com.google.common.util.concurrent.MoreExecutors;
-import java.io.IOException;
import java.util.concurrent.ExecutionException;
import org.junit.Before;
import org.junit.Ignore;
import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
import org.opendaylight.yangtools.yang.data.impl.schema.builder.impl.ImmutableContainerNodeBuilder;
import org.opendaylight.yangtools.yang.model.api.SchemaContext;
-import org.opendaylight.yangtools.yang.model.parser.api.YangSyntaxErrorException;
-import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
public class InMemoryDataStoreTest {
private InMemoryDOMDataStore domStore;
@Before
- public void setupStore() throws IOException, YangSyntaxErrorException, ReactorException {
+ public void setupStore() throws Exception {
domStore = new InMemoryDOMDataStore("TEST", MoreExecutors.newDirectExecutorService());
schemaContext = TestModel.createTestContext();
domStore.onGlobalContextUpdated(schemaContext);
assertNotNull(domStore);
- DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
assertNotNull(readTx);
- DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
assertNotNull(writeTx);
/**
* Writes /test in writeTx.
*/
- NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
+ final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
writeTx.write(TestModel.TEST_PATH, testNode);
/**
* Reads /test from writeTx Read should return container.
*/
- ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+ final ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
assertEquals("read: data", testNode, writeTxContainer.get().get());
/**
* Reads /test from readTx Read should return Absent.
*/
- ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
+ final ListenableFuture<Optional<NormalizedNode<?, ?>>> readTxContainer = readTx.read(TestModel.TEST_PATH);
assertEquals("read: isPresent", false, readTxContainer.get().isPresent());
}
@Test
public void testTransactionCommit() throws InterruptedException, ExecutionException {
- DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
assertNotNull(writeTx);
/**
* Writes /test in writeTx.
*/
- NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
+ final NormalizedNode<?, ?> testNode = ImmutableNodes.containerNode(TestModel.TEST_QNAME);
writeTx.write(TestModel.TEST_PATH, testNode);
/**
* Reads /test from writeTx Read should return container.
*/
- ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
+ final ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = writeTx.read(TestModel.TEST_PATH);
assertEquals("read: isPresent", true, writeTxContainer.get().isPresent());
assertEquals("read: data", testNode, writeTxContainer.get().get());
- DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
+ final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
assertThreePhaseCommit(cohort);
- Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
- .get();
+ final Optional<NormalizedNode<?, ?>> afterCommitRead =
+ domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
assertEquals("After commit read: isPresent", true, afterCommitRead.isPresent());
assertEquals("After commit read: data", testNode, afterCommitRead.get());
}
@Test
public void testExistsForExistingData() throws Exception {
- DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
assertNotNull(writeTx);
- ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
+ final ContainerNode containerNode = ImmutableContainerNodeBuilder.create()
.withNodeIdentifier(new NodeIdentifier(TestModel.TEST_QNAME))
.addChild(ImmutableNodes.mapNodeBuilder(TestModel.OUTER_LIST_QNAME)
.addChild(ImmutableNodes.mapEntry(TestModel.OUTER_LIST_QNAME,
assertEquals(Boolean.TRUE, exists.get());
- DOMStoreThreePhaseCommitCohort ready = writeTx.ready();
+ final DOMStoreThreePhaseCommitCohort ready = writeTx.ready();
ready.preCommit().get();
ready.commit().get();
- DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
assertNotNull(readTx);
exists =
@Test
public void testExistsForNonExistingData() throws Exception {
- DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
assertNotNull(writeTx);
ListenableFuture<Boolean> exists = writeTx.exists(TestModel.TEST_PATH);
assertEquals(Boolean.FALSE, exists.get());
- DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
assertNotNull(readTx);
exists =
@Test(expected = ReadFailedException.class)
public void testExistsThrowsReadFailedException() throws Exception {
- DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
assertNotNull(readTx);
readTx.close();
@Test(expected = ReadFailedException.class)
public void testReadWithReadOnlyTransactionClosed() throws Throwable {
- DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadOnlyTransaction();
assertNotNull(readTx);
readTx.close();
@Test(expected = ReadFailedException.class)
public void testReadWithReadOnlyTransactionFailure() throws Throwable {
- DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
+ final DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
Mockito.doThrow(new RuntimeException("mock ex")).when(mockSnapshot)
.readNode(Mockito.any(YangInstanceIdentifier.class));
- DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadTransaction("1", true, mockSnapshot);
+ final DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadTransaction("1", true, mockSnapshot);
doReadAndThrowEx(readTx);
}
@Test(expected = ReadFailedException.class)
public void testReadWithReadWriteTransactionClosed() throws Throwable {
- DOMStoreReadTransaction readTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadTransaction readTx = domStore.newReadWriteTransaction();
assertNotNull(readTx);
readTx.close();
@Test(expected = ReadFailedException.class)
public void testReadWithReadWriteTransactionFailure() throws Throwable {
- DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
- DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
+ final DataTreeSnapshot mockSnapshot = Mockito.mock(DataTreeSnapshot.class);
+ final DataTreeModification mockModification = Mockito.mock(DataTreeModification.class);
Mockito.doThrow(new RuntimeException("mock ex")).when(mockModification)
.readNode(Mockito.any(YangInstanceIdentifier.class));
Mockito.doReturn(mockModification).when(mockSnapshot).newModification();
@SuppressWarnings("unchecked")
- TransactionReadyPrototype<String> mockReady = Mockito.mock(TransactionReadyPrototype.class);
- DOMStoreReadTransaction readTx = SnapshotBackedTransactions.newReadWriteTransaction(
- "1", false, mockSnapshot, mockReady);
+ final TransactionReadyPrototype<String> mockReady = Mockito.mock(TransactionReadyPrototype.class);
+ final DOMStoreReadTransaction readTx =
+ SnapshotBackedTransactions.newReadWriteTransaction("1", false, mockSnapshot, mockReady);
doReadAndThrowEx(readTx);
}
private static void doReadAndThrowEx(final DOMStoreReadTransaction readTx) throws Throwable {
try {
readTx.read(TestModel.TEST_PATH).get();
- } catch (ExecutionException e) {
+ } catch (final ExecutionException e) {
throw e.getCause();
}
}
@Test(expected = IllegalStateException.class)
public void testWriteWithTransactionReady() throws Exception {
- DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
+ final DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
writeTx.ready();
@Test(expected = IllegalStateException.class)
public void testReadyWithTransactionAlreadyReady() throws Exception {
- DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
+ final DOMStoreWriteTransaction writeTx = domStore.newWriteOnlyTransaction();
writeTx.ready();
@Test
public void testTransactionAbort() throws InterruptedException, ExecutionException {
- DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction writeTx = domStore.newReadWriteTransaction();
assertNotNull(writeTx);
assertTestContainerWrite(writeTx);
- DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
+ final DOMStoreThreePhaseCommitCohort cohort = writeTx.ready();
assertTrue(cohort.canCommit().get().booleanValue());
cohort.preCommit().get();
cohort.abort().get();
- Optional<NormalizedNode<?, ?>> afterCommitRead = domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH)
- .get();
+ final Optional<NormalizedNode<?, ?>> afterCommitRead =
+ domStore.newReadOnlyTransaction().read(TestModel.TEST_PATH).get();
assertFalse(afterCommitRead.isPresent());
}
@Test
public void testTransactionChain() throws InterruptedException, ExecutionException {
- DOMStoreTransactionChain txChain = domStore.createTransactionChain();
+ final DOMStoreTransactionChain txChain = domStore.createTransactionChain();
assertNotNull(txChain);
/**
* We alocate new read-write transaction and write /test.
*/
- DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction firstTx = txChain.newReadWriteTransaction();
assertTestContainerWrite(firstTx);
/**
* We alocate chained transaction - read transaction, note first one is
* still not commited to datastore.
*/
- DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
+ final DOMStoreReadTransaction secondReadTx = txChain.newReadOnlyTransaction();
/**
* We test if we are able to read data from tx, read should not fail
* We alocate next transaction, which is still based on first one, but
* is read-write.
*/
- DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction thirdDeleteTx = txChain.newReadWriteTransaction();
/**
* We test existence of /test in third transaction container should
/**
* third transaction is sealed.
*/
- DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
+ final DOMStoreThreePhaseCommitCohort thirdDeleteTxCohort = thirdDeleteTx.ready();
/**
* We commit first transaction.
assertThreePhaseCommit(firstWriteTxCohort);
// Alocates store transacion
- DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
+ final DOMStoreReadTransaction storeReadTx = domStore.newReadOnlyTransaction();
/**
* We verify transaction is commited to store, container should exists
* in datastore.
@Test
@Ignore
public void testTransactionConflict() throws InterruptedException, ExecutionException {
- DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
- DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction txOne = domStore.newReadWriteTransaction();
+ final DOMStoreReadWriteTransaction txTwo = domStore.newReadWriteTransaction();
assertTestContainerWrite(txOne);
assertTestContainerWrite(txTwo);
private static Optional<NormalizedNode<?, ?>> assertTestContainerExists(final DOMStoreReadTransaction readTx)
throws InterruptedException, ExecutionException {
- ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
+ final ListenableFuture<Optional<NormalizedNode<?, ?>>> writeTxContainer = readTx.read(TestModel.TEST_PATH);
assertTrue(writeTxContainer.get().isPresent());
return writeTxContainer.get();
}