From 831acaf8cfd92b08e29be0b4c7c15b9e19a8c6ef Mon Sep 17 00:00:00 2001 From: MohamedHelmyKassab <137485958+MohamedHelmyKassab@users.noreply.github.com> Date: Mon, 12 Feb 2024 19:59:03 +0200 Subject: [PATCH] This PR is related to BAEL-7528 (#15865) * Update pom.xml Add: core-java-string-operations-8 * 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" --- .../core-java-string-operations-8/README.md | 2 + .../core-java-string-operations-8/pom.xml | 72 +++++++++++++++++++ .../EmailAndPhoneMaskingUnitTest.java | 53 ++++++++++++++ core-java-modules/pom.xml | 1 + 4 files changed, 128 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-8/README.md create mode 100644 core-java-modules/core-java-string-operations-8/pom.xml create mode 100644 core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/emailandphonemasking/EmailAndPhoneMaskingUnitTest.java diff --git a/core-java-modules/core-java-string-operations-8/README.md b/core-java-modules/core-java-string-operations-8/README.md new file mode 100644 index 0000000000..2dce44d217 --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/README.md @@ -0,0 +1,2 @@ + +### Relevant Articles: diff --git a/core-java-modules/core-java-string-operations-8/pom.xml b/core-java-modules/core-java-string-operations-8/pom.xml new file mode 100644 index 0000000000..2495abccf8 --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/pom.xml @@ -0,0 +1,72 @@ + + + 4.0.0 + core-java-string-operations-8 + core-java-string-operations-8 + jar + + + com.baeldung.core-java-modules + core-java-modules + 0.0.1-SNAPSHOT + + + + + org.apache.commons + commons-lang3 + ${apache.commons.lang3.version} + + + org.apache.commons + commons-text + ${commons-text.version} + + + org.liquibase + liquibase-core + 4.9.1 + test + + + org.junit.jupiter + junit-jupiter + 5.8.1 + test + + + org.liquibase + liquibase-core + 4.9.1 + test + + + junit + junit + 4.13.2 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + 11 + 11 + 3.13.0 + 1.10.0 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/emailandphonemasking/EmailAndPhoneMaskingUnitTest.java b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/emailandphonemasking/EmailAndPhoneMaskingUnitTest.java new file mode 100644 index 0000000000..e390c2d2cd --- /dev/null +++ b/core-java-modules/core-java-string-operations-8/src/test/java/com/baeldung/emailandphonemasking/EmailAndPhoneMaskingUnitTest.java @@ -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); + } + + +} diff --git a/core-java-modules/pom.xml b/core-java-modules/pom.xml index 227da74156..c30a240f4a 100644 --- a/core-java-modules/pom.xml +++ b/core-java-modules/pom.xml @@ -199,6 +199,7 @@ core-java-string-operations-2 core-java-string-operations-6 core-java-string-operations-7 + core-java-string-operations-8 core-java-regex core-java-regex-2 core-java-regex-3