-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Addressing deprecations in RestTemplateBuilder (#667)
* 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
1 parent
dd8d341
commit b83c747
Showing
4 changed files
with
211 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/resources/META-INF/rewrite/replace-methods-rest-template-builder.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
166 changes: 166 additions & 0 deletions
166
...oot_3_4/java/org/openrewrite/java/spring/boot3/ReplaceRestTemplateBuilderMethodsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 -> {}); | ||
} | ||
} | ||
""" | ||
) | ||
); | ||
} | ||
} |