Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
db0f224
WiP
steve-aom-elliott Nov 21, 2025
1ee3aac
WiP 2 - am able to get `.build()` added, but seems to drop the after …
steve-aom-elliott Nov 24, 2025
a5d633f
Expanding tests and conversions to include headers methods
steve-aom-elliott Nov 25, 2025
e05bb49
Adapted the failing recipe to add as much debug/retry information as …
Jenson3210 Nov 27, 2025
bea4715
Reapplying paddings from arguments as they were prior to chaining `.b…
steve-aom-elliott Nov 27, 2025
03d0f2a
wip
steve-aom-elliott Dec 11, 2025
e4c5f2a
WiP - Ripped out a lot
steve-aom-elliott Dec 12, 2025
78bd41e
wip classpath jars
steve-aom-elliott Jan 7, 2026
86ba868
iterate over a list of old to new replacement methods
dsgrieve Jan 12, 2026
349a3af
use template for adding .build() on MockResponseBuilder
dsgrieve Jan 14, 2026
de9f20c
try to handle chained call
dsgrieve Jan 15, 2026
6533f7e
inline static patchReturnTypeAndName method call
dsgrieve Jan 15, 2026
04357bd
Apply suggestions from code review
dsgrieve Jan 15, 2026
701dbc6
Update src/test/java/org/openrewrite/java/testing/junit5/UpgradeOkHtt…
dsgrieve Jan 15, 2026
63af1a9
Update src/main/java/org/openrewrite/java/testing/junit5/UpdateMockWe…
dsgrieve Jan 15, 2026
db04069
patch .build() return type and name
dsgrieve Jan 16, 2026
1112188
replace edits lost in merge
dsgrieve Jan 16, 2026
c56c46c
update recipes.csv
dsgrieve Jan 16, 2026
1c4be6a
add test for method name changes and test with MockResponse return type
dsgrieve Jan 19, 2026
b5faa24
fix unit tests
dsgrieve Jan 19, 2026
76c47bb
clean up
dsgrieve Jan 21, 2026
e43c594
convert MockResponse "with" methods
dsgrieve Jan 21, 2026
a471228
fix line wrap
dsgrieve Jan 21, 2026
b7ba58c
Use separate test classes for the two cases
timtebeek Jan 22, 2026
e14c33b
Remove unused field
timtebeek Jan 22, 2026
ee6d74d
remove duplicate code in patchReturnTypeAndName
dsgrieve Jan 22, 2026
e0ab385
Inline the delegating `patchReturnTypeAndName`
timtebeek Jan 22, 2026
a269a71
Replace the ChangePackage with a call to UpdateMockWebServerMockResponse
timtebeek Jan 22, 2026
cdb7af1
Use `ChangeMethodName` instead of `MethodInvocationReplacement`
timtebeek Jan 22, 2026
ca0cc34
Inline the `patchReturnTypeAndName` used once
timtebeek Jan 22, 2026
6cbc52b
Move existing recipes into `getRecipeList()`
timtebeek Jan 22, 2026
ed5e923
Move replacements down to only usage
timtebeek Jan 22, 2026
282594b
Change `methodInvocationsToAdjust` to local field
timtebeek Jan 22, 2026
2e589c0
Drop unnecessary list of indices
timtebeek Jan 22, 2026
f381a20
Always carry over prefix from arg
timtebeek Jan 22, 2026
5edb94a
Simplify `patchBuilderBuildReturnTypeAndName`
timtebeek Jan 22, 2026
42e7ed1
Use inlined `JavaType.ShallowClass.build`
timtebeek Jan 22, 2026
61bd01d
Inline `patchBuilderBuildReturnTypeAndName`
timtebeek Jan 22, 2026
6e7bc5b
Final touches
timtebeek Jan 22, 2026
3b43f53
Merge branch 'main' into 1589-correcting-mockwebserver-migration
timtebeek Jan 22, 2026
5d3439e
Change return type before renaming method; add missing imports
timtebeek Jan 22, 2026
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
5 changes: 5 additions & 0 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ recipeDependencies {
parserClasspath("com.google.guava:guava:33.5.0-jre")
parserClasspath("com.squareup.okhttp3:mockwebserver:3.14.9")
parserClasspath("com.squareup.okhttp3:mockwebserver:4.10.0")
parserClasspath("com.squareup.okhttp3:mockwebserver3:5.1.0")
parserClasspath("junit:junit:4.+")
parserClasspath("org.apiguardian:apiguardian-api:1.1.2")
parserClasspath("org.assertj:assertj-core:3.+")
Expand Down Expand Up @@ -60,6 +61,10 @@ recipeDependencies {
testParserClasspath("org.testcontainers:testcontainers-kafka:2.0.1")
testParserClasspath("org.testcontainers:testcontainers-localstack:2.0.1")
testParserClasspath("org.testcontainers:testcontainers-mysql:2.0.1")

testParserClasspath("com.squareup.okhttp3:okhttp:4.10.0")
testParserClasspath("com.squareup.okhttp3:okhttp-jvm:5.1.0")
testParserClasspath("com.squareup.okio:okio:3.12.0")
}

val rewriteVersion = rewriteRecipe.rewriteVersion.get()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* Copyright 2025 the original author or authors.
* <p>
* Licensed under the Moderne Source Available License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* <p>
* https://docs.moderne.io/licensing/moderne-source-available-license
* <p>
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.openrewrite.java.testing.junit5;

import lombok.Getter;
import org.openrewrite.*;
import org.openrewrite.internal.ListUtils;
import org.openrewrite.java.*;
import org.openrewrite.java.search.UsesType;
import org.openrewrite.java.tree.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class UpdateMockWebServerMockResponse extends Recipe {
private static final String OLD_MOCKRESPONSE_FQN = "okhttp3.mockwebserver.MockResponse";
private static final String NEW_MOCKRESPONSE_FQN = "mockwebserver3.MockResponse";
private static final String NEW_MOCKRESPONSE_FQN_BUILDER = NEW_MOCKRESPONSE_FQN + "$Builder";

@Getter
final String displayName = "OkHttp `MockWebServer` `MockResponse` to 5.x `MockWebServer3` `MockResponse`";

@Getter
final String description = "Replace usages of OkHttp MockWebServer `MockResponse` with 5.x MockWebServer3 `MockResponse` and it's `Builder`.";

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return Preconditions.check(new UsesType<>(OLD_MOCKRESPONSE_FQN, false), new JavaIsoVisitor<ExecutionContext>() {
@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext ctx) {
J.MethodInvocation mi = super.visitMethodInvocation(method, ctx);
return mi.withArguments(ListUtils.map(mi.getArguments(),
arg -> TypeUtils.isAssignableTo(OLD_MOCKRESPONSE_FQN, arg.getType()) ?
// Wrap MockResponse.Builder arguments with .build()
appendBuildInvocation(ctx, arg) : arg));
}

private J.MethodInvocation appendBuildInvocation(ExecutionContext ctx, Expression arg) {
String nl = arg instanceof J.MethodInvocation ? "\n" : "";
J.MethodInvocation builder = JavaTemplate
.builder("#{any(" + NEW_MOCKRESPONSE_FQN_BUILDER + ")}" + nl + ".build()")
.javaParser(JavaParser.fromJavaVersion().classpathFromResources(ctx, "mockwebserver3"))
.imports("mockwebserver3.MockResponse", "mockwebserver3.MockResponse.Builder")
.build()
.apply(new Cursor(getCursor(), arg), arg.getCoordinates().replace(), arg);
JavaType.Method buildMethodType = new JavaType.Method(
null,
Flag.Public.getBitMask() | Flag.Final.getBitMask(),
JavaType.ShallowClass.build(NEW_MOCKRESPONSE_FQN_BUILDER),
"build",
JavaType.ShallowClass.build(NEW_MOCKRESPONSE_FQN),
(List<String>) null,
null,
null,
null,
null,
null
);
return builder.withPrefix(arg.getPrefix())
.withMethodType(buildMethodType)
.withName(builder.getName().withType(buildMethodType));
}
});
}

private static final Map<String, String> REPLACEMENTS = new HashMap<String, String>() {{
put("setBody(*)", "body");
put("setBodyDelay(long, java.util.concurrent.TimeUnit)", "bodyDelay");
put("setChunkedBody(*, int)", "chunkedBody");
put("setErrorCode(int)", "code");
put("setHeaders(okhttp3.Headers)", "headers");
put("setHeadersDelay(long, java.util.concurrent.TimeUnit)", "headersDelay");
put("setHttp2ErrorCode(int)", "code");
put("setResponseCode(int)", "code");
put("setStatus(java.lang.String)", "status");
put("setThrottleBody(long, long, java.util.concurrent.TimeUnit)", "throttleBody");
put("setTrailers(okhttp3.Headers)", "trailers");
put("withPush(okhttp3.mockwebserver.PushPromise)", "addPush");
put("withSettings(okhttp3.internal.http2.Settings)", "settings");
put("withWebSocketUpgrade(okhttp3.WebSocketListener)", "webSocketUpgrade");
}};

@Override
public List<Recipe> getRecipeList() {
List<Recipe> recipes = new ArrayList<>();
for (Map.Entry<String, String> rep : REPLACEMENTS.entrySet()) {
String methodPattern = OLD_MOCKRESPONSE_FQN.replace("$", ".") + "#" + rep.getKey();
recipes.add(new ChangeMethodInvocationReturnType(methodPattern, NEW_MOCKRESPONSE_FQN_BUILDER.replace("$", ".")));
recipes.add(new ChangeMethodName(methodPattern, rep.getValue(), true, false));
}
recipes.add(new ChangeType(OLD_MOCKRESPONSE_FQN, NEW_MOCKRESPONSE_FQN_BUILDER, true));
recipes.add(new ChangePackage("okhttp3.mockwebserver", "mockwebserver3", false));
return recipes;
}
}
Loading
Loading