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