1
1
package software .amazon .payloadoffloading ;
2
2
3
3
import com .amazonaws .AmazonClientException ;
4
+ import com .amazonaws .services .s3 .model .CannedAccessControlList ;
4
5
import com .amazonaws .services .s3 .model .SSEAwsKeyManagementParams ;
5
6
import junitparams .JUnitParamsRunner ;
6
- import junitparams .Parameters ;
7
7
import org .hamcrest .Matchers ;
8
8
import org .junit .Before ;
9
9
import org .junit .Rule ;
@@ -35,65 +35,23 @@ public void setup() {
35
35
payloadStore = new S3BackedPayloadStore (s3Dao , S3_BUCKET_NAME );
36
36
}
37
37
38
- private Object [] testData () {
39
- // Here, we create separate mock of S3Dao because JUnitParamsRunner collects parameters
40
- // for tests well before invocation of @Before or @BeforeClass methods.
41
- // That means our default s3Dao mock isn't instantiated until then. For parameterized tests,
42
- // we instantiate our local S3Dao mock per combination, pass it to S3BackedPayloadStore and also pass it
43
- // as test parameter to allow verifying calls to the mockS3Dao.
44
- S3Dao noEncryptionS3Dao = mock (S3Dao .class );
45
- S3Dao defaultEncryptionS3Dao = mock (S3Dao .class );
46
- S3Dao customerKMSKeyEncryptionS3Dao = mock (S3Dao .class );
47
- return new Object [][]{
48
- // No S3 SSE-KMS encryption
49
- {
50
- new S3BackedPayloadStore (noEncryptionS3Dao , S3_BUCKET_NAME ),
51
- null ,
52
- noEncryptionS3Dao
53
- },
54
- // S3 SSE-KMS encryption with AWS managed KMS keys
55
- {
56
- new S3BackedPayloadStore (defaultEncryptionS3Dao , S3_BUCKET_NAME , new SSEAwsKeyManagementParams ()),
57
- new SSEAwsKeyManagementParams (),
58
- defaultEncryptionS3Dao
59
- },
60
- // S3 SSE-KMS encryption with customer managed KMS key
61
- {
62
- new S3BackedPayloadStore (customerKMSKeyEncryptionS3Dao , S3_BUCKET_NAME ,
63
- new SSEAwsKeyManagementParams (S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID )),
64
- new SSEAwsKeyManagementParams (S3_SERVER_SIDE_ENCRYPTION_KMS_KEY_ID ),
65
- customerKMSKeyEncryptionS3Dao
66
- }
67
- };
68
- }
69
-
70
38
@ Test
71
- @ Parameters (method = "testData" )
72
- public void testStoreOriginalPayloadOnSuccess (PayloadStore payloadStore ,
73
- SSEAwsKeyManagementParams expectedParams , S3Dao mockS3Dao ) {
39
+ public void testStoreOriginalPayloadOnSuccess () {
74
40
String actualPayloadPointer = payloadStore .storeOriginalPayload (ANY_PAYLOAD , ANY_PAYLOAD_LENGTH );
75
41
76
42
ArgumentCaptor <String > keyCaptor = ArgumentCaptor .forClass (String .class );
77
43
ArgumentCaptor <SSEAwsKeyManagementParams > sseArgsCaptor = ArgumentCaptor .forClass (SSEAwsKeyManagementParams .class );
44
+ ArgumentCaptor <CannedAccessControlList > cannedArgsCaptor = ArgumentCaptor .forClass (CannedAccessControlList .class );
78
45
79
- verify (mockS3Dao , times (1 )).storeTextInS3 (eq (S3_BUCKET_NAME ), keyCaptor .capture (),
80
- sseArgsCaptor . capture (), eq (ANY_PAYLOAD ), eq (ANY_PAYLOAD_LENGTH ));
46
+ verify (s3Dao , times (1 )).storeTextInS3 (eq (S3_BUCKET_NAME ), keyCaptor .capture (),
47
+ eq (ANY_PAYLOAD ), eq (ANY_PAYLOAD_LENGTH ));
81
48
82
49
PayloadS3Pointer expectedPayloadPointer = new PayloadS3Pointer (S3_BUCKET_NAME , keyCaptor .getValue ());
83
50
assertEquals (expectedPayloadPointer .toJson (), actualPayloadPointer );
84
-
85
- if (expectedParams == null ) {
86
- assertTrue (sseArgsCaptor .getValue () == null );
87
- } else {
88
- assertEquals (expectedParams .getAwsKmsKeyId (), sseArgsCaptor .getValue ().getAwsKmsKeyId ());
89
- }
90
51
}
91
52
92
53
@ Test
93
- @ Parameters (method = "testData" )
94
- public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects (PayloadStore payloadStore ,
95
- SSEAwsKeyManagementParams expectedParams ,
96
- S3Dao mockS3Dao ) {
54
+ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects () {
97
55
//Store any payload
98
56
String anyActualPayloadPointer = payloadStore
99
57
.storeOriginalPayload (ANY_PAYLOAD , ANY_PAYLOAD_LENGTH );
@@ -104,11 +62,8 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
104
62
105
63
ArgumentCaptor <String > anyOtherKeyCaptor = ArgumentCaptor .forClass (String .class );
106
64
107
- ArgumentCaptor <SSEAwsKeyManagementParams > sseArgsCaptor = ArgumentCaptor
108
- .forClass (SSEAwsKeyManagementParams .class );
109
-
110
- verify (mockS3Dao , times (2 )).storeTextInS3 (eq (S3_BUCKET_NAME ), anyOtherKeyCaptor .capture (),
111
- sseArgsCaptor .capture (), eq (ANY_PAYLOAD ), eq (ANY_PAYLOAD_LENGTH ));
65
+ verify (s3Dao , times (2 )).storeTextInS3 (eq (S3_BUCKET_NAME ), anyOtherKeyCaptor .capture (),
66
+ eq (ANY_PAYLOAD ), eq (ANY_PAYLOAD_LENGTH ));
112
67
113
68
String anyS3Key = anyOtherKeyCaptor .getAllValues ().get (0 );
114
69
String anyOtherS3Key = anyOtherKeyCaptor .getAllValues ().get (1 );
@@ -121,26 +76,15 @@ public void testStoreOriginalPayloadDoesAlwaysCreateNewObjects(PayloadStore payl
121
76
122
77
assertThat (anyS3Key , Matchers .not (anyOtherS3Key ));
123
78
assertThat (anyActualPayloadPointer , Matchers .not (anyOtherActualPayloadPointer ));
124
-
125
- if (expectedParams == null ) {
126
- assertTrue (sseArgsCaptor .getAllValues ().stream ().allMatch (actualParams -> actualParams == null ));
127
- } else {
128
- assertTrue (sseArgsCaptor .getAllValues ().stream ().allMatch (actualParams ->
129
- (actualParams .getAwsKmsKeyId () == null && expectedParams .getAwsKmsKeyId () == null )
130
- || (actualParams .getAwsKmsKeyId ().equals (expectedParams .getAwsKmsKeyId ()))));
131
- }
132
79
}
133
80
134
81
@ Test
135
- @ Parameters (method = "testData" )
136
- public void testStoreOriginalPayloadOnS3Failure (PayloadStore payloadStore ,
137
- SSEAwsKeyManagementParams expectedParams , S3Dao mockS3Dao ) {
82
+ public void testStoreOriginalPayloadOnS3Failure () {
138
83
doThrow (new AmazonClientException ("S3 Exception" ))
139
- .when (mockS3Dao )
84
+ .when (s3Dao )
140
85
.storeTextInS3 (
141
86
any (String .class ),
142
87
any (String .class ),
143
- expectedParams == null ? isNull () : any (SSEAwsKeyManagementParams .class ),
144
88
any (String .class ),
145
89
any (Long .class ));
146
90
0 commit comments