Skip to content

Commit ff9dab6

Browse files
Updating tests to use assertThrows
1 parent 28aeee2 commit ff9dab6

File tree

6 files changed

+22
-42
lines changed

6 files changed

+22
-42
lines changed

src/main/java/com/amazonaws/encryptionsdk/kms/AwsKmsCmkId.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ public final class AwsKmsCmkId {
3030
private String keyId;
3131

3232
private AwsKmsCmkId(String keyId) throws MalformedArnException {
33-
Validate.notBlank(keyId, "keyId is required");
33+
Validate.notBlank(keyId, "keyId must be neither null, empty nor whitespace");
3434

3535
if (keyId.startsWith(ARN_PREFIX)) {
3636
try {

src/test/java/com/amazonaws/crypto/examples/FileStreamingExampleTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,14 @@ class FileStreamingExampleTest {
2626
@Test
2727
void testEncryptAndDecrypt() throws IOException {
2828
final File tempFile = File.createTempFile("FileStreamingExampleTest-TempTestData", ".tmp");
29-
final File encryptedFile = new File(tempFile.getPath() + ".encrypted");
29+
final File encryptedFile = new File(tempFile.getPath() + ".encrypted");
3030
final File decryptedFile = new File(tempFile.getPath() + ".decrypted");
3131
tempFile.deleteOnExit();
3232
encryptedFile.deleteOnExit();
3333
decryptedFile.deleteOnExit();
3434

35-
try(BufferedWriter writer = Files.newBufferedWriter(tempFile.toPath())) {
36-
for(int i = 0; i < 1000 ; i++) {
35+
try (BufferedWriter writer = Files.newBufferedWriter(tempFile.toPath())) {
36+
for (int i = 0; i < 1000 ; i++) {
3737
writer.write(RandomStringUtils.randomAlphanumeric(100));
3838
writer.newLine();
3939
}

src/test/java/com/amazonaws/encryptionsdk/AwsCryptoTest.java

+8-18
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import static org.junit.Assert.assertEquals;
2222
import static org.junit.Assert.assertFalse;
2323
import static org.junit.Assert.assertTrue;
24-
import static org.junit.Assert.fail;
2524
import static org.mockito.ArgumentMatchers.any;
2625
import static org.mockito.Mockito.reset;
2726
import static org.mockito.Mockito.spy;
@@ -126,15 +125,10 @@ private void doTamperedEncryptDecrypt(final CryptoAlgorithm cryptoAlg, final int
126125
plaintextBytes,
127126
encryptionContext).getResult();
128127
cipherText[cipherText.length - 2] ^= (byte) 0xff;
129-
try {
130-
encryptionClient_.decryptData(
131-
masterKeyProvider,
132-
cipherText
133-
).getResult();
134-
fail("Expected BadCiphertextException");
135-
} catch (final BadCiphertextException ex) {
136-
// Expected exception
137-
}
128+
129+
assertThrows(BadCiphertextException.class, () -> encryptionClient_.decryptData(
130+
masterKeyProvider,
131+
cipherText));
138132
}
139133

140134
private void doTamperedEncryptDecryptWithKeyring(final CryptoAlgorithm cryptoAlg, final int byteSize, final int frameSize) {
@@ -150,14 +144,10 @@ private void doTamperedEncryptDecryptWithKeyring(final CryptoAlgorithm cryptoAlg
150144
.keyring(keyring)
151145
.plaintext(plaintextBytes).build()).getResult();
152146
cipherText[cipherText.length - 2] ^= (byte) 0xff;
153-
try {
154-
encryptionClient_.decrypt(DecryptRequest.builder()
155-
.keyring(keyring)
156-
.ciphertext(cipherText).build());
157-
fail("Expected BadCiphertextException");
158-
} catch (final BadCiphertextException ex) {
159-
// Expected exception
160-
}
147+
148+
assertThrows(BadCiphertextException.class, () -> encryptionClient_.decrypt(DecryptRequest.builder()
149+
.keyring(keyring)
150+
.ciphertext(cipherText).build()));
161151
}
162152

163153
private void doEncryptDecryptWithParsedCiphertext(final int byteSize, final int frameSize) {

src/test/java/com/amazonaws/encryptionsdk/DecryptRequestTest.java

+5-4
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,11 @@ void testKeyringUsesDefaultCmm() {
5656
.plaintext(new byte[]{4, 5, 6})
5757
.build()).getResult();
5858

59-
60-
assertTrue(DecryptRequest.builder()
59+
final CryptoMaterialsManager cryptoMaterialsManager = DecryptRequest.builder()
6160
.keyring(keyring)
62-
.ciphertext(ciphertext).build().cryptoMaterialsManager()
63-
instanceof DefaultCryptoMaterialsManager);
61+
.ciphertext(ciphertext).build()
62+
.cryptoMaterialsManager();
63+
64+
assertTrue(cryptoMaterialsManager instanceof DefaultCryptoMaterialsManager);
6465
}
6566
}

src/test/java/com/amazonaws/encryptionsdk/DefaultCryptoMaterialsManagerTest.java

+5-15
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
import static org.junit.Assert.assertEquals;
77
import static org.junit.Assert.assertNotNull;
88
import static org.junit.Assert.assertNull;
9-
import static org.junit.Assert.fail;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
1010
import static org.mockito.Matchers.any;
1111
import static org.mockito.Matchers.argThat;
1212
import static org.mockito.Matchers.same;
@@ -414,13 +414,8 @@ public void decrypt_whenTrailingSigMissing_throwsException() {
414414
.setEncryptionContext(Collections.emptyMap())
415415
.build();
416416

417-
try {
418-
new DefaultCryptoMaterialsManager(mk1).decryptMaterials(request);
419-
fail("expected exception");
420-
} catch (AwsCryptoException e) {
421-
// ok
422-
continue;
423-
}
417+
assertThrows(AwsCryptoException.class, () ->
418+
new DefaultCryptoMaterialsManager(mk1).decryptMaterials(request));
424419
}
425420
}
426421

@@ -441,13 +436,8 @@ public void decrypt_whenTrailingSigMissingForKeyring_throwsException() {
441436
.setEncryptionContext(Collections.emptyMap())
442437
.build();
443438

444-
try {
445-
new DefaultCryptoMaterialsManager(keyring1).decryptMaterials(request);
446-
fail("expected exception");
447-
} catch (AwsCryptoException e) {
448-
// ok
449-
continue;
450-
}
439+
assertThrows(AwsCryptoException.class, () ->
440+
new DefaultCryptoMaterialsManager(keyring1).decryptMaterials(request));
451441
}
452442
}
453443
}

src/test/java/com/amazonaws/encryptionsdk/kms/AwsKmsCmkIdTest.java

-1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,5 @@ void testToString() {
5858
void testEquals() {
5959
assertEquals(AwsKmsCmkId.fromString(VALID_ARN), AwsKmsCmkId.fromString(VALID_ARN));
6060
assertNotEquals(AwsKmsCmkId.fromString(VALID_ALIAS), AwsKmsCmkId.fromString(VALID_ALIAS_ARN));
61-
6261
}
6362
}

0 commit comments

Comments
 (0)