Skip to content

Commit 49c4366

Browse files
committed
Use IdentifierUtils and add more tests
1 parent cd24b2d commit 49c4366

File tree

2 files changed

+133
-36
lines changed

2 files changed

+133
-36
lines changed

migration-tool/src/main/java/software/amazon/awssdk/migration/internal/recipe/HttpSettingsToHttpClient.java

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;
4444
import software.amazon.awssdk.http.apache.ApacheHttpClient;
4545
import software.amazon.awssdk.http.nio.netty.NettyNioAsyncHttpClient;
46+
import software.amazon.awssdk.migration.internal.utils.IdentifierUtils;
4647
import software.amazon.awssdk.migration.internal.utils.SdkTypeUtils;
4748
import software.amazon.awssdk.utils.CollectionUtils;
4849
import software.amazon.awssdk.utils.Logger;
@@ -65,7 +66,10 @@
6566
* to v2:
6667
* <p>
6768
* {@snippet :
69+
* ClientOverrideConfiguration clientConfiguration = ClientOverrideConfiguration.builder().build();
70+
*
6871
* SqsClient sqs = SqsClient.builder()
72+
* .overrideConfiguration(clientConfiguration)
6973
* .httpClientBuilder(ApacheHttpClient.builder()
7074
* .maxConnections(100)
7175
* .tcpKeepAlive(true))
@@ -258,15 +262,8 @@ private J.MethodInvocation configureHttpClientBuilder(J.MethodInvocation method,
258262
Collections.emptyList()
259263
);
260264

261-
J.Identifier httpClientBuilderName = new J.Identifier(
262-
Tree.randomId(),
263-
Space.EMPTY,
264-
Markers.EMPTY,
265-
Collections.emptyList(),
266-
"httpClientBuilder",
267-
null,
268-
null
269-
);
265+
J.Identifier httpClientBuilderName =
266+
IdentifierUtils.makeId("httpClientBuilder", builderType);
270267

271268
J.MethodInvocation httpClientBuilderMethodInvoke = new J.MethodInvocation(
272269
Tree.randomId(),
@@ -366,25 +363,11 @@ private JContainer<Expression> httpClientBuilderInvoke(Map<String, Expression> h
366363
Collections.emptyList()
367364
);
368365

369-
J.Identifier httpClient = new J.Identifier(
370-
Tree.randomId(),
371-
Space.EMPTY,
372-
Markers.EMPTY,
373-
Collections.emptyList(),
374-
httpClientClassName.getSimpleName(),
375-
httpClientType,
376-
null
377-
);
366+
J.Identifier httpClient =
367+
IdentifierUtils.makeId(httpClientClassName.getSimpleName(), httpClientType);
378368

379-
J.Identifier httpClientBuilderName = new J.Identifier(
380-
Tree.randomId(),
381-
Space.EMPTY,
382-
Markers.EMPTY,
383-
Collections.emptyList(),
384-
"builder",
385-
null,
386-
null
387-
);
369+
J.Identifier httpClientBuilderName =
370+
IdentifierUtils.makeId("builder", httpClientBuilderType);
388371

389372
J.MethodInvocation httpClientBuilderMethodInvoke = new J.MethodInvocation(
390373
Tree.randomId(),
@@ -411,15 +394,8 @@ private JContainer<Expression> httpClientBuilderInvoke(Map<String, Expression> h
411394
String settingName = entry.getKey();
412395
Expression value = entry.getValue();
413396

414-
J.Identifier settingBuilderName = new J.Identifier(
415-
Tree.randomId(),
416-
Space.EMPTY,
417-
Markers.EMPTY,
418-
Collections.emptyList(),
419-
settingName,
420-
httpClientBuilderType,
421-
null
422-
);
397+
J.Identifier settingBuilderName =
398+
IdentifierUtils.makeId(settingName, httpClientBuilderType);
423399

424400
List<JavaType> parametersTypes = Collections.singletonList(value.getType());
425401

migration-tool/src/test/java/software/amazon/awssdk/migration/recipe/ChangeConfigTypesTest.java

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,127 @@ void configurationWithHttpSettings_localVariable_shouldRemoveAndSetOnSdkClient()
168168
);
169169
}
170170

171+
@Test
172+
@EnabledOnJre({JRE.JAVA_8})
173+
void configurationWithHttpSettings_memberVariable_shouldRemoveAndSetOnSdkClient() {
174+
175+
rewriteRun(
176+
recipeSpec -> recipeSpec.expectedCyclesThatMakeChanges(2),
177+
java(
178+
"import com.amazonaws.ClientConfiguration;\n"
179+
+ "import com.amazonaws.services.sqs.AmazonSQS;\n"
180+
+ "import com.amazonaws.services.sqs.AmazonSQSClient;\n"
181+
+ "\n"
182+
+ "public class Example {\n"
183+
+ " private ClientConfiguration clientConfiguration = new ClientConfiguration()\n"
184+
+ " .withRequestTimeout(1000)\n"
185+
+ " .withMaxConnections(2000)\n"
186+
+ " .withConnectionTimeout(3000)\n"
187+
+ " .withTcpKeepAlive(true)\n"
188+
+ " .withSocketTimeout(4000)\n"
189+
+ " .withConnectionTTL(5000)\n"
190+
+ " .withConnectionMaxIdleMillis(6000);\n"
191+
+ "\n"
192+
+ " public void test() {\n"
193+
+ " AmazonSQS sqs = AmazonSQSClient.builder()\n"
194+
+ " .withClientConfiguration(clientConfiguration)\n"
195+
+ " .build();\n"
196+
+ " }\n"
197+
+ "}",
198+
"import software.amazon.awssdk.http.apache.ApacheHttpClient;\n"
199+
+ "import software.amazon.awssdk.services.sqs.SqsClient;\n"
200+
+ "import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;\n"
201+
+ "\n"
202+
+ "import java.time.Duration;\n"
203+
+ "\n"
204+
+ "public class Example {\n"
205+
+ " private ClientOverrideConfiguration clientConfiguration =ClientOverrideConfiguration.builder()\n"
206+
+ " .apiCallAttemptTimeout(Duration.ofMillis(1000)).build();\n"
207+
+ "\n"
208+
+ " public void test() {\n"
209+
+ " SqsClient sqs = SqsClient.builder()\n"
210+
+ " .overrideConfiguration(clientConfiguration)\n"
211+
+ " .httpClientBuilder(ApacheHttpClient.builder()\n"
212+
+ " .connectionMaxIdleTime(Duration.ofMillis(6000))\n"
213+
+ " .tcpKeepAlive(true)\n"
214+
+ " .socketTimeout(Duration.ofMillis(4000))\n"
215+
+ " .connectionTimeToLive(Duration.ofMillis(5000))\n"
216+
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
217+
+ " .maxConnections(2000))\n"
218+
+ " .build();\n"
219+
+ " }\n"
220+
+ "}"
221+
)
222+
);
223+
}
224+
225+
@Test
226+
@EnabledOnJre({JRE.JAVA_8})
227+
void configurationWithHttpSettings_usedByMultipleSdkClients_shouldRemoveAndSetOnSdkClients() {
228+
229+
rewriteRun(
230+
recipeSpec -> recipeSpec.expectedCyclesThatMakeChanges(2),
231+
java(
232+
"import com.amazonaws.ClientConfiguration;\n"
233+
+ "import com.amazonaws.services.sqs.AmazonSQS;\n"
234+
+ "import com.amazonaws.services.sqs.AmazonSQSClient;\n"
235+
+ "\n"
236+
+ "public class Example {\n"
237+
+ " private static final ClientConfiguration CONFIGURATION = new ClientConfiguration()\n"
238+
+ " .withRequestTimeout(1000)\n"
239+
+ " .withMaxConnections(2000)\n"
240+
+ " .withConnectionTimeout(3000)\n"
241+
+ " .withTcpKeepAlive(true)\n"
242+
+ " .withSocketTimeout(4000)\n"
243+
+ " .withConnectionTTL(5000)\n"
244+
+ " .withConnectionMaxIdleMillis(6000);\n"
245+
+ " private final AmazonSQS sqsMemberVariable = AmazonSQSClient.builder()\n"
246+
+ " .withClientConfiguration(CONFIGURATION)\n"
247+
+ " .build();\n"
248+
+ "\n"
249+
+ " public void test() {\n"
250+
+ " AmazonSQS sqs = AmazonSQSClient.builder()\n"
251+
+ " .withClientConfiguration(CONFIGURATION)\n"
252+
+ " .build();\n"
253+
+ " }\n"
254+
+ "}",
255+
"import software.amazon.awssdk.http.apache.ApacheHttpClient;\n"
256+
+ "import software.amazon.awssdk.services.sqs.SqsClient;\n"
257+
+ "import software.amazon.awssdk.core.client.config.ClientOverrideConfiguration;\n"
258+
+ "\n"
259+
+ "import java.time.Duration;\n"
260+
+ "\n"
261+
+ "public class Example {\n"
262+
+ " private static final ClientOverrideConfiguration CONFIGURATION =ClientOverrideConfiguration.builder()\n"
263+
+ " .apiCallAttemptTimeout(Duration.ofMillis(1000)).build();\n"
264+
+ " private final SqsClient sqsMemberVariable = SqsClient.builder()\n"
265+
+ " .overrideConfiguration(CONFIGURATION)\n"
266+
+ " .httpClientBuilder(ApacheHttpClient.builder()\n"
267+
+ " .connectionMaxIdleTime(Duration.ofMillis(6000))\n"
268+
+ " .tcpKeepAlive(true)\n"
269+
+ " .socketTimeout(Duration.ofMillis(4000))\n"
270+
+ " .connectionTimeToLive(Duration.ofMillis(5000))\n"
271+
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
272+
+ " .maxConnections(2000))\n"
273+
+ " .build();\n"
274+
+ "\n"
275+
+ " public void test() {\n"
276+
+ " SqsClient sqs = SqsClient.builder()\n"
277+
+ " .overrideConfiguration(CONFIGURATION)\n"
278+
+ " .httpClientBuilder(ApacheHttpClient.builder()\n"
279+
+ " .connectionMaxIdleTime(Duration.ofMillis(6000))\n"
280+
+ " .tcpKeepAlive(true)\n"
281+
+ " .socketTimeout(Duration.ofMillis(4000))\n"
282+
+ " .connectionTimeToLive(Duration.ofMillis(5000))\n"
283+
+ " .connectionTimeout(Duration.ofMillis(3000))\n"
284+
+ " .maxConnections(2000))\n"
285+
+ " .build();\n"
286+
+ " }\n"
287+
+ "}"
288+
)
289+
);
290+
}
291+
171292
@Test
172293
@EnabledOnJre({JRE.JAVA_8})
173294
void configurationWithHttpSettings_methodReference_shouldRemoveAndSetOnSdkClient() {

0 commit comments

Comments
 (0)