Search in sources :

Example 6 with ReactiveTransaction

use of org.springframework.transaction.ReactiveTransaction 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);
        }
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) Publisher(org.reactivestreams.Publisher) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) Mockito.times(org.mockito.Mockito.times) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) Fail.fail(org.assertj.core.api.Fail.fail) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Mockito.verify(org.mockito.Mockito.verify) Mockito.verifyNoInteractions(org.mockito.Mockito.verifyNoInteractions) Test(org.junit.jupiter.api.Test) TransactionSystemException(org.springframework.transaction.TransactionSystemException) BDDMockito.given(org.mockito.BDDMockito.given) TransactionContext(org.springframework.transaction.reactive.TransactionContext) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) Method(java.lang.reflect.Method) Mockito.mock(org.mockito.Mockito.mock) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) StepVerifier(reactor.test.StepVerifier)

Aggregations

ReactiveTransaction (org.springframework.transaction.ReactiveTransaction)6 ReactiveTransactionManager (org.springframework.transaction.ReactiveTransactionManager)6 Test (org.junit.jupiter.api.Test)5 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)3 Mono (reactor.core.publisher.Mono)3 Method (java.lang.reflect.Method)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Fail.fail (org.assertj.core.api.Fail.fail)2 BeforeEach (org.junit.jupiter.api.BeforeEach)2 BDDMockito.given (org.mockito.BDDMockito.given)2 Mockito.mock (org.mockito.Mockito.mock)2 Mockito.times (org.mockito.Mockito.times)2 Mockito.verify (org.mockito.Mockito.verify)2 Mockito.verifyNoInteractions (org.mockito.Mockito.verifyNoInteractions)2 Publisher (org.reactivestreams.Publisher)2 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)2 TransactionSystemException (org.springframework.transaction.TransactionSystemException)2 TransactionContext (org.springframework.transaction.reactive.TransactionContext)2 StepVerifier (reactor.test.StepVerifier)2 IOException (java.io.IOException)1