From 2c3eb4d34d3f0136199886e5e37865f24788c5fe Mon Sep 17 00:00:00 2001 From: Marius Barbulescu Date: Sun, 12 Jan 2025 20:10:45 +0100 Subject: [PATCH 1/3] add support for HttpStatus in SimplifyWebTestClientCalls --- .../http/SimplifyWebTestClientCalls.java | 72 +++++++++++- .../http/SimplifyWebTestClientCallsTest.java | 105 +++++++++++++++++- 2 files changed, 167 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java index e256c1ea..c2e73d2e 100644 --- a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java +++ b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java @@ -27,6 +27,8 @@ import org.openrewrite.java.tree.J; import org.openrewrite.java.tree.JavaType; +import java.util.List; + import static java.util.Collections.emptyList; public class SimplifyWebTestClientCalls extends Recipe { @@ -50,7 +52,8 @@ public TreeVisitor getVisitor() { public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = super.visitMethodInvocation(method, ctx); if (IS_EQUAL_TO_MATCHER.matches(m.getMethodType())) { - final int statusCode = extractStatusCode(m.getArguments().get(0)); + final int statusCode = extractStatusCode(m.getArguments() + .get(0)); switch (statusCode) { case 200: return replaceMethod(m, "isOk()"); @@ -84,27 +87,86 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu } private int extractStatusCode(Expression expression) { + if (expression instanceof J.FieldAccess) { + //isEqualTo(HttpStatus.OK) + J.FieldAccess fa = (J.FieldAccess) expression; + if (fa.getTarget() instanceof J.Identifier) { + J.Identifier target = (J.Identifier) fa.getTarget(); + if (target.getSimpleName() + .equals("HttpStatus")) { + String value = fa.getSimpleName(); + switch (value) { + case "OK": + return 200; + case "CREATED": + return 201; + case "ACCEPTED": + return 202; + case "NO_CONTENT": + return 204; + case "FOUND": + return 302; + case "SEE_OTHER": + return 303; + case "NOT_MODIFIED": + return 304; + case "TEMPORARY_REDIRECT": + return 307; + case "PERMANENT_REDIRECT": + return 308; + case "BAD_REQUEST": + return 400; + case "UNAUTHORIZED": + return 401; + case "FORBIDDEN": + return 403; + case "NOT_FOUND": + return 404; + } + + } + } + } if (expression instanceof J.Literal) { + //isEqualTo(200) Object raw = ((J.Literal) expression).getValue(); if (raw instanceof Integer) { return (int) raw; } } - return -1; // HttpStatus is not yet supported + if (expression instanceof J.MethodInvocation) { + //isEqualTo(HttpStatus.valueOf(200)) + //isEqualTo(HttpStatusCode.valueOf(200)) + J.MethodInvocation methodInvocation = (J.MethodInvocation) expression; + List arguments = methodInvocation.getArguments(); + if (arguments.size() == 1 && arguments.get(0) instanceof J.Literal) { + Object raw = ((J.Literal) arguments.get(0)).getValue(); + if (raw instanceof Integer) { + return (int) raw; + } + } + } + return -1; } private J.MethodInvocation replaceMethod(J.MethodInvocation method, String methodName) { - J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates().replaceMethod()); + J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates() + .replaceMethod()); JavaType.Method type = methodInvocation .getMethodType() .withParameterNames(emptyList()) .withParameterTypes(emptyList()); - return methodInvocation + J.MethodInvocation result = methodInvocation .withArguments(emptyList()) .withMethodType(type) - .withName(methodInvocation.getName().withType(type)); + .withName(methodInvocation.getName() + .withType(type)); + maybeRemoveImport("org.springframework.http.HttpStatusCode"); + maybeRemoveImport("org.springframework.http.HttpStatus"); + return result; } }); } } + diff --git a/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java b/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java index 7d64a2e2..28c386c6 100644 --- a/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java +++ b/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java @@ -15,7 +15,6 @@ */ package org.openrewrite.java.spring.http; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; @@ -36,9 +35,9 @@ public void defaults(RecipeSpec spec) { spec .recipe(new SimplifyWebTestClientCalls()) .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")) + .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")) .parser(KotlinParser.builder() - .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")); + .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")); } @DocumentExample @@ -165,14 +164,14 @@ void someMethod() { } @Test - @Disabled("Yet to be implemented") - void usesIsOkForHttpStatus200() { + void usesIsOkForHttpStatusValueOf200() { rewriteRun( //language=java java( """ import org.springframework.test.web.reactive.server.WebTestClient; import org.springframework.http.HttpStatus; + class Test { private final WebTestClient webClient = WebTestClient.bindToServer().build(); void someMethod() { @@ -187,6 +186,47 @@ void someMethod() { """, """ import org.springframework.test.web.reactive.server.WebTestClient; + + class Test { + private final WebTestClient webClient = WebTestClient.bindToServer().build(); + void someMethod() { + webClient + .post() + .uri("/some/value") + .exchange() + .expectStatus() + .isOk(); + } + } + """ + ) + ); + } + + @Test + void usesIsOkForHttpStatusValueCodeOf200() { + rewriteRun( + //language=java + java( + """ + import org.springframework.test.web.reactive.server.WebTestClient; + import org.springframework.http.HttpStatusCode; + + class Test { + private final WebTestClient webClient = WebTestClient.bindToServer().build(); + void someMethod() { + webClient + .post() + .uri("/some/value") + .exchange() + .expectStatus() + .isEqualTo(HttpStatusCode.valueOf(200)); + } + } + """, + """ + import org.springframework.test.web.reactive.server.WebTestClient; + class Test { private final WebTestClient webClient = WebTestClient.bindToServer().build(); void someMethod() { @@ -203,6 +243,61 @@ void someMethod() { ); } + @ParameterizedTest + @CsvSource({ + "OK,isOk()", + "CREATED,isCreated()", + "ACCEPTED,isAccepted()", + "NO_CONTENT,isNoContent()", + "FOUND,isFound()", + "SEE_OTHER,isSeeOther()", + "NOT_MODIFIED,isNotModified()", + "TEMPORARY_REDIRECT,isTemporaryRedirect()", + "PERMANENT_REDIRECT,isPermanentRedirect()", + "BAD_REQUEST,isBadRequest()", + "UNAUTHORIZED,isUnauthorized()", + "FORBIDDEN,isForbidden()", + "NOT_FOUND,isNotFound()" + }) + void usesIsOkForHttpStatusValue(String httpStatus, String method) { + rewriteRun( + //language=java + java( + """ + import org.springframework.test.web.reactive.server.WebTestClient; + import org.springframework.http.HttpStatus; + + class Test { + private final WebTestClient webClient = WebTestClient.bindToServer().build(); + void someMethod() { + webClient + .post() + .uri("/some/value") + .exchange() + .expectStatus() + .isEqualTo(HttpStatus.%s); + } + } + """.formatted(httpStatus), + """ + import org.springframework.test.web.reactive.server.WebTestClient; + + class Test { + private final WebTestClient webClient = WebTestClient.bindToServer().build(); + void someMethod() { + webClient + .post() + .uri("/some/value") + .exchange() + .expectStatus() + .%s; + } + } + """.formatted(method) + ) + ); + } + @Test void doesNotUseIsOkForHttpStatus300() { rewriteRun( From 68c513251a127ba6f6778668b3f7d4453bec3c03 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 12 Jan 2025 21:39:48 +0100 Subject: [PATCH 2/3] Minor polish --- .../http/SimplifyWebTestClientCalls.java | 30 +++++++------------ .../http/SimplifyWebTestClientCallsTest.java | 4 +-- 2 files changed, 12 insertions(+), 22 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java index c2e73d2e..4c647091 100644 --- a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java +++ b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java @@ -52,8 +52,7 @@ public TreeVisitor getVisitor() { public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) { J.MethodInvocation m = super.visitMethodInvocation(method, ctx); if (IS_EQUAL_TO_MATCHER.matches(m.getMethodType())) { - final int statusCode = extractStatusCode(m.getArguments() - .get(0)); + final int statusCode = extractStatusCode(m.getArguments().get(0)); switch (statusCode) { case 200: return replaceMethod(m, "isOk()"); @@ -91,11 +90,8 @@ private int extractStatusCode(Expression expression) { //isEqualTo(HttpStatus.OK) J.FieldAccess fa = (J.FieldAccess) expression; if (fa.getTarget() instanceof J.Identifier) { - J.Identifier target = (J.Identifier) fa.getTarget(); - if (target.getSimpleName() - .equals("HttpStatus")) { - String value = fa.getSimpleName(); - switch (value) { + if ("HttpStatus".equals(((J.Identifier) fa.getTarget()).getSimpleName())) { + switch (fa.getSimpleName()) { case "OK": return 200; case "CREATED": @@ -123,18 +119,15 @@ private int extractStatusCode(Expression expression) { case "NOT_FOUND": return 404; } - } } - } - if (expression instanceof J.Literal) { + } else if (expression instanceof J.Literal) { //isEqualTo(200) Object raw = ((J.Literal) expression).getValue(); if (raw instanceof Integer) { return (int) raw; } - } - if (expression instanceof J.MethodInvocation) { + } else if (expression instanceof J.MethodInvocation) { //isEqualTo(HttpStatus.valueOf(200)) //isEqualTo(HttpStatusCode.valueOf(200)) J.MethodInvocation methodInvocation = (J.MethodInvocation) expression; @@ -150,20 +143,17 @@ private int extractStatusCode(Expression expression) { } private J.MethodInvocation replaceMethod(J.MethodInvocation method, String methodName) { - J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates() - .replaceMethod()); + maybeRemoveImport("org.springframework.http.HttpStatus"); + maybeRemoveImport("org.springframework.http.HttpStatusCode"); + J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates().replaceMethod()); JavaType.Method type = methodInvocation .getMethodType() .withParameterNames(emptyList()) .withParameterTypes(emptyList()); - J.MethodInvocation result = methodInvocation + return methodInvocation .withArguments(emptyList()) .withMethodType(type) - .withName(methodInvocation.getName() - .withType(type)); - maybeRemoveImport("org.springframework.http.HttpStatusCode"); - maybeRemoveImport("org.springframework.http.HttpStatus"); - return result; + .withName(methodInvocation.getName().withType(type)); } }); diff --git a/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java b/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java index 28c386c6..14059211 100644 --- a/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java +++ b/src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java @@ -35,9 +35,9 @@ public void defaults(RecipeSpec spec) { spec .recipe(new SimplifyWebTestClientCalls()) .parser(JavaParser.fromJavaVersion() - .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")) + .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")) .parser(KotlinParser.builder() - .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")); + .classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6")); } @DocumentExample From b2f9cd79f08fc58e3ca6c8b85722f3474e8f6a64 Mon Sep 17 00:00:00 2001 From: Tim te Beek Date: Sun, 12 Jan 2025 21:42:51 +0100 Subject: [PATCH 3/3] Apply code suggestions --- .../java/spring/http/SimplifyWebTestClientCalls.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java index 4c647091..6c46b9b4 100644 --- a/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java +++ b/src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java @@ -154,9 +154,7 @@ private J.MethodInvocation replaceMethod(J.MethodInvocation method, String metho .withArguments(emptyList()) .withMethodType(type) .withName(methodInvocation.getName().withType(type)); - } }); } } -