Search in sources :

Example 11 with ConnectionFactory

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

the class MapConnectionFactoryLookupUnitTests method addingConnectionFactoryPermitsOverride.

@Test
public void addingConnectionFactoryPermitsOverride() {
    Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
    DummyConnectionFactory overriddenConnectionFactory = new DummyConnectionFactory();
    DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
    connectionFactories.put(CONNECTION_FACTORY_NAME, overriddenConnectionFactory);
    MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup();
    lookup.setConnectionFactories(connectionFactories);
    lookup.addConnectionFactory(CONNECTION_FACTORY_NAME, expectedConnectionFactory);
    ConnectionFactory connectionFactory = lookup.getConnectionFactory(CONNECTION_FACTORY_NAME);
    assertThat(connectionFactory).isNotNull().isSameAs(expectedConnectionFactory);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) HashMap(java.util.HashMap) Test(org.junit.jupiter.api.Test)

Example 12 with ConnectionFactory

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

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenDatabaseUpWithFailureValidationQuery.

@Test
void healthIndicatorWhenDatabaseUpWithFailureValidationQuery() {
    CloseableConnectionFactory connectionFactory = createTestDatabase();
    try {
        String invalidValidationQuery = "SELECT COUNT(*) from DOES_NOT_EXIST";
        ReactiveHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory, invalidValidationQuery);
        healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
            assertThat(actual.getStatus()).isEqualTo(Status.DOWN);
            assertThat(actual.getDetails()).contains(entry("database", "H2"), entry("validationQuery", invalidValidationQuery));
            assertThat(actual.getDetails()).containsOnlyKeys("database", "error", "validationQuery");
        }).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) Test(org.junit.jupiter.api.Test)

Example 13 with ConnectionFactory

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

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenDatabaseDownWithConnectionValidation.

@Test
void healthIndicatorWhenDatabaseDownWithConnectionValidation() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.getMetadata()).willReturn(() -> "mock");
    RuntimeException exception = new RuntimeException("test");
    given(connectionFactory.create()).willReturn(Mono.error(exception));
    ConnectionFactoryHealthIndicator healthIndicator = new ConnectionFactoryHealthIndicator(connectionFactory);
    healthIndicator.health().as(StepVerifier::create).assertNext((actual) -> {
        assertThat(actual.getStatus()).isEqualTo(Status.DOWN);
        assertThat(actual.getDetails()).containsOnly(entry("database", "mock"), entry("validationQuery", "validate(REMOTE)"), entry("error", "java.lang.RuntimeException: test"));
    }).verifyComplete();
}
Also used : 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) ConnectionFactory(io.r2dbc.spi.ConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) CloseableConnectionFactory(io.r2dbc.h2.CloseableConnectionFactory) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 14 with ConnectionFactory

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

the class ConnectionFactoryBuilderTests method buildWhenDerivedWithNewCredentialsReturnsNewConnectionFactory.

@Test
void buildWhenDerivedWithNewCredentialsReturnsNewConnectionFactory() {
    ConnectionFactory connectionFactory = ConnectionFactoryBuilder.withUrl(EmbeddedDatabaseConnection.H2.getUrl(UUID.randomUUID().toString())).build();
    ConnectionFactoryOptions initialOptions = ((OptionsCapableConnectionFactory) connectionFactory).getOptions();
    ConnectionFactory derived = ConnectionFactoryBuilder.derivedFrom(connectionFactory).username("admin").password("secret").build();
    ConnectionFactoryOptions derivedOptions = ((OptionsCapableConnectionFactory) derived).getOptions();
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.USER)).isEqualTo("admin");
    assertThat(derivedOptions.getRequiredValue(ConnectionFactoryOptions.PASSWORD)).isEqualTo("secret");
    assertMatchingOptions(derivedOptions, initialOptions, ConnectionFactoryOptions.CONNECT_TIMEOUT, ConnectionFactoryOptions.DATABASE, ConnectionFactoryOptions.DRIVER, ConnectionFactoryOptions.HOST, ConnectionFactoryOptions.PORT, ConnectionFactoryOptions.PROTOCOL, ConnectionFactoryOptions.SSL);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ConnectionFactoryOptions(io.r2dbc.spi.ConnectionFactoryOptions) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 15 with ConnectionFactory

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

the class ConnectionFactoryBuilderTests method buildShouldExposeConnectionFactory.

@Test
void buildShouldExposeConnectionFactory() {
    String databaseName = UUID.randomUUID().toString();
    ConnectionFactory connectionFactory = ConnectionFactoryBuilder.withUrl(EmbeddedDatabaseConnection.H2.getUrl(databaseName)).build();
    assertThat(connectionFactory).isNotNull();
    assertThat(connectionFactory.getMetadata().getName()).isEqualTo(H2ConnectionFactoryMetadata.NAME);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ConnectionFactory (io.r2dbc.spi.ConnectionFactory)25 Test (org.junit.jupiter.api.Test)17 H2ConnectionFactory (io.r2dbc.h2.H2ConnectionFactory)11 CloseableConnectionFactory (io.r2dbc.h2.CloseableConnectionFactory)5 H2ConnectionOption (io.r2dbc.h2.H2ConnectionOption)5 Connection (io.r2dbc.spi.Connection)5 Result (io.r2dbc.spi.Result)5 ValidationDepth (io.r2dbc.spi.ValidationDepth)5 Collections (java.util.Collections)5 UUID (java.util.UUID)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 Assertions.entry (org.assertj.core.api.Assertions.entry)5 BDDMockito.given (org.mockito.BDDMockito.given)5 Mockito.mock (org.mockito.Mockito.mock)5 ReactiveHealthIndicator (org.springframework.boot.actuate.health.ReactiveHealthIndicator)5 Status (org.springframework.boot.actuate.health.Status)5 Flux (reactor.core.publisher.Flux)5 Mono (reactor.core.publisher.Mono)5 StepVerifier (reactor.test.StepVerifier)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)4