From 878a1077e0a826325ca8a89c3ecddddbd5e2128b Mon Sep 17 00:00:00 2001 From: Ryan Liang Date: Mon, 21 Aug 2023 19:10:27 -0700 Subject: [PATCH] Refactor the encryptiondecryptionutilstests Signed-off-by: Ryan Liang --- ...ava => EncryptionDecryptionUtilsTest.java} | 25 ++++++++----------- 1 file changed, 11 insertions(+), 14 deletions(-) rename src/test/java/org/opensearch/security/authtoken/jwt/{EncryptionDecryptionUtilTest.java => EncryptionDecryptionUtilsTest.java} (78%) diff --git a/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java similarity index 78% rename from src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java rename to src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java index 165116f757..34b9b3a100 100644 --- a/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilTest.java +++ b/src/test/java/org/opensearch/security/authtoken/jwt/EncryptionDecryptionUtilsTest.java @@ -15,7 +15,7 @@ import org.junit.Test; import java.util.Base64; -public class EncryptionDecryptionUtilTest { +public class EncryptionDecryptionUtilsTest { @Test public void testEncryptDecrypt() { @@ -36,12 +36,9 @@ public void testDecryptingWithWrongKey() { String encryptedString = EncryptionDecryptionUtil.encrypt(secret1, data); - try { - EncryptionDecryptionUtil.decrypt(secret2, encryptedString); - Assert.fail("Should have thrown an exception when decrypting with a wrong key"); - } catch (RuntimeException e) { - // Expected exception - } + RuntimeException ex = Assert.assertThrows(RuntimeException.class, () -> EncryptionDecryptionUtil.decrypt(secret2, encryptedString)); + + Assert.assertEquals("The cipher was unable to perform pass over data", ex.getMessage()); } @Test @@ -49,12 +46,12 @@ public void testDecryptingCorruptedData() { String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); String corruptedEncryptedString = "corruptedData"; - try { - EncryptionDecryptionUtil.decrypt(secret, corruptedEncryptedString); - Assert.fail("Should have thrown an exception when trying to decrypt corrupted data"); - } catch (RuntimeException e) { - // Expected exception - } + RuntimeException ex = Assert.assertThrows( + RuntimeException.class, + () -> EncryptionDecryptionUtil.decrypt(secret, corruptedEncryptedString) + ); + + Assert.assertEquals("Last unit does not have enough valid bits", ex.getMessage()); } @Test @@ -69,7 +66,7 @@ public void testEncryptDecryptEmptyString() { } @Test(expected = NullPointerException.class) - public void testEncryptDecryptNullValue() { + public void testEncryptNullValue() { String secret = Base64.getEncoder().encodeToString("mySecretKey12345".getBytes()); String data = null;