Skip to content

Commit

Permalink
Addressing deprecations in RestTemplateBuilder (#667)
Browse files Browse the repository at this point in the history
* Addressing deprecations in RestTemplateBuilder

* Apply suggestions from code review

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>

* Slight polish

* Move test and provide necessary classpath entries

* Similarly move `ReplaceMockBeanAndSpyBeanTest`

* Add language hints to ReplaceMockBeanAndSpyBeanTest

---------

Co-authored-by: Tim te Beek <[email protected]>
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
Co-authored-by: Tim te Beek <[email protected]>
  • Loading branch information
4 people authored Jan 15, 2025
1 parent dd8d341 commit b83c747
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 13 deletions.
5 changes: 4 additions & 1 deletion build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
group = "org.openrewrite.recipe"
description = "Eliminate legacy Spring patterns and migrate between major Spring Boot versions. Automatically."

val springBootVersions: List<String> = listOf("1_5", "2_1", "2_2", "2_3", "2_4", "2_5", "2_6", "2_7", "3_0", "3_2", "3_3")
val springBootVersions: List<String> = listOf("1_5", "2_1", "2_2", "2_3", "2_4", "2_5", "2_6", "2_7", "3_0", "3_2", "3_3", "3_4")
val springSecurityVersions: List<String> = listOf("5_7", "5_8", "6_2")

val sourceSetNames: Map<String, List<String>> = mapOf(
Expand Down Expand Up @@ -261,6 +261,9 @@ dependencies {
"testWithSpringBoot_3_2RuntimeOnly"("org.springframework.boot:spring-boot-starter:3.2.+")
"testWithSpringBoot_3_2RuntimeOnly"("org.springframework.boot:spring-boot-starter-test:3.2.+")

"testWithSpringBoot_3_4RuntimeOnly"("org.springframework.boot:spring-boot:3.4.+")
"testWithSpringBoot_3_4RuntimeOnly"("org.springframework:spring-web:6.2.+")

"testWithSpringSecurity_5_7RuntimeOnly"("org.springframework:spring-context:5.3.+")
"testWithSpringSecurity_5_7RuntimeOnly"("org.springframework.boot:spring-boot-starter:2.7.+")
"testWithSpringSecurity_5_7RuntimeOnly"("org.springframework.boot:spring-boot:2.7.+")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#
# Copyright 2024 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.
#
---
type: specs.openrewrite.org/v1beta/recipe
name: org.openrewrite.java.boot3.ReplaceRestTemplateBuilderMethods
displayName: Replace deprecated setters in `RestTemplateBuilder`
description: Replaces `setConnectTimeout`, `setReadTimeout`, and `setSslBundle` method invocations with `connectTimeout`, `readTimeout`, and `sslBundle` respectively.
recipeList:
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.boot.web.client.RestTemplateBuilder setConnectTimeout(java.time.Duration)
newMethodName: connectTimeout
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.boot.web.client.RestTemplateBuilder setReadTimeout(java.time.Duration)
newMethodName: readTimeout
- org.openrewrite.java.ChangeMethodName:
methodPattern: org.springframework.boot.web.client.RestTemplateBuilder setSslBundle(org.springframework.boot.ssl.SslBundle)
newMethodName: sslBundle
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public void defaults(RecipeSpec spec) {
@Test
void replacesMockBeanWithMockitoBean() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -48,7 +48,6 @@ public class SomeTest {
private String someService;
}
""",
// Expected output after applying the recipe
"""
import org.springframework.test.context.bean.override.mockito.MockitoBean;
Expand All @@ -64,7 +63,7 @@ public class SomeTest {
@Test
void replacesMockBeanWithMockitoBeanWithAttributes() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.mockito.Answers;
Expand All @@ -75,7 +74,6 @@ public class SomeTest {
private String someService;
}
""",
// Expected output after applying the recipe
"""
import org.mockito.Answers;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
Expand All @@ -92,7 +90,7 @@ public class SomeTest {
@Test
void replacesMockBeanWithParamsWithMockitoBeanWithParams() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -102,7 +100,6 @@ public class SomeTest {
private String someService;
}
""",
// Expected output after applying the recipe
"""
import org.springframework.test.context.bean.override.mockito.MockitoBean;
Expand All @@ -118,6 +115,7 @@ public class SomeTest {
@Test
void doesNotChangeOtherAnnotations() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.MockBean;
Expand Down Expand Up @@ -146,6 +144,7 @@ public class SomeTest {
@Test
void handlesNoMockBeanImport() {
rewriteRun(
//language=java
java(
"""
public class SomeTest {
Expand All @@ -166,7 +165,7 @@ public class SomeTest {
@Test
void replacesMockBeanWithMockitoBeanAndSpyBeanWithMockitoSpyBean() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.MockBean;
Expand All @@ -180,7 +179,6 @@ public class SomeTest {
private String someMockService;
}
""",
// Expected output after applying the recipe
"""
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
Expand All @@ -200,7 +198,7 @@ public class SomeTest {
@Test
void replacesSpyBeanWithMockitoSpyBean() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.SpyBean;
Expand All @@ -210,7 +208,6 @@ public class SomeTest {
private String someService;
}
""",
// Expected output after applying the recipe
"""
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
Expand All @@ -226,7 +223,7 @@ public class SomeTest {
@Test
void replacesSpyBeanWithMockitoSpyBeanwithAttributes() {
rewriteRun(
// Input source file before applying the recipe
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.SpyBean;
Expand All @@ -236,7 +233,6 @@ public class SomeTest {
private String someService;
}
""",
// Expected output after applying the recipe
"""
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
Expand All @@ -252,6 +248,7 @@ public class SomeTest {
@Test
void doesNotChangeOtherAnnotationsSpyBean() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.test.mock.mockito.SpyBean;
Expand Down Expand Up @@ -280,6 +277,7 @@ public class SomeTest {
@Test
void handlesNoSpyBeanImport() {
rewriteRun(
//language=java
java(
"""
public class SomeTest {
Expand All @@ -300,6 +298,7 @@ public class SomeTest {
@Test
void doesNothingWhenNoAnnotationPresent() {
rewriteRun(
//language=java
java(
"""
public class SomeTest {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
/*
* Copyright 2024 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.spring.boot3;

import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test;
import org.openrewrite.DocumentExample;
import org.openrewrite.java.JavaParser;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;

import static org.openrewrite.java.Assertions.java;

class ReplaceRestTemplateBuilderMethodsTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
spec.recipeFromResources("org.openrewrite.java.boot3.ReplaceRestTemplateBuilderMethods")
.parser(JavaParser.fromJavaVersion().classpath("spring-boot", "spring-web"));
}

@DocumentExample
@Test
void replacesSetConnectTimeout() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
class Example {
public void configure(RestTemplateBuilder builder) {
builder.setConnectTimeout(java.time.Duration.ofSeconds(10));
}
}
""",
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
class Example {
public void configure(RestTemplateBuilder builder) {
builder.connectTimeout(java.time.Duration.ofSeconds(10));
}
}
"""
)
);
}

@Test
void replacesSetReadTimeout() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
class Example {
public void configure(RestTemplateBuilder builder) {
builder.setReadTimeout(java.time.Duration.ofSeconds(10));
}
}
""",
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
class Example {
public void configure(RestTemplateBuilder builder) {
builder.readTimeout(java.time.Duration.ofSeconds(10));
}
}
"""
)
);
}

@Test
void replacesSetSslBundle() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.ssl.SslBundle;
class Example {
public void configure(RestTemplateBuilder builder, SslBundle sslBundle) {
builder.setSslBundle(sslBundle);
}
}
""",
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.ssl.SslBundle;
class Example {
public void configure(RestTemplateBuilder builder, SslBundle sslBundle) {
builder.sslBundle(sslBundle);
}
}
"""
)
);
}

@Disabled("Not yet implemented")
@Test
void replacesRequestFactory() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
class Example {
public void configure(RestTemplateBuilder builder, ClientHttpRequestFactory factory) {
builder.requestFactory(factory);
}
}
""",
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
class Example {
public void configure(RestTemplateBuilder builder, ClientHttpRequestFactory factory) {
builder.requestFactoryBuilder(factory);
}
}
"""
)
);
}

@Test
void doesNothingWhenNoDeprecatedMethodsPresent() {
rewriteRun(
//language=java
java(
"""
import org.springframework.boot.web.client.RestTemplateBuilder;
class Example {
public void configure(RestTemplateBuilder builder) {
builder.additionalCustomizers(customizer -> {});
}
}
"""
)
);
}
}

0 comments on commit b83c747

Please sign in to comment.