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 7dbd39bb842 Add `create mask rule` DistSQL unit test (#23219)
7dbd39bb842 is described below
commit 7dbd39bb8427ffb17a1b115a72c6d487a9df317f
Author: Zichao <[email protected]>
AuthorDate: Sun Jan 1 05:43:23 2023 +1300
Add `create mask rule` DistSQL unit test (#23219)
---
.../update/CreateMaskRuleStatementUpdater.java | 3 +-
.../update/CreateMaskRuleStatementUpdaterTest.java | 103 +++++++++++++++++++++
2 files changed, 104 insertions(+), 2 deletions(-)
diff --git
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdater.java
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdater.java
index 006a298c88d..be026619197 100644
---
a/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdater.java
+++
b/features/mask/distsql/handler/src/main/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdater.java
@@ -20,7 +20,6 @@ package org.apache.shardingsphere.mask.distsql.handler.update;
import
org.apache.shardingsphere.distsql.handler.exception.algorithm.InvalidAlgorithmConfigurationException;
import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
import
org.apache.shardingsphere.distsql.handler.update.RuleDefinitionCreateUpdater;
-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.infra.util.spi.type.typed.TypedSPIRegistry;
@@ -73,7 +72,7 @@ public final class CreateMaskRuleStatementUpdater implements
RuleDefinitionCreat
}
@Override
- public RuleConfiguration buildToBeCreatedRuleConfiguration(final
MaskRuleConfiguration currentRuleConfig, final CreateMaskRuleStatement
sqlStatement) {
+ public MaskRuleConfiguration buildToBeCreatedRuleConfiguration(final
MaskRuleConfiguration currentRuleConfig, final CreateMaskRuleStatement
sqlStatement) {
return MaskRuleStatementConverter.convert(sqlStatement.getRules());
}
diff --git
a/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java
new file mode 100644
index 00000000000..d079519e2e5
--- /dev/null
+++
b/features/mask/distsql/handler/src/test/java/org/apache/shardingsphere/mask/distsql/handler/update/CreateMaskRuleStatementUpdaterTest.java
@@ -0,0 +1,103 @@
+/*
+ * 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.algorithm.InvalidAlgorithmConfigurationException;
+import
org.apache.shardingsphere.distsql.handler.exception.rule.DuplicateRuleException;
+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.CreateMaskRuleStatement;
+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 CreateMaskRuleStatementUpdaterTest {
+
+ @Mock(answer = Answers.RETURNS_DEEP_STUBS)
+ private ShardingSphereDatabase database;
+
+ private final CreateMaskRuleStatementUpdater updater = new
CreateMaskRuleStatementUpdater();
+
+ @Test(expected = DuplicateRuleException.class)
+ public void assertCheckSQLStatementWithDuplicateMaskRule() {
+ updater.checkSQLStatement(database,
createDuplicatedSQLStatement(false, "MD5"), getCurrentRuleConfig());
+ }
+
+ @Test(expected = InvalidAlgorithmConfigurationException.class)
+ public void assertCheckSQLStatementWithInvalidAlgorithm() {
+ updater.checkSQLStatement(database, createSQLStatement(false,
"INVALID_TYPE"), null);
+ }
+
+ @Test
+ public void assertCreateMaskRule() {
+ MaskRuleConfiguration currentRuleConfig = getCurrentRuleConfig();
+ CreateMaskRuleStatement sqlStatement = createSQLStatement(false,
"MD5");
+ updater.checkSQLStatement(database, sqlStatement, currentRuleConfig);
+ MaskRuleConfiguration toBeCreatedRuleConfig =
updater.buildToBeCreatedRuleConfiguration(currentRuleConfig, sqlStatement);
+ updater.updateCurrentRuleConfiguration(currentRuleConfig,
toBeCreatedRuleConfig);
+ assertThat(currentRuleConfig.getTables().size(), is(4));
+
assertTrue(currentRuleConfig.getMaskAlgorithms().containsKey("t_mask_1_user_id_md5"));
+
assertTrue(currentRuleConfig.getMaskAlgorithms().containsKey("t_order_1_order_id_md5"));
+ }
+
+ private CreateMaskRuleStatement createSQLStatement(final boolean
ifNotExists, final String algorithmType) {
+ MaskColumnSegment tMaskColumnSegment = new
MaskColumnSegment("user_id", new AlgorithmSegment(algorithmType, new
Properties()));
+ MaskColumnSegment tOrderColumnSegment = new
MaskColumnSegment("order_id", new AlgorithmSegment(algorithmType, new
Properties()));
+ MaskRuleSegment tMaskRuleSegment = new MaskRuleSegment("t_mask_1",
Collections.singleton(tMaskColumnSegment));
+ MaskRuleSegment tOrderRuleSegment = new MaskRuleSegment("t_order_1",
Collections.singleton(tOrderColumnSegment));
+ Collection<MaskRuleSegment> rules = new LinkedList<>();
+ rules.add(tMaskRuleSegment);
+ rules.add(tOrderRuleSegment);
+ return new CreateMaskRuleStatement(ifNotExists, rules);
+ }
+
+ private CreateMaskRuleStatement createDuplicatedSQLStatement(final boolean
ifNotExists, final String algorithmType) {
+ MaskColumnSegment tMaskColumnSegment = new
MaskColumnSegment("user_id", new AlgorithmSegment(algorithmType, new
Properties()));
+ MaskColumnSegment tOrderColumnSegment = new
MaskColumnSegment("order_id", new AlgorithmSegment(algorithmType, new
Properties()));
+ MaskRuleSegment tMaskRuleSegment = new MaskRuleSegment("t_mask",
Collections.singleton(tMaskColumnSegment));
+ MaskRuleSegment tOrderRuleSegment = new MaskRuleSegment("t_order",
Collections.singleton(tOrderColumnSegment));
+ Collection<MaskRuleSegment> rules = new LinkedList<>();
+ rules.add(tMaskRuleSegment);
+ rules.add(tOrderRuleSegment);
+ return new CreateMaskRuleStatement(ifNotExists, rules);
+ }
+
+ private MaskRuleConfiguration getCurrentRuleConfig() {
+ Collection<MaskTableRuleConfiguration> rules = new LinkedList<>();
+ rules.add(new MaskTableRuleConfiguration("t_mask",
Collections.emptyList()));
+ rules.add(new MaskTableRuleConfiguration("t_order",
Collections.emptyList()));
+ return new MaskRuleConfiguration(rules, new HashMap<>());
+ }
+}