Skip to content

Commit 4f8b704

Browse files
authored
Deprecate ExtendedClientConfiguration functions (#53)
Added support for deprecate ExtendedClientConfiguration functions
1 parent e2ea202 commit 4f8b704

File tree

4 files changed

+262
-4
lines changed

4 files changed

+262
-4
lines changed

src/main/java/com/amazon/sqs/javamessaging/ExtendedClientConfiguration.java

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,121 @@ public ExtendedClientConfiguration withPayloadSupportDisabled() {
149149
this.setPayloadSupportDisabled();
150150
return this;
151151
}
152+
153+
/**
154+
* Enables support for large-payload messages.
155+
*
156+
* @param s3
157+
* Amazon S3 client which is going to be used for storing
158+
* large-payload messages.
159+
* @param s3BucketName
160+
* Name of the bucket which is going to be used for storing
161+
* large-payload messages. The bucket must be already created and
162+
* configured in s3.
163+
*
164+
* @deprecated Instead use {@link #setPayloadSupportEnabled(AmazonS3, String, boolean)}
165+
*/
166+
@Deprecated
167+
public void setLargePayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
168+
this.setPayloadSupportEnabled(s3, s3BucketName);
169+
}
170+
171+
/**
172+
* Enables support for large-payload messages.
173+
*
174+
* @param s3
175+
* Amazon S3 client which is going to be used for storing
176+
* large-payload messages.
177+
* @param s3BucketName
178+
* Name of the bucket which is going to be used for storing
179+
* large-payload messages. The bucket must be already created and
180+
* configured in s3.
181+
* @return the updated ExtendedClientConfiguration object.
182+
*
183+
* @deprecated Instead use {@link #withPayloadSupportEnabled(AmazonS3, String)}
184+
*/
185+
@Deprecated
186+
public ExtendedClientConfiguration withLargePayloadSupportEnabled(AmazonS3 s3, String s3BucketName) {
187+
setLargePayloadSupportEnabled(s3, s3BucketName);
188+
return this;
189+
}
190+
191+
/**
192+
* Disables support for large-payload messages.
193+
*
194+
* @deprecated Instead use {@link #setPayloadSupportDisabled()}
195+
*/
196+
@Deprecated
197+
public void setLargePayloadSupportDisabled() {
198+
this.setPayloadSupportDisabled();
199+
}
200+
201+
/**
202+
* Disables support for large-payload messages.
203+
* @return the updated ExtendedClientConfiguration object.
204+
*
205+
* @deprecated Instead use {@link #withPayloadSupportDisabled()}
206+
*/
207+
@Deprecated
208+
public ExtendedClientConfiguration withLargePayloadSupportDisabled() {
209+
setLargePayloadSupportDisabled();
210+
return this;
211+
}
212+
213+
/**
214+
* Check if the support for large-payload message if enabled.
215+
* @return true if support for large-payload messages is enabled.
216+
*
217+
* @deprecated Instead use {@link #isPayloadSupportEnabled()}
218+
*/
219+
@Deprecated
220+
public boolean isLargePayloadSupportEnabled() {
221+
return isPayloadSupportEnabled();
222+
}
223+
224+
/**
225+
* Sets the message size threshold for storing message payloads in Amazon
226+
* S3.
227+
*
228+
* @param messageSizeThreshold
229+
* Message size threshold to be used for storing in Amazon S3.
230+
* Default: 256KB.
231+
*
232+
* @deprecated Instead use {@link #setPayloadSizeThreshold(int)}
233+
*/
234+
@Deprecated
235+
public void setMessageSizeThreshold(int messageSizeThreshold) {
236+
this.setPayloadSizeThreshold(messageSizeThreshold);
237+
}
238+
239+
/**
240+
* Sets the message size threshold for storing message payloads in Amazon
241+
* S3.
242+
*
243+
* @param messageSizeThreshold
244+
* Message size threshold to be used for storing in Amazon S3.
245+
* Default: 256KB.
246+
* @return the updated ExtendedClientConfiguration object.
247+
*
248+
* @deprecated Instead use {@link #withPayloadSizeThreshold(int)}
249+
*/
250+
@Deprecated
251+
public ExtendedClientConfiguration withMessageSizeThreshold(int messageSizeThreshold) {
252+
setMessageSizeThreshold(messageSizeThreshold);
253+
return this;
254+
}
255+
256+
/**
257+
* Gets the message size threshold for storing message payloads in Amazon
258+
* S3.
259+
*
260+
* @return Message size threshold which is being used for storing in Amazon
261+
* S3. Default: 256KB.
262+
*
263+
* @deprecated Instead use {@link #getPayloadSizeThreshold()}
264+
*/
265+
@Deprecated
266+
public int getMessageSizeThreshold() {
267+
return getPayloadSizeThreshold();
268+
}
152269
}

src/main/java/com/amazon/sqs/javamessaging/SQSExtendedClientConstants.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,18 @@
1717

1818

1919
public class SQSExtendedClientConstants {
20+
// This constant is shared with SNSExtendedClient
21+
// SNS team should be notified of any changes made to this
2022
public static final String RESERVED_ATTRIBUTE_NAME = "ExtendedPayloadSize";
23+
24+
// This constant is shared with SNSExtendedClient
25+
// SNS team should be notified of any changes made to this
2126
public static final int MAX_ALLOWED_ATTRIBUTES = 10 - 1; // 10 for SQS, 1 for the reserved attribute
27+
28+
// This constant is shared with SNSExtendedClient
29+
// SNS team should be notified of any changes made to this
2230
public static final int DEFAULT_MESSAGE_SIZE_THRESHOLD = 262144;
31+
2332
public static final String S3_BUCKET_NAME_MARKER = "-..s3BucketName..-";
2433
public static final String S3_KEY_MARKER = "-..s3Key..-";
2534
}

src/test/java/com/amazon/sqs/javamessaging/AmazonSQSExtendedClientTest.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class AmazonSQSExtendedClientTest {
4747
private AmazonSQS extendedSqsWithCustomKMS;
4848
private AmazonSQS extendedSqsWithDefaultKMS;
4949
private AmazonSQS extendedSqsWithGenericReservedAttributeName;
50+
private AmazonSQS extendedSqsWithDeprecatedMethods;
5051
private AmazonSQS mockSqsBackend;
5152
private AmazonS3 mockS3;
5253
private static final String S3_BUCKET_NAME = "test-bucket-name";
@@ -80,10 +81,75 @@ public void setupClients() {
8081
ExtendedClientConfiguration extendedClientConfigurationWithGenericReservedAttributeName = new ExtendedClientConfiguration()
8182
.withPayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withLegacyReservedAttributeNameDisabled();
8283

84+
ExtendedClientConfiguration extendedClientConfigurationDeprecated = new ExtendedClientConfiguration()
85+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);
86+
8387
extendedSqsWithDefaultConfig = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
8488
extendedSqsWithCustomKMS = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithCustomKMS));
8589
extendedSqsWithDefaultKMS = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithDefaultKMS));
8690
extendedSqsWithGenericReservedAttributeName = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationWithGenericReservedAttributeName));
91+
extendedSqsWithDeprecatedMethods = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfigurationDeprecated));
92+
}
93+
94+
@Test
95+
public void testWhenSendMessageWithLargePayloadSupportDisabledThenS3IsNotUsedAndSqsBackendIsResponsibleToFailItWithDeprecatedMethod() {
96+
int messageLength = MORE_THAN_SQS_SIZE_LIMIT;
97+
String messageBody = generateStringWithLength(messageLength);
98+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
99+
.withLargePayloadSupportDisabled();
100+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
101+
102+
SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
103+
sqsExtended.sendMessage(messageRequest);
104+
105+
verify(mockS3, never()).putObject(isA(PutObjectRequest.class));
106+
verify(mockSqsBackend).sendMessage(eq(messageRequest));
107+
}
108+
109+
@Test
110+
public void testWhenSendMessageWithAlwaysThroughS3AndMessageIsSmallThenItIsStillStoredInS3WithDeprecatedMethod() {
111+
int messageLength = LESS_THAN_SQS_SIZE_LIMIT;
112+
String messageBody = generateStringWithLength(messageLength);
113+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
114+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withAlwaysThroughS3(true);
115+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));
116+
117+
SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
118+
sqsExtended.sendMessage(messageRequest);
119+
120+
verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
121+
}
122+
123+
@Test
124+
public void testWhenSendMessageWithSetMessageSizeThresholdThenThresholdIsHonoredWithDeprecatedMethod() {
125+
int messageLength = ARBITRARY_SMALLER_THRESHOLD * 2;
126+
String messageBody = generateStringWithLength(messageLength);
127+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
128+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME).withMessageSizeThreshold(ARBITRARY_SMALLER_THRESHOLD);
129+
130+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mock(AmazonSQSClient.class), extendedClientConfiguration));
131+
132+
SendMessageRequest messageRequest = new SendMessageRequest(SQS_QUEUE_URL, messageBody);
133+
sqsExtended.sendMessage(messageRequest);
134+
verify(mockS3, times(1)).putObject(isA(PutObjectRequest.class));
135+
}
136+
137+
@Test
138+
public void testReceiveMessageMultipleTimesDoesNotAdditionallyAlterReceiveMessageRequestWithDeprecatedMethod() {
139+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration()
140+
.withLargePayloadSupportEnabled(mockS3, S3_BUCKET_NAME);
141+
AmazonSQS sqsExtended = spy(new AmazonSQSExtendedClient(mockSqsBackend, extendedClientConfiguration));
142+
when(mockSqsBackend.receiveMessage(isA(ReceiveMessageRequest.class))).thenReturn(new ReceiveMessageResult());
143+
144+
ReceiveMessageRequest messageRequest = new ReceiveMessageRequest();
145+
ReceiveMessageRequest expectedRequest = new ReceiveMessageRequest()
146+
.withMessageAttributeNames(AmazonSQSExtendedClient.LEGACY_RESERVED_ATTRIBUTE_NAME, SQSExtendedClientConstants.RESERVED_ATTRIBUTE_NAME);
147+
148+
sqsExtended.receiveMessage(messageRequest);
149+
Assert.assertEquals(expectedRequest, messageRequest);
150+
151+
sqsExtended.receiveMessage(messageRequest);
152+
Assert.assertEquals(expectedRequest, messageRequest);
87153
}
88154

89155
@Test

src/test/java/com/amazon/sqs/javamessaging/ExtendedClientConfigurationTest.java

Lines changed: 70 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.amazon.sqs.javamessaging;
1717

1818
import com.amazonaws.services.s3.AmazonS3;
19+
import com.amazonaws.services.s3.model.PutObjectRequest;
1920
import com.amazonaws.services.s3.model.SSEAwsKeyManagementParams;
2021

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

34-
@Before
35-
public void setup() {
36-
}
37-
3835
@Test
3936
public void testCopyConstructor() {
4037
AmazonS3 s3 = mock(AmazonS3.class);
@@ -99,4 +96,73 @@ public void testLargePayloadSupportEnabledWithDeleteFromS3Disabled() {
9996
Assert.assertNotNull(extendedClientConfiguration.getAmazonS3Client());
10097
Assert.assertEquals(s3BucketName, extendedClientConfiguration.getS3BucketName());
10198
}
99+
100+
@Test
101+
public void testCopyConstructorDeprecated() {
102+
103+
AmazonS3 s3 = mock(AmazonS3.class);
104+
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);
105+
106+
boolean alwaysThroughS3 = true;
107+
int messageSizeThreshold = 500;
108+
109+
ExtendedClientConfiguration extendedClientConfig = new ExtendedClientConfiguration();
110+
111+
extendedClientConfig.withLargePayloadSupportEnabled(s3, s3BucketName)
112+
.withAlwaysThroughS3(alwaysThroughS3).withMessageSizeThreshold(messageSizeThreshold);
113+
114+
ExtendedClientConfiguration newExtendedClientConfig = new ExtendedClientConfiguration(extendedClientConfig);
115+
116+
Assert.assertEquals(s3, newExtendedClientConfig.getAmazonS3Client());
117+
Assert.assertEquals(s3BucketName, newExtendedClientConfig.getS3BucketName());
118+
Assert.assertTrue(newExtendedClientConfig.isLargePayloadSupportEnabled());
119+
Assert.assertEquals(alwaysThroughS3, newExtendedClientConfig.isAlwaysThroughS3());
120+
Assert.assertEquals(messageSizeThreshold, newExtendedClientConfig.getMessageSizeThreshold());
121+
122+
Assert.assertNotSame(newExtendedClientConfig, extendedClientConfig);
123+
}
124+
125+
@Test
126+
public void testLargePayloadSupportEnabled() {
127+
128+
AmazonS3 s3 = mock(AmazonS3.class);
129+
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);
130+
131+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
132+
extendedClientConfiguration.setLargePayloadSupportEnabled(s3, s3BucketName);
133+
134+
Assert.assertTrue(extendedClientConfiguration.isLargePayloadSupportEnabled());
135+
Assert.assertNotNull(extendedClientConfiguration.getAmazonS3Client());
136+
Assert.assertEquals(s3BucketName, extendedClientConfiguration.getS3BucketName());
137+
138+
}
139+
140+
@Test
141+
public void testDisableLargePayloadSupport() {
142+
143+
AmazonS3 s3 = mock(AmazonS3.class);
144+
when(s3.putObject(isA(PutObjectRequest.class))).thenReturn(null);
145+
146+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
147+
extendedClientConfiguration.setLargePayloadSupportDisabled();
148+
149+
Assert.assertNull(extendedClientConfiguration.getAmazonS3Client());
150+
Assert.assertNull(extendedClientConfiguration.getS3BucketName());
151+
152+
verify(s3, never()).putObject(isA(PutObjectRequest.class));
153+
}
154+
155+
@Test
156+
public void testMessageSizeThreshold() {
157+
158+
ExtendedClientConfiguration extendedClientConfiguration = new ExtendedClientConfiguration();
159+
160+
Assert.assertEquals(SQSExtendedClientConstants.DEFAULT_MESSAGE_SIZE_THRESHOLD,
161+
extendedClientConfiguration.getMessageSizeThreshold());
162+
163+
int messageLength = 1000;
164+
extendedClientConfiguration.setMessageSizeThreshold(messageLength);
165+
Assert.assertEquals(messageLength, extendedClientConfiguration.getMessageSizeThreshold());
166+
167+
}
102168
}

0 commit comments

Comments
 (0)