Search in sources :

Example 1 with Connection

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

the class ConnectionFactoryHealthIndicatorTests method healthIndicatorWhenConnectionValidationFails.

@Test
void healthIndicatorWhenConnectionValidationFails() {
    ConnectionFactory connectionFactory = mock(ConnectionFactory.class);
    given(connectionFactory.getMetadata()).willReturn(() -> "mock");
    Connection connection = mock(Connection.class);
    given(connection.validate(ValidationDepth.REMOTE)).willReturn(Mono.just(false));
    given(connection.close()).willReturn(Mono.empty());
    given(connectionFactory.create()).willAnswer((invocation) -> Mono.just(connection));
    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)"));
    }).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) Connection(io.r2dbc.spi.Connection) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 2 with Connection

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

the class R2dbcTransactionManagerUnitTests method testSimpleTransaction.

@Test
void testSimpleTransaction() {
    TestTransactionSynchronization sync = new TestTransactionSynchronization(TransactionSynchronization.STATUS_COMMITTED);
    AtomicInteger commits = new AtomicInteger();
    when(connectionMock.commitTransaction()).thenReturn(Mono.fromRunnable(commits::incrementAndGet));
    TransactionalOperator operator = TransactionalOperator.create(tm);
    ConnectionFactoryUtils.getConnection(connectionFactoryMock).flatMap(connection -> TransactionSynchronizationManager.forCurrentTransaction().doOnNext(synchronizationManager -> synchronizationManager.registerSynchronization(sync))).as(operator::transactional).as(StepVerifier::create).expectNextCount(1).verifyComplete();
    assertThat(commits).hasValue(1);
    verify(connectionMock).isAutoCommit();
    verify(connectionMock).beginTransaction();
    verify(connectionMock).commitTransaction();
    verify(connectionMock).close();
    verifyNoMoreInteractions(connectionMock);
    assertThat(sync.beforeCommitCalled).isTrue();
    assertThat(sync.afterCommitCalled).isTrue();
    assertThat(sync.beforeCompletionCalled).isTrue();
    assertThat(sync.afterCompletionCalled).isTrue();
}
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) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 3 with Connection

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

the class SingleConnectionFactoryUnitTests method shouldAllocateSameConnection.

@Test
void shouldAllocateSameConnection() {
    SingleConnectionFactory factory = new SingleConnectionFactory("r2dbc:h2:mem:///foo", false);
    Mono<? extends Connection> cf1 = factory.create();
    Mono<? extends Connection> cf2 = factory.create();
    Connection c1 = cf1.block();
    Connection c2 = cf2.block();
    assertThat(c1).isSameAs(c2);
    factory.destroy();
}
Also used : Connection(io.r2dbc.spi.Connection) H2Connection(io.r2dbc.h2.H2Connection) Test(org.junit.jupiter.api.Test)

Example 4 with Connection

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

the class SingleConnectionFactoryUnitTests method releaseConnectionShouldNotCloseConnection.

@Test
void releaseConnectionShouldNotCloseConnection() {
    Connection connectionMock = mock(Connection.class);
    ConnectionFactoryMetadata metadata = mock(ConnectionFactoryMetadata.class);
    SingleConnectionFactory factory = new SingleConnectionFactory(connectionMock, metadata, true);
    Connection connection = factory.create().block();
    ConnectionFactoryUtils.releaseConnection(connection, factory).as(StepVerifier::create).verifyComplete();
    verify(connectionMock, never()).close();
}
Also used : Connection(io.r2dbc.spi.Connection) H2Connection(io.r2dbc.h2.H2Connection) ConnectionFactoryMetadata(io.r2dbc.spi.ConnectionFactoryMetadata) Test(org.junit.jupiter.api.Test)

Example 5 with Connection

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

the class SingleConnectionFactoryUnitTests method releaseConnectionShouldCloseUnrelatedConnection.

@Test
void releaseConnectionShouldCloseUnrelatedConnection() {
    Connection connectionMock = mock(Connection.class);
    Connection otherConnection = mock(Connection.class);
    ConnectionFactoryMetadata metadata = mock(ConnectionFactoryMetadata.class);
    when(otherConnection.close()).thenReturn(Mono.empty());
    SingleConnectionFactory factory = new SingleConnectionFactory(connectionMock, metadata, false);
    factory.create().as(StepVerifier::create).expectNextCount(1).verifyComplete();
    ConnectionFactoryUtils.releaseConnection(otherConnection, factory).as(StepVerifier::create).verifyComplete();
    verify(otherConnection).close();
}
Also used : Connection(io.r2dbc.spi.Connection) H2Connection(io.r2dbc.h2.H2Connection) StepVerifier(reactor.test.StepVerifier) ConnectionFactoryMetadata(io.r2dbc.spi.ConnectionFactoryMetadata) Test(org.junit.jupiter.api.Test)

Aggregations

Connection (io.r2dbc.spi.Connection)11 Test (org.junit.jupiter.api.Test)10 Mono (reactor.core.publisher.Mono)6 StepVerifier (reactor.test.StepVerifier)6 H2Connection (io.r2dbc.h2.H2Connection)5 ConnectionFactory (io.r2dbc.spi.ConnectionFactory)5 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 BeforeEach (org.junit.jupiter.api.BeforeEach)4 BDDMockito.mock (org.mockito.BDDMockito.mock)4 BDDMockito.verify (org.mockito.BDDMockito.verify)4 BDDMockito.when (org.mockito.BDDMockito.when)4 IsolationLevel (io.r2dbc.spi.IsolationLevel)3 R2dbcBadGrammarException (io.r2dbc.spi.R2dbcBadGrammarException)3 Statement (io.r2dbc.spi.Statement)3 Assertions.fail (org.assertj.core.api.Assertions.fail)3 ArgumentMatchers.any (org.mockito.ArgumentMatchers.any)3 ArgumentMatchers.anyBoolean (org.mockito.ArgumentMatchers.anyBoolean)3 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)3 BDDMockito.never (org.mockito.BDDMockito.never)3