Skip to content

Commit f0ff282

Browse files
authored
Shbikram/de conflict tests (#2207)
* chore(java): deconflicts the test run across various Java and OS versions * chore(java): attempts to deconflict tests across different java and OS versions * chore(java): fixing bugs in test scenario leading to failures * chore(java): fixes format issue and also addressing ddb parallel test run failure * chore(java): Adds afterTest cleanup method to clean DDB items created in test run
1 parent b4d88f1 commit f0ff282

27 files changed

Lines changed: 272 additions & 40 deletions

DynamoDbEncryption/runtimes/java/src/test/sdkv1/com/amazonaws/services/dynamodbv2/mapper/integration/ScanITCase.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,17 @@ public void testParallelScanExceptionHandling() {
237237
assertNotNull(ase.getErrorCode());
238238
assertNotNull(ase.getErrorType());
239239
assertNotNull(ase.getMessage());
240+
} catch (java.util.concurrent.RejectedExecutionException e) {
241+
// This is expected when the thread pool shuts down after the first
242+
// segment fails with AmazonServiceException, causing the second
243+
// segment's task to be rejected.
240244
} catch (Exception e) {
241-
fail("Should have seen the AmazonServiceException");
245+
fail(
246+
"Should have seen AmazonServiceException or RejectedExecutionException, but got: " +
247+
e.getClass().getName() +
248+
": " +
249+
e.getMessage()
250+
);
242251
}
243252
}
244253

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright Amazon.com Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: Apache-2.0
3+
package software.amazon.cryptography.examples;
4+
5+
import java.util.concurrent.ThreadLocalRandom;
6+
7+
/**
8+
* Shared utilities for examples. The TEST_SUFFIX provides a unique suffix
9+
* per JVM process so that parallel CI matrix jobs (different Java versions / OS)
10+
* do not overwrite each other's items in the shared DynamoDB test table.
11+
*
12+
* Each example uses a hardcoded partition key like "rawEcdhKeyringItem".
13+
* In CI, multiple JVMs run in parallel against the same table. Without
14+
* isolation, one JVM can overwrite another's item, causing decrypt failures.
15+
* By appending a random suffix, each JVM writes to its own partition key
16+
* (e.g., "rawEcdhKeyringItem-482913") while keeping cross-example reads
17+
* working within the same JVM.
18+
*/
19+
public class ExampleUtils {
20+
21+
private static final String TEST_SUFFIX = String.valueOf(
22+
ThreadLocalRandom.current().nextInt(100000, 999999)
23+
);
24+
25+
/**
26+
* Returns a partition key value unique to this JVM process.
27+
* Example: "rawEcdhKeyringItem" becomes "rawEcdhKeyringItem-482913"
28+
*/
29+
public static String uniquePk(String base) {
30+
return base + "-" + TEST_SUFFIX;
31+
}
32+
}

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/clientsupplier/ClientSupplierExample.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTableEncryptionConfig;
1616
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTablesEncryptionConfig;
1717
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.model.CryptoAction;
18+
import software.amazon.cryptography.examples.ExampleUtils;
1819
import software.amazon.cryptography.materialproviders.IKeyring;
1920
import software.amazon.cryptography.materialproviders.MaterialProviders;
2021
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkDiscoveryMultiKeyringInput;
@@ -166,7 +167,10 @@ public static void ClientSupplierPutItemGetItem(
166167
final HashMap<String, AttributeValue> item = new HashMap<>();
167168
item.put(
168169
"partition_key",
169-
AttributeValue.builder().s("clientSupplierItem").build()
170+
AttributeValue
171+
.builder()
172+
.s(ExampleUtils.uniquePk("clientSupplierItem"))
173+
.build()
170174
);
171175
item.put("sort_key", AttributeValue.builder().n("0").build());
172176
item.put(
@@ -191,7 +195,10 @@ public static void ClientSupplierPutItemGetItem(
191195
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
192196
keyToGet.put(
193197
"partition_key",
194-
AttributeValue.builder().s("clientSupplierItem").build()
198+
AttributeValue
199+
.builder()
200+
.s(ExampleUtils.uniquePk("clientSupplierItem"))
201+
.build()
195202
);
196203
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
197204

@@ -289,7 +296,10 @@ public static void ClientSupplierPutItemGetItem(
289296
new HashMap<>();
290297
onlyReplicaKeyKeyToGet.put(
291298
"partition_key",
292-
AttributeValue.builder().s("awsKmsMrkMultiKeyringItem").build()
299+
AttributeValue
300+
.builder()
301+
.s(ExampleUtils.uniquePk("awsKmsMrkMultiKeyringItem"))
302+
.build()
293303
);
294304
onlyReplicaKeyKeyToGet.put(
295305
"sort_key",

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/enhanced/EnhancedPutGetExample.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.CreateDynamoDbEncryptionInterceptorInput;
1818
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedClientEncryption;
1919
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedTableEncryptionConfig;
20+
import software.amazon.cryptography.examples.ExampleUtils;
21+
import software.amazon.cryptography.examples.ExampleUtils;
2022
import software.amazon.cryptography.materialproviders.IKeyring;
2123
import software.amazon.cryptography.materialproviders.MaterialProviders;
2224
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkMultiKeyringInput;
@@ -153,7 +155,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
153155
// The item will be encrypted client-side according to your
154156
// configuration above before it is sent to DynamoDb.
155157
final SimpleClass4 item = new SimpleClass4();
156-
item.setPartitionKey("EnhancedPutGetExample");
158+
item.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample"));
157159
item.setSortKey(0);
158160
item.setAttribute1("encrypt and sign me!");
159161
item.setAttribute2("sign me!");
@@ -167,7 +169,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
167169
// original item.
168170
final Key key = Key
169171
.builder()
170-
.partitionValue("EnhancedPutGetExample")
172+
.partitionValue(ExampleUtils.uniquePk("EnhancedPutGetExample"))
171173
.sortValue(0)
172174
.build();
173175

@@ -181,7 +183,8 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
181183

182184
// retrieve the same record via a Query
183185
PageIterable<SimpleClass4> items = table.query(
184-
QueryConditional.keyEqualTo(k -> k.partitionValue("EnhancedPutGetExample")
186+
QueryConditional.keyEqualTo(k ->
187+
k.partitionValue(ExampleUtils.uniquePk("EnhancedPutGetExample"))
185188
)
186189
);
187190
List<SimpleClass4> itemList = new ArrayList<SimpleClass4>();

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/enhanced/LombokPutGetExample.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedClientEncryption;
2828
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedTableEncryptionConfig;
2929
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.DynamoDbItemEncryptor;
30+
import software.amazon.cryptography.examples.ExampleUtils;
31+
import software.amazon.cryptography.examples.ExampleUtils;
3032
import software.amazon.cryptography.materialproviders.IKeyring;
3133
import software.amazon.cryptography.materialproviders.MaterialProviders;
3234
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkMultiKeyringInput;
@@ -163,7 +165,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
163165
// configuration above before it is sent to DynamoDb.
164166
final SimpleViaLombok.SimpleViaLombokBuilder itemBuilder =
165167
SimpleViaLombok.builder();
166-
itemBuilder.partitionKey("LombokPutGetExample");
168+
itemBuilder.partitionKey(ExampleUtils.uniquePk("LombokPutGetExample"));
167169
itemBuilder.sortKey(0);
168170
itemBuilder.attribute1("encrypt and sign me!");
169171
itemBuilder.attribute2("sign me!");
@@ -176,7 +178,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
176178
// original item.
177179
final Key key = Key
178180
.builder()
179-
.partitionValue("LombokPutGetExample")
181+
.partitionValue(ExampleUtils.uniquePk("LombokPutGetExample"))
180182
.sortValue(0)
181183
.build();
182184

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/enhanced/SingleTableExample.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.CreateDynamoDbEncryptionInterceptorInput;
1717
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedClientEncryption;
1818
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.DynamoDbEnhancedTableEncryptionConfig;
19+
import software.amazon.cryptography.examples.ExampleUtils;
20+
import software.amazon.cryptography.examples.ExampleUtils;
1921
import software.amazon.cryptography.materialproviders.IKeyring;
2022
import software.amazon.cryptography.materialproviders.MaterialProviders;
2123
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkMultiKeyringInput;
@@ -168,21 +170,21 @@ public static void TransactWriteItems(String kmsKeyId, String ddbTableName) {
168170
// The item will be encrypted client-side according to your
169171
// configuration above before it is sent to DynamoDb.
170172
final SimpleClass item1 = new SimpleClass();
171-
item1.setPartitionKey("EnhancedPutGetExample1");
173+
item1.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample1"));
172174
item1.setSortKey(0);
173175
item1.setAttribute1("item1 encrypt and sign me!");
174176
item1.setAttribute2("item1 sign me!");
175177
item1.setAttribute3("item1 ignore me!");
176178

177179
final SimpleClass2 item2 = new SimpleClass2();
178-
item2.setPartitionKey("EnhancedPutGetExample2");
180+
item2.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample2"));
179181
item2.setSortKey(0);
180182
item2.setAttribute4("item2 encrypt and sign me!");
181183
item2.setAttribute5("item2 sign me!");
182184
item2.setAttribute3("item2 ignore me!");
183185

184186
final SimpleClass3 item3 = new SimpleClass3();
185-
item3.setPartitionKey("EnhancedPutGetExample3");
187+
item3.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample3"));
186188
item3.setSortKey(0);
187189
item3.setAttribute6("item3 encrypt and sign me!");
188190
item3.setAttribute2("item3 sign me!");
@@ -202,15 +204,15 @@ public static void TransactWriteItems(String kmsKeyId, String ddbTableName) {
202204
// The item will be decrypted client-side, and you will get back the
203205
// original item.
204206
final SimpleClass key1 = new SimpleClass();
205-
key1.setPartitionKey("EnhancedPutGetExample1");
207+
key1.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample1"));
206208
key1.setSortKey(0);
207209

208210
final SimpleClass2 key2 = new SimpleClass2();
209-
key2.setPartitionKey("EnhancedPutGetExample2");
211+
key2.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample2"));
210212
key2.setSortKey(0);
211213

212214
final SimpleClass3 key3 = new SimpleClass3();
213-
key3.setPartitionKey("EnhancedPutGetExample3");
215+
key3.setPartitionKey(ExampleUtils.uniquePk("EnhancedPutGetExample3"));
214216
key3.setSortKey(0);
215217

216218
final TransactGetItemsEnhancedRequest getRequest =

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/enhanced/TableSchemaBuilderPutGetExample.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@
3131
import software.amazon.cryptography.dbencryptionsdk.dynamodb.enhancedclient.SignOnlyTag;
3232
import software.amazon.cryptography.dbencryptionsdk.dynamodb.itemencryptor.DynamoDbItemEncryptor;
3333
import software.amazon.cryptography.examples.ConfigUtils;
34+
import software.amazon.cryptography.examples.ExampleUtils;
35+
import software.amazon.cryptography.examples.ExampleUtils;
3436
import software.amazon.cryptography.materialproviders.IKeyring;
3537
import software.amazon.cryptography.materialproviders.MaterialProviders;
3638
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkMultiKeyringInput;
@@ -212,7 +214,9 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
212214
// The item will be encrypted client-side according to your
213215
// configuration above before it is sent to DynamoDb.
214216
final SimpleClass item = new SimpleClass();
215-
item.setPartitionKey("TableSchemaBuilderPutGetExample");
217+
item.setPartitionKey(
218+
ExampleUtils.uniquePk("TableSchemaBuilderPutGetExample")
219+
);
216220
item.setSortKey(0);
217221
item.setAttribute1("encrypt and sign me!");
218222
item.setAttribute2("sign me!");
@@ -224,7 +228,7 @@ public static void PutItemGetItem(String kmsKeyId, String ddbTableName) {
224228
// original item.
225229
final Key key = Key
226230
.builder()
227-
.partitionValue("TableSchemaBuilderPutGetExample")
231+
.partitionValue(ExampleUtils.uniquePk("TableSchemaBuilderPutGetExample"))
228232
.sortValue(0)
229233
.build();
230234

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/keyring/KmsEcdhKeyringExample.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTableEncryptionConfig;
4141
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTablesEncryptionConfig;
4242
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.model.CryptoAction;
43+
import software.amazon.cryptography.examples.ExampleUtils;
4344
import software.amazon.cryptography.materialproviders.IKeyring;
4445
import software.amazon.cryptography.materialproviders.MaterialProviders;
4546
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsEcdhKeyringInput;
@@ -339,7 +340,10 @@ private static void GetItemWithKeyring(
339340
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
340341
keyToGet.put(
341342
"partition_key",
342-
AttributeValue.builder().s("awsKmsEcdhKeyringItem").build()
343+
AttributeValue
344+
.builder()
345+
.s(ExampleUtils.uniquePk("awsKmsEcdhKeyringItem"))
346+
.build()
343347
);
344348
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
345349

@@ -456,7 +460,10 @@ private static void PutGetItemWithKeyring(
456460
final HashMap<String, AttributeValue> item = new HashMap<>();
457461
item.put(
458462
"partition_key",
459-
AttributeValue.builder().s("awsKmsEcdhKeyringItem").build()
463+
AttributeValue
464+
.builder()
465+
.s(ExampleUtils.uniquePk("awsKmsEcdhKeyringItem"))
466+
.build()
460467
);
461468
item.put("sort_key", AttributeValue.builder().n("0").build());
462469
item.put(
@@ -481,7 +488,10 @@ private static void PutGetItemWithKeyring(
481488
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
482489
keyToGet.put(
483490
"partition_key",
484-
AttributeValue.builder().s("awsKmsEcdhKeyringItem").build()
491+
AttributeValue
492+
.builder()
493+
.s(ExampleUtils.uniquePk("awsKmsEcdhKeyringItem"))
494+
.build()
485495
);
486496
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
487497

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/keyring/KmsRsaKeyringExample.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTableEncryptionConfig;
3030
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTablesEncryptionConfig;
3131
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.model.CryptoAction;
32+
import software.amazon.cryptography.examples.ExampleUtils;
3233
import software.amazon.cryptography.materialproviders.IKeyring;
3334
import software.amazon.cryptography.materialproviders.MaterialProviders;
3435
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsRsaKeyringInput;
@@ -201,7 +202,10 @@ public static void KmsRsaKeyringGetItemPutItem(
201202
final HashMap<String, AttributeValue> item = new HashMap<>();
202203
item.put(
203204
"partition_key",
204-
AttributeValue.builder().s("awsKmsRsaKeyringItem").build()
205+
AttributeValue
206+
.builder()
207+
.s(ExampleUtils.uniquePk("awsKmsRsaKeyringItem"))
208+
.build()
205209
);
206210
item.put("sort_key", AttributeValue.builder().n("0").build());
207211
item.put(
@@ -226,7 +230,10 @@ public static void KmsRsaKeyringGetItemPutItem(
226230
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
227231
keyToGet.put(
228232
"partition_key",
229-
AttributeValue.builder().s("awsKmsRsaKeyringItem").build()
233+
AttributeValue
234+
.builder()
235+
.s(ExampleUtils.uniquePk("awsKmsRsaKeyringItem"))
236+
.build()
230237
);
231238
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
232239

Examples/runtimes/java/DynamoDbEncryption/src/main/java/software/amazon/cryptography/examples/keyring/MrkDiscoveryMultiKeyringExample.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTableEncryptionConfig;
1717
import software.amazon.cryptography.dbencryptionsdk.dynamodb.model.DynamoDbTablesEncryptionConfig;
1818
import software.amazon.cryptography.dbencryptionsdk.structuredencryption.model.CryptoAction;
19+
import software.amazon.cryptography.examples.ExampleUtils;
1920
import software.amazon.cryptography.materialproviders.IKeyring;
2021
import software.amazon.cryptography.materialproviders.MaterialProviders;
2122
import software.amazon.cryptography.materialproviders.model.CreateAwsKmsMrkDiscoveryMultiKeyringInput;
@@ -157,7 +158,10 @@ public static void MultiMrkDiscoveryKeyringGetItemPutItem(
157158
final HashMap<String, AttributeValue> item = new HashMap<>();
158159
item.put(
159160
"partition_key",
160-
AttributeValue.builder().s("awsKmsMrkDiscoveryMultiKeyringItem").build()
161+
AttributeValue
162+
.builder()
163+
.s(ExampleUtils.uniquePk("awsKmsMrkDiscoveryMultiKeyringItem"))
164+
.build()
161165
);
162166
item.put("sort_key", AttributeValue.builder().n("0").build());
163167
item.put(
@@ -250,7 +254,10 @@ public static void MultiMrkDiscoveryKeyringGetItemPutItem(
250254
final HashMap<String, AttributeValue> keyToGet = new HashMap<>();
251255
keyToGet.put(
252256
"partition_key",
253-
AttributeValue.builder().s("awsKmsMrkDiscoveryMultiKeyringItem").build()
257+
AttributeValue
258+
.builder()
259+
.s(ExampleUtils.uniquePk("awsKmsMrkDiscoveryMultiKeyringItem"))
260+
.build()
254261
);
255262
keyToGet.put("sort_key", AttributeValue.builder().n("0").build());
256263

0 commit comments

Comments
 (0)