diff --git a/src/main/java/pl/gov/coi/pomocua/ads/phone/PhoneUtil.java b/src/main/java/pl/gov/coi/pomocua/ads/phone/PhoneUtil.java index 82badace..80a77cf5 100644 --- a/src/main/java/pl/gov/coi/pomocua/ads/phone/PhoneUtil.java +++ b/src/main/java/pl/gov/coi/pomocua/ads/phone/PhoneUtil.java @@ -9,12 +9,18 @@ import java.util.Optional; public class PhoneUtil { + private static final String PLUS = "+"; private static final String DEFAULT_REGION = "PL"; private static final PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance(); + private static PhoneNumber parse(String value) throws NumberParseException { + String phoneNumber = StringUtils.prependIfMissing(value, PLUS); + return phoneNumberUtil.parse(phoneNumber, DEFAULT_REGION); + } + public static boolean isValid(String value) { try { - PhoneNumber phoneNumber = phoneNumberUtil.parse(value, DEFAULT_REGION); + PhoneNumber phoneNumber = parse(value); return phoneNumberUtil.isValidNumber(phoneNumber); } catch (NumberParseException e) { return false; @@ -25,7 +31,7 @@ public static Optional getPhoneDetails(String value) { if (StringUtils.isEmpty(value)) return Optional.empty(); try { - PhoneNumber phoneNumber = phoneNumberUtil.parse(value, DEFAULT_REGION); + PhoneNumber phoneNumber = parse(value); return Optional.of(new PhoneDetails(String.valueOf(phoneNumber.getCountryCode()), String.valueOf(phoneNumber.getNationalNumber()))); } catch (NumberParseException e) { diff --git a/src/test/java/pl/gov/coi/pomocua/ads/phone/PhoneUtilTest.java b/src/test/java/pl/gov/coi/pomocua/ads/phone/PhoneUtilTest.java new file mode 100644 index 00000000..6b6476f4 --- /dev/null +++ b/src/test/java/pl/gov/coi/pomocua/ads/phone/PhoneUtilTest.java @@ -0,0 +1,30 @@ +package pl.gov.coi.pomocua.ads.phone; + +import org.junit.jupiter.api.Test; + +import java.util.Optional; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +class PhoneUtilTest { + + @Test + void getPhoneDetails_expectSameResultWithOrWithoutPlus() { + + //when + Optional phoneDetailsWithPlus = PhoneUtil.getPhoneDetails("+380671232233"); + + Optional phoneDetailsWithoutPlus = PhoneUtil.getPhoneDetails("380671232233"); + + //then + + assertThat(phoneDetailsWithPlus).isPresent(); + assertThat(phoneDetailsWithoutPlus).isPresent(); + + assertAll( + () -> assertThat(phoneDetailsWithPlus).isEqualTo(phoneDetailsWithoutPlus), + () -> assertThat(phoneDetailsWithPlus.get().countryCode()).isEqualTo("380") + ); + } +} \ No newline at end of file