Skip to content

Commit d0364cb

Browse files
Add support for HttpStatus in SimplifyWebTestClientCalls (#664)
* add support for HttpStatus in SimplifyWebTestClientCalls * Minor polish * Apply code suggestions --------- Co-authored-by: Tim te Beek <tim@moderne.io>
1 parent 4192cfb commit d0364cb

File tree

2 files changed

+151
-6
lines changed

2 files changed

+151
-6
lines changed

src/main/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCalls.java

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.openrewrite.java.tree.J;
2828
import org.openrewrite.java.tree.JavaType;
2929

30+
import java.util.List;
31+
3032
import static java.util.Collections.emptyList;
3133

3234
public class SimplifyWebTestClientCalls extends Recipe {
@@ -84,16 +86,65 @@ public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, Execu
8486
}
8587

8688
private int extractStatusCode(Expression expression) {
87-
if (expression instanceof J.Literal) {
89+
if (expression instanceof J.FieldAccess) {
90+
//isEqualTo(HttpStatus.OK)
91+
J.FieldAccess fa = (J.FieldAccess) expression;
92+
if (fa.getTarget() instanceof J.Identifier) {
93+
if ("HttpStatus".equals(((J.Identifier) fa.getTarget()).getSimpleName())) {
94+
switch (fa.getSimpleName()) {
95+
case "OK":
96+
return 200;
97+
case "CREATED":
98+
return 201;
99+
case "ACCEPTED":
100+
return 202;
101+
case "NO_CONTENT":
102+
return 204;
103+
case "FOUND":
104+
return 302;
105+
case "SEE_OTHER":
106+
return 303;
107+
case "NOT_MODIFIED":
108+
return 304;
109+
case "TEMPORARY_REDIRECT":
110+
return 307;
111+
case "PERMANENT_REDIRECT":
112+
return 308;
113+
case "BAD_REQUEST":
114+
return 400;
115+
case "UNAUTHORIZED":
116+
return 401;
117+
case "FORBIDDEN":
118+
return 403;
119+
case "NOT_FOUND":
120+
return 404;
121+
}
122+
}
123+
}
124+
} else if (expression instanceof J.Literal) {
125+
//isEqualTo(200)
88126
Object raw = ((J.Literal) expression).getValue();
89127
if (raw instanceof Integer) {
90128
return (int) raw;
91129
}
130+
} else if (expression instanceof J.MethodInvocation) {
131+
//isEqualTo(HttpStatus.valueOf(200))
132+
//isEqualTo(HttpStatusCode.valueOf(200))
133+
J.MethodInvocation methodInvocation = (J.MethodInvocation) expression;
134+
List<Expression> arguments = methodInvocation.getArguments();
135+
if (arguments.size() == 1 && arguments.get(0) instanceof J.Literal) {
136+
Object raw = ((J.Literal) arguments.get(0)).getValue();
137+
if (raw instanceof Integer) {
138+
return (int) raw;
139+
}
140+
}
92141
}
93-
return -1; // HttpStatus is not yet supported
142+
return -1;
94143
}
95144

96145
private J.MethodInvocation replaceMethod(J.MethodInvocation method, String methodName) {
146+
maybeRemoveImport("org.springframework.http.HttpStatus");
147+
maybeRemoveImport("org.springframework.http.HttpStatusCode");
97148
J.MethodInvocation methodInvocation = JavaTemplate.apply(methodName, getCursor(), method.getCoordinates().replaceMethod());
98149
JavaType.Method type = methodInvocation
99150
.getMethodType()
@@ -103,7 +154,6 @@ private J.MethodInvocation replaceMethod(J.MethodInvocation method, String metho
103154
.withArguments(emptyList())
104155
.withMethodType(type)
105156
.withName(methodInvocation.getName().withType(type));
106-
107157
}
108158
});
109159
}

src/test/java/org/openrewrite/java/spring/http/SimplifyWebTestClientCallsTest.java

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
*/
1616
package org.openrewrite.java.spring.http;
1717

18-
import org.junit.jupiter.api.Disabled;
1918
import org.junit.jupiter.api.Test;
2019
import org.junit.jupiter.params.ParameterizedTest;
2120
import org.junit.jupiter.params.provider.CsvSource;
@@ -165,14 +164,14 @@ void someMethod() {
165164
}
166165

167166
@Test
168-
@Disabled("Yet to be implemented")
169-
void usesIsOkForHttpStatus200() {
167+
void usesIsOkForHttpStatusValueOf200() {
170168
rewriteRun(
171169
//language=java
172170
java(
173171
"""
174172
import org.springframework.test.web.reactive.server.WebTestClient;
175173
import org.springframework.http.HttpStatus;
174+
176175
class Test {
177176
private final WebTestClient webClient = WebTestClient.bindToServer().build();
178177
void someMethod() {
@@ -187,6 +186,47 @@ void someMethod() {
187186
""",
188187
"""
189188
import org.springframework.test.web.reactive.server.WebTestClient;
189+
190+
class Test {
191+
private final WebTestClient webClient = WebTestClient.bindToServer().build();
192+
void someMethod() {
193+
webClient
194+
.post()
195+
.uri("/some/value")
196+
.exchange()
197+
.expectStatus()
198+
.isOk();
199+
}
200+
}
201+
"""
202+
)
203+
);
204+
}
205+
206+
@Test
207+
void usesIsOkForHttpStatusValueCodeOf200() {
208+
rewriteRun(
209+
//language=java
210+
java(
211+
"""
212+
import org.springframework.test.web.reactive.server.WebTestClient;
213+
import org.springframework.http.HttpStatusCode;
214+
215+
class Test {
216+
private final WebTestClient webClient = WebTestClient.bindToServer().build();
217+
void someMethod() {
218+
webClient
219+
.post()
220+
.uri("/some/value")
221+
.exchange()
222+
.expectStatus()
223+
.isEqualTo(HttpStatusCode.valueOf(200));
224+
}
225+
}
226+
""",
227+
"""
228+
import org.springframework.test.web.reactive.server.WebTestClient;
229+
190230
class Test {
191231
private final WebTestClient webClient = WebTestClient.bindToServer().build();
192232
void someMethod() {
@@ -203,6 +243,61 @@ void someMethod() {
203243
);
204244
}
205245

246+
@ParameterizedTest
247+
@CsvSource({
248+
"OK,isOk()",
249+
"CREATED,isCreated()",
250+
"ACCEPTED,isAccepted()",
251+
"NO_CONTENT,isNoContent()",
252+
"FOUND,isFound()",
253+
"SEE_OTHER,isSeeOther()",
254+
"NOT_MODIFIED,isNotModified()",
255+
"TEMPORARY_REDIRECT,isTemporaryRedirect()",
256+
"PERMANENT_REDIRECT,isPermanentRedirect()",
257+
"BAD_REQUEST,isBadRequest()",
258+
"UNAUTHORIZED,isUnauthorized()",
259+
"FORBIDDEN,isForbidden()",
260+
"NOT_FOUND,isNotFound()"
261+
})
262+
void usesIsOkForHttpStatusValue(String httpStatus, String method) {
263+
rewriteRun(
264+
//language=java
265+
java(
266+
"""
267+
import org.springframework.test.web.reactive.server.WebTestClient;
268+
import org.springframework.http.HttpStatus;
269+
270+
class Test {
271+
private final WebTestClient webClient = WebTestClient.bindToServer().build();
272+
void someMethod() {
273+
webClient
274+
.post()
275+
.uri("/some/value")
276+
.exchange()
277+
.expectStatus()
278+
.isEqualTo(HttpStatus.%s);
279+
}
280+
}
281+
""".formatted(httpStatus),
282+
"""
283+
import org.springframework.test.web.reactive.server.WebTestClient;
284+
285+
class Test {
286+
private final WebTestClient webClient = WebTestClient.bindToServer().build();
287+
void someMethod() {
288+
webClient
289+
.post()
290+
.uri("/some/value")
291+
.exchange()
292+
.expectStatus()
293+
.%s;
294+
}
295+
}
296+
""".formatted(method)
297+
)
298+
);
299+
}
300+
206301
@Test
207302
void doesNotUseIsOkForHttpStatus300() {
208303
rewriteRun(

0 commit comments

Comments
 (0)