Search in sources :

Example 71 with StepVerifier

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

the class DefaultDatabaseClientUnitTests method selectShouldEmitFirstValue.

@Test
void selectShouldEmitFirstValue() {
    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)).first().as(StepVerifier::create).expectNext("Walter").verifyComplete();
}
Also used : MockResult(io.r2dbc.spi.test.MockResult) MockRowMetadata(io.r2dbc.spi.test.MockRowMetadata) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 72 with StepVerifier

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

the class DefaultDatabaseClientUnitTests method rowsUpdatedShouldEmitSingleValue.

@Test
@SuppressWarnings("unchecked")
void rowsUpdatedShouldEmitSingleValue() {
    Result result = mock(Result.class);
    when(result.getRowsUpdated()).thenReturn(Mono.empty(), Mono.just(2), Flux.just(1, 2, 3));
    mockStatementFor("DROP TABLE tab;", result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    databaseClient.sql("DROP TABLE tab;").fetch().rowsUpdated().as(StepVerifier::create).expectNextCount(1).verifyComplete();
}
Also used : StepVerifier(reactor.test.StepVerifier) Result(io.r2dbc.spi.Result) MockResult(io.r2dbc.spi.test.MockResult) Test(org.junit.jupiter.api.Test)

Example 73 with StepVerifier

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

the class DefaultDatabaseClientUnitTests method shouldApplyExecuteFunction.

@Test
void shouldApplyExecuteFunction() {
    Statement statement = mockStatement();
    MockResult result = mockSingleColumnResult(MockRow.builder().identified(0, Object.class, "Walter"));
    DatabaseClient databaseClient = databaseClientBuilder.executeFunction(stmnt -> Mono.just(result)).build();
    databaseClient.sql("SELECT").fetch().all().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    verifyNoInteractions(statement);
}
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) Statement(io.r2dbc.spi.Statement) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 74 with StepVerifier

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

the class AbstractReactiveTransactionAspectTests method cannotCreateTransaction.

/**
 * Simulate a transaction infrastructure failure.
 * Shouldn't invoke target method.
 */
@Test
public void cannotCreateTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = getNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
    // Expect a transaction
    CannotCreateTransactionException ex = new CannotCreateTransactionException("foobar", null);
    given(rtm.getReactiveTransaction(txatt)).willThrow(ex);
    DefaultTestBean tb = new DefaultTestBean() {

        @Override
        public Mono<String> getName() {
            throw new UnsupportedOperationException("Shouldn't have invoked target method when couldn't create transaction for transactional method");
        }
    };
    TestBean itb = (TestBean) advised(tb, rtm, tas);
    itb.getName().as(StepVerifier::create).expectError(CannotCreateTransactionException.class).verify();
}
Also used : CannotCreateTransactionException(org.springframework.transaction.CannotCreateTransactionException) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Method(java.lang.reflect.Method) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 75 with StepVerifier

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

the class AbstractReactiveTransactionAspectTests method cannotCommitTransaction.

/**
 * Simulate failure of the underlying transaction infrastructure to commit.
 * Check that the target method was invoked, but that the transaction
 * infrastructure exception was thrown to the client
 */
@Test
public void cannotCommitTransaction() throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute();
    Method m = setNameMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    // Method m2 = getNameMethod;
    // No attributes for m2
    ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
    ReactiveTransaction status = mock(ReactiveTransaction.class);
    given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
    UnexpectedRollbackException ex = new UnexpectedRollbackException("foobar", null);
    given(rtm.commit(status)).willReturn(Mono.error(ex));
    given(rtm.rollback(status)).willReturn(Mono.empty());
    DefaultTestBean tb = new DefaultTestBean();
    TestBean itb = (TestBean) advised(tb, rtm, tas);
    String name = "new name";
    Mono.from(itb.setName(name)).as(StepVerifier::create).consumeErrorWith(throwable -> {
        assertThat(throwable.getClass()).isEqualTo(RuntimeException.class);
        assertThat(throwable.getCause()).isEqualTo(ex);
    }).verify();
    // Should have invoked target and changed name
    itb.getName().as(StepVerifier::create).expectNext(name).verifyComplete();
}
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) Method(java.lang.reflect.Method) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) UnexpectedRollbackException(org.springframework.transaction.UnexpectedRollbackException) StepVerifier(reactor.test.StepVerifier) 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