15
15
16
16
package com .amazon .sqs .javamessaging ;
17
17
18
+ import static com .amazon .sqs .javamessaging .StringTestUtil .generateStringWithLength ;
19
+
18
20
import com .amazonaws .AmazonServiceException ;
19
21
import com .amazonaws .services .s3 .AmazonS3 ;
20
22
import com .amazonaws .services .s3 .model .CannedAccessControlList ;
35
37
import com .amazonaws .services .sqs .model .SendMessageBatchRequestEntry ;
36
38
import com .amazonaws .services .sqs .model .SendMessageRequest ;
37
39
import com .amazonaws .util .StringInputStream ;
40
+ import org .junit .jupiter .api .AfterEach ;
38
41
import org .junit .jupiter .api .BeforeEach ;
39
42
import org .junit .jupiter .api .Test ;
40
43
import org .mockito .ArgumentCaptor ;
44
+ import org .mockito .MockedStatic ;
41
45
import software .amazon .payloadoffloading .PayloadS3Pointer ;
42
46
43
47
import java .util .ArrayList ;
44
- import java .util .Arrays ;
45
48
import java .util .List ;
46
49
import java .util .Map ;
47
50
import java .util .UUID ;
55
58
import static org .junit .jupiter .api .Assertions .fail ;
56
59
import static org .mockito .ArgumentMatchers .any ;
57
60
import static org .mockito .ArgumentMatchers .anyString ;
61
+ import static org .mockito .ArgumentMatchers .argThat ;
58
62
import static org .mockito .ArgumentMatchers .eq ;
59
63
import static org .mockito .Mockito .doThrow ;
60
64
import static org .mockito .Mockito .isA ;
61
65
import static org .mockito .Mockito .mock ;
66
+ import static org .mockito .Mockito .mockStatic ;
62
67
import static org .mockito .Mockito .never ;
63
68
import static org .mockito .Mockito .spy ;
64
69
import static org .mockito .Mockito .times ;
@@ -76,11 +81,16 @@ public class AmazonSQSExtendedClientTest {
76
81
private AmazonSQS extendedSqsWithDefaultKMS ;
77
82
private AmazonSQS extendedSqsWithGenericReservedAttributeName ;
78
83
private AmazonSQS extendedSqsWithDeprecatedMethods ;
84
+ private AmazonSQS extendedSqsWithS3KeyPrefix ;
79
85
private AmazonSQS mockSqsBackend ;
80
86
private AmazonS3 mockS3 ;
87
+ private MockedStatic <UUID > uuidMockStatic ;
88
+
81
89
private static final String S3_BUCKET_NAME = "test-bucket-name" ;
82
90
private static final String SQS_QUEUE_URL = "test-queue-url" ;
83
91
private static final String S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID = "test-customer-managed-kms-key-id" ;
92
+ private static final String S3_KEY_PREFIX = "test-s3-key-prefix" ;
93
+ private static final String S3_KEY_UUID = "test-s3-key-uuid" ;
84
94
85
95
private static final int LESS_THAN_SQS_SIZE_LIMIT = 3 ;
86
96
private static final int SQS_SIZE_LIMIT = 262144 ;
@@ -91,6 +101,7 @@ public class AmazonSQSExtendedClientTest {
91
101
92
102
@ BeforeEach
93
103
public void setupClients () {
104
+ uuidMockStatic = mockStatic (UUID .class );
94
105
mockS3 = mock (AmazonS3 .class );
95
106
mockSqsBackend = mock (AmazonSQS .class );
96
107
when (mockS3 .putObject (isA (PutObjectRequest .class ))).thenReturn (null );
@@ -111,11 +122,25 @@ public void setupClients() {
111
122
112
123
ExtendedClientConfiguration extendedClientConfigurationDeprecated = new ExtendedClientConfiguration ().withPayloadSupportEnabled (mockS3 , S3_BUCKET_NAME );
113
124
125
+ ExtendedClientConfiguration extendedClientConfigurationWithS3KeyPrefix = new ExtendedClientConfiguration ()
126
+ .withPayloadSupportEnabled (mockS3 , S3_BUCKET_NAME )
127
+ .withS3KeyPrefix (S3_KEY_PREFIX );
128
+
129
+ UUID uuidMock = mock (UUID .class );
130
+ when (uuidMock .toString ()).thenReturn (S3_KEY_UUID );
131
+ uuidMockStatic .when (UUID ::randomUUID ).thenReturn (uuidMock );
132
+
114
133
extendedSqsWithDefaultConfig = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfiguration ));
115
134
extendedSqsWithCustomKMS = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithCustomKMS ));
116
135
extendedSqsWithDefaultKMS = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithDefaultKMS ));
117
136
extendedSqsWithGenericReservedAttributeName = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithGenericReservedAttributeName ));
118
137
extendedSqsWithDeprecatedMethods = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationDeprecated ));
138
+ extendedSqsWithS3KeyPrefix = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithS3KeyPrefix ));
139
+ }
140
+
141
+ @ AfterEach
142
+ public void tearDown () {
143
+ uuidMockStatic .close ();
119
144
}
120
145
121
146
@ Test
@@ -571,6 +596,30 @@ public void testWhenSendMessageWIthCannedAccessControlListDefined() {
571
596
assertEquals (expected , captor .getValue ().getCannedAcl ());
572
597
}
573
598
599
+ @ Test
600
+ public void testWhenSendLargeMessageWithS3PrefixKeyDefined () {
601
+ String messageBody = generateStringWithLength (MORE_THAN_SQS_SIZE_LIMIT );
602
+
603
+ SendMessageRequest messageRequest = new SendMessageRequest (SQS_QUEUE_URL , messageBody );
604
+
605
+ extendedSqsWithS3KeyPrefix .sendMessage (messageRequest );
606
+
607
+ verify (mockS3 , times (1 )).putObject (
608
+ argThat ((PutObjectRequest obj ) -> obj .getKey ().equals (S3_KEY_PREFIX + S3_KEY_UUID )));
609
+ }
610
+
611
+ @ Test
612
+ public void testWhenSendLargeMessageWithUndefinedS3PrefixKey () {
613
+ String messageBody = generateStringWithLength (MORE_THAN_SQS_SIZE_LIMIT );
614
+
615
+ SendMessageRequest messageRequest = new SendMessageRequest (SQS_QUEUE_URL , messageBody );
616
+
617
+ extendedSqsWithDefaultConfig .sendMessage (messageRequest );
618
+
619
+ verify (mockS3 , times (1 )).putObject (
620
+ argThat ((PutObjectRequest obj ) -> obj .getKey ().equals (S3_KEY_UUID )));
621
+ }
622
+
574
623
private void testReceiveMessage_when_MessageIsLarge (String reservedAttributeName ) throws Exception {
575
624
Message message = new Message ().addMessageAttributesEntry (reservedAttributeName , mock (MessageAttributeValue .class ));
576
625
String pointer = new PayloadS3Pointer (S3_BUCKET_NAME , "S3Key" ).toJson ();
@@ -607,11 +656,4 @@ private String getLargeReceiptHandle(String s3Key, String originalReceiptHandle)
607
656
private String getSampleLargeReceiptHandle () {
608
657
return getLargeReceiptHandle (UUID .randomUUID ().toString (), UUID .randomUUID ().toString ());
609
658
}
610
-
611
- private String generateStringWithLength (int messageLength ) {
612
- char [] charArray = new char [messageLength ];
613
- Arrays .fill (charArray , 'x' );
614
- return new String (charArray );
615
- }
616
-
617
659
}
0 commit comments