use of org.opendaylight.controller.md.sal.common.api.data.DataStoreUnavailableException in project controller by opendaylight.
the class LegacyDOMDataBrokerAdapterTest method testWriteOnlyTransaction.
@Test
public void testWriteOnlyTransaction() throws Exception {
// Test successful write operations and submit
DOMDataWriteTransaction tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
verify(mockWriteTx).write(TestModel.TEST_PATH, dataNode);
tx.merge(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
verify(mockWriteTx).merge(TestModel.TEST_PATH, dataNode);
tx.delete(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH);
verify(mockWriteTx).delete(TestModel.TEST_PATH);
CheckedFuture<Void, TransactionCommitFailedException> submitFuture = tx.submit();
submitFuture.get(5, TimeUnit.SECONDS);
InOrder inOrder = inOrder(mockCommitCohort);
inOrder.verify(mockCommitCohort).canCommit();
inOrder.verify(mockCommitCohort).preCommit();
inOrder.verify(mockCommitCohort).commit();
// Test cancel
tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
tx.cancel();
verify(mockWriteTx).close();
// Test submit with OptimisticLockFailedException
String errorMsg = "mock OptimisticLockFailedException";
Throwable cause = new ConflictingModificationAppliedException(TestModel.TEST_PATH, "mock");
doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.OptimisticLockFailedException(errorMsg, cause))).when(mockCommitCohort).canCommit();
try {
tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
submitFuture = tx.submit();
submitFuture.checkedGet(5, TimeUnit.SECONDS);
fail("Expected OptimisticLockFailedException");
} catch (OptimisticLockFailedException e) {
assertEquals("getMessage", errorMsg, e.getMessage());
assertEquals("getCause", cause, e.getCause());
}
// Test submit with TransactionCommitFailedException
errorMsg = "mock TransactionCommitFailedException";
cause = new DataValidationFailedException(TestModel.TEST_PATH, "mock");
doReturn(Futures.immediateFailedFuture(new org.opendaylight.mdsal.common.api.TransactionCommitFailedException(errorMsg, cause))).when(mockCommitCohort).canCommit();
try {
tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
submitFuture = tx.submit();
submitFuture.checkedGet(5, TimeUnit.SECONDS);
fail("Expected TransactionCommitFailedException");
} catch (TransactionCommitFailedException e) {
assertEquals("getMessage", errorMsg, e.getMessage());
assertEquals("getCause", cause, e.getCause());
}
// Test submit with DataStoreUnavailableException
errorMsg = "mock NoShardLeaderException";
cause = new NoShardLeaderException("mock");
doReturn(Futures.immediateFailedFuture(cause)).when(mockCommitCohort).canCommit();
try {
tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
submitFuture = tx.submit();
submitFuture.checkedGet(5, TimeUnit.SECONDS);
fail("Expected TransactionCommitFailedException");
} catch (TransactionCommitFailedException e) {
assertEquals("getCause type", DataStoreUnavailableException.class, e.getCause().getClass());
assertEquals("Root cause", cause, e.getCause().getCause());
}
// Test submit with RuntimeException
errorMsg = "mock RuntimeException";
cause = new RuntimeException(errorMsg);
doReturn(Futures.immediateFailedFuture(cause)).when(mockCommitCohort).canCommit();
try {
tx = adapter.newWriteOnlyTransaction();
tx.put(LogicalDatastoreType.CONFIGURATION, TestModel.TEST_PATH, dataNode);
submitFuture = tx.submit();
submitFuture.checkedGet(5, TimeUnit.SECONDS);
fail("Expected TransactionCommitFailedException");
} catch (TransactionCommitFailedException e) {
assertEquals("getCause", cause, e.getCause());
}
}
Aggregations