1
1
package software .amazon .payloadoffloading ;
2
2
3
3
import junitparams .JUnitParamsRunner ;
4
- import junitparams .Parameters ;
5
4
import org .hamcrest .Matchers ;
6
5
import org .junit .Before ;
7
6
import org .junit .Rule ;
11
10
import org .mockito .ArgumentCaptor ;
12
11
import software .amazon .awssdk .core .exception .SdkClientException ;
13
12
import software .amazon .awssdk .core .exception .SdkException ;
14
-
15
- import java .util .Objects ;
13
+ import software .amazon .awssdk .services .s3 .model .ObjectCannedACL ;
16
14
17
15
import static org .junit .Assert .*;
18
16
import static org .mockito .ArgumentMatchers .any ;
21
19
@ RunWith (JUnitParamsRunner .class )
22
20
public class S3BackedPayloadStoreTest {
23
21
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 ();
27
22
private static final String ANY_PAYLOAD = "AnyPayload" ;
28
23
private static final String ANY_S3_KEY = "AnyS3key" ;
29
24
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 ;
31
25
private PayloadStore payloadStore ;
32
26
private S3Dao s3Dao ;
33
27
@@ -40,63 +34,23 @@ public void setup() {
40
34
payloadStore = new S3BackedPayloadStore (s3Dao , S3_BUCKET_NAME );
41
35
}
42
36
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
-
74
37
@ Test
75
- @ Parameters (method = "testData" )
76
- public void testStoreOriginalPayloadOnSuccess (PayloadStore payloadStore , ServerSideEncryptionStrategy expectedParams , S3Dao mockS3Dao ) {
38
+ public void testStoreOriginalPayloadOnSuccess () {
77
39
String actualPayloadPointer = payloadStore .storeOriginalPayload (ANY_PAYLOAD );
78
40
79
41
ArgumentCaptor <String > keyCaptor = ArgumentCaptor .forClass (String .class );
80
42
ArgumentCaptor <ServerSideEncryptionStrategy > sseArgsCaptor = ArgumentCaptor .forClass (ServerSideEncryptionStrategy .class );
43
+ ArgumentCaptor <ObjectCannedACL > cannedArgsCaptor = ArgumentCaptor .forClass (ObjectCannedACL .class );
81
44
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 ));
84
47
85
48
PayloadS3Pointer expectedPayloadPointer = new PayloadS3Pointer (S3_BUCKET_NAME , keyCaptor .getValue ());
86
49
assertEquals (expectedPayloadPointer .toJson (), actualPayloadPointer );
87
-
88
- if (expectedParams == null ) {
89
- assertTrue (sseArgsCaptor .getValue () == null );
90
- } else {
91
- assertEquals (expectedParams , sseArgsCaptor .getValue ());
92
- }
93
50
}
94
51
95
52
@ Test
96
- @ Parameters (method = "testData" )
97
- public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects (PayloadStore payloadStore ,
98
- ServerSideEncryptionStrategy expectedParams ,
99
- S3Dao mockS3Dao ) {
53
+ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects () {
100
54
//Store any payload
101
55
String anyActualPayloadPointer = payloadStore .storeOriginalPayload (ANY_PAYLOAD );
102
56
@@ -105,9 +59,10 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
105
59
106
60
ArgumentCaptor <String > anyOtherKeyCaptor = ArgumentCaptor .forClass (String .class );
107
61
ArgumentCaptor <ServerSideEncryptionStrategy > sseArgsCaptor = ArgumentCaptor .forClass (ServerSideEncryptionStrategy .class );
62
+ ArgumentCaptor <ObjectCannedACL > cannedArgsCaptor = ArgumentCaptor .forClass (ObjectCannedACL .class );
108
63
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 ));
111
66
112
67
String anyS3Key = anyOtherKeyCaptor .getAllValues ().get (0 );
113
68
String anyOtherS3Key = anyOtherKeyCaptor .getAllValues ().get (1 );
@@ -120,25 +75,15 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
120
75
121
76
assertThat (anyS3Key , Matchers .not (anyOtherS3Key ));
122
77
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
- }
130
78
}
131
79
132
80
@ Test
133
- @ Parameters (method = "testData" )
134
- public void testStoreOriginalPayloadOnS3Failure (PayloadStore payloadStore , ServerSideEncryptionStrategy awsKmsKeyId , S3Dao mockS3Dao ) {
81
+ public void testStoreOriginalPayloadOnS3Failure () {
135
82
doThrow (SdkException .create ("S3 Exception" , new Throwable ()))
136
- .when (mockS3Dao )
83
+ .when (s3Dao )
137
84
.storeTextInS3 (
138
85
any (String .class ),
139
86
any (String .class ),
140
- // Can be String or null
141
- any (),
142
87
any (String .class ));
143
88
144
89
exception .expect (SdkException .class );
0 commit comments