Search in sources :

Example 76 with StepVerifier

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

the class AbstractReactiveTransactionAspectTests method doTestRollbackOnException.

/**
 * Check that the when exception thrown by the target can produce the
 * desired behavior with the appropriate transaction attribute.
 * @param ex exception to be thrown by the target
 * @param shouldRollback whether this should cause a transaction rollback
 */
@SuppressWarnings("serial")
protected void doTestRollbackOnException(final Exception ex, final boolean shouldRollback, boolean rollbackException) throws Exception {
    TransactionAttribute txatt = new DefaultTransactionAttribute() {

        @Override
        public boolean rollbackOn(Throwable t) {
            assertThat(t).isSameAs(ex);
            return shouldRollback;
        }
    };
    Method m = exceptionalMethod;
    MapTransactionAttributeSource tas = new MapTransactionAttributeSource();
    tas.register(m, txatt);
    ReactiveTransaction status = mock(ReactiveTransaction.class);
    ReactiveTransactionManager rtm = mock(ReactiveTransactionManager.class);
    // Gets additional call(s) from TransactionControl
    given(rtm.getReactiveTransaction(txatt)).willReturn(Mono.just(status));
    TransactionSystemException tex = new TransactionSystemException("system exception");
    if (rollbackException) {
        if (shouldRollback) {
            given(rtm.rollback(status)).willReturn(Mono.error(tex));
        } else {
            given(rtm.commit(status)).willReturn(Mono.error(tex));
        }
    } else {
        given(rtm.commit(status)).willReturn(Mono.empty());
        given(rtm.rollback(status)).willReturn(Mono.empty());
    }
    DefaultTestBean tb = new DefaultTestBean();
    TestBean itb = (TestBean) advised(tb, rtm, tas);
    itb.exceptional(ex).as(StepVerifier::create).expectErrorSatisfies(actual -> {
        if (rollbackException) {
            assertThat(actual).isEqualTo(tex);
        } else {
            assertThat(actual).isEqualTo(ex);
        }
    }).verify();
    if (!rollbackException) {
        if (shouldRollback) {
            verify(rtm).rollback(status);
        } else {
            verify(rtm).commit(status);
        }
    }
}
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) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) Method(java.lang.reflect.Method) TransactionSystemException(org.springframework.transaction.TransactionSystemException) StepVerifier(reactor.test.StepVerifier)

Example 77 with StepVerifier

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

the class ReactiveTransactionSupportTests method existingTransaction.

@Test
public void existingTransaction() {
    ReactiveTransactionManager tm = new ReactiveTestTransactionManager(true, true);
    tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_SUPPORTS)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
        assertThat(actual.getTransaction()).isNotNull();
        assertThat(actual.isNewTransaction()).isFalse();
    }).verifyComplete();
    tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_REQUIRED)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
        assertThat(actual.getTransaction()).isNotNull();
        assertThat(actual.isNewTransaction()).isFalse();
    }).verifyComplete();
    tm.getReactiveTransaction(new DefaultTransactionDefinition(TransactionDefinition.PROPAGATION_MANDATORY)).contextWrite(TransactionContextManager.createTransactionContext()).cast(GenericReactiveTransaction.class).as(StepVerifier::create).consumeNextWith(actual -> {
        assertThat(actual.getTransaction()).isNotNull();
        assertThat(actual.isNewTransaction()).isFalse();
    }).verifyComplete();
}
Also used : Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) StepVerifier(reactor.test.StepVerifier) TransactionDefinition(org.springframework.transaction.TransactionDefinition) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mono(reactor.core.publisher.Mono) ReactiveTransaction(org.springframework.transaction.ReactiveTransaction) IllegalTransactionStateException(org.springframework.transaction.IllegalTransactionStateException) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) DefaultTransactionDefinition(org.springframework.transaction.support.DefaultTransactionDefinition) ReactiveTransactionManager(org.springframework.transaction.ReactiveTransactionManager) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 78 with StepVerifier

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

the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method saveAuthorizedClientWhenSaveThenLoadReturnsSaved.

@Test
public void saveAuthorizedClientWhenSaveThenLoadReturnsSaved() {
    Authentication principal = createPrincipal();
    final OAuth2AuthorizedClient expected = createAuthorizedClient(principal, this.clientRegistration);
    this.authorizedClientService.saveAuthorizedClient(expected, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(expected.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(expected.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(expected.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(expected.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(expected.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(expected.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEqualTo(expected.getAccessToken().getScopes());
        assertThat(authorizedClient.getRefreshToken().getTokenValue()).isEqualTo(expected.getRefreshToken().getTokenValue());
        assertThat(authorizedClient.getRefreshToken().getIssuedAt()).isEqualTo(expected.getRefreshToken().getIssuedAt());
    }).verifyComplete();
    // Test save/load of NOT NULL attributes only
    principal = createPrincipal();
    OAuth2AuthorizedClient updatedExpectedPrincipal = createAuthorizedClient(principal, this.clientRegistration, true);
    this.authorizedClientService.saveAuthorizedClient(updatedExpectedPrincipal, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(updatedExpectedPrincipal.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(updatedExpectedPrincipal.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(updatedExpectedPrincipal.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEmpty();
        assertThat(authorizedClient.getRefreshToken()).isNull();
    }).verifyComplete();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) ConnectionFactoryInitializer(org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) HashSet(java.util.HashSet) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Result(io.r2dbc.spi.Result) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) Mono(reactor.core.publisher.Mono) CompositeDatabasePopulator(org.springframework.r2dbc.connection.init.CompositeDatabasePopulator) Instant(java.time.Instant) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ResourceDatabasePopulator(org.springframework.r2dbc.connection.init.ResourceDatabasePopulator) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) Authentication(org.springframework.security.core.Authentication) OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Authentication(org.springframework.security.core.Authentication) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 79 with StepVerifier

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

the class R2dbcReactiveOAuth2AuthorizedClientServiceTests method loadAuthorizedClientWhenExistsThenReturnAuthorizedClient.

@Test
public void loadAuthorizedClientWhenExistsThenReturnAuthorizedClient() {
    Authentication principal = createPrincipal();
    OAuth2AuthorizedClient expected = createAuthorizedClient(principal, this.clientRegistration);
    this.authorizedClientService.saveAuthorizedClient(expected, principal).as(StepVerifier::create).verifyComplete();
    this.authorizedClientService.loadAuthorizedClient(this.clientRegistration.getRegistrationId(), principal.getName()).as(StepVerifier::create).assertNext((authorizedClient) -> {
        assertThat(authorizedClient).isNotNull();
        assertThat(authorizedClient.getClientRegistration()).isEqualTo(expected.getClientRegistration());
        assertThat(authorizedClient.getPrincipalName()).isEqualTo(expected.getPrincipalName());
        assertThat(authorizedClient.getAccessToken().getTokenType()).isEqualTo(expected.getAccessToken().getTokenType());
        assertThat(authorizedClient.getAccessToken().getTokenValue()).isEqualTo(expected.getAccessToken().getTokenValue());
        assertThat(authorizedClient.getAccessToken().getIssuedAt()).isEqualTo(expected.getAccessToken().getIssuedAt());
        assertThat(authorizedClient.getAccessToken().getExpiresAt()).isEqualTo(expected.getAccessToken().getExpiresAt());
        assertThat(authorizedClient.getAccessToken().getScopes()).isEqualTo(expected.getAccessToken().getScopes());
        assertThat(authorizedClient.getRefreshToken().getTokenValue()).isEqualTo(expected.getRefreshToken().getTokenValue());
        assertThat(authorizedClient.getRefreshToken().getIssuedAt()).isEqualTo(expected.getRefreshToken().getIssuedAt());
    }).verifyComplete();
}
Also used : ConnectionFactory(io.r2dbc.spi.ConnectionFactory) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) StepVerifier(reactor.test.StepVerifier) TestingAuthenticationToken(org.springframework.security.authentication.TestingAuthenticationToken) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) ClassPathResource(org.springframework.core.io.ClassPathResource) ConnectionFactoryInitializer(org.springframework.r2dbc.connection.init.ConnectionFactoryInitializer) DataRetrievalFailureException(org.springframework.dao.DataRetrievalFailureException) DatabaseClient(org.springframework.r2dbc.core.DatabaseClient) HashSet(java.util.HashSet) BDDMockito.given(org.mockito.BDDMockito.given) Duration(java.time.Duration) Assertions.assertThatExceptionOfType(org.assertj.core.api.Assertions.assertThatExceptionOfType) Result(io.r2dbc.spi.Result) OAuth2AccessToken(org.springframework.security.oauth2.core.OAuth2AccessToken) TestClientRegistrations(org.springframework.security.oauth2.client.registration.TestClientRegistrations) ReactiveClientRegistrationRepository(org.springframework.security.oauth2.client.registration.ReactiveClientRegistrationRepository) Mono(reactor.core.publisher.Mono) CompositeDatabasePopulator(org.springframework.r2dbc.connection.init.CompositeDatabasePopulator) Instant(java.time.Instant) ClientRegistration(org.springframework.security.oauth2.client.registration.ClientRegistration) ResourceDatabasePopulator(org.springframework.r2dbc.connection.init.ResourceDatabasePopulator) Test(org.junit.jupiter.api.Test) Flux(reactor.core.publisher.Flux) H2ConnectionFactory(io.r2dbc.h2.H2ConnectionFactory) Authentication(org.springframework.security.core.Authentication) OAuth2RefreshToken(org.springframework.security.oauth2.core.OAuth2RefreshToken) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Authentication(org.springframework.security.core.Authentication) StepVerifier(reactor.test.StepVerifier) Test(org.junit.jupiter.api.Test)

Example 80 with StepVerifier

use of reactor.test.StepVerifier 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)

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