11package software .amazon .payloadoffloading ;
22
33import junitparams .JUnitParamsRunner ;
4- import junitparams .Parameters ;
54import org .hamcrest .Matchers ;
65import org .junit .Before ;
76import org .junit .Rule ;
1110import org .mockito .ArgumentCaptor ;
1211import software .amazon .awssdk .core .exception .SdkClientException ;
1312import software .amazon .awssdk .core .exception .SdkException ;
14-
15- import java .util .Objects ;
13+ import software .amazon .awssdk .services .s3 .model .ObjectCannedACL ;
1614
1715import static org .junit .Assert .*;
1816import static org .mockito .ArgumentMatchers .any ;
2119@ RunWith (JUnitParamsRunner .class )
2220public class S3BackedPayloadStoreTest {
2321 private static final String S3_BUCKET_NAME = "test-bucket-name" ;
24- private static final String S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID = "test-customer-managed-kms-key-id" ;
25- private static final ServerSideEncryptionStrategy KMS_WITH_CUSTOMER_KEY = ServerSideEncryptionFactory .customerKey (S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID );
26- private static final ServerSideEncryptionStrategy KMS_WITH_AWS_MANAGED_CMK = ServerSideEncryptionFactory .awsManagedCmk ();
2722 private static final String ANY_PAYLOAD = "AnyPayload" ;
2823 private static final String ANY_S3_KEY = "AnyS3key" ;
2924 private static final String INCORRECT_POINTER_EXCEPTION_MSG = "Failed to read the S3 object pointer from given string" ;
30- private static final Long ANY_PAYLOAD_LENGTH = 300000L ;
3125 private PayloadStore payloadStore ;
3226 private S3Dao s3Dao ;
3327
@@ -40,63 +34,23 @@ public void setup() {
4034 payloadStore = new S3BackedPayloadStore (s3Dao , S3_BUCKET_NAME );
4135 }
4236
43- private Object [] testData () {
44- // Here, we create separate mock of S3Dao because JUnitParamsRunner collects parameters
45- // for tests well before invocation of @Before or @BeforeClass methods.
46- // That means our default s3Dao mock isn't instantiated until then. For parameterized tests,
47- // we instantiate our local S3Dao mock per combination, pass it to S3BackedPayloadStore and also pass it
48- // as test parameter to allow verifying calls to the mockS3Dao.
49- S3Dao noEncryptionS3Dao = mock (S3Dao .class );
50- S3Dao defaultEncryptionS3Dao = mock (S3Dao .class );
51- S3Dao customerKMSKeyEncryptionS3Dao = mock (S3Dao .class );
52- return new Object [][]{
53- // No S3 SSE-KMS encryption
54- {
55- new S3BackedPayloadStore (noEncryptionS3Dao , S3_BUCKET_NAME ),
56- null ,
57- noEncryptionS3Dao
58- },
59- // S3 SSE-KMS encryption with AWS managed KMS keys
60- {
61- new S3BackedPayloadStore (defaultEncryptionS3Dao , S3_BUCKET_NAME , KMS_WITH_AWS_MANAGED_CMK ),
62- KMS_WITH_AWS_MANAGED_CMK ,
63- defaultEncryptionS3Dao
64- },
65- // S3 SSE-KMS encryption with customer managed KMS key
66- {
67- new S3BackedPayloadStore (customerKMSKeyEncryptionS3Dao , S3_BUCKET_NAME , KMS_WITH_CUSTOMER_KEY ),
68- KMS_WITH_CUSTOMER_KEY ,
69- customerKMSKeyEncryptionS3Dao
70- }
71- };
72- }
73-
7437 @ Test
75- @ Parameters (method = "testData" )
76- public void testStoreOriginalPayloadOnSuccess (PayloadStore payloadStore , ServerSideEncryptionStrategy expectedParams , S3Dao mockS3Dao ) {
38+ public void testStoreOriginalPayloadOnSuccess () {
7739 String actualPayloadPointer = payloadStore .storeOriginalPayload (ANY_PAYLOAD );
7840
7941 ArgumentCaptor <String > keyCaptor = ArgumentCaptor .forClass (String .class );
8042 ArgumentCaptor <ServerSideEncryptionStrategy > sseArgsCaptor = ArgumentCaptor .forClass (ServerSideEncryptionStrategy .class );
43+ ArgumentCaptor <ObjectCannedACL > cannedArgsCaptor = ArgumentCaptor .forClass (ObjectCannedACL .class );
8144
82- verify (mockS3Dao , times (1 )).storeTextInS3 (eq (S3_BUCKET_NAME ), keyCaptor .capture (),
83- sseArgsCaptor . capture (), eq (ANY_PAYLOAD ));
45+ verify (s3Dao , times (1 )).storeTextInS3 (eq (S3_BUCKET_NAME ), keyCaptor .capture (),
46+ eq (ANY_PAYLOAD ));
8447
8548 PayloadS3Pointer expectedPayloadPointer = new PayloadS3Pointer (S3_BUCKET_NAME , keyCaptor .getValue ());
8649 assertEquals (expectedPayloadPointer .toJson (), actualPayloadPointer );
87-
88- if (expectedParams == null ) {
89- assertTrue (sseArgsCaptor .getValue () == null );
90- } else {
91- assertEquals (expectedParams , sseArgsCaptor .getValue ());
92- }
9350 }
9451
9552 @ Test
96- @ Parameters (method = "testData" )
97- public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects (PayloadStore payloadStore ,
98- ServerSideEncryptionStrategy expectedParams ,
99- S3Dao mockS3Dao ) {
53+ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects () {
10054 //Store any payload
10155 String anyActualPayloadPointer = payloadStore .storeOriginalPayload (ANY_PAYLOAD );
10256
@@ -105,9 +59,10 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
10559
10660 ArgumentCaptor <String > anyOtherKeyCaptor = ArgumentCaptor .forClass (String .class );
10761 ArgumentCaptor <ServerSideEncryptionStrategy > sseArgsCaptor = ArgumentCaptor .forClass (ServerSideEncryptionStrategy .class );
62+ ArgumentCaptor <ObjectCannedACL > cannedArgsCaptor = ArgumentCaptor .forClass (ObjectCannedACL .class );
10863
109- verify (mockS3Dao , times (2 )).storeTextInS3 (eq (S3_BUCKET_NAME ), anyOtherKeyCaptor .capture (),
110- sseArgsCaptor . capture (), eq (ANY_PAYLOAD ));
64+ verify (s3Dao , times (2 )).storeTextInS3 (eq (S3_BUCKET_NAME ), anyOtherKeyCaptor .capture (),
65+ eq (ANY_PAYLOAD ));
11166
11267 String anyS3Key = anyOtherKeyCaptor .getAllValues ().get (0 );
11368 String anyOtherS3Key = anyOtherKeyCaptor .getAllValues ().get (1 );
@@ -120,25 +75,15 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
12075
12176 assertThat (anyS3Key , Matchers .not (anyOtherS3Key ));
12277 assertThat (anyActualPayloadPointer , Matchers .not (anyOtherActualPayloadPointer ));
123-
124- if (expectedParams == null ) {
125- assertTrue (sseArgsCaptor .getAllValues ().stream ().allMatch (Objects ::isNull ));
126- } else {
127- assertTrue (sseArgsCaptor .getAllValues ().stream ().allMatch (actualParams ->
128- actualParams .equals (expectedParams )));
129- }
13078 }
13179
13280 @ Test
133- @ Parameters (method = "testData" )
134- public void testStoreOriginalPayloadOnS3Failure (PayloadStore payloadStore , ServerSideEncryptionStrategy awsKmsKeyId , S3Dao mockS3Dao ) {
81+ public void testStoreOriginalPayloadOnS3Failure () {
13582 doThrow (SdkException .create ("S3 Exception" , new Throwable ()))
136- .when (mockS3Dao )
83+ .when (s3Dao )
13784 .storeTextInS3 (
13885 any (String .class ),
13986 any (String .class ),
140- // Can be String or null
141- any (),
14287 any (String .class ));
14388
14489 exception .expect (SdkException .class );
0 commit comments