strongduanmu commented on code in PR #23404: URL: https://github.com/apache/shardingsphere/pull/23404#discussion_r1064120886
########## features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.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.algorithm.replace; + +import org.apache.shardingsphere.test.util.PropertiesBuilder; +import org.apache.shardingsphere.test.util.PropertiesBuilder.Property; +import org.junit.Before; +import org.junit.Test; + +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; + +public final class LandlineNumberRandomAlgorithmTest { + + private LandlineNumberRandomAlgorithm maskAlgorithm; + + @Before + public void setUp() { + maskAlgorithm = new LandlineNumberRandomAlgorithm(); + maskAlgorithm.init(PropertiesBuilder.build(new Property("landline-numbers", "025, 027, 028, 029, 0310, 0311, 0313"))); + } + + @Test + public void assertMask() { + assertThat(maskAlgorithm.mask(""), is("")); + assertThat(maskAlgorithm.mask("0251234567"), not("0251234567")); + assertThat(maskAlgorithm.mask("03101234567"), not("03101234567")); + } +} Review Comment: Hi @lushaorong, can you update built-in mask algorithm document and mask algorithm spi document? ########## features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithm.java: ########## @@ -0,0 +1,76 @@ +/* + * 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.algorithm.replace; + +import com.google.common.base.Splitter; +import com.google.common.base.Strings; +import lombok.Getter; +import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil; +import org.apache.shardingsphere.mask.spi.MaskAlgorithm; + +import java.util.List; +import java.util.Properties; +import java.util.Random; + +/** + * Landline number replace algorithm. + */ +public final class LandlineNumberRandomAlgorithm implements MaskAlgorithm<Object, String> { + + private static final String LANDLINE_NUMBERS = "landline-numbers"; + + private List<String> landLineNumbers; + + @Getter + private Properties props; + + @Override + public String mask(final Object plainValue) { + String result = null == plainValue ? null : String.valueOf(plainValue); + if (Strings.isNullOrEmpty(result)) { + return result; + } + return landLineNumbers.stream().filter(result::startsWith).findFirst().map(each -> createRandValue(result, each)).orElse(result); + } + + private String createRandValue(final String plainValue, final String landLineNumber) { + Random random = new Random(); + StringBuilder result = new StringBuilder(); + result.append(landLineNumbers.get(random.nextInt(landLineNumbers.size()))); + for (int i = landLineNumber.length(); i < plainValue.length(); i++) { + result.append(Character.forDigit(random.nextInt(10), 10)); + } + return result.toString(); + } + + @Override + public void init(final Properties props) { Review Comment: Please move init method before mask method. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
