15
15
16
16
package com .amazon .sqs .javamessaging ;
17
17
18
+ import static com .amazon .sqs .javamessaging .StringTestUtil .generateStringWithLength ;
19
+
20
+ import org .junit .jupiter .api .AfterEach ;
18
21
import org .junit .jupiter .api .BeforeEach ;
19
22
import org .junit .jupiter .api .Test ;
20
23
import org .mockito .ArgumentCaptor ;
24
+ import org .mockito .MockedStatic ;
21
25
import software .amazon .awssdk .awscore .AwsRequestOverrideConfiguration ;
22
26
import software .amazon .awssdk .core .ApiName ;
23
27
import software .amazon .awssdk .core .ResponseInputStream ;
62
66
import static org .junit .jupiter .api .Assertions .assertNull ;
63
67
import static org .junit .jupiter .api .Assertions .assertTrue ;
64
68
import static org .mockito .ArgumentMatchers .any ;
69
+ import static org .mockito .ArgumentMatchers .argThat ;
65
70
import static org .mockito .ArgumentMatchers .eq ;
66
71
import static org .mockito .Mockito .isA ;
67
72
import static org .mockito .Mockito .mock ;
73
+ import static org .mockito .Mockito .mockStatic ;
68
74
import static org .mockito .Mockito .never ;
69
75
import static org .mockito .Mockito .spy ;
70
76
import static org .mockito .Mockito .times ;
@@ -82,11 +88,16 @@ public class AmazonSQSExtendedClientTest {
82
88
private SqsClient extendedSqsWithDefaultKMS ;
83
89
private SqsClient extendedSqsWithGenericReservedAttributeName ;
84
90
private SqsClient extendedSqsWithDeprecatedMethods ;
91
+ private SqsClient extendedSqsWithS3KeyPrefix ;
85
92
private SqsClient mockSqsBackend ;
86
93
private S3Client mockS3 ;
94
+
95
+ private MockedStatic <UUID > uuidMockStatic ;
87
96
private static final String S3_BUCKET_NAME = "test-bucket-name" ;
88
97
private static final String SQS_QUEUE_URL = "test-queue-url" ;
89
98
private static final String S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID = "test-customer-managed-kms-key-id" ;
99
+ private static final String S3_KEY_PREFIX = "test-s3-key-prefix" ;
100
+ private static final String S3_KEY_UUID = "test-s3-key-uuid" ;
90
101
91
102
private static final int LESS_THAN_SQS_SIZE_LIMIT = 3 ;
92
103
private static final int SQS_SIZE_LIMIT = 262144 ;
@@ -101,6 +112,7 @@ public class AmazonSQSExtendedClientTest {
101
112
102
113
@ BeforeEach
103
114
public void setupClients () {
115
+ uuidMockStatic = mockStatic (UUID .class );
104
116
mockS3 = mock (S3Client .class );
105
117
mockSqsBackend = mock (SqsClient .class );
106
118
when (mockS3 .putObject (isA (PutObjectRequest .class ), isA (RequestBody .class ))).thenReturn (null );
@@ -121,11 +133,25 @@ public void setupClients() {
121
133
122
134
ExtendedClientConfiguration extendedClientConfigurationDeprecated = new ExtendedClientConfiguration ().withPayloadSupportEnabled (mockS3 , S3_BUCKET_NAME );
123
135
136
+ ExtendedClientConfiguration extendedClientConfigurationWithS3KeyPrefix = new ExtendedClientConfiguration ()
137
+ .withPayloadSupportEnabled (mockS3 , S3_BUCKET_NAME )
138
+ .withS3KeyPrefix (S3_KEY_PREFIX );
139
+
140
+ UUID uuidMock = mock (UUID .class );
141
+ when (uuidMock .toString ()).thenReturn (S3_KEY_UUID );
142
+ uuidMockStatic .when (UUID ::randomUUID ).thenReturn (uuidMock );
143
+
124
144
extendedSqsWithDefaultConfig = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfiguration ));
125
145
extendedSqsWithCustomKMS = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithCustomKMS ));
126
146
extendedSqsWithDefaultKMS = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithDefaultKMS ));
127
147
extendedSqsWithGenericReservedAttributeName = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithGenericReservedAttributeName ));
128
148
extendedSqsWithDeprecatedMethods = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationDeprecated ));
149
+ extendedSqsWithS3KeyPrefix = spy (new AmazonSQSExtendedClient (mockSqsBackend , extendedClientConfigurationWithS3KeyPrefix ));
150
+ }
151
+
152
+ @ AfterEach
153
+ public void tearDown () {
154
+ uuidMockStatic .close ();
129
155
}
130
156
131
157
@ Test
@@ -617,6 +643,32 @@ public void testWhenSendMessageWIthCannedAccessControlListDefined() {
617
643
assertEquals (expected , captor .getValue ().acl ());
618
644
}
619
645
646
+ @ Test
647
+ public void testWhenSendLargeMessageWithS3PrefixKeyDefined () {
648
+ String messageBody = generateStringWithLength (MORE_THAN_SQS_SIZE_LIMIT );
649
+
650
+ SendMessageRequest messageRequest = SendMessageRequest .builder ().queueUrl (SQS_QUEUE_URL ).messageBody (messageBody ).build ();
651
+
652
+ extendedSqsWithS3KeyPrefix .sendMessage (messageRequest );
653
+
654
+ verify (mockS3 , times (1 )).putObject (
655
+ argThat ((PutObjectRequest obj ) -> obj .key ().equals (S3_KEY_PREFIX + S3_KEY_UUID )),
656
+ isA (RequestBody .class ));
657
+ }
658
+
659
+ @ Test
660
+ public void testWhenSendLargeMessageWithUndefinedS3PrefixKey () {
661
+ String messageBody = generateStringWithLength (MORE_THAN_SQS_SIZE_LIMIT );
662
+
663
+ SendMessageRequest messageRequest = SendMessageRequest .builder ().queueUrl (SQS_QUEUE_URL ).messageBody (messageBody ).build ();
664
+
665
+ extendedSqsWithDefaultConfig .sendMessage (messageRequest );
666
+
667
+ verify (mockS3 , times (1 )).putObject (
668
+ argThat ((PutObjectRequest obj ) -> obj .key ().equals (S3_KEY_UUID )),
669
+ isA (RequestBody .class ));
670
+ }
671
+
620
672
private void testReceiveMessage_when_MessageIsLarge (String reservedAttributeName ) {
621
673
String pointer = new PayloadS3Pointer (S3_BUCKET_NAME , "S3Key" ).toJson ();
622
674
Message message = Message .builder ()
@@ -665,11 +717,4 @@ private String getLargeReceiptHandle(String s3Key, String originalReceiptHandle)
665
717
private String getSampleLargeReceiptHandle (String originalReceiptHandle ) {
666
718
return getLargeReceiptHandle (UUID .randomUUID ().toString (), originalReceiptHandle );
667
719
}
668
-
669
- private String generateStringWithLength (int messageLength ) {
670
- char [] charArray = new char [messageLength ];
671
- Arrays .fill (charArray , 'x' );
672
- return new String (charArray );
673
- }
674
-
675
720
}
0 commit comments