Skip to content

Commit

Permalink
Merge pull request #131 from mndzielski/POM-568-phonenumber-without-plus
Browse files Browse the repository at this point in the history
POM-568 - Phone number without plus support
  • Loading branch information
mndzielski authored Apr 29, 2022
2 parents 6a2df65 + f679870 commit bf14968
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/main/java/pl/gov/coi/pomocua/ads/phone/PhoneUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -25,7 +31,7 @@ public static Optional<PhoneDetails> 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) {
Expand Down
30 changes: 30 additions & 0 deletions src/test/java/pl/gov/coi/pomocua/ads/phone/PhoneUtilTest.java
Original file line number Diff line number Diff line change
@@ -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<PhoneDetails> phoneDetailsWithPlus = PhoneUtil.getPhoneDetails("+380671232233");

Optional<PhoneDetails> phoneDetailsWithoutPlus = PhoneUtil.getPhoneDetails("380671232233");

//then

assertThat(phoneDetailsWithPlus).isPresent();
assertThat(phoneDetailsWithoutPlus).isPresent();

assertAll(
() -> assertThat(phoneDetailsWithPlus).isEqualTo(phoneDetailsWithoutPlus),
() -> assertThat(phoneDetailsWithPlus.get().countryCode()).isEqualTo("380")
);
}
}

0 comments on commit bf14968

Please sign in to comment.