Skip to content

Commit

Permalink
Deprecate ExtendedClientConfiguration functions (#53)
Browse files Browse the repository at this point in the history
Added support for deprecate ExtendedClientConfiguration functions
  • Loading branch information
adam-aws authored Jul 29, 2020
1 parent e2ea202 commit 4f8b704
Show file tree
Hide file tree
Showing 4 changed files with 262 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,121 @@ public ExtendedClientConfiguration withPayloadSupportDisabled() {
this.setPayloadSupportDisabled();
return this;
}

/**
* Enables support for large-payload messages.
*
* @param s3
* Amazon S3 client which is going to be used for storing
* large-payload messages.
* @param s3BucketName
* Name of the bucket which is going to be used for storing
* large-payload messages. The bucket must be already created and
* configured in s3.
*
* @deprecated Instead use {@link #setPayloadSupportEnabled(AmazonS3, String, boolean)}
*/
@Deprecated
public void setLargePayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
this.setPayloadSupportEnabled(s3, s3BucketName);
}

/**
* Enables support for large-payload messages.
*
* @param s3
* Amazon S3 client which is going to be used for storing
* large-payload messages.
* @param s3BucketName
* Name of the bucket which is going to be used for storing
* large-payload messages. The bucket must be already created and
* configured in s3.
* @return the updated ExtendedClientConfiguration object.
*
* @deprecated Instead use {@link #withPayloadSupportEnabled(AmazonS3, String)}
*/
@Deprecated
public ExtendedClientConfiguration withLargePayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
setLargePayloadSupportEnabled(s3, s3BucketName);
return this;
}

/**
* Disables support for large-payload messages.
*
* @deprecated Instead use {@link #setPayloadSupportDisabled()}
*/
@Deprecated
public void setLargePayloadSupportDisabled() {
this.setPayloadSupportDisabled();
}

/**
* Disables support for large-payload messages.
* @return the updated ExtendedClientConfiguration object.
*
* @deprecated Instead use {@link #withPayloadSupportDisabled()}
*/
@Deprecated
public ExtendedClientConfiguration withLargePayloadSupportDisabled() {
setLargePayloadSupportDisabled();
return this;
}

/**
* Check if the support for large-payload message if enabled.
* @return true if support for large-payload messages is enabled.
*
* @deprecated Instead use {@link #isPayloadSupportEnabled()}
*/
@Deprecated
public boolean isLargePayloadSupportEnabled() {
return isPayloadSupportEnabled();
}

/**
* Sets the message size threshold for storing message payloads in Amazon
* S3.
*
* @param messageSizeThreshold
* Message size threshold to be used for storing in Amazon S3.
* Default: 256KB.
*
* @deprecated Instead use {@link #setPayloadSizeThreshold(int)}
*/
@Deprecated
public void setMessageSizeThreshold(int messageSizeThreshold) {
this.setPayloadSizeThreshold(messageSizeThreshold);
}

/**
* Sets the message size threshold for storing message payloads in Amazon
* S3.
*
* @param messageSizeThreshold
* Message size threshold to be used for storing in Amazon S3.
* Default: 256KB.
* @return the updated ExtendedClientConfiguration object.
*
* @deprecated Instead use {@link #withPayloadSizeThreshold(int)}
*/
@Deprecated
public ExtendedClientConfiguration withMessageSizeThreshold(int messageSizeThreshold) {
setMessageSizeThreshold(messageSizeThreshold);
return this;
}

/**
* Gets the message size threshold for storing message payloads in Amazon
* S3.
*
* @return Message size threshold which is being used for storing in Amazon
* S3. Default: 256KB.
*
* @deprecated Instead use {@link #getPayloadSizeThreshold()}
*/
@Deprecated
public int getMessageSizeThreshold() {
return getPayloadSizeThreshold();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,18 @@


public class SQSExtendedClientConstants {
// This constant is shared with SNSExtendedClient
// SNS team should be notified of any changes made to this
public static final String RESERVED_ATTRIBUTE_NAME = "ExtendedPayloadSize";

// This constant is shared with SNSExtendedClient
// SNS team should be notified of any changes made to this
public static final int MAX_ALLOWED_ATTRIBUTES = 10 - 1; // 10 for SQS, 1 for the reserved attribute

// This constant is shared with SNSExtendedClient
// SNS team should be notified of any changes made to this
public static final int DEFAULT_MESSAGE_SIZE_THRESHOLD = 262144;

public static final String S3_BUCKET_NAME_MARKER = "-..s3BucketName..-";
public static final String S3_KEY_MARKER = "-..s3Key..-";
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public class AmazonSQSExtendedClientTest {
private AmazonSQS extendedSqsWithCustomKMS;
private AmazonSQS extendedSqsWithDefaultKMS;
private AmazonSQS extendedSqsWithGenericReservedAttributeName;
private AmazonSQS extendedSqsWithDeprecatedMethods;
private AmazonSQS mockSqsBackend;
private AmazonS3 mockS3;
private static final String S3_BUCKET_NAME = "test-bucket-name";
Expand Down Expand Up @@ -80,10 +81,75 @@ public void setupClients() {
ExtendedClientConfiguration extendedClientConfigurationWithGenericReservedAttributeName = new ExtendedClientConfiguration()
.withPayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withLegacyReservedAttributeNameDisabled();

ExtendedClientConfiguration extendedClientConfigurationDeprecated = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);

extendedSqsWithDefaultConfig = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
extendedSqsWithCustomKMS = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithCustomKMS));
extendedSqsWithDefaultKMS = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithDefaultKMS));
extendedSqsWithGenericReservedAttributeName = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithGenericReservedAttributeName));
extendedSqsWithDeprecatedMethods = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationDeprecated));
}

@Test
public void testWhenSendMessageWithLargePayloadSupportDisabledThenS3IsNotUsedAndSqsBackendIsResponsibleToFailItWithDeprecatedMethod() {
int messageLength = MORE_THAN_SQS_SIZE_LIMIT;
String messageBody = generateStringWithLength(messageLength);
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportDisabled();
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));

SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
sqsExtended.sendMessage(messageRequest);

verify(mockS3, never()).putObject(isA(PutObjectRequest.class));
verify(mockSqsBackend).sendMessage(eq(messageRequest));
}

@Test
public void testWhenSendMessageWithAlwaysThroughS3AndMessageIsSmallThenItIsStillStoredInS3WithDeprecatedMethod() {
int messageLength = LESS_THAN_SQS_SIZE_LIMIT;
String messageBody = generateStringWithLength(messageLength);
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withAlwaysThroughS3(true);
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));

SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
sqsExtended.sendMessage(messageRequest);

verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}

@Test
public void testWhenSendMessageWithSetMessageSizeThresholdThenThresholdIsHonoredWithDeprecatedMethod() {
int messageLength = ARBITRARY_SMALLER_THRESHOLD * 2;
String messageBody = generateStringWithLength(messageLength);
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withMessageSizeThreshold(ARBITRARY_SMALLER_THRESHOLD);

AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));

SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
sqsExtended.sendMessage(messageRequest);
verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
}

@Test
public void testReceiveMessageMultipleTimesDoesNotAdditionallyAlterReceiveMessageRequestWithDeprecatedMethod() {
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
when(mockSqsBackend.receiveMessage(isA(ReceiveMessageRequest.class))).thenReturn(new ReceiveMessageResult());

ReceiveMessageRequest messageRequest = new ReceiveMessageRequest();
ReceiveMessageRequest expectedRequest = new ReceiveMessageRequest()
.withMessageAttributeNames(AmazonSQSExtendedClient.LEGACY_RESERVED_ATTRIBUTE_NAME, SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);

sqsExtended.receiveMessage(messageRequest);
Assert.assertEquals(expectedRequest, messageRequest);

sqsExtended.receiveMessage(messageRequest);
Assert.assertEquals(expectedRequest, messageRequest);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.amazon.sqs.javamessaging;

import com.amazonaws.services.s3.AmazonS3;
import com.amazonaws.services.s3.model.PutObjectRequest;
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;

import org.junit.Assert;
Expand All @@ -31,10 +32,6 @@ public class ExtendedClientConfigurationTest {
private static String s3BucketName = "test-bucket-name";
private static String s3ServerSideEncryptionKMSKeyId = "test-customer-managed-kms-key-id";

@Before
public void setup() {
}

@Test
public void testCopyConstructor() {
AmazonS3 s3 = mock(AmazonS3.class);
Expand Down Expand Up @@ -99,4 +96,73 @@ public void testLargePayloadSupportEnabledWithDeleteFromS3Disabled() {
Assert.assertNotNull(extendedClientConfiguration.getAmazonS3Client());
Assert.assertEquals(s3BucketName, extendedClientConfiguration.getS3BucketName());
}

@Test
public void testCopyConstructorDeprecated() {

AmazonS3 s3 = mock(AmazonS3.class);
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);

boolean alwaysThroughS3 = true;
int messageSizeThreshold = 500;

ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration();

extendedClientConfig.withLargePayloadSupportEnabled(s3, s3BucketName)
.withAlwaysThroughS3(alwaysThroughS3).withMessageSizeThreshold(messageSizeThreshold);

ExtendedClientConfiguration newExtendedClientConfig = new ExtendedClientConfiguration(extendedClientConfig);

Assert.assertEquals(s3, newExtendedClientConfig.getAmazonS3Client());
Assert.assertEquals(s3BucketName, newExtendedClientConfig.getS3BucketName());
Assert.assertTrue(newExtendedClientConfig.isLargePayloadSupportEnabled());
Assert.assertEquals(alwaysThroughS3, newExtendedClientConfig.isAlwaysThroughS3());
Assert.assertEquals(messageSizeThreshold, newExtendedClientConfig.getMessageSizeThreshold());

Assert.assertNotSame(newExtendedClientConfig, extendedClientConfig);
}

@Test
public void testLargePayloadSupportEnabled() {

AmazonS3 s3 = mock(AmazonS3.class);
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);

ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
extendedClientConfiguration.setLargePayloadSupportEnabled(s3, s3BucketName);

Assert.assertTrue(extendedClientConfiguration.isLargePayloadSupportEnabled());
Assert.assertNotNull(extendedClientConfiguration.getAmazonS3Client());
Assert.assertEquals(s3BucketName, extendedClientConfiguration.getS3BucketName());

}

@Test
public void testDisableLargePayloadSupport() {

AmazonS3 s3 = mock(AmazonS3.class);
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);

ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
extendedClientConfiguration.setLargePayloadSupportDisabled();

Assert.assertNull(extendedClientConfiguration.getAmazonS3Client());
Assert.assertNull(extendedClientConfiguration.getS3BucketName());

verify(s3, never()).putObject(isA(PutObjectRequest.class));
}

@Test
public void testMessageSizeThreshold() {

ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();

Assert.assertEquals(SQSExtendedClientConstants.DEFAULT_MESSAGE_SIZE_THRESHOLD,
extendedClientConfiguration.getMessageSizeThreshold());

int messageLength = 1000;
extendedClientConfiguration.setMessageSizeThreshold(messageLength);
Assert.assertEquals(messageLength, extendedClientConfiguration.getMessageSizeThreshold());

}
}

0 comments on commit 4f8b704

Please sign in to comment.