Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for HttpStatus in SimplifyWebTestClientCalls #664

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -50,7 +52,8 @@ public TreeVisitor<?, ExecutionContext> 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()");
Expand Down Expand Up @@ -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<Expression> 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;

}
});
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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() {
Expand All @@ -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() {
Expand All @@ -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(
Expand Down
Loading