Search in sources :

Example 21 with ConnectionFactory

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

the class AbstractRoutingConnectionFactory method determineTargetConnectionFactory.

/**
 * Retrieve the current target {@link ConnectionFactory}. Determines the
 * {@link #determineCurrentLookupKey() current lookup key}, performs a lookup
 * in the {@link #setTargetConnectionFactories targetConnectionFactories} map,
 * falls back to the specified {@link #setDefaultTargetConnectionFactory default
 * target ConnectionFactory} if necessary.
 * @return {@link Mono} emitting the current {@link ConnectionFactory} as
 * per {@link #determineCurrentLookupKey()}
 * @see #determineCurrentLookupKey()
 */
protected Mono<ConnectionFactory> determineTargetConnectionFactory() {
    Assert.state(this.resolvedConnectionFactories != null, "ConnectionFactory router not initialized");
    Mono<Object> lookupKey = determineCurrentLookupKey().defaultIfEmpty(FALLBACK_MARKER);
    return lookupKey.handle((key, sink) -> {
        ConnectionFactory connectionFactory = this.resolvedConnectionFactories.get(key);
        if (connectionFactory == null && (key == FALLBACK_MARKER || this.lenientFallback)) {
            connectionFactory = this.resolvedDefaultConnectionFactory;
        }
        if (connectionFactory == null) {
            sink.error(new IllegalStateException(String.format("Cannot determine target ConnectionFactory for lookup key '%s'", key == FALLBACK_MARKER ? null : key)));
            return;
        }
        sink.next(connectionFactory);
    });
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory)

Example 22 with ConnectionFactory

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

the class MapConnectionFactoryLookupUnitTests method shouldLookupConnectionFactory.

@Test
public void shouldLookupConnectionFactory() {
    Map<String, ConnectionFactory> connectionFactories = new HashMap<>();
    DummyConnectionFactory expectedConnectionFactory = new DummyConnectionFactory();
    connectionFactories.put(CONNECTION_FACTORY_NAME, expectedConnectionFactory);
    MapConnectionFactoryLookup lookup = new MapConnectionFactoryLookup();
    lookup.setConnectionFactories(connectionFactories);
    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 23 with ConnectionFactory

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

the class DefaultDatabaseClientUnitTests method before.

@BeforeEach
@SuppressWarnings({ "rawtypes", "unchecked" })
void before() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    when(connectionFactory.create()).thenReturn((Publisher) Mono.just(connection));
    when(connection.close()).thenReturn(Mono.empty());
    databaseClientBuilder = DatabaseClient.builder().connectionFactory(connectionFactory).bindMarkers(BindMarkersFactory.indexed("$", 1));
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 24 with ConnectionFactory

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

the class DefaultDatabaseClientUnitTests method connectionFactoryIsExposed.

@Test
void connectionFactoryIsExposed() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    DatabaseClient databaseClient = DatabaseClient.builder().connectionFactory(connectionFactory).bindMarkers(BindMarkersFactory.anonymous("?")).build();
    assertThat(databaseClient.getConnectionFactory()).isSameAs(connectionFactory);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) Test(org.junit.jupiter.api.Test)

Example 25 with ConnectionFactory

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

the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method setUp.

@BeforeEach
public void setUp() {
    final ConnectionFactory connectionFactory = createDb();
    this.clientRegistration = TestClientRegistrations.clientRegistration().build();
    this.clientRegistrationRepository = mock(ReactiveClientRegistrationRepository.class);
    given(this.clientRegistrationRepository.findByRegistrationId(anyString())).willReturn(Mono.just(this.clientRegistration));
    this.databaseClient = DatabaseClient.create(connectionFactory);
    this.authorizedClientService = new R2dbcReactiveOAuth2AuthorizedClientService(this.databaseClient, this.clientRegistrationRepository);
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) BeforeEach(org.junit.jupiter.api.BeforeEach)

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