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 d0f3747f2cd Add test cases on MaskRule (#30659)
d0f3747f2cd is described below
commit d0f3747f2cdb99dceefc482eebaa80f1f3f52cfb
Author: risk <[email protected]>
AuthorDate: Wed Mar 27 23:39:52 2024 +0800
Add test cases on MaskRule (#30659)
* issue-30583 create
* Add test cases on MaskRule
* fix spotless problem
---
.../shardingsphere/mask/rule/MaskRuleTest.java | 74 ++++++++++++++++++++++
1 file changed, 74 insertions(+)
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java
new file mode 100644
index 00000000000..1fb44a11782
--- /dev/null
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/rule/MaskRuleTest.java
@@ -0,0 +1,74 @@
+/*
+ * 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.rule;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import
org.apache.shardingsphere.infra.algorithm.core.config.AlgorithmConfiguration;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import
org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import
org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import java.util.Collections;
+import java.util.Optional;
+import java.util.Properties;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class MaskRuleTest {
+
+ private MaskRule maskRule;
+
+ private MaskRuleConfiguration mockConfiguration;
+
+ @BeforeEach
+ public void setUp() {
+ mockConfiguration = getRuleConfiguration();
+ maskRule = new MaskRule(mockConfiguration);
+ }
+
+ @Test
+ public void assertTableNameExists() {
+ String tableName = "t_mask";
+ Optional<MaskTable> result = maskRule.findMaskTable(tableName);
+ assertTrue(result.isPresent());
+ MaskTable maskTable = result.get();
+ Optional<MaskAlgorithm> algorithm = maskTable.findAlgorithm("user_id");
+ String value = (String) algorithm.get().mask("test");
+ String matchValue = DigestUtils.md5Hex("test");
+ assertEquals(value, matchValue);
+ }
+
+ @Test
+ public void assertTableNameNoExists() {
+ String tableName = "non_existent_table";
+ Optional<MaskTable> result = maskRule.findMaskTable(tableName);
+ assertFalse(result.isPresent());
+ }
+
+ private MaskRuleConfiguration getRuleConfiguration() {
+ MaskColumnRuleConfiguration maskColumnRuleConfig = new
MaskColumnRuleConfiguration("user_id", "t_mask_user_id_md5");
+ MaskTableRuleConfiguration maskTableRuleConfig = new
MaskTableRuleConfiguration("t_mask",
Collections.singleton(maskColumnRuleConfig));
+ AlgorithmConfiguration algorithmConfig = new
AlgorithmConfiguration("md5", new Properties());
+ return new
MaskRuleConfiguration(Collections.singleton(maskTableRuleConfig),
Collections.singletonMap("t_mask_user_id_md5", algorithmConfig));
+ }
+}