diff --git a/src/main/resources/META-INF/rewrite/classpath/okhttp-4.12.0.jar b/src/main/resources/META-INF/rewrite/classpath/okhttp-4.12.0.jar new file mode 100644 index 000000000..faf3fa860 Binary files /dev/null and b/src/main/resources/META-INF/rewrite/classpath/okhttp-4.12.0.jar differ diff --git a/src/main/resources/META-INF/rewrite/okhttp-exlicit-version.yml b/src/main/resources/META-INF/rewrite/okhttp-exlicit-version.yml new file mode 100644 index 000000000..474276c4b --- /dev/null +++ b/src/main/resources/META-INF/rewrite/okhttp-exlicit-version.yml @@ -0,0 +1,31 @@ +# +# Copyright 2024 the original author or authors. +#

+# 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 +#

+# https://www.apache.org/licenses/LICENSE-2.0 +#

+# 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.java.boot3.OkHttpExplicitVersion +displayName: Add explicit `OkHttp` Dependency +description: Adds `OkHttp` as an explicit dependency if it is used in the project. +recipeList: + - org.openrewrite.java.dependencies.AddDependency: + groupId: com.squareup.okhttp3 + artifactId: okhttp-bom + version: 4.12.0 + onlyIfUsing: okhttp3.* + classifier: import + extension: pom + configuration: implementation + scope: runtime + type: pom diff --git a/src/test/java/org/openrewrite/java/spring/boot3/OkHttpExplicitVersionTest.java b/src/test/java/org/openrewrite/java/spring/boot3/OkHttpExplicitVersionTest.java new file mode 100644 index 000000000..cd0b6658c --- /dev/null +++ b/src/test/java/org/openrewrite/java/spring/boot3/OkHttpExplicitVersionTest.java @@ -0,0 +1,121 @@ +/* + * Copyright 2024 the original author or authors. + *

+ * 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 + *

+ * https://www.apache.org/licenses/LICENSE-2.0 + *

+ * 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.spring.boot3; + +import org.junit.jupiter.api.Test; +import org.openrewrite.DocumentExample; +import org.openrewrite.InMemoryExecutionContext; +import org.openrewrite.java.JavaParser; +import org.openrewrite.test.RecipeSpec; +import org.openrewrite.test.RewriteTest; + +import static org.openrewrite.java.Assertions.*; +import static org.openrewrite.maven.Assertions.pomXml; + +class OkHttpExplicitVersionTest implements RewriteTest { + + @Override + public void defaults(RecipeSpec spec) { + spec.recipeFromResources("org.openrewrite.java.boot3.OkHttpExplicitVersion") + .parser(JavaParser.fromJavaVersion() + .classpathFromResources(new InMemoryExecutionContext(), + "okhttp")); + } + + @DocumentExample + @Test + void addOkHttpDependencyIfUsed() { + rewriteRun( + mavenProject("example-project", + srcMainJava( + java( + """ + import okhttp3.Request; + + public class Test { + public static void main(String[] args) { + Request request = new Request.Builder() + .url("url") + .build(); + } + } + """ + ) + ), + pomXml( + """ + + 4.0.0 + com.example + example-project + 1.0-SNAPSHOT + + """, + """ + + 4.0.0 + com.example + example-project + 1.0-SNAPSHOT + + + com.squareup.okhttp3 + okhttp-bom + 4.12.0 + import + pom + runtime + + + + """ + ) + ) + ); + } + + @Test + void doNothingifUnused() { + rewriteRun( + mavenProject("example-project", + srcMainJava( + java( + """ + public class Test { + public static void main(String[] args) { + + } + } + """ + ) + ), + pomXml( + """ + + 4.0.0 + com.example + example-project + 1.0-SNAPSHOT + + """ + ) + ) + ); + } +}