This is an automated email from the ASF dual-hosted git repository.

jianglongtao 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 6a6466bfb2e Add `alter mask rule` DistSQL unit test (#23231)
6a6466bfb2e is described below

commit 6a6466bfb2e2fe8f4aa363e9b93ef08a79cae7fd
Author: Zichao <[email protected]>
AuthorDate: Sun Jan 1 05:44:37 2023 +1300

    Add `alter mask rule` DistSQL unit test (#23231)
---
 .../update/AlterMaskRuleStatementUpdater.java      |   3 +-
 .../update/AlterMaskRuleStatementUpdaterTest.java  | 100 +++++++++++++++++++++
 2 files changed, 101 insertions(+), 2 deletions(-)

diff --git 
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
index bfead9a594d..901b755f98f 100644
--- 
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
+++ 
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdater.java
@@ -19,7 +19,6 @@ package org.apache.shardingsphere.mask.distsql.handler.update;
 
 import 
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
 import 
org.apache.shardingsphere.distsql.handler.update.RuleDefinitionAlterUpdater;
-import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
 import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
 import 
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
 import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
@@ -59,7 +58,7 @@ public final class AlterMaskRuleStatementUpdater implements 
RuleDefinitionAlterU
     }
     
     @Override
-    public RuleConfiguration buildToBeAlteredRuleConfiguration(final 
AlterMaskRuleStatement sqlStatement) {
+    public MaskRuleConfiguration buildToBeAlteredRuleConfiguration(final 
AlterMaskRuleStatement sqlStatement) {
         return MaskRuleStatementConverter.convert(sqlStatement.getRules());
     }
     
diff --git 
a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java
 
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java
new file mode 100644
index 00000000000..5cb05399bd3
--- /dev/null
+++ 
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/AlterMaskRuleStatementUpdaterTest.java
@@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shardingsphere.mask.distsql.handler.update;
+
+import 
org.apache.shardingsphere.distsql.handler.exception.rule.MissingRequiredRuleException;
+import org.apache.shardingsphere.distsql.parser.segment.AlgorithmSegment;
+import 
org.apache.shardingsphere.infra.metadata.database.ShardingSphereDatabase;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import 
org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.distsql.parser.segment.MaskColumnSegment;
+import org.apache.shardingsphere.mask.distsql.parser.segment.MaskRuleSegment;
+import 
org.apache.shardingsphere.mask.distsql.parser.statement.AlterMaskRuleStatement;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Answers;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.LinkedList;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(MockitoJUnitRunner.class)
+public final class AlterMaskRuleStatementUpdaterTest {
+    
+    @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+    private ShardingSphereDatabase database;
+    
+    private final AlterMaskRuleStatementUpdater updater = new 
AlterMaskRuleStatementUpdater();
+    
+    @Test(expected = MissingRequiredRuleException.class)
+    public void assertCheckSQLStatementWithoutCurrentRule() {
+        updater.checkSQLStatement(database, createSQLStatement("MD5"), null);
+    }
+    
+    @Test(expected = MissingRequiredRuleException.class)
+    public void assertCheckSQLStatementWithoutToBeAlteredRules() {
+        updater.checkSQLStatement(database, createSQLStatement("MD5"), new 
MaskRuleConfiguration(Collections.emptyList(), Collections.emptyMap()));
+    }
+    
+    @Test(expected = MissingRequiredRuleException.class)
+    public void assertCheckSQLStatementWithoutToBeAlteredAlgorithm() {
+        updater.checkSQLStatement(database, 
createSQLStatement("INVALID_TYPE"), createCurrentRuleConfig());
+    }
+    
+    @Test(expected = MissingRequiredRuleException.class)
+    public void assertCheckSQLStatementWithIncompleteDataType() {
+        MaskColumnSegment columnSegment = new MaskColumnSegment("user_id", new 
AlgorithmSegment("test", new Properties()));
+        MaskRuleSegment ruleSegment = new MaskRuleSegment("t_mask", 
Collections.singleton(columnSegment));
+        AlterMaskRuleStatement statement = new 
AlterMaskRuleStatement(Collections.singleton(ruleSegment));
+        updater.checkSQLStatement(database, statement, 
createCurrentRuleConfig());
+    }
+    
+    @Test
+    public void assertUpdate() {
+        MaskRuleConfiguration currentRuleConfig = createCurrentRuleConfig();
+        MaskColumnSegment columnSegment = new MaskColumnSegment("order_id",
+                new AlgorithmSegment("MD5", new Properties()));
+        MaskRuleSegment ruleSegment = new MaskRuleSegment("t_order", 
Collections.singleton(columnSegment));
+        AlterMaskRuleStatement sqlStatement = new 
AlterMaskRuleStatement(Collections.singleton(ruleSegment));
+        MaskRuleConfiguration toBeAlteredRuleConfig = 
updater.buildToBeAlteredRuleConfiguration(sqlStatement);
+        updater.updateCurrentRuleConfiguration(currentRuleConfig, 
toBeAlteredRuleConfig);
+        assertThat(currentRuleConfig.getMaskAlgorithms().size(), is(1));
+        
assertTrue(currentRuleConfig.getMaskAlgorithms().containsKey("t_order_order_id_md5"));
+    }
+    
+    private AlterMaskRuleStatement createSQLStatement(final String 
algorithmName) {
+        MaskColumnSegment columnSegment = new MaskColumnSegment("user_id",
+                new AlgorithmSegment(algorithmName, new Properties()));
+        MaskRuleSegment ruleSegment = new MaskRuleSegment("t_mask", 
Collections.singleton(columnSegment));
+        return new AlterMaskRuleStatement(Collections.singleton(ruleSegment));
+    }
+    
+    private MaskRuleConfiguration createCurrentRuleConfig() {
+        Collection<MaskTableRuleConfiguration> tableRuleConfigs = new 
LinkedList<>();
+        tableRuleConfigs.add(new MaskTableRuleConfiguration("t_order", 
Collections.emptyList()));
+        return new MaskRuleConfiguration(tableRuleConfigs, new HashMap<>());
+    }
+}

Reply via email to