Skip to content

Commit

Permalink
AWS SDK 2 upgrade: Fix some unit tests in messaging - spring-atticgh-267
Browse files Browse the repository at this point in the history
  • Loading branch information
Kristine Jetzke committed Feb 14, 2020
1 parent 2b6e744 commit 3a72d5d
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected void doParse(Element element, ParserContext parserContext,
BeanDefinitionBuilder builder) {
builder.addConstructorArgReference(
getCustomClientOrDefaultClientBeanName(element, parserContext,
"amazon-sns", "com.amazonaws.services.sns.AmazonSNSClient"));
"amazon-sns", "software.amazon.awssdk.services.sns.SnsClient"));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class NotificationMessagingTemplateBeanDefinitionParser

private static final String DEFAULT_DESTINATION_ATTRIBUTE = "default-destination";

private static final String SNS_CLIENT_CLASS_NAME = "com.amazonaws.services.sns.AmazonSNSClient";
private static final String SNS_CLIENT_CLASS_NAME = "software.amazon.awssdk.services.sns.SnsClient";

@Override
protected Class<?> getBeanClass(Element element) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import org.junit.Before;
import org.junit.Test;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.awscore.client.config.AwsClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.ServiceMetadata;
import software.amazon.awssdk.services.sns.SnsClient;
Expand Down Expand Up @@ -90,7 +93,8 @@ public void enableSns_withProvidedCredentials_shouldBeUsedToCreateClient()
SnsClient amazonSns = this.webApplicationContext.getBean(SnsClient.class);

// Assert
assertThat(ReflectionTestUtils.getField(amazonSns, "awsCredentialsProvider"))
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSns, "clientConfiguration");
assertThat(clientConfiguration.option(AwsClientOption.CREDENTIALS_PROVIDER))
.isEqualTo(SnsConfigurationWithCredentials.AWS_CREDENTIALS_PROVIDER);
}

Expand Down Expand Up @@ -124,9 +128,9 @@ public void enableSns_withRegionProvided_shouldBeUsedToCreateClient()
SnsClient amazonSns = this.webApplicationContext.getBean(SnsClient.class);

// Assert
assertThat(ReflectionTestUtils.getField(amazonSns, "endpoint").toString())
.isEqualTo("https://"
+ ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1));
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSns, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo("https://" + ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1));
}

private NotificationStatusHandlerMethodArgumentResolver getNotificationStatusHandlerMethodArgumentResolver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.net.URI;

import org.junit.Test;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.ServiceMetadata;
import software.amazon.awssdk.services.sns.SnsClient;
Expand Down Expand Up @@ -63,10 +65,9 @@ public void parseInternal_customRegion_configuresHandlerMethodArgumentResolverWi
SnsClient snsClient = context.getBean(SnsClient.class);

// Assert
assertThat(ReflectionTestUtils.getField(snsClient, "endpoint")).isEqualTo(new URI(
"https",
ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1).toString(), null,
null));
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(snsClient, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT)).isEqualTo(
new URI("https", ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1).toString(), null, null));
}

// @checkstyle:off
Expand All @@ -82,10 +83,9 @@ public void parseInternal_customRegionProvider_configuresHandlerMethodArgumentRe
SnsClient snsClient = context.getBean(SnsClient.class);

// Assert
assertThat(ReflectionTestUtils.getField(snsClient, "endpoint")).isEqualTo(new URI(
"https",
ServiceMetadata.of("sns").endpointFor(Region.US_WEST_2).toString(), null,
null));
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(snsClient, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT)).isEqualTo(
new URI("https", ServiceMetadata.of("sns").endpointFor(Region.US_WEST_2).toString(), null, null));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.List;

import org.junit.Test;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.regions.ServiceMetadata;
import software.amazon.awssdk.services.sns.SnsClient;
Expand Down Expand Up @@ -151,9 +153,9 @@ public void parseInternal_withCustomRegion_shouldConfigureDefaultClientWithCusto

// Assert
SnsClient amazonSns = registry.getBean(SnsClient.class);
assertThat(ReflectionTestUtils.getField(amazonSns, "endpoint").toString())
.isEqualTo("https://"
+ ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1));
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSns, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo("https://" + ServiceMetadata.of("sns").endpointFor(Region.EU_WEST_1));
}

@Test
Expand All @@ -169,9 +171,9 @@ public void parseInternal_withCustomRegionProvider_shouldConfigureDefaultClientW

// Assert
SnsClient amazonSns = registry.getBean(SnsClient.class);
assertThat(ReflectionTestUtils.getField(amazonSns, "endpoint").toString())
.isEqualTo("https://"
+ ServiceMetadata.of("sns").endpointFor(Region.CN_NORTH_1));
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSns, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo("https://" + ServiceMetadata.of("sns").endpointFor(Region.CN_NORTH_1));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ public void parseInternal_withCustomRegion_shouldConfigureDefaultClientWithCusto
SqsAsyncClient amazonSqs = registry.getBean(SqsAsyncClient.class);
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSqs, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo(ServiceMetadata.of("sqs").endpointFor(Region.SA_EAST_1));
.isEqualTo("https://" + ServiceMetadata.of("sqs").endpointFor(Region.SA_EAST_1));
}

@Test
Expand All @@ -195,7 +195,7 @@ public void parseInternal_withCustomRegionProvider_shouldConfigureDefaultClientW
SqsAsyncClient amazonSqs = registry.getBean(SqsAsyncClient.class);
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSqs, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo(ServiceMetadata.of("sqs").endpointFor(Region.AP_SOUTHEAST_2));
.isEqualTo("https://" + ServiceMetadata.of("sqs").endpointFor(Region.AP_SOUTHEAST_2));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@

package org.springframework.cloud.aws.messaging.config.xml;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.junit.Test;
import software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption;
import software.amazon.awssdk.core.client.config.SdkClientConfiguration;
import software.amazon.awssdk.core.client.config.SdkClientOption;
import software.amazon.awssdk.regions.Region;
Expand Down Expand Up @@ -49,9 +51,10 @@ public void parseInternal_minimalConfiguration_createsBufferedClientWithoutExpli
// Assert
SqsAsyncClient asyncClient = beanFactory.getBean("customClient", SqsAsyncClient.class);
assertThat(asyncClient).isNotNull();
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) ReflectionTestUtils.getField(asyncClient,
"executorService");
assertThat(threadPoolExecutor.getCorePoolSize()).isEqualTo(50);
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(asyncClient, "clientConfiguration");
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) clientConfiguration.option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR);
// //TODO SDK2 migration: default value is now 8, s. SdkDefaultClientBuilder.resolveAsyncFutureCompletionExecutor() Check of it used to be 50
assertThat(threadPoolExecutor.getCorePoolSize()).isEqualTo(8);
}


Expand Down Expand Up @@ -87,9 +90,10 @@ public void parseInternal_withCustomTasExecutor_createsBufferedClientWithCustomT
// Assert
SqsAsyncClient asyncClient = beanFactory.getBean("customClient", SqsAsyncClient.class);
assertThat(asyncClient).isNotNull();
ShutdownSuppressingExecutorServiceAdapter executor = (ShutdownSuppressingExecutorServiceAdapter) ReflectionTestUtils
.getField(asyncClient, "executorService");
assertThat(ReflectionTestUtils.getField(executor, "taskExecutor"))
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(asyncClient, "clientConfiguration");
Executor executor = clientConfiguration.option(SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR);
ShutdownSuppressingExecutorServiceAdapter customExecutor = (ShutdownSuppressingExecutorServiceAdapter) ReflectionTestUtils.getField(executor, "executor");
assertThat(ReflectionTestUtils.getField(customExecutor, "taskExecutor"))
.isSameAs(beanFactory.getBean("myThreadPoolTaskExecutor"));
}

Expand All @@ -108,7 +112,7 @@ public void parseInternal_withCustomRegion_shouldConfigureDefaultClientWithCusto
SqsAsyncClient amazonSqs = registry.getBean(SqsAsyncClient.class);
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSqs, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo(ServiceMetadata.of("sqs").endpointFor(Region.EU_WEST_1));
.isEqualTo("https://" + ServiceMetadata.of("sqs").endpointFor(Region.EU_WEST_1));
}

@Test
Expand All @@ -126,7 +130,7 @@ public void parseInternal_withCustomRegionProvider_shouldConfigureDefaultClientW
SqsAsyncClient amazonSqs = registry.getBean(SqsAsyncClient.class);
SdkClientConfiguration clientConfiguration = (SdkClientConfiguration) ReflectionTestUtils.getField(amazonSqs, "clientConfiguration");
assertThat(clientConfiguration.option(SdkClientOption.ENDPOINT).toString())
.isEqualTo(ServiceMetadata.of("sqs").endpointFor(Region.AP_SOUTHEAST_2));
.isEqualTo("https://" + ServiceMetadata.of("sqs").endpointFor(Region.AP_SOUTHEAST_2));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ public void sendMessage_withBinaryMessageHeader_shouldBeSentAsBinaryMessageAttri
// Assert
assertThat(sent).isTrue();
assertThat(publishRequestArgumentCaptor.getValue().messageAttributes()
.get(headerName).binaryValue()).isEqualTo(headerValue);
.get(headerName).binaryValue().asByteBuffer()).isEqualTo(headerValue);
assertThat(publishRequestArgumentCaptor.getValue().messageAttributes()
.get(headerName).dataType()).isEqualTo(MessageAttributeDataTypes.BINARY);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
http://www.springframework.org/schema/cloud/aws/context
http://www.springframework.org/schema/cloud/aws/context/spring-cloud-aws-context.xsd">

<bean id="myClient" class="com.amazonaws.services.sqs.AmazonSQSAsyncClient"/>
<bean id="myClient" class="software.amazon.awssdk.services.sqs.SqsAsyncClient" factory-method="create"/>

<aws-messaging:annotation-driven-queue-listener amazon-sqs="myClient"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,5 @@
<aws-messaging:notification-argument-resolver id="notificationArgumentResolver"
amazon-sns="customSnsClient"/>

<bean id="customSnsClient" class="com.amazonaws.services.sns.AmazonSNSClient"/>
<bean id="customSnsClient" class="software.amazon.awssdk.services.sns.SnsClient" factory-method="create"/>
</beans>
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
amazon-sns="amazonSns"/>

<bean id="amazonSns" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.amazonaws.services.sns.AmazonSNS"/>
<constructor-arg value="software.amazon.awssdk.services.sns.SnsClient"/>
</bean>

<bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
amazon-sns="amazonSns"/>

<bean id="amazonSns" class="org.mockito.Mockito" factory-method="mock">
<constructor-arg value="com.amazonaws.services.sns.AmazonSNS"/>
<constructor-arg value="software.amazon.awssdk.services.sns.SnsClient"/>
</bean>

<bean
Expand Down

0 comments on commit 3a72d5d

Please sign in to comment.