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);
}
}
}
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();
}
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();
}
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();
}
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();
}
}
Aggregations