Search in sources :

Example 26 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.

the class R2dbcTransactionManagerUnitTests method testRollbackFails.

@Test
@SuppressWarnings("unchecked")
void testRollbackFails() {
    when(connectionMock.rollbackTransaction()).thenReturn(Mono.defer(() -> Mono.error(new R2dbcBadGrammarException("Commit should fail"))), Mono.empty());
    TransactionalOperator operator = TransactionalOperator.create(tm);
    operator.execute(reactiveTransaction -> {
        reactiveTransaction.setRollbackOnly();
        return ConnectionFactoryUtils.getConnection(connectionFactoryMock).doOnNext(connection -> connection.createStatement("foo")).then();
    }).as(StepVerifier::create).verifyError(IllegalTransactionStateException.class);
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).createStatement("foo");
    verify(connectionMock, never()).commitTransaction();
    verify(connectionMock).rollbackTransaction();
    verify(connectionMock).close();
    verifyNoMoreInteractions(connectionMock);
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) StepVerifier(reactor.test.StepVerifier) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) ArgumentMatchers.anyBoolean(org.mockito.ArgumentMatchers.anyBoolean) BDDMockito.when(org.mockito.BDDMockito.when) BDDMockito.verify(org.mockito.BDDMockito.verify) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) TransactionSynchronization(org.springframework.transaction.reactive.TransactionSynchronization) BDDMockito.never(org.mockito.BDDMockito.never) R2dbcBadGrammarException(io.r2dbc.spi.R2dbcBadGrammarException) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) Mono(reactor.core.publisher.Mono) BDDMockito.verifyNoMoreInteractions(org.mockito.BDDMockito.verifyNoMoreInteractions) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) TransactionSynchronizationManager(org.springframework.transaction.reactive.TransactionSynchronizationManager) IsolationLevel(io.r2dbc.spi.IsolationLevel) Assertions.fail(org.assertj.core.api.Assertions.fail) BDDMockito.mock(org.mockito.BDDMockito.mock) Statement(io.r2dbc.spi.Statement) BDDMockito.reset(org.mockito.BDDMockito.reset) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) R2dbcBadGrammarException(io.r2dbc.spi.R2dbcBadGrammarException) Test(org.junit.jupiter.api.Test)

Example 27 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-framework by spring-projects.

the class TransactionAwareConnectionFactoryProxyUnitTests method shouldEmitBoundConnection.

@Test
void shouldEmitBoundConnection() {
    when(connectionMock1.beginTransaction()).thenReturn(Mono.empty());
    when(connectionMock1.commitTransaction()).thenReturn(Mono.empty());
    when(connectionMock1.close()).thenReturn(Mono.empty());
    TransactionalOperator rxtx = TransactionalOperator.create(tm);
    AtomicReference<Connection> transactionalConnection = new AtomicReference<>();
    TransactionAwareConnectionFactoryProxy proxyCf = new TransactionAwareConnectionFactoryProxy(connectionFactoryMock);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).doOnNext(transactionalConnection::set).flatMap(connection -> proxyCf.create().doOnNext(wrappedConnection -> assertThat(((Wrapped<?>) wrappedConnection).unwrap()).isSameAs(connection))).as(rxtx::transactional).flatMapMany(Connection::close).as(StepVerifier::create).verifyComplete();
    verifyNoInteractions(connectionMock2);
    verifyNoInteractions(connectionMock3);
    verify(connectionFactoryMock, times(1)).create();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) Assertions.assertThatIllegalStateException(org.assertj.core.api.Assertions.assertThatIllegalStateException) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) BDDMockito.times(org.mockito.BDDMockito.times) Mono(reactor.core.publisher.Mono) AtomicReference(java.util.concurrent.atomic.AtomicReference) BDDMockito.when(org.mockito.BDDMockito.when) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) BDDMockito.verify(org.mockito.BDDMockito.verify) Wrapped(io.r2dbc.spi.Wrapped) BDDMockito.mock(org.mockito.BDDMockito.mock) BDDMockito.verifyNoInteractions(org.mockito.BDDMockito.verifyNoInteractions) Wrapped(io.r2dbc.spi.Wrapped) Connection(io.r2dbc.spi.Connection) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.jupiter.api.Test)

Example 28 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.

the class ReactiveFirestoreTransactionManagerTest method triggerCommitCorrectly.

@Test
public void triggerCommitCorrectly() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    template.findById(Mono.just("e1"), FirestoreTemplateTests.TestEntity.class).concatWith(template.findById(Mono.just("e2"), FirestoreTemplateTests.TestEntity.class)).as(operator::transactional).as(StepVerifier::create).expectNext(new FirestoreTemplateTests.TestEntity("e1", 100L), new FirestoreTemplateTests.TestEntity("e2", 100L)).verifyComplete();
    verify(this.firestoreStub).beginTransaction(any(), any());
    verify(this.firestoreStub).commit(any(), any());
    GetDocumentRequest request1 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e1").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request1), any());
    GetDocumentRequest request2 = GetDocumentRequest.newBuilder().setName(this.parent + "/testEntities/" + "e2").setTransaction(ByteString.copyFromUtf8("transaction1")).build();
    verify(this.firestoreStub, times(1)).getDocument(eq(request2), any());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) StepVerifier(reactor.test.StepVerifier) GetDocumentRequest(com.google.firestore.v1.GetDocumentRequest) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Test(org.junit.Test)

Example 29 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.

the class ReactiveFirestoreTransactionManagerTest method triggerRollbackCorrectly.

@Test
public void triggerRollbackCorrectly() {
    FirestoreTemplate template = getFirestoreTemplate();
    ReactiveFirestoreTransactionManager txManager = new ReactiveFirestoreTransactionManager(this.firestoreStub, this.parent);
    TransactionalOperator operator = TransactionalOperator.create(txManager);
    template.findById(Mono.defer(() -> {
        throw new FirestoreDataException("BOOM!");
    }), FirestoreTemplateTests.TestEntity.class).as(operator::transactional).as(StepVerifier::create).expectError().verify();
    verify(this.firestoreStub, times(1)).beginTransaction(any(), any());
    verify(this.firestoreStub, times(0)).commit(any(), any());
    verify(this.firestoreStub, times(1)).rollback(any(), any());
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) FirestoreDataException(org.springframework.cloud.gcp.data.firestore.FirestoreDataException) FirestoreTemplateTests(org.springframework.cloud.gcp.data.firestore.FirestoreTemplateTests) StepVerifier(reactor.test.StepVerifier) FirestoreTemplate(org.springframework.cloud.gcp.data.firestore.FirestoreTemplate) Test(org.junit.Test)

Example 30 with TransactionalOperator

use of org.springframework.transaction.reactive.TransactionalOperator in project spring-cloud-gcp by spring-cloud.

the class FirestoreRepositoryIntegrationTests method transactionalOperatorTest.

// end::repository_built_in[]
@Test
public void transactionalOperatorTest() {
    // tag::repository_transactional_operator[]
    DefaultTransactionDefinition transactionDefinition = new DefaultTransactionDefinition();
    transactionDefinition.setReadOnly(false);
    TransactionalOperator operator = TransactionalOperator.create(this.txManager, transactionDefinition);
    // end::repository_transactional_operator[]
    // tag::repository_operations_in_a_transaction[]
    User alice = new User("Alice", 29);
    User bob = new User("Bob", 60);
    this.userRepository.save(alice).then(this.userRepository.save(bob)).as(operator::transactional).block();
    this.userRepository.findAll().flatMap(a -> {
        a.setAge(a.getAge() - 1);
        return this.userRepository.save(a);
    }).as(operator::transactional).collectList().block();
    assertThat(this.userRepository.findAll().map(User::getAge).collectList().block()).containsExactlyInAnyOrder(28, 59);
// end::repository_operations_in_a_transaction[]
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) IntStream(java.util.stream.IntStream) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Arrays(java.util.Arrays) Order(org.springframework.data.domain.Sort.Order) Assume.assumeThat(org.junit.Assume.assumeThat) BeforeClass(org.junit.BeforeClass) UserRepository(org.springframework.cloud.gcp.data.firestore.entities.UserRepository) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) RunWith(org.junit.runner.RunWith) Autowired(org.springframework.beans.factory.annotation.Autowired) Sort(org.springframework.data.domain.Sort) SpringRunner(org.springframework.test.context.junit4.SpringRunner) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) Before(org.junit.Before) User(org.springframework.cloud.gcp.data.firestore.entities.User) TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) PageRequest(org.springframework.data.domain.PageRequest) Test(org.junit.Test) Mono(reactor.core.publisher.Mono) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) Flux(reactor.core.publisher.Flux) List(java.util.List) Address(org.springframework.cloud.gcp.data.firestore.entities.User.Address) ReactiveFirestoreTransactionManager(org.springframework.cloud.gcp.data.firestore.transaction.ReactiveFirestoreTransactionManager) ContextConfiguration(org.springframework.test.context.ContextConfiguration) Matchers.is(org.hamcrest.Matchers.is) Collections(java.util.Collections) Mockito.reset(org.mockito.Mockito.reset) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) User(org.springframework.cloud.gcp.data.firestore.entities.User) Test(org.junit.Test)

Aggregations

TransactionalOperator (org.springframework.transaction.reactive.TransactionalOperator)30 Test (org.junit.jupiter.api.Test)25 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)23 StepVerifier (reactor.test.StepVerifier)20 Mono (reactor.core.publisher.Mono)14 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)9 BeforeEach (org.junit.jupiter.api.BeforeEach)8 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)8 TransactionDefinition (org.springframework.transaction.TransactionDefinition)7 TransactionSynchronizationManager (org.springframework.transaction.reactive.TransactionSynchronizationManager)7 Connection (io.r2dbc.spi.Connection)6 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)6 R2dbcBadGrammarException (io.r2dbc.spi.R2dbcBadGrammarException)6 Statement (io.r2dbc.spi.Statement)6 ClientSession (com.mongodb.reactivestreams.client.ClientSession)5 MongoDatabase (com.mongodb.reactivestreams.client.MongoDatabase)5 ServerSession (com.mongodb.session.ServerSession)5 IsolationLevel (io.r2dbc.spi.IsolationLevel)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Assertions.fail (org.assertj.core.api.Assertions.fail)5