use of org.springframework.transaction.ReactiveTransactionManager in project spring-framework by spring-projects.
the class AbstractReactiveTransactionAspectTests method transactionShouldSucceed.
/**
* Check that a transaction is created and committed.
*/
@Test
public void transactionShouldSucceed() throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute();
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(getNameMethod, txatt);
ReactiveTransaction status = mock(ReactiveTransaction.class);
ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
// expect a transaction
given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
given(rtm.commit(status)).willReturn(Mono.empty());
DefaultTestBean tb = new DefaultTestBean();
TestBean itb = (TestBean) advised(tb, rtm, tas);
itb.getName().as(StepVerifier::create).verifyComplete();
verify(rtm).commit(status);
}
use of org.springframework.transaction.ReactiveTransactionManager in project spring-framework by spring-projects.
the class AbstractReactiveTransactionAspectTests method doTestRollbackOnException.
/**
* Check that the when exception thrown by the target can produce the
* desired behavior with the appropriate transaction attribute.
* @param ex exception to be thrown by the target
* @param shouldRollback whether this should cause a transaction rollback
*/
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
TransactionAttribute txatt = new DefaultTransactionAttribute() {
@Override
public boolean rollbackOn(Throwable t) {
assertThat(t).isSameAs(ex);
return shouldRollback;
}
};
Method m = exceptionalMethod;
MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
tas.register(m, txatt);
ReactiveTransaction status = mock(ReactiveTransaction.class);
ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
// Gets additional call(s) from TransactionControl
given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
TransactionSystemException tex = new TransactionSystemException("system exception");
if (rollbackException) {
if (shouldRollback) {
given(rtm.rollback(status)).willReturn(Mono.error(tex));
} else {
given(rtm.commit(status)).willReturn(Mono.error(tex));
}
} else {
given(rtm.commit(status)).willReturn(Mono.empty());
given(rtm.rollback(status)).willReturn(Mono.empty());
}
DefaultTestBean tb = new DefaultTestBean();
TestBean itb = (TestBean) advised(tb, rtm, tas);
itb.exceptional(ex).as(StepVerifier::create).expectErrorSatisfies(actual -> {
if (rollbackException) {
assertThat(actual).isEqualTo(tex);
} else {
assertThat(actual).isEqualTo(ex);
}
}).verify();
if (!rollbackException) {
if (shouldRollback) {
verify(rtm).rollback(status);
} else {
verify(rtm).commit(status);
}
}
}
use of org.springframework.transaction.ReactiveTransactionManager in project spring-framework by spring-projects.
the class ReactiveTransactionSupportTests method existingTransaction.
@Test
public void existingTransaction() {
ReactiveTransactionManager tm = new ReactiveTestTransactionManager(true, true);
tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getTransaction()).isNotNull();
assertThat(actual.isNewTransaction()).isFalse();
}).verifyComplete();
tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getTransaction()).isNotNull();
assertThat(actual.isNewTransaction()).isFalse();
}).verifyComplete();
tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
assertThat(actual.getTransaction()).isNotNull();
assertThat(actual.isNewTransaction()).isFalse();
}).verifyComplete();
}
Aggregations