Search in sources :

Example 11 with ReactiveTransactionManager

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);
}
Also used : ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Test(org.junit.jupiter.api.Test)

Example 12 with ReactiveTransactionManager

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);
        }
    }
}
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)

Example 13 with ReactiveTransactionManager

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();
}
Also used : Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Aggregations

ReactiveTransactionManager (org.springframework.transaction.ReactiveTransactionManager)13 Test (org.junit.jupiter.api.Test)11 ReactiveTransaction (org.springframework.transaction.ReactiveTransaction)9 Mono (reactor.core.publisher.Mono)6 Method (java.lang.reflect.Method)4 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)4 Flux (reactor.core.publisher.Flux)4 StepVerifier (reactor.test.StepVerifier)4 Publisher (org.reactivestreams.Publisher)3 CannotCreateTransactionException (org.springframework.transaction.CannotCreateTransactionException)3 IllegalTransactionStateException (org.springframework.transaction.IllegalTransactionStateException)3 TransactionDefinition (org.springframework.transaction.TransactionDefinition)3 TransactionSystemException (org.springframework.transaction.TransactionSystemException)3 UnexpectedRollbackException (org.springframework.transaction.UnexpectedRollbackException)3 Log (org.apache.commons.logging.Log)2 LogFactory (org.apache.commons.logging.LogFactory)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