Skip to content

Commit

Permalink
SimplifyWebTestClientCalls does not work on Kotlin code (#663)
Browse files Browse the repository at this point in the history
* copy SimplifyWebTestClientCallsTest to SimplifyWebTestClientCallsKotlinTest to reproduce the issue with SimplifyWebTestClientCalls on Kotlin code

* support both int and long status codes as Kotlin uses long instead of int

* Use a nested class for the Kotlin tests & minimize diff

* Simplify after upstream fix in rewrite-kotlin

* Match any argument, as kotlin.Int fails to match (int)

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
barbulescu and timtebeek authored Jan 11, 2025
1 parent e614bc6 commit 4192cfb
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 21 deletions.
1 change: 1 addition & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@ dependencies {
// for generating properties migration configurations
testImplementation("io.github.classgraph:classgraph:latest.release")
testImplementation("org.openrewrite:rewrite-java-17")
testImplementation("org.openrewrite:rewrite-kotlin:$rewriteVersion")
testImplementation("org.openrewrite.recipe:rewrite-migrate-java:$rewriteVersion")
testImplementation("org.openrewrite.recipe:rewrite-testing-frameworks:$rewriteVersion")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,15 @@
import org.openrewrite.java.JavaTemplate;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.search.UsesMethod;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.JavaType;

import static java.util.Collections.emptyList;

public class SimplifyWebTestClientCalls extends Recipe {

private static final MethodMatcher IS_EQUAL_TO_INT_MATCHER = new MethodMatcher("org.springframework.test.web.reactive.server.StatusAssertions isEqualTo(int)");
private static final MethodMatcher IS_EQUAL_TO_MATCHER = new MethodMatcher("org.springframework.test.web.reactive.server.StatusAssertions isEqualTo(..)");

@Override
public String getDisplayName() {
Expand All @@ -44,12 +45,12 @@ public String getDescription() {

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesMethod<>(IS_EQUAL_TO_INT_MATCHER), new JavaIsoVisitor<ExecutionContext>() {
return Preconditions.check(new UsesMethod<>(IS_EQUAL_TO_MATCHER), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation m = super.visitMethodInvocation(method, ctx);
if (IS_EQUAL_TO_INT_MATCHER.matches(m.getMethodType())) {
int statusCode = (int) ((J.Literal) m.getArguments().get(0)).getValue();
if (IS_EQUAL_TO_MATCHER.matches(m.getMethodType())) {
final int statusCode = extractStatusCode(m.getArguments().get(0));
switch (statusCode) {
case 200:
return replaceMethod(m, "isOk()");
Expand Down Expand Up @@ -82,11 +83,26 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return m;
}

private int extractStatusCode(Expression expression) {
if (expression instanceof J.Literal) {
Object raw = ((J.Literal) expression).getValue();
if (raw instanceof Integer) {
return (int) raw;
}
}
return -1; // HttpStatus is not yet supported
}

private J.MethodInvocation replaceMethod(J.MethodInvocation method, String methodName) {
JavaTemplate template = JavaTemplate.builder(methodName).build();
J.MethodInvocation methodInvocation = template.apply(getCursor(), method.getCoordinates().replaceMethod());
JavaType.Method type = methodInvocation.getMethodType().withParameterNames(emptyList()).withParameterTypes(emptyList());
return methodInvocation.withArguments(emptyList()).withMethodType(type).withName(methodInvocation.getName().withType(type));
J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates().replaceMethod());
JavaType.Method type = methodInvocation
.getMethodType()
.withParameterNames(emptyList())
.withParameterTypes(emptyList());
return methodInvocation
.withArguments(emptyList())
.withMethodType(type)
.withName(methodInvocation.getName().withType(type));

}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@
import org.openrewrite.DocumentExample;
import org.openrewrite.InMemoryExecutionContext;
import org.openrewrite.java.JavaParser;
import org.openrewrite.kotlin.KotlinParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;
import static org.openrewrite.kotlin.Assertions.kotlin;

class SimplifyWebTestClientCallsTest implements RewriteTest {

Expand All @@ -34,25 +36,27 @@ public void defaults(RecipeSpec spec) {
spec
.recipe(new SimplifyWebTestClientCalls())
.parser(JavaParser.fromJavaVersion()
.classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6"))
.parser(KotlinParser.builder()
.classpathFromResources(new InMemoryExecutionContext(), "spring-web-6", "spring-test-6"));
}

@DocumentExample
@ParameterizedTest
@CsvSource({
"200,isOk()",
"201,isCreated()",
"202,isAccepted()",
"204,isNoContent()",
"302,isFound()",
"303,isSeeOther()",
"304,isNotModified()",
"307,isTemporaryRedirect()",
"308,isPermanentRedirect()",
"400,isBadRequest()",
"401,isUnauthorized()",
"403,isForbidden()",
"404,isNotFound()"
"200,isOk()",
"201,isCreated()",
"202,isAccepted()",
"204,isNoContent()",
"302,isFound()",
"303,isSeeOther()",
"304,isNotModified()",
"307,isTemporaryRedirect()",
"308,isPermanentRedirect()",
"400,isBadRequest()",
"401,isUnauthorized()",
"403,isForbidden()",
"404,isNotFound()"
})
void replacesAllIntStatusCodes(String httpStatus, String method) {
rewriteRun(
Expand Down Expand Up @@ -94,6 +98,47 @@ void someMethod() {
);
}

@Test
void replaceKotlinInt() {
rewriteRun(
//language=kotlin
kotlin(
"""
import org.springframework.test.web.reactive.server.WebTestClient
class Test {
val webClient: WebTestClient = WebTestClient.bindToServer().build()
fun someMethod() {
webClient
.post()
.uri("/some/url")
.bodyValue("someValue")
.exchange()
.expectStatus()
.isEqualTo(200)
}
}
""",
"""
import org.springframework.test.web.reactive.server.WebTestClient
class Test {
val webClient: WebTestClient = WebTestClient.bindToServer().build()
fun someMethod() {
webClient
.post()
.uri("/some/url")
.bodyValue("someValue")
.exchange()
.expectStatus()
.isOk()
}
}
"""
)
);
}

@Test
void doesNotReplaceUnspecificStatusCode() {
rewriteRun(
Expand Down

0 comments on commit 4192cfb

Please sign in to comment.