This is an automated email from the ASF dual-hosted git repository.
zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new 8458cdc9d76 Add more test cases on AESEncryptAlgorithm (#33629)
8458cdc9d76 is described below
commit 8458cdc9d767accf7ea394f52ab2a44235d8bf53
Author: Liang Zhang <[email protected]>
AuthorDate: Tue Nov 12 16:09:47 2024 +0800
Add more test cases on AESEncryptAlgorithm (#33629)
---
.../standard/AESEncryptAlgorithmTest.java | 55 +++++-----------------
.../aes/AESCryptographicAlgorithmTest.java | 33 +++----------
2 files changed, 17 insertions(+), 71 deletions(-)
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java
index f3e941d015f..1b16c3e0b2c 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/algorithm/standard/AESEncryptAlgorithmTest.java
@@ -17,63 +17,27 @@
package org.apache.shardingsphere.encrypt.algorithm.standard;
-import org.apache.commons.codec.digest.DigestUtils;
import org.apache.shardingsphere.encrypt.spi.EncryptAlgorithm;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
import
org.apache.shardingsphere.infra.algorithm.core.context.AlgorithmSQLContext;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Answers;
-import org.mockito.MockedStatic;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.times;
class AESEncryptAlgorithmTest {
- private EncryptAlgorithm encryptAlgorithm;
-
- @BeforeEach
- void setUp() {
- encryptAlgorithm = TypedSPILoader.getService(EncryptAlgorithm.class,
"AES", PropertiesBuilder.build(new Property("aes-key-value", "test"), new
Property("digest-algorithm-name", "SHA-1")));
- }
-
- @Test
- void assertDigestAlgorithm() {
- MockedStatic<DigestUtils> digestUtilsMockedStatic =
mockStatic(DigestUtils.class, Answers.CALLS_REAL_METHODS);
- TypedSPILoader.getService(EncryptAlgorithm.class, "AES",
PropertiesBuilder.build(new Property("aes-key-value", "test"), new
Property("digest-algorithm-name", "SHA-1")));
- digestUtilsMockedStatic.verify(() -> DigestUtils.getDigest("SHA-1"),
times(1));
- digestUtilsMockedStatic.close();
- }
-
- @Test
- void assertCreateNewInstanceWithoutAESKey() {
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(EncryptAlgorithm.class, "AES"));
- }
-
- @Test
- void assertCreateNewInstanceWithEmptyAESKey() {
- assertThrows(AlgorithmInitializationException.class, () ->
encryptAlgorithm.init(PropertiesBuilder.build(new Property("aes-key-value",
""))));
- }
-
- @Test
- void assertCreateNewInstanceWithEmptyDigestAlgorithm() {
- assertThrows(AlgorithmInitializationException.class, () ->
encryptAlgorithm.init(PropertiesBuilder.build(new Property("aes-key-value",
"123456abc"),
- new Property("digest-algorithm-name", ""))));
- }
+ private final EncryptAlgorithm encryptAlgorithm =
TypedSPILoader.getService(
+ EncryptAlgorithm.class, "AES", PropertiesBuilder.build(new
Property("aes-key-value", "test"), new Property("digest-algorithm-name",
"SHA-1")));
@Test
void assertEncrypt() {
- Object actual = encryptAlgorithm.encrypt("test",
mock(AlgorithmSQLContext.class));
- assertThat(actual, is("dSpPiyENQGDUXMKFMJPGWA=="));
+ assertThat(encryptAlgorithm.encrypt("test",
mock(AlgorithmSQLContext.class)), is("dSpPiyENQGDUXMKFMJPGWA=="));
}
@Test
@@ -83,12 +47,15 @@ class AESEncryptAlgorithmTest {
@Test
void assertDecrypt() {
- Object actual = encryptAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA==",
mock(AlgorithmSQLContext.class));
- assertThat(actual.toString(), is("test"));
+ assertThat(encryptAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA==",
mock(AlgorithmSQLContext.class)), is("test"));
}
@Test
- void assertDecryptNullValue() {
- assertNull(encryptAlgorithm.decrypt(null,
mock(AlgorithmSQLContext.class)));
+ void assertToConfiguration() {
+ AlgorithmConfiguration actual = encryptAlgorithm.toConfiguration();
+ assertThat(actual.getType(), is("AES"));
+ assertThat(actual.getProps().size(), is(2));
+ assertThat(actual.getProps().getProperty("aes-key-value"), is("test"));
+ assertThat(actual.getProps().getProperty("digest-algorithm-name"),
is("SHA-1"));
}
}
diff --git
a/infra/algorithm/type/cryptographic/type/aes/src/test/java/org/apache/shardingsphere/infra/algorithm/cryptographic/aes/AESCryptographicAlgorithmTest.java
b/infra/algorithm/type/cryptographic/type/aes/src/test/java/org/apache/shardingsphere/infra/algorithm/cryptographic/aes/AESCryptographicAlgorithmTest.java
index 3cf523cd3bf..2881cebc5ca 100644
---
a/infra/algorithm/type/cryptographic/type/aes/src/test/java/org/apache/shardingsphere/infra/algorithm/cryptographic/aes/AESCryptographicAlgorithmTest.java
+++
b/infra/algorithm/type/cryptographic/type/aes/src/test/java/org/apache/shardingsphere/infra/algorithm/cryptographic/aes/AESCryptographicAlgorithmTest.java
@@ -17,41 +17,22 @@
package org.apache.shardingsphere.infra.algorithm.cryptographic.aes;
-import org.apache.commons.codec.digest.DigestUtils;
import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
import
org.apache.shardingsphere.infra.algorithm.cryptographic.core.CryptographicAlgorithm;
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
-import org.mockito.Answers;
-import org.mockito.MockedStatic;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.mockStatic;
-import static org.mockito.Mockito.times;
class AESCryptographicAlgorithmTest {
- private CryptographicAlgorithm cryptographicAlgorithm;
-
- @BeforeEach
- void setUp() {
- cryptographicAlgorithm =
- TypedSPILoader.getService(CryptographicAlgorithm.class, "AES",
PropertiesBuilder.build(new Property("aes-key-value", "test"), new
Property("digest-algorithm-name", "SHA-1")));
- }
-
- @Test
- void assertDigestAlgorithm() {
- MockedStatic<DigestUtils> digestUtilsMockedStatic =
mockStatic(DigestUtils.class, Answers.CALLS_REAL_METHODS);
- TypedSPILoader.getService(CryptographicAlgorithm.class, "AES",
PropertiesBuilder.build(new Property("aes-key-value", "test"), new
Property("digest-algorithm-name", "SHA-1")));
- digestUtilsMockedStatic.verify(() -> DigestUtils.getDigest("SHA-1"),
times(1));
- digestUtilsMockedStatic.close();
- }
+ private final CryptographicAlgorithm cryptographicAlgorithm =
TypedSPILoader.getService(
+ CryptographicAlgorithm.class, "AES", PropertiesBuilder.build(new
Property("aes-key-value", "test"), new Property("digest-algorithm-name",
"SHA-1")));
@Test
void assertCreateNewInstanceWithoutAESKey() {
@@ -65,14 +46,13 @@ class AESCryptographicAlgorithmTest {
@Test
void assertCreateNewInstanceWithEmptyDigestAlgorithm() {
- assertThrows(AlgorithmInitializationException.class, () ->
cryptographicAlgorithm.init(PropertiesBuilder.build(new
Property("aes-key-value", "123456abc"),
- new Property("digest-algorithm-name", ""))));
+ assertThrows(AlgorithmInitializationException.class, () ->
cryptographicAlgorithm.init(
+ PropertiesBuilder.build(new Property("aes-key-value",
"123456abc"), new Property("digest-algorithm-name", ""))));
}
@Test
void assertEncrypt() {
- Object actual = cryptographicAlgorithm.encrypt("test");
- assertThat(actual, is("dSpPiyENQGDUXMKFMJPGWA=="));
+ assertThat(cryptographicAlgorithm.encrypt("test"),
is("dSpPiyENQGDUXMKFMJPGWA=="));
}
@Test
@@ -82,8 +62,7 @@ class AESCryptographicAlgorithmTest {
@Test
void assertDecrypt() {
- Object actual =
cryptographicAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA==");
- assertThat(actual.toString(), is("test"));
+ assertThat(cryptographicAlgorithm.decrypt("dSpPiyENQGDUXMKFMJPGWA=="),
is("test"));
}
@Test