From f4ab9b4e7cd7f751a3fe5fcfe291349d975e8074 Mon Sep 17 00:00:00 2001 From: Phil Werli Date: Thu, 11 Apr 2024 20:38:49 +0200 Subject: [PATCH] Introduce `StepVerifierLastStepVerifyErrorSatisfiesAssertJ` Refaster rule --- .../refasterrules/ReactorRules.java | 25 +++++++++++++++++++ .../refasterrules/ReactorRulesTestInput.java | 19 ++++++++++++++ .../refasterrules/ReactorRulesTestOutput.java | 15 +++++++++++ 3 files changed, 59 insertions(+) diff --git a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java index bc7f29fc8eb..df5559ec8e2 100644 --- a/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java +++ b/error-prone-contrib/src/main/java/tech/picnic/errorprone/refasterrules/ReactorRules.java @@ -41,6 +41,7 @@ import java.util.function.Predicate; import java.util.function.Supplier; import java.util.stream.Collector; +import org.assertj.core.api.Assertions; import org.jspecify.annotations.Nullable; import org.reactivestreams.Publisher; import reactor.core.publisher.Flux; @@ -1928,6 +1929,30 @@ Duration after(StepVerifier.LastStep step, Consumer consumer) { } } + /** + * Prefer {@link StepVerifier.LastStep#verifyErrorSatisfies(Consumer)} with AssertJ over more + * contrived alternatives. + */ + static final class StepVerifierLastStepVerifyErrorSatisfiesAssertJ { + @BeforeTemplate + void before(StepVerifier.LastStep step, Class clazz, String message) { + Refaster.anyOf( + step.expectError() + .verifyThenAssertThat() + .hasOperatorErrorOfType(clazz) + .hasOperatorErrorWithMessage(message), + step.expectError(clazz).verifyThenAssertThat().hasOperatorErrorWithMessage(message), + step.expectErrorMessage(message).verifyThenAssertThat().hasOperatorErrorOfType(clazz)); + } + + @AfterTemplate + @UseImportPolicy(STATIC_IMPORT_ALWAYS) + void after(StepVerifier.LastStep step, Class clazz, String message) { + step.verifyErrorSatisfies( + t -> Assertions.assertThat(t).isInstanceOf(clazz).hasMessage(message)); + } + } + /** * Prefer {@link StepVerifier.LastStep#verifyErrorMessage(String)} over more verbose alternatives. */ diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java index b934750af1b..0e5fdaaa6a1 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestInput.java @@ -655,6 +655,25 @@ Duration testStepVerifierLastStepVerifyErrorSatisfies() { return Mono.empty().as(StepVerifier::create).expectErrorSatisfies(t -> {}).verify(); } + void testStepVerifierLastStepVerifyErrorSatisfiesAssertJ() { + Mono.empty() + .as(StepVerifier::create) + .expectError() + .verifyThenAssertThat() + .hasOperatorErrorOfType(IllegalStateException.class) + .hasOperatorErrorWithMessage("foo"); + Mono.empty() + .as(StepVerifier::create) + .expectError(IllegalStateException.class) + .verifyThenAssertThat() + .hasOperatorErrorWithMessage("bar"); + Mono.empty() + .as(StepVerifier::create) + .expectErrorMessage("baz") + .verifyThenAssertThat() + .hasOperatorErrorOfType(IllegalStateException.class); + } + Duration testStepVerifierLastStepVerifyErrorMessage() { return Mono.empty().as(StepVerifier::create).expectErrorMessage("foo").verify(); } diff --git a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java index 1a95d60cdaa..dd50924a2a9 100644 --- a/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java +++ b/error-prone-contrib/src/test/resources/tech/picnic/errorprone/refasterrules/ReactorRulesTestOutput.java @@ -636,6 +636,21 @@ Duration testStepVerifierLastStepVerifyErrorSatisfies() { return Mono.empty().as(StepVerifier::create).verifyErrorSatisfies(t -> {}); } + void testStepVerifierLastStepVerifyErrorSatisfiesAssertJ() { + Mono.empty() + .as(StepVerifier::create) + .verifyErrorSatisfies( + t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("foo")); + Mono.empty() + .as(StepVerifier::create) + .verifyErrorSatisfies( + t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("bar")); + Mono.empty() + .as(StepVerifier::create) + .verifyErrorSatisfies( + t -> assertThat(t).isInstanceOf(IllegalStateException.class).hasMessage("baz")); + } + Duration testStepVerifierLastStepVerifyErrorMessage() { return Mono.empty().as(StepVerifier::create).verifyErrorMessage("foo"); }