Skip to content

Commit

Permalink
feat: add okhttp4 recipe (#8)
Browse files Browse the repository at this point in the history
* feat: okhttp4

* Update src/test/java/org/openrewrite/okhttp/MigrateToOkHttp4Test.java

Co-authored-by: Tim te Beek <[email protected]>

* fix: remove extra )

* Apply suggestions from code review

---------

Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
SimonVerhoeven and timtebeek authored Nov 30, 2023
1 parent 7238220 commit 5f2cb15
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
36 changes: 36 additions & 0 deletions src/main/resources/META-INF/rewrite/okhttp-4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#
# Copyright 2023 the original author or authors.
# <p>
# Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
# <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.
#

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.okhttp.UpgradeOkHttp4
displayName: Migrate to OkHttp 4.x
description: This recipe will apply changes commonly needed when migrating to OkHttp 4.x.
recipeList:
# https://square.github.io/okhttp/changelogs/upgrading_to_okhttp_4/
- org.openrewrite.okhttp.UpgradeOkHttp4Dependencies
- org.openrewrite.okio.UpgradeOkio3

---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.okhttp.UpgradeOkHttp4Dependencies
displayName: Migrate OkHttp dependencies to 4.x
description: Migrate OkHttp dependencies to 4.x.
recipeList:
- org.openrewrite.java.dependencies.ChangeDependency:
oldGroupId: com.squareup.okhttp3
oldArtifactId: okhttp
newVersion: 4.x
2 changes: 1 addition & 1 deletion src/main/resources/META-INF/rewrite/okhttp-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ displayName: Migrate to OkHttp 5.x
description: This recipe will apply changes commonly needed when migrating to OkHttp 5.x.
recipeList:
# https://square.github.io/okhttp/changelogs/changelog/
- org.openrewrite.okhttp.UpgradeOkHttp4
- org.openrewrite.okhttp.UpgradeOkHttp5Dependencies
- org.openrewrite.okhttp.ReorderRequestBodyCreateArguments
- org.openrewrite.okio.UpgradeOkio3

---
type: specs.openrewrite.org/v1beta/recipe
Expand Down
87 changes: 87 additions & 0 deletions src/test/java/org/openrewrite/okhttp/MigrateToOkHttp4Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* Copyright 2023 the original author or authors.
* <p>
* Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
* <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.okhttp;

import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.openrewrite.maven.Assertions.pomXml;

class MigrateToOkHttp4Test implements RewriteTest {

public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.okhttp.UpgradeOkHttp4");
}

@Nested
class Dependencies {
@Test
@DocumentExample
void mavenDependency4x() {
rewriteRun(
//language=xml
pomXml(
"""
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>3.14.9</version>
</dependency>
</dependencies>
</project>
""",
spec -> spec.after(actual -> {
Matcher matcher = Pattern.compile("<version>(4\\.\\d+\\.\\d+(-(alpha|beta)\\.\\d+)?)</version>").matcher(actual);
assertTrue(matcher.find(), actual);
return """
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>%s</version>
</dependency>
</dependencies>
</project>
""".formatted(matcher.group(1));
}
)
)
);
}
}
}

0 comments on commit 5f2cb15

Please sign in to comment.