mirror of
https://github.com/signalapp/Signal-Server
synced 2026-04-23 09:08:02 +01:00
Normalize Benin phone numbers to the new format before sending to registration service
This commit is contained in:
@@ -6,17 +6,26 @@
|
||||
package org.whispersystems.textsecuregcm.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
import com.google.i18n.phonenumbers.NumberParseException;
|
||||
import com.google.i18n.phonenumbers.PhoneNumberUtil;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.stream.Stream;
|
||||
import com.google.i18n.phonenumbers.Phonenumber;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.Arguments;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
class UtilTest {
|
||||
// libphonenumber 8.13.50 and on generate new-format numbers for Benin
|
||||
private static final String NEW_FORMAT_BENIN_E164_STRING = PhoneNumberUtil.getInstance()
|
||||
.format(PhoneNumberUtil.getInstance().getExampleNumber("BJ"), PhoneNumberUtil.PhoneNumberFormat.E164);
|
||||
private static final String OLD_FORMAT_BENIN_E164_STRING = NEW_FORMAT_BENIN_E164_STRING.replaceFirst("01", "");
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
@@ -28,16 +37,10 @@ class UtilTest {
|
||||
final String usE164 = PhoneNumberUtil.getInstance().format(
|
||||
PhoneNumberUtil.getInstance().getExampleNumber("US"), PhoneNumberUtil.PhoneNumberFormat.E164);
|
||||
|
||||
// libphonenumber 8.13.50 and on generate new-format numbers for Benin
|
||||
final String newFormatBeninE164 = PhoneNumberUtil.getInstance()
|
||||
.format(PhoneNumberUtil.getInstance().getExampleNumber("BJ"), PhoneNumberUtil.PhoneNumberFormat.E164);
|
||||
|
||||
final String oldFormatBeninE164 = newFormatBeninE164.replaceFirst("01", "");
|
||||
|
||||
return List.of(
|
||||
Arguments.of(usE164, List.of(usE164)),
|
||||
Arguments.of(newFormatBeninE164, List.of(newFormatBeninE164, oldFormatBeninE164)),
|
||||
Arguments.of(oldFormatBeninE164, List.of(oldFormatBeninE164, newFormatBeninE164))
|
||||
Arguments.of(NEW_FORMAT_BENIN_E164_STRING, List.of(NEW_FORMAT_BENIN_E164_STRING, OLD_FORMAT_BENIN_E164_STRING)),
|
||||
Arguments.of(OLD_FORMAT_BENIN_E164_STRING, List.of(OLD_FORMAT_BENIN_E164_STRING, NEW_FORMAT_BENIN_E164_STRING))
|
||||
);
|
||||
}
|
||||
|
||||
@@ -66,4 +69,43 @@ class UtilTest {
|
||||
void startsWithDecimal(final long number, final long prefix, final boolean expectStartsWithPrefix) {
|
||||
assertEquals(expectStartsWithPrefix, Util.startsWithDecimal(number, prefix));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void isOldFormatBeninPhoneNumber4(final Phonenumber.PhoneNumber beninNumber, final boolean isOldFormatBeninNumber) {
|
||||
if (isOldFormatBeninNumber) {
|
||||
assertTrue(Util.isOldFormatBeninPhoneNumber(beninNumber));
|
||||
} else {
|
||||
assertFalse(Util.isOldFormatBeninPhoneNumber(beninNumber));
|
||||
}
|
||||
}
|
||||
|
||||
private static Stream<Arguments> isOldFormatBeninPhoneNumber4() throws NumberParseException {
|
||||
final Phonenumber.PhoneNumber oldFormatBeninE164 = PhoneNumberUtil.getInstance().parse(OLD_FORMAT_BENIN_E164_STRING, null);
|
||||
final Phonenumber.PhoneNumber newFormatBeninE164 = PhoneNumberUtil.getInstance().parse(NEW_FORMAT_BENIN_E164_STRING, null);
|
||||
|
||||
return Stream.of(
|
||||
Arguments.of(oldFormatBeninE164, true),
|
||||
Arguments.of(newFormatBeninE164, false),
|
||||
Arguments.of(PhoneNumberUtil.getInstance().getExampleNumber("US"), false)
|
||||
);
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource
|
||||
void normalizeBeninPhoneNumber(final Phonenumber.PhoneNumber beninNumber, final Phonenumber.PhoneNumber expectedBeninNumber)
|
||||
throws NumberParseException {
|
||||
assertTrue(expectedBeninNumber.exactlySameAs(Util.canonicalizePhoneNumber(beninNumber)));
|
||||
}
|
||||
|
||||
private static Stream<Arguments> normalizeBeninPhoneNumber() throws NumberParseException {
|
||||
final Phonenumber.PhoneNumber oldFormatBeninPhoneNumber = PhoneNumberUtil.getInstance().parse(OLD_FORMAT_BENIN_E164_STRING, null);
|
||||
final Phonenumber.PhoneNumber newFormatBeninPhoneNumber = PhoneNumberUtil.getInstance().parse(NEW_FORMAT_BENIN_E164_STRING, null);
|
||||
final Phonenumber.PhoneNumber usPhoneNumber = PhoneNumberUtil.getInstance().getExampleNumber("US");
|
||||
return Stream.of(
|
||||
Arguments.of(newFormatBeninPhoneNumber, newFormatBeninPhoneNumber),
|
||||
Arguments.of(oldFormatBeninPhoneNumber, newFormatBeninPhoneNumber),
|
||||
Arguments.of(usPhoneNumber, usPhoneNumber)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user