Search in sources :

Example 1 with Result

use of io.r2dbc.spi.Result in project spring-boot by spring-projects.

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenDatabaseUpWithSuccessValidationQuery.

@Test
void healthIndicatorWhenDatabaseUpWithSuccessValidationQuery() {
    CloseableConnectionFactory connectionFactory = createTestDatabase();
    try {
        String customValidationQuery = "SELECT COUNT(*) from HEALTH_TEST";
        String createTableStatement = "CREATE TABLE HEALTH_TEST (id INTEGER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY)";
        Mono.from(connectionFactory.create()).flatMapMany((it) -> Flux.from(it.createStatement(createTableStatement).execute()).flatMap(Result::getRowsUpdated).thenMany(it.close())).as(StepVerifier::create).verifyComplete();
        ReactiveHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory, customValidationQuery);
        healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
            assertThat(actual.getStatus()).isEqualTo(Status.UP);
            assertThat(actual.getDetails()).containsOnly(entry("database", "H2"), entry("result", 0L), entry("validationQuery", customValidationQuery));
        }).verifyComplete();
    } finally {
        StepVerifier.create(connectionFactory.close()).verifyComplete();
    }
}
Also used : ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Status(org.springframework.boot.actuate.health.Status) StepVerifier(reactor.test.StepVerifier) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) H2ConnectionOption(io.r2dbc.h2.H2ConnectionOption) Mono(reactor.core.publisher.Mono) UUID(java.util.UUID) ReactiveHealthIndicator(org.springframework.boot.actuate.health.ReactiveHealthIndicator) Assertions.entry(org.assertj.core.api.Assertions.entry) Connection(io.r2dbc.spi.Connection) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) BDDMockito.given(org.mockito.BDDMockito.given) Result(io.r2dbc.spi.Result) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) ValidationDepth(io.r2dbc.spi.ValidationDepth) Collections(java.util.Collections) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) StepVerifier(reactor.test.StepVerifier) Result(io.r2dbc.spi.Result) Test(org.junit.jupiter.api.Test)

Example 2 with Result

use of io.r2dbc.spi.Result in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method selectShouldEmitAllValues.

@Test
void selectShouldEmitAllValues() {
    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)).all().as(StepVerifier::create).expectNext("Walter").expectNext("White").verifyComplete();
}
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 3 with Result

use of io.r2dbc.spi.Result in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method shouldApplyStatementFilterFunctions.

@Test
void shouldApplyStatementFilterFunctions() {
    MockResult result = MockResult.builder().build();
    Statement statement = mockStatement(result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT").filter((s, next) -> next.execute(s.returnGeneratedValues("foo"))).filter((s, next) -> next.execute(s.returnGeneratedValues("bar"))).fetch().all().as(StepVerifier::create).verifyComplete();
    InOrder inOrder = inOrder(statement);
    inOrder.verify(statement).returnGeneratedValues("foo");
    inOrder.verify(statement).returnGeneratedValues("bar");
    inOrder.verify(statement).execute();
    inOrder.verifyNoMoreInteractions();
}
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) InOrder(org.mockito.InOrder) Statement(io.r2dbc.spi.Statement) Test(org.junit.jupiter.api.Test)

Example 4 with Result

use of io.r2dbc.spi.Result in project spring-framework by spring-projects.

the class DefaultDatabaseClientUnitTests method shouldApplySimpleStatementFilterFunctions.

@Test
void shouldApplySimpleStatementFilterFunctions() {
    MockResult result = mockSingleColumnEmptyResult();
    Statement statement = mockStatement(result);
    DatabaseClient databaseClient = databaseClientBuilder.build();
    databaseClient.sql("SELECT").filter(s -> s.returnGeneratedValues("foo")).filter(s -> s.returnGeneratedValues("bar")).fetch().all().as(StepVerifier::create).verifyComplete();
    InOrder inOrder = inOrder(statement);
    inOrder.verify(statement).returnGeneratedValues("foo");
    inOrder.verify(statement).returnGeneratedValues("bar");
    inOrder.verify(statement).execute();
    inOrder.verifyNoMoreInteractions();
}
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) InOrder(org.mockito.InOrder) Statement(io.r2dbc.spi.Statement) Test(org.junit.jupiter.api.Test)

Example 5 with Result

use of io.r2dbc.spi.Result in project jOOQ by jOOQ.

the class R2DBC method block.

@SuppressWarnings("unchecked")
static final <T> T block(Publisher<? extends T> publisher) {
    Object complete = new Object();
    LinkedBlockingQueue<Object> queue = new LinkedBlockingQueue<>();
    publisher.subscribe(subscriber(s -> s.request(1), queue::add, queue::add, () -> queue.add(complete)));
    try {
        Object result = queue.take();
        if (result instanceof Throwable)
            throw new DataAccessException("Exception when blocking on publisher", (Throwable) result);
        else if (result == complete)
            return null;
        else
            return (T) result;
    } catch (InterruptedException e) {
        throw new DataAccessException("Exception when blocking on publisher", e);
    }
}
Also used : Time(java.sql.Time) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) BiFunction(java.util.function.BiFunction) Batch(io.r2dbc.spi.Batch) ByteBuffer(java.nio.ByteBuffer) Array(java.sql.Array) BigDecimal(java.math.BigDecimal) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DefaultResultSet(org.jooq.tools.jdbc.DefaultResultSet) LocalTime(java.time.LocalTime) SQLType(java.sql.SQLType) Result(io.r2dbc.spi.Result) DataTypeException(org.jooq.exception.DataTypeException) SQLDialect(org.jooq.SQLDialect) DataType(org.jooq.DataType) JooqLogger(org.jooq.tools.JooqLogger) JDBCUtils.safeClose(org.jooq.tools.jdbc.JDBCUtils.safeClose) Timestamp(java.sql.Timestamp) Builder(io.r2dbc.spi.ConnectionFactoryOptions.Builder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Field(org.jooq.Field) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Cursor(org.jooq.Cursor) Option(io.r2dbc.spi.Option) Connection(io.r2dbc.spi.Connection) LocalDate(java.time.LocalDate) StringUtils.defaultIfNull(org.jooq.tools.StringUtils.defaultIfNull) Query(org.jooq.Query) Internal.subscriber(org.jooq.impl.Internal.subscriber) Guard(org.jooq.impl.ThreadGuard.Guard) ResultSetMetaData(java.sql.ResultSetMetaData) Types(java.sql.Types) DefaultPreparedStatement(org.jooq.tools.jdbc.DefaultPreparedStatement) ConnectionFactories(io.r2dbc.spi.ConnectionFactories) Row(io.r2dbc.spi.Row) XML(org.jooq.XML) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Tools.visitAll(org.jooq.impl.Tools.visitAll) LocalDateTime(java.time.LocalDateTime) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Tools.recordFactory(org.jooq.impl.Tools.recordFactory) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) BindingGetResultSetContext(org.jooq.BindingGetResultSetContext) ConcurrentMap(java.util.concurrent.ConcurrentMap) RowMetadata(io.r2dbc.spi.RowMetadata) SQLException(java.sql.SQLException) ColumnMetadata(io.r2dbc.spi.ColumnMetadata) EMPTY_PARAM(org.jooq.impl.Tools.EMPTY_PARAM) Tools.abstractDMLQuery(org.jooq.impl.Tools.abstractDMLQuery) Subscriber(org.reactivestreams.Subscriber) MockArray(org.jooq.tools.jdbc.MockArray) Record(org.jooq.Record) DataAccessException(org.jooq.exception.DataAccessException) Properties(java.util.Properties) Tools.translate(org.jooq.impl.Tools.translate) SettingsTools(org.jooq.conf.SettingsTools) Publisher(org.reactivestreams.Publisher) JSON(org.jooq.JSON) Settings(org.jooq.conf.Settings) Tools.abstractResultQuery(org.jooq.impl.Tools.abstractResultQuery) Converter(org.jooq.Converter) Date(java.sql.Date) Consumer(java.util.function.Consumer) Param(org.jooq.Param) AtomicLong(java.util.concurrent.atomic.AtomicLong) Configuration(org.jooq.Configuration) NAMED(org.jooq.conf.ParamType.NAMED) Tools.fields(org.jooq.impl.Tools.fields) Rendered(org.jooq.impl.DefaultRenderContext.Rendered) Subscription(org.reactivestreams.Subscription) JSONB(org.jooq.JSONB) Statement(io.r2dbc.spi.Statement) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) DataAccessException(org.jooq.exception.DataAccessException)

Aggregations

Result (io.r2dbc.spi.Result)9 Connection (io.r2dbc.spi.Connection)7 Test (org.junit.jupiter.api.Test)7 StepVerifier (reactor.test.StepVerifier)7 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)6 Statement (io.r2dbc.spi.Statement)6 MockResult (io.r2dbc.spi.test.MockResult)6 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)6 BeforeEach (org.junit.jupiter.api.BeforeEach)6 Publisher (org.reactivestreams.Publisher)6 Subscription (org.reactivestreams.Subscription)6 Flux (reactor.core.publisher.Flux)6 Mono (reactor.core.publisher.Mono)6 MockColumnMetadata (io.r2dbc.spi.test.MockColumnMetadata)5 MockRow (io.r2dbc.spi.test.MockRow)5 MockRowMetadata (io.r2dbc.spi.test.MockRowMetadata)5 Arrays (java.util.Arrays)5 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 ArgumentMatchers.eq (org.mockito.ArgumentMatchers.eq)5