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 263a80508c5 Refactor EncryptRuleConfigurationCheckerTest (#33638)
263a80508c5 is described below
commit 263a80508c598141a6a0cb65f4d0b0a6941a457a
Author: Liang Zhang <[email protected]>
AuthorDate: Wed Nov 13 15:41:26 2024 +0800
Refactor EncryptRuleConfigurationCheckerTest (#33638)
* Refactor EncryptRuleConfigurationCheckerTest
* Refactor EncryptRuleConfigurationCheckerTest
---
.../EncryptRuleConfigurationCheckerTest.java | 88 +++++++++++-----------
1 file changed, 42 insertions(+), 46 deletions(-)
diff --git
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
index 533d668d9f9..1b5d97f2bbc 100644
---
a/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
+++
b/features/encrypt/core/src/test/java/org/apache/shardingsphere/encrypt/checker/config/EncryptRuleConfigurationCheckerTest.java
@@ -25,85 +25,81 @@ import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfigurat
import
org.apache.shardingsphere.infra.algorithm.core.exception.UnregisteredAlgorithmException;
import
org.apache.shardingsphere.infra.config.rule.checker.RuleConfigurationChecker;
import org.apache.shardingsphere.infra.spi.type.ordered.OrderedSPILoader;
+import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.Collection;
import java.util.Collections;
+import java.util.Map;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.when;
class EncryptRuleConfigurationCheckerTest {
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("rawtypes")
+ private RuleConfigurationChecker checker;
+
+ @BeforeEach
+ void setUp() {
+ checker =
OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class,
Collections.singleton(EncryptRuleConfiguration.class)).get(EncryptRuleConfiguration.class);
+ }
+
+ @SuppressWarnings("unchecked")
@Test
- void assertCheckWhenConfigValidConfiguration() {
- EncryptRuleConfiguration ruleConfig = createValidConfiguration();
- RuleConfigurationChecker checker =
OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class,
Collections.singleton(ruleConfig.getClass())).get(ruleConfig.getClass());
- checker.check("test", ruleConfig, Collections.emptyMap(),
Collections.emptyList());
+ void assertCheckSuccess() {
+ EncryptRuleConfiguration ruleConfig = createValidRuleConfiguration();
+ checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList());
}
- private EncryptRuleConfiguration createValidConfiguration() {
- EncryptRuleConfiguration result = mock(EncryptRuleConfiguration.class);
-
when(result.getEncryptors()).thenReturn(Collections.singletonMap("aes_encryptor",
new AlgorithmConfiguration("MD5", new Properties())));
+ private EncryptRuleConfiguration createValidRuleConfiguration() {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
- Collection<EncryptColumnRuleConfiguration> columns =
Collections.singleton(columnRuleConfig);
- when(result.getTables()).thenReturn(Collections.singleton(new
EncryptTableRuleConfiguration("t_encrypt", columns)));
- return result;
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.singletonMap("aes_encryptor", new AlgorithmConfiguration("MD5", new
Properties()));
+ return new EncryptRuleConfiguration(tables, encryptors);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("unchecked")
@Test
- void assertCheckWhenConfigInvalidCipherColumn() {
- EncryptRuleConfiguration config =
createInvalidCipherColumnConfiguration();
- RuleConfigurationChecker checker =
OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class,
Collections.singleton(config.getClass())).get(config.getClass());
- assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("test", config, Collections.emptyMap(), Collections.emptyList()));
+ void assertCheckWithInvalidCipherColumn() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithInvalidCipherColumn();
+ assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
}
- private EncryptRuleConfiguration createInvalidCipherColumnConfiguration() {
- EncryptRuleConfiguration result = mock(EncryptRuleConfiguration.class);
- when(result.getEncryptors()).thenReturn(Collections.emptyMap());
+ private EncryptRuleConfiguration
createRuleConfigurationWithInvalidCipherColumn() {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
- Collection<EncryptColumnRuleConfiguration> columns =
Collections.singleton(columnRuleConfig);
- when(result.getTables()).thenReturn(Collections.singleton(new
EncryptTableRuleConfiguration("t_encrypt", columns)));
- return result;
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ return new EncryptRuleConfiguration(tables, encryptors);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("unchecked")
@Test
- void assertCheckWhenConfigInvalidAssistColumn() {
- EncryptRuleConfiguration config =
createInvalidAssistColumnConfiguration();
- RuleConfigurationChecker checker =
OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class,
Collections.singleton(config.getClass())).get(config.getClass());
- assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("test", config, Collections.emptyMap(), Collections.emptyList()));
+ void assertCheckWithInvalidAssistColumn() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithInvalidAssistColumn();
+ assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
}
- private EncryptRuleConfiguration createInvalidAssistColumnConfiguration() {
- EncryptRuleConfiguration result = mock(EncryptRuleConfiguration.class);
- when(result.getEncryptors()).thenReturn(Collections.emptyMap());
+ private EncryptRuleConfiguration
createRuleConfigurationWithInvalidAssistColumn() {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
- Collection<EncryptColumnRuleConfiguration> columns =
Collections.singleton(columnRuleConfig);
columnRuleConfig.setAssistedQuery(new
EncryptColumnItemRuleConfiguration("user_assisted", "aes_assisted_encryptor"));
- when(result.getTables()).thenReturn(Collections.singleton(new
EncryptTableRuleConfiguration("t_encrypt", columns)));
- return result;
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ return new EncryptRuleConfiguration(tables, encryptors);
}
- @SuppressWarnings({"rawtypes", "unchecked"})
+ @SuppressWarnings("unchecked")
@Test
- void assertCheckWhenConfigInvalidLikeColumn() {
- EncryptRuleConfiguration config =
createInvalidLikeColumnConfiguration();
- RuleConfigurationChecker checker =
OrderedSPILoader.getServicesByClass(RuleConfigurationChecker.class,
Collections.singleton(config.getClass())).get(config.getClass());
- assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("test", config, Collections.emptyMap(), Collections.emptyList()));
+ void assertCheckWithInvalidLikeColumn() {
+ EncryptRuleConfiguration ruleConfig =
createRuleConfigurationWithInvalidLikeColumn();
+ assertThrows(UnregisteredAlgorithmException.class, () ->
checker.check("foo_db", ruleConfig, Collections.emptyMap(),
Collections.emptyList()));
}
- private EncryptRuleConfiguration createInvalidLikeColumnConfiguration() {
- EncryptRuleConfiguration result = mock(EncryptRuleConfiguration.class);
- when(result.getEncryptors()).thenReturn(Collections.emptyMap());
+ private EncryptRuleConfiguration
createRuleConfigurationWithInvalidLikeColumn() {
EncryptColumnRuleConfiguration columnRuleConfig = new
EncryptColumnRuleConfiguration("user_id", new
EncryptColumnItemRuleConfiguration("user_cipher", "aes_encryptor"));
- Collection<EncryptColumnRuleConfiguration> columns =
Collections.singleton(columnRuleConfig);
columnRuleConfig.setLikeQuery(new
EncryptColumnItemRuleConfiguration("user_like", "like_cn_encryptor"));
- when(result.getTables()).thenReturn(Collections.singleton(new
EncryptTableRuleConfiguration("t_encrypt", columns)));
- return result;
+ Collection<EncryptTableRuleConfiguration> tables =
Collections.singleton(new EncryptTableRuleConfiguration("t_encrypt",
Collections.singleton(columnRuleConfig)));
+ Map<String, AlgorithmConfiguration> encryptors =
Collections.emptyMap();
+ return new EncryptRuleConfiguration(tables, encryptors);
}
}