Skip to content

Commit 38c14d6

Browse files
committed
chore(java): Adds afterTest cleanup method to clean DDB items created in test run
1 parent 614c082 commit 38c14d6

13 files changed

Lines changed: 89 additions & 4 deletions

File tree

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,20 @@
55
import java.util.concurrent.ThreadLocalRandom;
66

77
/**
8-
* Shared utilities for examples. The TEST_PREFIX provides a unique suffix
8+
* Shared utilities for examples. The TEST_SUFFIX provides a unique suffix
99
* per JVM process so that parallel CI matrix jobs (different Java versions / OS)
1010
* do not overwrite each other's items in the shared DynamoDB test table.
1111
*
1212
* Each example uses a hardcoded partition key like "rawEcdhKeyringItem".
1313
* In CI, multiple JVMs run in parallel against the same table. Without
1414
* isolation, one JVM can overwrite another's item, causing decrypt failures.
15-
* By appending a random prefix, each JVM writes to its own partition key
15+
* By appending a random suffix, each JVM writes to its own partition key
1616
* (e.g., "rawEcdhKeyringItem-482913") while keeping cross-example reads
1717
* working within the same JVM.
1818
*/
1919
public class ExampleUtils {
2020

21-
private static final String TEST_PREFIX = String.valueOf(
21+
private static final String TEST_SUFFIX = String.valueOf(
2222
ThreadLocalRandom.current().nextInt(100000, 999999)
2323
);
2424

@@ -27,6 +27,6 @@ public class ExampleUtils {
2727
* Example: "rawEcdhKeyringItem" becomes "rawEcdhKeyringItem-482913"
2828
*/
2929
public static String uniquePk(String base) {
30-
return base + "-" + TEST_PREFIX;
30+
return base + "-" + TEST_SUFFIX;
3131
}
3232
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/TestUtils.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,20 @@ public class TestUtils {
4242
public static final String TEST_DDB_TABLE_NAME =
4343
"DynamoDbEncryptionInterceptorTestTable";
4444

45+
/**
46+
* Deletes a test item using the standard table schema (partition_key S, sort_key N).
47+
* Use this for items written by examples that use ExampleUtils.uniquePk().
48+
*/
49+
public static void cleanUpExampleItem(final String partitionKeyBase) {
50+
cleanUpDDBItem(
51+
TEST_DDB_TABLE_NAME,
52+
"partition_key",
53+
"sort_key",
54+
ExampleUtils.uniquePk(partitionKeyBase),
55+
"0"
56+
);
57+
}
58+
4559
/**
4660
* Deletes an item from a DynamoDB table.
4761
*

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/clientsupplier/TestClientSupplierExample.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.List;
5+
import org.testng.annotations.AfterTest;
56
import org.testng.annotations.Test;
67
import software.amazon.awssdk.regions.Region;
78
import software.amazon.cryptography.examples.TestUtils;
@@ -35,4 +36,10 @@ public void TestClientSupplierExample() {
3536
regions
3637
);
3738
}
39+
40+
@AfterTest
41+
public void cleanup() {
42+
TestUtils.cleanUpExampleItem("clientSupplierItem");
43+
TestUtils.cleanUpExampleItem("awsKmsMrkMultiKeyringItem");
44+
}
3845
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/enhanced/TestEnhancedPutGetExample.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package software.amazon.cryptography.examples.enhanced;
22

3+
import org.testng.annotations.AfterTest;
34
import org.testng.annotations.Test;
45
import software.amazon.cryptography.examples.TestUtils;
56

@@ -28,4 +29,11 @@ public void TestTableSchemaBuilderPutGet() {
2829
TestUtils.TEST_DDB_TABLE_NAME
2930
);
3031
}
32+
33+
@AfterTest
34+
public void cleanup() {
35+
TestUtils.cleanUpExampleItem("EnhancedPutGetExample");
36+
TestUtils.cleanUpExampleItem("LombokPutGetExample");
37+
TestUtils.cleanUpExampleItem("TableSchemaBuilderPutGetExample");
38+
}
3139
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/enhanced/TestSingleTableExample.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package software.amazon.cryptography.examples.enhanced;
22

3+
import org.testng.annotations.AfterTest;
34
import org.testng.annotations.Test;
45
import software.amazon.cryptography.examples.TestUtils;
56

@@ -12,4 +13,11 @@ public void TestEnhancedSingleTable() {
1213
TestUtils.TEST_DDB_TABLE_NAME
1314
);
1415
}
16+
17+
@AfterTest
18+
public void cleanup() {
19+
TestUtils.cleanUpExampleItem("EnhancedPutGetExample1");
20+
TestUtils.cleanUpExampleItem("EnhancedPutGetExample2");
21+
TestUtils.cleanUpExampleItem("EnhancedPutGetExample3");
22+
}
1523
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/keyring/TestKmsEcdhKeyringExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.shouldGetNewPublicKeys;
66
import static software.amazon.cryptography.examples.keyring.KmsEcdhKeyringExample.writePublicKeyPemForEccKey;
77

8+
import org.testng.annotations.AfterTest;
89
import org.testng.annotations.Test;
910
import software.amazon.cryptography.examples.TestUtils;
1011

@@ -46,4 +47,9 @@ public void TestKmsEcdhKeyringExampleDiscovery() {
4647
TestUtils.TEST_KMS_ECDH_KEY_ID_P256_RECIPIENT
4748
);
4849
}
50+
51+
@AfterTest
52+
public void cleanup() {
53+
TestUtils.cleanUpExampleItem("awsKmsEcdhKeyringItem");
54+
}
4955
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/keyring/TestKmsRsaKeyringExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static software.amazon.cryptography.examples.keyring.KmsRsaKeyringExample.shouldGetNewPublicKey;
44
import static software.amazon.cryptography.examples.keyring.KmsRsaKeyringExample.writePublicKeyPemForRsaKey;
55

6+
import org.testng.annotations.AfterTest;
67
import org.testng.annotations.Test;
78
import software.amazon.cryptography.examples.TestUtils;
89

@@ -22,4 +23,9 @@ public void TestKmsRsaKeyringExample() {
2223
TestUtils.TEST_KMS_RSA_KEY_ID
2324
);
2425
}
26+
27+
@AfterTest
28+
public void cleanup() {
29+
TestUtils.cleanUpExampleItem("awsKmsRsaKeyringItem");
30+
}
2531
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/keyring/TestMrkDiscoveryMultiKeyringExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.util.Collections;
44
import java.util.List;
5+
import org.testng.annotations.AfterTest;
56
import org.testng.annotations.Test;
67
import software.amazon.cryptography.examples.TestUtils;
78

@@ -21,4 +22,9 @@ public void TestMrkDiscoveryMultiKeyringExample() {
2122
regions
2223
);
2324
}
25+
26+
@AfterTest
27+
public void cleanup() {
28+
TestUtils.cleanUpExampleItem("awsKmsMrkDiscoveryMultiKeyringItem");
29+
}
2430
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/keyring/TestMrkMultiKeyringExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package software.amazon.cryptography.examples.keyring;
22

3+
import org.testng.annotations.AfterTest;
34
import org.testng.annotations.Test;
45
import software.amazon.cryptography.examples.TestUtils;
56

@@ -14,4 +15,9 @@ public void TestMrkMultiKeyringExample() {
1415
TestUtils.TEST_MRK_REPLICA_KEY_ID_US_EAST_1
1516
);
1617
}
18+
19+
@AfterTest
20+
public void cleanup() {
21+
TestUtils.cleanUpExampleItem("awsKmsMrkMultiKeyringItem");
22+
}
1723
}

Examples/runtimes/java/DynamoDbEncryption/src/test/java/software/amazon/cryptography/examples/keyring/TestMultiKeyringExample.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package software.amazon.cryptography.examples.keyring;
22

33
import java.nio.ByteBuffer;
4+
import org.testng.annotations.AfterTest;
45
import org.testng.annotations.Test;
56
import software.amazon.cryptography.examples.TestUtils;
67

@@ -17,4 +18,9 @@ public void TestMultiKeyringExample() {
1718
aesKeyBytes
1819
);
1920
}
21+
22+
@AfterTest
23+
public void cleanup() {
24+
TestUtils.cleanUpExampleItem("multiKeyringItem");
25+
}
2026
}

0 commit comments

Comments
 (0)