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 cfb87d29f4d Refactor CreateEncryptRuleExecutorTest (#38365)
cfb87d29f4d is described below
commit cfb87d29f4dc1cb105a96674860d411972e27903
Author: Liang Zhang <[email protected]>
AuthorDate: Sat Mar 7 12:04:50 2026 +0800
Refactor CreateEncryptRuleExecutorTest (#38365)
* Refactor CreateEncryptRuleExecutorTest
* Refactor CreateEncryptRuleExecutorTest
---
.../update/CreateEncryptRuleExecutorTest.java | 47 +++++++---------------
1 file changed, 15 insertions(+), 32 deletions(-)
diff --git
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java
index 2d65dd09135..0c579379693 100644
---
a/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java
+++
b/features/encrypt/distsql/handler/src/test/java/org/apache/shardingsphere/encrypt/distsql/handler/update/CreateEncryptRuleExecutorTest.java
@@ -20,7 +20,6 @@ package
org.apache.shardingsphere.encrypt.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.engine.update.DistSQLUpdateExecuteEngine;
import org.apache.shardingsphere.distsql.segment.AlgorithmSegment;
import org.apache.shardingsphere.encrypt.config.EncryptRuleConfiguration;
-import
org.apache.shardingsphere.encrypt.config.rule.EncryptTableRuleConfiguration;
import
org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnItemSegment;
import org.apache.shardingsphere.encrypt.distsql.segment.EncryptColumnSegment;
import org.apache.shardingsphere.encrypt.distsql.segment.EncryptRuleSegment;
@@ -41,7 +40,6 @@ import org.mockito.ArgumentMatchers;
import java.sql.SQLException;
import java.util.Arrays;
-import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Properties;
@@ -50,6 +48,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
+import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -60,30 +59,25 @@ class CreateEncryptRuleExecutorTest {
EncryptRule rule = mock(EncryptRule.class);
when(rule.getAllTableNames()).thenReturn(Arrays.asList("t_user",
"t_order"));
assertThrows(DuplicateRuleException.class,
- () -> new DistSQLUpdateExecuteEngine(createSQLStatement(false,
"MD5"), "foo_db", mockContextManager(rule), null).executeUpdate());
+ () -> new
DistSQLUpdateExecuteEngine(createSQLStatement("MD5"), "foo_db",
mockContextManager(rule), null).executeUpdate());
}
@Test
void assertExecuteUpdateWithoutToBeCreatedEncryptors() {
- EncryptRule rule = mock(EncryptRule.class);
assertThrows(ServiceProviderNotFoundException.class,
- () -> new DistSQLUpdateExecuteEngine(createSQLStatement(false,
"INVALID_TYPE"), "foo_db", mockContextManager(rule), null).executeUpdate());
+ () -> new
DistSQLUpdateExecuteEngine(createSQLStatement("INVALID_TYPE"), "foo_db",
mockContextManager(mock(EncryptRule.class)), null).executeUpdate());
}
@Test
void assertExecuteUpdateWithConflictedColumnNames() {
- EncryptRule rule = mock(EncryptRule.class);
-
when(rule.getConfiguration()).thenReturn(getCurrentRuleConfiguration());
assertThrows(InvalidRuleConfigurationException.class,
- () -> new
DistSQLUpdateExecuteEngine(createConflictColumnNameSQLStatement(), "foo_db",
mockContextManager(rule), null).executeUpdate());
+ () -> new
DistSQLUpdateExecuteEngine(createConflictColumnNameSQLStatement(), "foo_db",
mockContextManager(mock(EncryptRule.class)), null).executeUpdate());
}
@Test
void assertExecuteUpdateAESEncryptRuleWithPropertiesNotExists() {
- CreateEncryptRuleStatement sqlStatement =
createWrongAESEncryptorSQLStatement();
- EncryptRule rule = mock(EncryptRule.class);
-
when(rule.getConfiguration()).thenReturn(getCurrentRuleConfiguration());
- assertThrows(AlgorithmInitializationException.class, () -> new
DistSQLUpdateExecuteEngine(sqlStatement, "foo_db", mockContextManager(rule),
null).executeUpdate());
+ assertThrows(AlgorithmInitializationException.class,
+ () -> new
DistSQLUpdateExecuteEngine(createWrongAESEncryptorSQLStatement(), "foo_db",
mockContextManager(mock(EncryptRule.class)), null).executeUpdate());
}
private CreateEncryptRuleStatement createWrongAESEncryptorSQLStatement() {
@@ -103,24 +97,23 @@ class CreateEncryptRuleExecutorTest {
@Test
void assertExecuteUpdateWithIfNotExists() throws SQLException {
EncryptRule rule = mock(EncryptRule.class);
- CreateEncryptRuleStatement sqlStatement =
createAESEncryptRuleSQLStatement(true);
+ CreateEncryptRuleStatement sqlStatement =
createAESEncryptRuleSQLStatement();
ContextManager contextManager = mockContextManager(rule);
new DistSQLUpdateExecuteEngine(sqlStatement, "foo_db", contextManager,
null).executeUpdate();
MetaDataManagerPersistService metaDataManagerPersistService =
contextManager.getPersistServiceFacade().getModeFacade().getMetaDataManagerService();
metaDataManagerPersistService.alterRuleConfiguration(any(),
ArgumentMatchers.argThat(this::assertIfNotExistsRuleConfiguration));
}
- private CreateEncryptRuleStatement createAESEncryptRuleSQLStatement(final
boolean ifNotExists) {
+ private CreateEncryptRuleStatement createAESEncryptRuleSQLStatement() {
EncryptColumnSegment encryptColumnSegment = new
EncryptColumnSegment("user_id",
new EncryptColumnItemSegment("user_cipher", new
AlgorithmSegment("AES", PropertiesBuilder.build(new Property("aes-key-value",
"abc"), new Property("digest-algorithm-name", "SHA-1")))),
new EncryptColumnItemSegment("assisted_column", null),
new EncryptColumnItemSegment("like_column", null));
- Collection<EncryptRuleSegment> rules = new LinkedList<>();
- rules.add(new EncryptRuleSegment("t_user",
Collections.singleton(encryptColumnSegment)));
- return new CreateEncryptRuleStatement(ifNotExists, rules);
+ EncryptRuleSegment encryptRuleSegment = new
EncryptRuleSegment("t_user", Collections.singleton(encryptColumnSegment));
+ return new CreateEncryptRuleStatement(true, new
LinkedList<>(Collections.singleton(encryptRuleSegment)));
}
- private CreateEncryptRuleStatement createSQLStatement(final boolean
ifNotExists, final String encryptorName) {
+ private CreateEncryptRuleStatement createSQLStatement(final String
encryptorName) {
EncryptColumnSegment tUserColumnSegment = new
EncryptColumnSegment("user_id",
new EncryptColumnItemSegment("user_cipher", new
AlgorithmSegment(encryptorName, new Properties())),
new EncryptColumnItemSegment("assisted_column", new
AlgorithmSegment(encryptorName, new Properties())),
@@ -129,12 +122,9 @@ class CreateEncryptRuleExecutorTest {
new EncryptColumnItemSegment("order_cipher", new
AlgorithmSegment(encryptorName, new Properties())),
new EncryptColumnItemSegment("assisted_column", new
AlgorithmSegment(encryptorName, new Properties())),
new EncryptColumnItemSegment("like_column", new
AlgorithmSegment(encryptorName, new Properties())));
- EncryptRuleSegment tUserRuleSegment = new EncryptRuleSegment("t_user",
Collections.singleton(tUserColumnSegment));
- EncryptRuleSegment tOrderRuleSegment = new
EncryptRuleSegment("t_order", Collections.singleton(tOrderColumnSegment));
- Collection<EncryptRuleSegment> rules = new LinkedList<>();
- rules.add(tUserRuleSegment);
- rules.add(tOrderRuleSegment);
- return new CreateEncryptRuleStatement(ifNotExists, rules);
+ EncryptRuleSegment userRuleSegment = new EncryptRuleSegment("t_user",
Collections.singleton(tUserColumnSegment));
+ EncryptRuleSegment orderRuleSegment = new
EncryptRuleSegment("t_order", Collections.singleton(tOrderColumnSegment));
+ return new CreateEncryptRuleStatement(false,
Arrays.asList(userRuleSegment, orderRuleSegment));
}
private CreateEncryptRuleStatement createConflictColumnNameSQLStatement() {
@@ -146,13 +136,6 @@ class CreateEncryptRuleExecutorTest {
return new CreateEncryptRuleStatement(false,
Collections.singleton(ruleSegment));
}
- private EncryptRuleConfiguration getCurrentRuleConfiguration() {
- Collection<EncryptTableRuleConfiguration> rules = new LinkedList<>();
- rules.add(new EncryptTableRuleConfiguration("t_user",
Collections.emptyList()));
- rules.add(new EncryptTableRuleConfiguration("t_order",
Collections.emptyList()));
- return new EncryptRuleConfiguration(rules, Collections.emptyMap());
- }
-
private boolean assertIfNotExistsRuleConfiguration(final
EncryptRuleConfiguration actual) {
assertFalse(actual.getTables().isEmpty());
assertFalse(actual.getEncryptors().isEmpty());
@@ -161,7 +144,7 @@ class CreateEncryptRuleExecutorTest {
private ContextManager mockContextManager(final EncryptRule rule) {
ShardingSphereDatabase database = mock(ShardingSphereDatabase.class,
RETURNS_DEEP_STUBS);
- when(database.getName()).thenReturn("foo_db");
+ lenient().when(database.getName()).thenReturn("foo_db");
when(database.getRuleMetaData()).thenReturn(new
RuleMetaData(Collections.singleton(rule)));
ContextManager result = mock(ContextManager.class, RETURNS_DEEP_STUBS);
when(result.getDatabase("foo_db")).thenReturn(database);