Skip to content

Commit

Permalink
Simplify after upstream fix in rewrite-kotlin
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Jan 11, 2025
1 parent 9ce9211 commit 4014f2d
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 84 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@

public class SimplifyWebTestClientCalls extends Recipe {

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

@Override
public String getDisplayName() {
Expand All @@ -52,7 +52,7 @@ public TreeVisitor<?, ExecutionContext> getVisitor() {
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())) {
final int statusCode = extractStatusCode(m);
final int statusCode = extractStatusCode(m.getArguments().get(0));
switch (statusCode) {
case 200:
return replaceMethod(m, "isOk()");
Expand Down Expand Up @@ -85,26 +85,14 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
return m;
}

private int extractStatusCode(J.MethodInvocation m) {
List<Expression> arguments = m.getArguments();
if (arguments.size() != 1) {
throw new IllegalArgumentException("Status code must be provided as the single argument to isEqualTo but received " + arguments);
}
Expression expression = arguments.get(0);
private int extractStatusCode(Expression expression) {
if (expression instanceof J.Literal) {
Object raw = ((J.Literal) expression).getValue();
if (raw instanceof Integer) {
return (int) raw;
} else if (raw instanceof Long) {
return ((Long) raw).intValue();
} else {
throw new IllegalArgumentException("Status code must be an int or long but received " + raw);
}
} else if (expression instanceof J.MethodInvocation) {
return -1; //HttpStatus is not yet supported
} else {
throw new IllegalArgumentException("First argument to isEqualTo must be a literal but received " + expression);
}
return -1; // HttpStatus is not yet supported
}

private J.MethodInvocation replaceMethod(J.MethodInvocation method, String methodName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.openrewrite.java.spring.http;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
Expand Down Expand Up @@ -45,19 +44,19 @@ public void defaults(RecipeSpec spec) {
@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 @@ -99,64 +98,45 @@ void someMethod() {
);
}

@Nested
class KotlinTest {
@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()"
})
void replacesAllIntStatusCodes(String httpStatus, String method) {
rewriteRun(
//language=kotlin
kotlin(
"""
import org.springframework.test.web.reactive.server.WebTestClient
@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(%s)
}
class Test {
val webClient: WebTestClient = WebTestClient.bindToServer().build()
fun someMethod() {
webClient
.post()
.uri("/some/url")
.bodyValue("someValue")
.exchange()
.expectStatus()
.isEqualTo(200)
}
""".formatted(httpStatus),
"""
import org.springframework.test.web.reactive.server.WebTestClient
}
""",
"""
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()
.%s
}
class Test {
val webClient: WebTestClient = WebTestClient.bindToServer().build()
fun someMethod() {
webClient
.post()
.uri("/some/url")
.bodyValue("someValue")
.exchange()
.expectStatus()
.isOk()
}
""".formatted(method)
)
);
}
}
"""
)
);
}

@Test
Expand Down

0 comments on commit 4014f2d

Please sign in to comment.