Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HTTP settings mapping part 1: remove HTTP settings from client configu… #5211

Merged
merged 2 commits into from
May 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.migration.internal.recipe;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Optional;
import java.util.Set;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.TreeVisitor;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.java.tree.Space;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;

@SdkInternalApi
public class HttpSettingsToHttpClient extends Recipe {

private static final Set<String> HTTP_SETTINGS_NAMES = new HashSet<>(Arrays.asList("connectionTimeout",
"connectionMaxIdleMillis",
"tcpKeepAlive",
"connectionTtl",
"socketTimeout",
"maxConnections"));

@Override
public String getDisplayName() {
return "Move HTTP settings on the ClientOverrideConfiguration to settings on the ApacheHttpClient";
}

@Override
public String getDescription() {
return "Move HTTP settings on the ClientOverrideConfiguration to settings on the ApacheHttpClient.";
}

@Override
public TreeVisitor<?, ExecutionContext> getVisitor() {
return new Visitor();
}

private static final class Visitor extends JavaIsoVisitor<ExecutionContext> {

@Override
public J.MethodInvocation visitMethodInvocation(J.MethodInvocation method, ExecutionContext executionContext) {
method = super.visitMethodInvocation(method, executionContext).cast();

if (!Optional.ofNullable(method.getMethodType()).map(mt -> mt.getDeclaringType())
.filter(t -> t.isAssignableTo(ClientOverrideConfiguration.class.getCanonicalName()))
.isPresent()) {
return method;
}

Expression methodSelectExpression = method.getSelect();
if (methodSelectExpression == null || !(methodSelectExpression instanceof J.MethodInvocation)) {
return method;
}

// method: ClientOverrideConfiguration.builder().maxConnection(xx).aNonHttpSetting(xx)
// method.getSelect: ClientOverrideConfiguration.builder().maxConnection(xx)
J.MethodInvocation selectInvoke = (J.MethodInvocation) methodSelectExpression;

String methodName = selectInvoke.getSimpleName();
if (!HTTP_SETTINGS_NAMES.contains(methodName)) {
return method;
}

if (!(selectInvoke.getSelect() instanceof J.MethodInvocation)) {
return method;
}

// select.getSelect: ClientOverrideConfiguration.builder()
J.MethodInvocation selectInvokeSelect = (J.MethodInvocation) selectInvoke.getSelect();
Space selectPrefix = selectInvoke.getPrefix();

// new method: ClientOverrideConfiguration.builder().aNonHttpSetting(xx)
method = method.withSelect(selectInvokeSelect).withPrefix(selectPrefix);

return method;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file 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 software.amazon.awssdk.migration.internal.recipe;

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

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.Properties;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledOnJre;
import org.junit.jupiter.api.condition.JRE;
import org.openrewrite.ExecutionContext;
import org.openrewrite.Recipe;
import org.openrewrite.config.Environment;
import org.openrewrite.config.YamlResourceLoader;
import org.openrewrite.java.Java8Parser;
import org.openrewrite.java.JavaIsoVisitor;
import org.openrewrite.java.MethodMatcher;
import org.openrewrite.java.tree.Expression;
import org.openrewrite.java.tree.J;
import org.openrewrite.test.RecipeSpec;
import org.openrewrite.test.RewriteTest;
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
import software.amazon.awssdk.migration.recipe.NewClassToBuilderPattern;

public class HttpSettingsToHttpClientTest implements RewriteTest {

@Override
public void defaults(RecipeSpec spec) {
try (InputStream stream = getClass().getResourceAsStream("/META-INF/rewrite/change-config-types.yml")) {
spec.recipes(Environment.builder()
.load(new YamlResourceLoader(stream, URI.create("rewrite.yml"), new Properties()))
.build()
.activateRecipes("software.amazon.awssdk.ChangeConfigTypes"),
new NewClassToBuilderPattern(),
new HttpSettingsToHttpClient());
} catch (IOException e) {
throw new RuntimeException(e);
}

spec.parser(Java8Parser.builder().classpath("aws-java-sdk-sqs", "aws-sdk-java", "sdk-core"));
}

@Test
@EnabledOnJre({JRE.JAVA_8})
void httpSettings_shouldRemove() {
rewriteRun(
java(
"import com.amazonaws.ClientConfiguration;\n"
+ "\n"
+ "public class Example {\n"
+ "\n"
+ " ClientConfiguration clientConfiguration = new ClientConfiguration()\n"
+ " .withMaxConnections(1000)\n"
+ " .withConnectionTimeout(1000)\n"
+ " .withTcpKeepAlive(true)\n"
+ " .withSocketTimeout(1000)\n"
+ " .withConnectionTTL(1000)\n"
+ " .withRequestTimeout(1000)\n"
+ " .withConnectionMaxIdleMillis(1000);\n"
+ "}",
"import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;\n"
+ "\n"
+ "import java.time.Duration;\n"
+ "\n"
+ "public class Example {\n"
+ "\n"
+ " ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder()\n"
+ " .apiCallAttemptTimeout(Duration.ofMillis(1000)).build();\n"
+ "}"
)
);
}
}
Loading