feat: 脱敏组件增加单元测试

This commit is contained in:
gaibu 2023-01-17 11:28:33 +08:00
parent 7b3fca2844
commit 42bc0d1519
7 changed files with 136 additions and 10 deletions

View File

@ -20,7 +20,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@DesensitizeBy(handler = EmailDesensitizationHandler.class)
public @interface EmailDesensitize {
public @interface Email {
/**
* 匹配的正则表达式

View File

@ -20,7 +20,7 @@ import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@DesensitizeBy(handler = DefaultRegexDesensitizationHandler.class)
public @interface RegexDesensitize {
public @interface Regex {
/**
* 匹配的正则表达式默认匹配所有

View File

@ -1,22 +1,22 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.RegexDesensitize;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
/**
* 默认正则脱敏处理器
*
* @author gaibu
*/
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<RegexDesensitize> {
public class DefaultRegexDesensitizationHandler extends AbstractRegexDesensitizationHandler<Regex> {
@Override
String getRegex(RegexDesensitize annotation) {
String getRegex(Regex annotation) {
return annotation.regex();
}
@Override
String getReplacer(RegexDesensitize annotation) {
String getReplacer(Regex annotation) {
return annotation.replacer();
}
}

View File

@ -1,21 +1,21 @@
package cn.iocoder.yudao.framework.desensitize.core.regex.handler;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.EmailDesensitize;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
/**
* 邮箱脱敏处理器
*
* @author gaibu
*/
public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<EmailDesensitize> {
public class EmailDesensitizationHandler extends AbstractRegexDesensitizationHandler<Email> {
@Override
String getRegex(EmailDesensitize annotation) {
String getRegex(Email annotation) {
return annotation.regex();
}
@Override
String getReplacer(EmailDesensitize annotation) {
String getReplacer(Email annotation) {
return annotation.replacer();
}
}

View File

@ -0,0 +1,88 @@
package cn.iocoder.yudao.framework.desensitize.core.slider;
import cn.iocoder.yudao.framework.common.util.json.JsonUtils;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Email;
import cn.iocoder.yudao.framework.desensitize.core.regex.annotation.Regex;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.BankCard;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.CarLicense;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.ChineseName;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.FixedPhone;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.IdCard;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Password;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.PhoneNumber;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Slider;
import cn.iocoder.yudao.framework.test.core.ut.BaseMockitoUnitTest;
import lombok.Data;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DesensitizeTest extends BaseMockitoUnitTest {
@Test
public void test() {
DesensitizeDemo desensitizeDemo = new DesensitizeDemo();
desensitizeDemo.setUserName("芋道源码");
desensitizeDemo.setBankCard("9988002866797031");
desensitizeDemo.setCarLicense("粤A66666");
desensitizeDemo.setFixedPhone("01086551122");
desensitizeDemo.setIdCard("530321199204074611");
desensitizeDemo.setPassword("123456");
desensitizeDemo.setPhoneNumber("13248765917");
desensitizeDemo.setSlider1("ABCDEFG");
desensitizeDemo.setSlider2("ABCDEFG");
desensitizeDemo.setSlider3("ABCDEFG");
desensitizeDemo.setEmail("1@eamil.com");
desensitizeDemo.setRegex("你好,我是芋道源码");
desensitizeDemo.setAddress("北京市海淀区上地十街10号");
desensitizeDemo.setOrigin("芋道源码");
DesensitizeDemo d = JsonUtils.parseObject(JsonUtils.toJsonString(desensitizeDemo), DesensitizeDemo.class);
assertEquals("芋***", d.getUserName());
assertEquals("998800********31", d.getBankCard());
assertEquals("粤A6***6", d.getCarLicense());
assertEquals("0108*****22", d.getFixedPhone());
assertEquals("530321**********11", d.getIdCard());
assertEquals("******", d.getPassword());
assertEquals("132****5917", d.getPhoneNumber());
assertEquals("#######", d.getSlider1());
assertEquals("ABC*EFG", d.getSlider2());
assertEquals("*******", d.getSlider3());
assertEquals("1****@eamil.com", d.getEmail());
assertEquals("你好,我是*", d.getRegex());
assertEquals("北京市海淀区上地十街10号*", d.getAddress());
assertEquals("芋道源码", d.getOrigin());
}
@Data
public static class DesensitizeDemo {
@ChineseName
private String userName;
@BankCard
private String bankCard;
@CarLicense
private String carLicense;
@FixedPhone
private String fixedPhone;
@IdCard
private String idCard;
@Password
private String password;
@PhoneNumber
private String phoneNumber;
@Slider(prefixKeep = 6,suffixKeep = 1,replacer = "#")
private String slider1;
@Slider(prefixKeep = 3,suffixKeep = 3)
private String slider2;
@Slider(prefixKeep = 10)
private String slider3;
@Email
private String email;
@Regex(regex = "芋道源码",replacer = "*")
private String regex;
@Address
private String address;
private String origin;
}
}

View File

@ -0,0 +1,27 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.annotation;
import cn.iocoder.yudao.framework.desensitize.core.base.annotation.DesensitizeBy;
import cn.iocoder.yudao.framework.desensitize.core.slider.handler.AddressHandler;
import com.fasterxml.jackson.annotation.JacksonAnnotationsInside;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/**
* 地址
*
* @author gaibu
*/
@Documented
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotationsInside
@DesensitizeBy(handler = AddressHandler.class)
public @interface Address {
String replacer() default "*";
}

View File

@ -0,0 +1,11 @@
package cn.iocoder.yudao.framework.desensitize.core.slider.handler;
import cn.iocoder.yudao.framework.desensitize.core.base.handler.DesensitizationHandler;
import cn.iocoder.yudao.framework.desensitize.core.slider.annotation.Address;
public class AddressHandler implements DesensitizationHandler<Address> {
@Override
public String desensitize(String origin, Address annotation) {
return origin + annotation.replacer();
}
}