Search in sources :

Example 66 with StepVerifier

use of reactor.test.StepVerifier in project spring-framework by spring-projects.

the class R2dbcTransactionManagerUnitTests method testRollback.

@Test
void testRollback() {
    AtomicInteger commits = new AtomicInteger();
    when(connectionMock.commitTransaction()).thenReturn(Mono.fromRunnable(commits::incrementAndGet));
    AtomicInteger rollbacks = new AtomicInteger();
    when(connectionMock.rollbackTransaction()).thenReturn(Mono.fromRunnable(rollbacks::incrementAndGet));
    TransactionalOperator operator = TransactionalOperator.create(tm);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).doOnNext(connection -> {
        throw new IllegalStateException();
    }).as(operator::transactional).as(StepVerifier::create).verifyError(IllegalStateException.class);
    assertThat(commits).hasValue(0);
    assertThat(rollbacks).hasValue(1);
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Test(org.junit.jupiter.api.Test)

Example 67 with StepVerifier

use of reactor.test.StepVerifier in project spring-framework by spring-projects.

the class AbstractTransactionalDatabaseClientIntegrationTests method shouldRollbackTransactionUsingTransactionalOperator.

@Test
public void shouldRollbackTransactionUsingTransactionalOperator() {
    DatabaseClient databaseClient = DatabaseClient.create(connectionFactory);
    TransactionalOperator transactionalOperator = TransactionalOperator.create(new R2dbcTransactionManager(connectionFactory), new DefaultTransactionDefinition());
    Flux<Integer> integerFlux = databaseClient.sql(getInsertIntoLegosetStatement()).bind(0, 42055).bind(1, "SCHAUFELRADBAGGER").bindNull(2, Integer.class).fetch().rowsUpdated().thenMany(Mono.fromSupplier(() -> {
        throw new IllegalStateException("failed");
    }));
    integerFlux.as(transactionalOperator::transactional).as(StepVerifier::create).expectError(IllegalStateException.class).verify();
    databaseClient.sql("SELECT id FROM legoset").fetch().first().as(StepVerifier::create).verifyComplete();
}
Also used : TransactionalOperator(org.springframework.transaction.reactive.TransactionalOperator) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) R2dbcTransactionManager(org.springframework.r2dbc.connection.R2dbcTransactionManager) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 68 with StepVerifier

use of reactor.test.StepVerifier in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method selectOneShouldFailWithException.

@Test
void selectOneShouldFailWithException() {
    MockRowMetadata metadata = MockRowMetadata.builder().columnMetadata(MockColumnMetadata.builder().name("name").javaType(String.class).build()).build();
    MockResult result = MockResult.builder().row(MockRow.builder().identified(0, Object.class, "Walter").metadata(metadata).build(), MockRow.builder().identified(0, Object.class, "White").metadata(metadata).build()).build();
    mockStatementFor("SELECT * FROM person", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT * FROM person").map(row -> row.get(0)).one().as(StepVerifier::create).verifyError(IncorrectResultSizeDataAccessException.class);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Strictness(org.mockito.quality.Strictness) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) IncorrectResultSizeDataAccessException(org.springframework.dao.IncorrectResultSizeDataAccessException) MockitoSettings(org.mockito.junit.jupiter.MockitoSettings) StepVerifier(reactor.test.StepVerifier) Mock(org.mockito.Mock) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) BDDMockito.inOrder(org.mockito.BDDMockito.inOrder) BDDMockito.times(org.mockito.BDDMockito.times) BDDMockito.when(org.mockito.BDDMockito.when) CoreSubscriber(reactor.core.CoreSubscriber) BDDMockito.verify(org.mockito.BDDMockito.verify) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BindMarkersFactory(org.springframework.r2dbc.core.binding.BindMarkersFactory) Result(io.r2dbc.spi.Result) Nullable(org.springframework.lang.Nullable) BDDMockito.doReturn(org.mockito.BDDMockito.doReturn) BDDMockito.verifyNoInteractions(org.mockito.BDDMockito.verifyNoInteractions) MockRow(io.r2dbc.spi.test.MockRow) BindTarget(org.springframework.r2dbc.core.binding.BindTarget) MockResult(io.r2dbc.spi.test.MockResult) MockRowMetadata(io.r2dbc.spi.test.MockRowMetadata) MockitoExtension(org.mockito.junit.jupiter.MockitoExtension) InOrder(org.mockito.InOrder) Publisher(org.reactivestreams.Publisher) Mono(reactor.core.publisher.Mono) BDDMockito.verifyNoMoreInteractions(org.mockito.BDDMockito.verifyNoMoreInteractions) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) Subscription(org.reactivestreams.Subscription) MockColumnMetadata(io.r2dbc.spi.test.MockColumnMetadata) BDDMockito.mock(org.mockito.BDDMockito.mock) Statement(io.r2dbc.spi.Statement) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) MockResult(io.r2dbc.spi.test.MockResult) MockRowMetadata(io.r2dbc.spi.test.MockRowMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 69 with StepVerifier

use of reactor.test.StepVerifier 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 70 with StepVerifier

use of reactor.test.StepVerifier 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)

Aggregations

StepVerifier (reactor.test.StepVerifier)82 Mono (reactor.core.publisher.Mono)56 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)48 Test (org.junit.jupiter.api.Test)45 Flux (reactor.core.publisher.Flux)41 Test (org.junit.Test)37 Duration (java.time.Duration)31 ClassPathResource (org.springframework.core.io.ClassPathResource)28 AbstractIntegrationTest (org.cloudfoundry.AbstractIntegrationTest)24 Collections (java.util.Collections)20 Mockito.mock (org.mockito.Mockito.mock)20 DefaultTransactionDefinition (org.springframework.transaction.support.DefaultTransactionDefinition)20 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)19 Connection (io.r2dbc.spi.Connection)17 IOException (java.io.IOException)17 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)17 BeforeEach (org.junit.jupiter.api.BeforeEach)16 Autowired (org.springframework.beans.factory.annotation.Autowired)16 Result (io.r2dbc.spi.Result)14 Path (java.nio.file.Path)14