You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/12/14 08:56:21 UTC
[shardingsphere] branch master updated: add YamlMaskConfiguration and Swapper (#22825)
This is an automated email from the ASF dual-hosted git repository.
duanzhengqiang 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 ad4825d8010 add YamlMaskConfiguration and Swapper (#22825)
ad4825d8010 is described below
commit ad4825d8010c70e630f15e9695797793201fb59f
Author: zzzwjZhang <46...@users.noreply.github.com>
AuthorDate: Wed Dec 14 16:56:13 2022 +0800
add YamlMaskConfiguration and Swapper (#22825)
* add YamlMaskConfiguration and Swapper
* fix checkstyle
Co-authored-by: zhangweijie <zh...@xinshiyun.com>
---
.../yaml/config/YamlMaskRuleConfiguration.java | 45 +++++++++++
.../rule/YamlMaskColumnRuleConfiguration.java | 34 ++++++++
.../rule/YamlMaskTableRuleConfiguration.java | 37 +++++++++
.../swapper/YamlMaskRuleConfigurationSwapper.java | 90 ++++++++++++++++++++++
.../YamlMaskColumnRuleConfigurationSwapper.java | 41 ++++++++++
.../YamlMaskTableRuleConfigurationSwapper.java | 57 ++++++++++++++
...nfig.swapper.rule.YamlRuleConfigurationSwapper} | 2 +-
...rg.apache.shardingsphere.mask.spi.MaskAlgorithm | 3 +-
.../YamlMaskRuleConfigurationSwapperTest.java | 75 ++++++++++++++++++
...YamlMaskColumnRuleConfigurationSwapperTest.java | 49 ++++++++++++
.../YamlMaskTableRuleConfigurationSwapperTest.java | 75 ++++++++++++++++++
11 files changed, 506 insertions(+), 2 deletions(-)
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/YamlMaskRuleConfiguration.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/YamlMaskRuleConfiguration.java
new file mode 100644
index 00000000000..ac2f6fbebe6
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/YamlMaskRuleConfiguration.java
@@ -0,0 +1,45 @@
+/*
+ * 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.yaml.config;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.pojo.rule.YamlRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Mask rule configuration for YAML.
+ */
+@Getter
+@Setter
+public final class YamlMaskRuleConfiguration implements YamlRuleConfiguration {
+
+ private Map<String, YamlMaskTableRuleConfiguration> tables = new LinkedHashMap<>();
+
+ private Map<String, YamlAlgorithmConfiguration> maskAlgorithms = new LinkedHashMap<>();
+
+ @Override
+ public Class<MaskRuleConfiguration> getRuleConfigurationType() {
+ return MaskRuleConfiguration.class;
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskColumnRuleConfiguration.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskColumnRuleConfiguration.java
new file mode 100644
index 00000000000..1eb693f9ad2
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskColumnRuleConfiguration.java
@@ -0,0 +1,34 @@
+/*
+ * 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.yaml.config.rule;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
+
+/**
+ * Mask column rule configuration for YAML.
+ */
+@Getter
+@Setter
+public final class YamlMaskColumnRuleConfiguration implements YamlConfiguration {
+
+ private String logicColumn;
+
+ private String maskAlgorithm;
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskTableRuleConfiguration.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskTableRuleConfiguration.java
new file mode 100644
index 00000000000..12cd4f67eb2
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/config/rule/YamlMaskTableRuleConfiguration.java
@@ -0,0 +1,37 @@
+/*
+ * 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.yaml.config.rule;
+
+import lombok.Getter;
+import lombok.Setter;
+import org.apache.shardingsphere.infra.util.yaml.YamlConfiguration;
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+/**
+ * Mask table rule configuration for YAML.
+ */
+@Getter
+@Setter
+public final class YamlMaskTableRuleConfiguration implements YamlConfiguration {
+
+ private String name;
+
+ private Map<String, YamlMaskColumnRuleConfiguration> columns = new LinkedHashMap<>();
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapper.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..d260927a885
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapper.java
@@ -0,0 +1,90 @@
+/*
+ * 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.yaml.swapper;
+
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.swapper.algorithm.YamlAlgorithmConfigurationSwapper;
+import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.constant.MaskOrder;
+import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.swapper.rule.YamlMaskTableRuleConfigurationSwapper;
+
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * YAML mask rule configuration swapper.
+ */
+public final class YamlMaskRuleConfigurationSwapper implements YamlRuleConfigurationSwapper<YamlMaskRuleConfiguration, MaskRuleConfiguration> {
+
+ private final YamlMaskTableRuleConfigurationSwapper tableSwapper = new YamlMaskTableRuleConfigurationSwapper();
+
+ private final YamlAlgorithmConfigurationSwapper algorithmSwapper = new YamlAlgorithmConfigurationSwapper();
+
+ @Override
+ public YamlMaskRuleConfiguration swapToYamlConfiguration(final MaskRuleConfiguration data) {
+ YamlMaskRuleConfiguration result = new YamlMaskRuleConfiguration();
+ data.getTables().forEach(each -> result.getTables().put(each.getName(), tableSwapper.swapToYamlConfiguration(each)));
+ data.getMaskAlgorithms().forEach((key, value) -> result.getMaskAlgorithms().put(key, algorithmSwapper.swapToYamlConfiguration(value)));
+ return result;
+ }
+
+ @Override
+ public MaskRuleConfiguration swapToObject(final YamlMaskRuleConfiguration yamlConfig) {
+ return new MaskRuleConfiguration(swapTables(yamlConfig), swapMaskAlgorithm(yamlConfig));
+ }
+
+ private Collection<MaskTableRuleConfiguration> swapTables(final YamlMaskRuleConfiguration yamlConfig) {
+ Collection<MaskTableRuleConfiguration> result = new LinkedList<>();
+ for (Map.Entry<String, YamlMaskTableRuleConfiguration> entry : yamlConfig.getTables().entrySet()) {
+ YamlMaskTableRuleConfiguration yamlMaskTableRuleConfig = entry.getValue();
+ yamlMaskTableRuleConfig.setName(entry.getKey());
+ result.add(tableSwapper.swapToObject(yamlMaskTableRuleConfig));
+ }
+ return result;
+ }
+
+ private Map<String, AlgorithmConfiguration> swapMaskAlgorithm(final YamlMaskRuleConfiguration yamlConfig) {
+ Map<String, AlgorithmConfiguration> result = new LinkedHashMap<>(yamlConfig.getMaskAlgorithms().size(), 1);
+ for (Map.Entry<String, YamlAlgorithmConfiguration> entry : yamlConfig.getMaskAlgorithms().entrySet()) {
+ result.put(entry.getKey(), algorithmSwapper.swapToObject(entry.getValue()));
+ }
+ return result;
+ }
+
+ @Override
+ public Class<MaskRuleConfiguration> getTypeClass() {
+ return MaskRuleConfiguration.class;
+ }
+
+ @Override
+ public String getRuleTagName() {
+ return "MASK";
+ }
+
+ @Override
+ public int getOrder() {
+ return MaskOrder.ORDER;
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapper.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..5b8007a3875
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapper.java
@@ -0,0 +1,41 @@
+/*
+ * 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.yaml.swapper.rule;
+
+import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
+
+/**
+ * YAML mask column rule configuration swapper.
+ */
+public final class YamlMaskColumnRuleConfigurationSwapper implements YamlConfigurationSwapper<YamlMaskColumnRuleConfiguration, MaskColumnRuleConfiguration> {
+
+ @Override
+ public YamlMaskColumnRuleConfiguration swapToYamlConfiguration(final MaskColumnRuleConfiguration data) {
+ YamlMaskColumnRuleConfiguration result = new YamlMaskColumnRuleConfiguration();
+ result.setLogicColumn(data.getLogicColumn());
+ result.setMaskAlgorithm(data.getMaskAlgorithm());
+ return result;
+ }
+
+ @Override
+ public MaskColumnRuleConfiguration swapToObject(final YamlMaskColumnRuleConfiguration yamlConfig) {
+ return new MaskColumnRuleConfiguration(yamlConfig.getLogicColumn(), yamlConfig.getMaskAlgorithm());
+ }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapper.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapper.java
new file mode 100644
index 00000000000..d6502bd9549
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapper.java
@@ -0,0 +1,57 @@
+/*
+ * 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.yaml.swapper.rule;
+
+import org.apache.shardingsphere.infra.util.yaml.swapper.YamlConfigurationSwapper;
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+
+import java.util.Collection;
+import java.util.LinkedList;
+import java.util.Map;
+
+/**
+ * YAML mask table rule configuration swapper.
+ */
+public final class YamlMaskTableRuleConfigurationSwapper implements YamlConfigurationSwapper<YamlMaskTableRuleConfiguration, MaskTableRuleConfiguration> {
+
+ private final YamlMaskColumnRuleConfigurationSwapper columnSwapper = new YamlMaskColumnRuleConfigurationSwapper();
+
+ @Override
+ public YamlMaskTableRuleConfiguration swapToYamlConfiguration(final MaskTableRuleConfiguration data) {
+ YamlMaskTableRuleConfiguration result = new YamlMaskTableRuleConfiguration();
+ for (MaskColumnRuleConfiguration each : data.getColumns()) {
+ result.getColumns().put(each.getLogicColumn(), columnSwapper.swapToYamlConfiguration(each));
+ }
+ result.setName(data.getName());
+ return result;
+ }
+
+ @Override
+ public MaskTableRuleConfiguration swapToObject(final YamlMaskTableRuleConfiguration yamlConfig) {
+ Collection<MaskColumnRuleConfiguration> columns = new LinkedList<>();
+ for (Map.Entry<String, YamlMaskColumnRuleConfiguration> entry : yamlConfig.getColumns().entrySet()) {
+ YamlMaskColumnRuleConfiguration yamlMaskColumnRuleConfig = entry.getValue();
+ yamlMaskColumnRuleConfig.setLogicColumn(entry.getKey());
+ columns.add(columnSwapper.swapToObject(yamlMaskColumnRuleConfig));
+ }
+ return new MaskTableRuleConfiguration(yamlConfig.getName(), columns);
+ }
+}
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
similarity index 91%
copy from features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
copy to features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
index 4d37c2cb819..a5f80f93a14 100644
--- a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapper
@@ -15,4 +15,4 @@
# limitations under the License.
#
-org.apache.shardingsphere.mask.algorithm.hash.MD5MaskAlgorithm
+org.apache.shardingsphere.mask.yaml.swapper.YamlMaskRuleConfigurationSwapper
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
index 4d37c2cb819..826c5f76174 100644
--- a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
@@ -14,5 +14,6 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#
-
+org.apache.shardingsphere.mask.algorithm.cover.KeepFirstNLastMMaskAlgorithm
+org.apache.shardingsphere.mask.algorithm.cover.KeepFromXToYMaskAlgorithm
org.apache.shardingsphere.mask.algorithm.hash.MD5MaskAlgorithm
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapperTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..52f67389c6b
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/YamlMaskRuleConfigurationSwapperTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.yaml.swapper;
+
+import org.apache.shardingsphere.infra.config.algorithm.AlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.pojo.algorithm.YamlAlgorithmConfiguration;
+import org.apache.shardingsphere.infra.yaml.config.swapper.rule.YamlRuleConfigurationSwapperFactory;
+import org.apache.shardingsphere.mask.api.config.MaskRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.YamlMaskRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Map;
+import java.util.Properties;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.mockito.Mockito.mock;
+
+public final class YamlMaskRuleConfigurationSwapperTest {
+
+ @Test
+ public void assertSwapToYamlConfiguration() {
+ YamlMaskRuleConfiguration actual = getSwapper().swapToYamlConfiguration(createMaskRuleConfiguration());
+ assertThat(actual.getTables().size(), is(1));
+ assertThat(actual.getMaskAlgorithms().size(), is(1));
+ }
+
+ private MaskRuleConfiguration createMaskRuleConfiguration() {
+ Collection<MaskTableRuleConfiguration> tables = Collections.singletonList(new MaskTableRuleConfiguration("tbl", Collections.emptyList()));
+ Map<String, AlgorithmConfiguration> encryptors = Collections.singletonMap("myMaskAlgorithm", new AlgorithmConfiguration("MD5", new Properties()));
+ return new MaskRuleConfiguration(tables, encryptors);
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ MaskRuleConfiguration actual = getSwapper().swapToObject(createYamlMaskRuleConfiguration());
+ assertThat(actual.getTables().size(), is(1));
+ assertThat(actual.getMaskAlgorithms().size(), is(1));
+ }
+
+ private YamlMaskRuleConfiguration createYamlMaskRuleConfiguration() {
+ YamlMaskRuleConfiguration result = new YamlMaskRuleConfiguration();
+ YamlMaskTableRuleConfiguration tableRuleConfig = new YamlMaskTableRuleConfiguration();
+ tableRuleConfig.setName("t_mask");
+ result.getTables().put("t_mask", tableRuleConfig);
+ YamlAlgorithmConfiguration algorithmConfig = new YamlAlgorithmConfiguration();
+ algorithmConfig.setType("MD5");
+ result.getMaskAlgorithms().put("md5_mask", algorithmConfig);
+ return result;
+ }
+
+ private YamlMaskRuleConfigurationSwapper getSwapper() {
+ MaskRuleConfiguration ruleConfig = mock(MaskRuleConfiguration.class);
+ return (YamlMaskRuleConfigurationSwapper) YamlRuleConfigurationSwapperFactory.getInstanceMapByRuleConfigurations(Collections.singletonList(ruleConfig)).get(ruleConfig);
+ }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapperTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..ecc336ee55c
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskColumnRuleConfigurationSwapperTest.java
@@ -0,0 +1,49 @@
+/*
+ * 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.yaml.swapper.rule;
+
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
+import org.junit.Test;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class YamlMaskColumnRuleConfigurationSwapperTest {
+
+ @Test
+ public void assertSwapToYamlConfiguration() {
+ YamlMaskColumnRuleConfigurationSwapper swapper = new YamlMaskColumnRuleConfigurationSwapper();
+ MaskColumnRuleConfiguration encryptColumnRuleConfig =
+ new MaskColumnRuleConfiguration("logicColumn", "md5_mask");
+ YamlMaskColumnRuleConfiguration actual = swapper.swapToYamlConfiguration(encryptColumnRuleConfig);
+ assertThat(actual.getLogicColumn(), is("logicColumn"));
+ assertThat(actual.getMaskAlgorithm(), is("md5_mask"));
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ YamlMaskColumnRuleConfigurationSwapper swapper = new YamlMaskColumnRuleConfigurationSwapper();
+ YamlMaskColumnRuleConfiguration yamlMaskColumnRuleConfig = new YamlMaskColumnRuleConfiguration();
+ yamlMaskColumnRuleConfig.setLogicColumn("logicColumn");
+ yamlMaskColumnRuleConfig.setMaskAlgorithm("md5_mask");
+ MaskColumnRuleConfiguration actual = swapper.swapToObject(yamlMaskColumnRuleConfig);
+ assertThat(actual.getLogicColumn(), is("logicColumn"));
+ assertThat(actual.getMaskAlgorithm(), is("md5_mask"));
+ }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapperTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapperTest.java
new file mode 100644
index 00000000000..7a8373c238f
--- /dev/null
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/yaml/swapper/rule/YamlMaskTableRuleConfigurationSwapperTest.java
@@ -0,0 +1,75 @@
+/*
+ * 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.yaml.swapper.rule;
+
+import org.apache.shardingsphere.mask.api.config.rule.MaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.api.config.rule.MaskTableRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskColumnRuleConfiguration;
+import org.apache.shardingsphere.mask.yaml.config.rule.YamlMaskTableRuleConfiguration;
+import org.junit.Test;
+
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.LinkedHashMap;
+import java.util.Map;
+
+import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.MatcherAssert.assertThat;
+
+public final class YamlMaskTableRuleConfigurationSwapperTest {
+
+ private final YamlMaskTableRuleConfigurationSwapper swapper = new YamlMaskTableRuleConfigurationSwapper();
+
+ @Test
+ public void assertSwapToYamlConfiguration() {
+ Collection<MaskColumnRuleConfiguration> encryptColumnRuleConfigs = Arrays.asList(
+ new MaskColumnRuleConfiguration("mask_column_1", "md5_mask"),
+ new MaskColumnRuleConfiguration("mask_column_2", "keep_from_x_to_y"),
+ new MaskColumnRuleConfiguration("mask_column_3", "keep_first_m_last_m"));
+ MaskTableRuleConfiguration encryptTableRuleConfig = new MaskTableRuleConfiguration("test_table", encryptColumnRuleConfigs);
+ YamlMaskTableRuleConfiguration actualYamlMaskTableRuleConfig = swapper.swapToYamlConfiguration(encryptTableRuleConfig);
+ assertThat(actualYamlMaskTableRuleConfig.getName(), is("test_table"));
+ Map<String, YamlMaskColumnRuleConfiguration> actualColumns = actualYamlMaskTableRuleConfig.getColumns();
+ assertThat(actualColumns.size(), is(3));
+ YamlMaskColumnRuleConfiguration actualYamlMaskColumnRuleConfigFirst = actualColumns.get("mask_column_1");
+ assertThat(actualYamlMaskColumnRuleConfigFirst.getMaskAlgorithm(), is("md5_mask"));
+ YamlMaskColumnRuleConfiguration actualYamlMaskColumnRuleConfigSecond = actualColumns.get("mask_column_2");
+ assertThat(actualYamlMaskColumnRuleConfigSecond.getMaskAlgorithm(), is("keep_from_x_to_y"));
+ YamlMaskColumnRuleConfiguration actualYamlMaskColumnRuleConfigThird = actualColumns.get("mask_column_3");
+ assertThat(actualYamlMaskColumnRuleConfigThird.getMaskAlgorithm(), is("keep_first_m_last_m"));
+ }
+
+ @Test
+ public void assertSwapToObject() {
+ YamlMaskColumnRuleConfiguration encryptColumnRuleConfig = new YamlMaskColumnRuleConfiguration();
+ encryptColumnRuleConfig.setLogicColumn("mask_column");
+ encryptColumnRuleConfig.setMaskAlgorithm("md5_mask");
+ Map<String, YamlMaskColumnRuleConfiguration> columns = new LinkedHashMap<>(1);
+ columns.put("mask_column", encryptColumnRuleConfig);
+ YamlMaskTableRuleConfiguration yamlMaskTableRuleConfig = new YamlMaskTableRuleConfiguration();
+ yamlMaskTableRuleConfig.setName("test_table");
+ yamlMaskTableRuleConfig.setColumns(columns);
+ MaskTableRuleConfiguration actualMaskTableRuleConfig = swapper.swapToObject(yamlMaskTableRuleConfig);
+ assertThat(actualMaskTableRuleConfig.getName(), is("test_table"));
+ Collection<MaskColumnRuleConfiguration> actualColumns = actualMaskTableRuleConfig.getColumns();
+ assertThat(actualColumns.size(), is(1));
+ MaskColumnRuleConfiguration actualMaskColumnRuleConfig = actualColumns.iterator().next();
+ assertThat(actualMaskColumnRuleConfig.getLogicColumn(), is("mask_column"));
+ assertThat(actualMaskColumnRuleConfig.getMaskAlgorithm(), is("md5_mask"));
+ }
+}