From bdb42ec042cfddcf73643dad4a23b4415196fe5f Mon Sep 17 00:00:00 2001 From: Alessio D'Innocenti Date: Tue, 22 Feb 2022 17:04:44 +0100 Subject: [PATCH] fix 2 letters names and surnames --- .../codicefiscale/internal/LastnamePart.java | 13 +++++++++---- .../codicefiscale/internal/NamePart.java | 2 ++ .../codicefiscale/internal/LastnamePartTest.java | 14 ++++++++++++-- .../codicefiscale/internal/NamePartTest.java | 12 +++++++++++- 4 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/main/java/it/kamaladafrica/codicefiscale/internal/LastnamePart.java b/src/main/java/it/kamaladafrica/codicefiscale/internal/LastnamePart.java index 159342b..c939e5c 100644 --- a/src/main/java/it/kamaladafrica/codicefiscale/internal/LastnamePart.java +++ b/src/main/java/it/kamaladafrica/codicefiscale/internal/LastnamePart.java @@ -1,13 +1,17 @@ package it.kamaladafrica.codicefiscale.internal; +import static it.kamaladafrica.codicefiscale.utils.PartUtils.extractConsonants; +import static it.kamaladafrica.codicefiscale.utils.PartUtils.extractVowels; +import static it.kamaladafrica.codicefiscale.utils.PartUtils.normalizeString; +import static org.apache.commons.lang3.Validate.matchesPattern; + +import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; + import lombok.AccessLevel; import lombok.EqualsAndHashCode; import lombok.RequiredArgsConstructor; import lombok.Value; -import org.apache.commons.lang3.Validate; - -import static it.kamaladafrica.codicefiscale.utils.PartUtils.*; -import static org.apache.commons.lang3.Validate.matchesPattern; @Value @RequiredArgsConstructor(access = AccessLevel.PRIVATE) @@ -25,6 +29,7 @@ public class LastnamePart extends AbstractPart { public static LastnamePart from(String value) { Validate.notEmpty(value, "invalid name: %s", value); matchesPattern(value, VALIDATION_PATTERN, "invalid value: %s", value); + value = StringUtils.removeEnd(value, "X"); return of(value); } diff --git a/src/main/java/it/kamaladafrica/codicefiscale/internal/NamePart.java b/src/main/java/it/kamaladafrica/codicefiscale/internal/NamePart.java index d239f44..8cf8344 100644 --- a/src/main/java/it/kamaladafrica/codicefiscale/internal/NamePart.java +++ b/src/main/java/it/kamaladafrica/codicefiscale/internal/NamePart.java @@ -5,6 +5,7 @@ import static it.kamaladafrica.codicefiscale.utils.PartUtils.normalizeString; import static org.apache.commons.lang3.Validate.matchesPattern; +import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.Validate; import lombok.AccessLevel; @@ -30,6 +31,7 @@ public class NamePart extends AbstractPart { public static NamePart from(String value) { Validate.notEmpty(value, "invalid name: %s", value); matchesPattern(value, VALIDATION_PATTERN, "invalid value: %s", value); + value = StringUtils.removeEnd(value, "X"); return of(value); } diff --git a/src/test/java/it/kamaladafrica/codicefiscale/internal/LastnamePartTest.java b/src/test/java/it/kamaladafrica/codicefiscale/internal/LastnamePartTest.java index fb3e696..e2e9e8b 100644 --- a/src/test/java/it/kamaladafrica/codicefiscale/internal/LastnamePartTest.java +++ b/src/test/java/it/kamaladafrica/codicefiscale/internal/LastnamePartTest.java @@ -1,9 +1,9 @@ package it.kamaladafrica.codicefiscale.internal; -import org.junit.Test; - import static org.junit.Assert.assertEquals; +import org.junit.Test; + public class LastnamePartTest { @Test @@ -16,4 +16,14 @@ public void testOf() { assertEquals("RSS", LastnamePart.of("Rossini").getValue()); } + @Test + public void testFromPlaceholder() { + assertEquals("BO", NamePart.from("BOX").getName()); + } + + @Test + public void testOf2Letters() { + assertEquals("BOX", NamePart.of("Bo").getValue()); + } + } diff --git a/src/test/java/it/kamaladafrica/codicefiscale/internal/NamePartTest.java b/src/test/java/it/kamaladafrica/codicefiscale/internal/NamePartTest.java index 635655d..2e4f416 100644 --- a/src/test/java/it/kamaladafrica/codicefiscale/internal/NamePartTest.java +++ b/src/test/java/it/kamaladafrica/codicefiscale/internal/NamePartTest.java @@ -8,7 +8,7 @@ public class NamePartTest { @Test public void testFromString() { - assertEquals("MRA", NamePart.from("MRA").getValue()); + assertEquals("MRA", NamePart.from("MRA").getName()); } @Test @@ -26,4 +26,14 @@ public void testOf3Consonants() { assertEquals("MRC", NamePart.of("Marco").getValue()); } + @Test + public void testFromPlaceholder() { + assertEquals("BO", NamePart.from("BOX").getName()); + } + + @Test + public void testOf2Letters() { + assertEquals("BOX", NamePart.of("Bo").getValue()); + } + }