Skip to content
Open
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
34 changes: 23 additions & 11 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

<properties>
<aws-java-sdk.version>2.14.19</aws-java-sdk.version>
<junit.version>5.10.0</junit.version>
</properties>

<developers>
Expand Down Expand Up @@ -63,11 +64,10 @@
<version>${aws-java-sdk.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
Expand All @@ -80,14 +80,20 @@
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>pl.pragmatists</groupId>
<artifactId>JUnitParams</artifactId>
<version>1.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.junit</groupId>
<artifactId>junit-bom</artifactId>
<version>${junit.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

<build>
<pluginManagement>
<plugins>
Expand Down Expand Up @@ -144,6 +150,12 @@
<autoReleaseAfterClose>true</autoReleaseAfterClose>
</configuration>
</plugin>
<plugins>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.1.2</version>
</plugin>
</plugins>
</plugins>
</build>

Expand Down
59 changes: 33 additions & 26 deletions src/test/java/software/amazon/sns/AmazonSNSExtendedClientTest.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
package software.amazon.sns;

import com.amazon.sqs.javamessaging.SQSExtendedClientConstants;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;

import software.amazon.awssdk.awscore.AwsRequestOverrideConfiguration;
Expand All @@ -22,9 +21,17 @@
import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class AmazonSNSExtendedClientTest {

Expand All @@ -39,7 +46,7 @@ public class AmazonSNSExtendedClientTest {
private SnsClient mockSnsBackend;
private S3Client mockS3;

@Before
@BeforeEach
public void setupClient() {
mockS3 = mock(S3Client.class);
mockSnsBackend = mock(SnsClient.class);
Expand All @@ -64,9 +71,9 @@ public void testPublishLargeMessageS3IsUsed() {
verify(mockSnsBackend, times(1)).publish(publishRequestCaptor.capture());

Map<String, MessageAttributeValue> attributes = publishRequestCaptor.getValue().messageAttributes();
Assert.assertEquals("Number", attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).dataType());
assertEquals("Number", attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).dataType());

Assert.assertEquals(messageBody.length(), (int) Integer.valueOf(attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).stringValue()));
assertEquals(messageBody.length(), (int) Integer.valueOf(attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).stringValue()));
}

@Test
Expand All @@ -84,9 +91,9 @@ public void testPublishLargeMessageS3IsUsedWithS3Key() {
verify(mockSnsBackend, times(1)).publish(publishRequestCaptor.capture());

Map<String, MessageAttributeValue> attributes = publishRequestCaptor.getValue().messageAttributes();
Assert.assertEquals("Number", attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).dataType());
assertEquals("Number", attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).dataType());

Assert.assertEquals(messageBody.length(), (int) Integer.valueOf(attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).stringValue()));
assertEquals(messageBody.length(), (int) Integer.valueOf(attributes.get(SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME).stringValue()));
}

@Test
Expand All @@ -101,7 +108,7 @@ public void testPublishSmallMessageS3IsNotUsed() {
verify(mockSnsBackend, times(1)).publish(publishRequestCaptor.capture());

Map<String, MessageAttributeValue> attributes = publishRequestCaptor.getValue().messageAttributes();
Assert.assertTrue(attributes.isEmpty());
assertTrue(attributes.isEmpty());
}

@Test
Expand Down Expand Up @@ -137,10 +144,10 @@ public void testPublishMessageWithJSONMessageStructureThrowsAmazonClientExceptio

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown.");
fail("An exception should have been thrown.");

} catch (SdkClientException exception) {
Assert.assertTrue(exception.getMessage().contains("SNS extended client does not support sending JSON messages"));
assertTrue(exception.getMessage().contains("SNS extended client does not support sending JSON messages"));
}
}

Expand Down Expand Up @@ -184,8 +191,8 @@ public void testPublishRequestDoesNotAlterPublishRequest() {
.build();
extendedSnsWithDefaultConfig.publish(publishRequest);

Assert.assertEquals(messageBody, publishRequest.message());
Assert.assertEquals(attrs, publishRequest.messageAttributes());
assertEquals(messageBody, publishRequest.message());
assertEquals(attrs, publishRequest.messageAttributes());
}

@Test
Expand All @@ -197,10 +204,10 @@ public void testThrowAmazonServiceExceptionWhenS3ThrowsAwsServiceException() {

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown.");
fail("An exception should have been thrown.");

} catch (SdkException exception) {
Assert.assertTrue(exception.getMessage().contains("Failed to store the message content in an S3 object."));
assertTrue(exception.getMessage().contains("Failed to store the message content in an S3 object."));
}
}

Expand All @@ -213,10 +220,10 @@ public void testThrowAmazonClientExceptionWhenS3ThrowsSdkClientException() {

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown");
fail("An exception should have been thrown");

} catch (SdkException exception) {
Assert.assertTrue(exception.getMessage().contains("Failed to store the message content in an S3 object."));
assertTrue(exception.getMessage().contains("Failed to store the message content in an S3 object."));
}
}

Expand All @@ -239,10 +246,10 @@ public void testThrowAmazonClientExceptionWhenReservedAttributeNameIsAlreadyUsed

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown");
fail("An exception should have been thrown");

} catch (SdkClientException exception) {
Assert.assertTrue(exception.getMessage().contains("Message attribute name " +
assertTrue(exception.getMessage().contains("Message attribute name " +
SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME + " is reserved for use by SNS extended client."));
}
}
Expand All @@ -266,10 +273,10 @@ public void testThrowAmazonClientExceptionWhenThereAreMoreThanAllowedMessageAttr

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown");
fail("An exception should have been thrown");

} catch (SdkClientException exception) {
Assert.assertTrue(exception.getMessage().contains("Number of message attributes [" + attributeNumber
assertTrue(exception.getMessage().contains("Number of message attributes [" + attributeNumber
+ "] exceeds the maximum allowed for large-payload messages ["
+ SQSExtendedClientConstants.MAX_ALLOWED_ATTRIBUTES + "]."));
}
Expand All @@ -295,10 +302,10 @@ public void testThrowAmazonClientExceptionWhenSizeOfMessageAttributeKeyIsLargerT

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown");
fail("An exception should have been thrown");

} catch (SdkClientException exception) {
Assert.assertTrue(exception.getMessage().contains("Total size of Message attributes is "
assertTrue(exception.getMessage().contains("Total size of Message attributes is "
+ expectedSize + " bytes which is larger than the threshold of " + SNSExtendedClientConfiguration.SNS_DEFAULT_MESSAGE_SIZE
+ " Bytes. Consider including the payload in the message body instead of message attributes."));
}
Expand All @@ -324,10 +331,10 @@ public void testThrowAmazonClientExceptionWhenSizeOfMessageAttributeValueIsLarge

try {
extendedSnsWithDefaultConfig.publish(publishRequest);
Assert.fail("An exception should have been thrown");
fail("An exception should have been thrown");

} catch (SdkClientException exception) {
Assert.assertTrue(exception.getMessage().contains("Total size of Message attributes is "
assertTrue(exception.getMessage().contains("Total size of Message attributes is "
+ expectedSize + " bytes which is larger than the threshold of " + SNSExtendedClientConfiguration.SNS_DEFAULT_MESSAGE_SIZE
+ " Bytes. Consider including the payload in the message body instead of message attributes."));
}
Expand Down