This PR is related to BAEL-7528 (#15865)

* Update pom.xml

Add:         <module>core-java-string-operations-8</module>

* This commit is related to BAEL-7528

This commit aims to add a new module "core-java-string-operations-8" to submit a test class "EmailAndPhoneMaskingUnitTest"
This commit is contained in:
MohamedHelmyKassab 2024-02-12 19:59:03 +02:00 committed by GitHub
parent 73a9ed952b
commit 831acaf8cf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 128 additions and 0 deletions

View File

@ -0,0 +1,2 @@
### Relevant Articles:

View File

@ -0,0 +1,72 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-operations-8</artifactId>
<name>core-java-string-operations-8</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${apache.commons.lang3.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>${maven.compiler.source}</source>
<target>${maven.compiler.target}</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<apache.commons.lang3.version>3.13.0</apache.commons.lang3.version>
<commons-text.version>1.10.0</commons-text.version>
</properties>
</project>

View File

@ -0,0 +1,53 @@
package com.baeldung.emailandphonemasking;
import org.junit.jupiter.api.Test;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class EmailAndPhoneMaskingUnitTest {
String phoneNumber = "+12344567890";
String expectedMaskedPhoneNumber = "+*******7890";
String email = "testemailaddress@example.com";
String expectedMaskedEmail = "te**************@example.com";
@Test
public void givenEmailAddress_whenUsingStringManipulation_thenMaskEmail() {
int atIndex = email.indexOf('@');
String repeatedString = IntStream.range(0, atIndex - 2).mapToObj(i -> "*").collect(Collectors.joining());
String maskedPart = email.substring(0, atIndex - repeatedString.length()) + repeatedString;
String maskedEmail = maskedPart + email.substring(atIndex);
assertEquals(expectedMaskedEmail, maskedEmail);
}
@Test
public void givenEmailAddress_whenUsingRegex_thenMaskEmail() {
int atIndex = email.indexOf('@');
String regex = "(.{2})(.*)(@.*)";
String repeatedAsterisks = "*".repeat(atIndex - 2);
String maskedEmail = email.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedEmail, maskedEmail);
}
@Test
public void givenPhoneNumber_whenUsingStringManipulation_thenMaskPhone() {
String maskedPhoneNumber = phoneNumber.replaceAll("\\d(?=\\d{4})", "*");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
@Test
public void givenPhoneNumber_whenUsingRegex_thenMaskPhone() {
int lastDigitsIndex = phoneNumber.length() - 5;
String regex = "(\\+)(\\d+)(\\d{4})";
String repeatedAsterisks = "*".repeat(Math.max(0, lastDigitsIndex));
String maskedPhoneNumber = phoneNumber.replaceAll(regex, "$1" + repeatedAsterisks + "$3");
assertEquals(expectedMaskedPhoneNumber, maskedPhoneNumber);
}
}

View File

@ -199,6 +199,7 @@
<module>core-java-string-operations-2</module>
<module>core-java-string-operations-6</module>
<module>core-java-string-operations-7</module>
<module>core-java-string-operations-8</module>
<module>core-java-regex</module>
<module>core-java-regex-2</module>
<module>core-java-regex-3</module>