package net.contargo.types.telephony;

import net.contargo.types.telephony.formatting.PhoneNumberFormattingException;

import org.junit.Test;

import org.junit.runner.RunWith;

import org.mockito.runners.MockitoJUnitRunner;

import java.util.Optional;

import static org.junit.Assert.*;


/**
 * @author  Julia Dasch - dasch@synyx.de
 */
@RunWith(MockitoJUnitRunner.class)
public class PhoneNumberTest {

    @Test
    public void ensureInternationalFormattingPhoneNumberFromValid() {

        final PhoneNumber phoneNumber = new PhoneNumber("00492342323523423");

        Optional<String> formattedPhoneNumber = phoneNumber.getInternationalFormatOfPhoneNumber();

        assertTrue(formattedPhoneNumber.isPresent());
        assertEquals("+49 234 2323 5234 23", formattedPhoneNumber.get());
    }


    @Test
    public void ensureInternationalFormattingPhoneNumberFromInvalidInputThrows()
        throws PhoneNumberFormattingException {

        final PhoneNumber phoneNumber = new PhoneNumber("i321%6&_-?`");
        assertFalse(phoneNumber.getInternationalFormatOfPhoneNumber().isPresent());
    }


    @Test
    public void ensureFormatsRussianMobile() {

        final PhoneNumber phoneNumber = new PhoneNumber("+7 (922) 555-1234");

        assertEquals("+7 922 5551 234", phoneNumber.getInternationalFormatOfPhoneNumber().get());
    }


    @Test
    public void ensureFormatsLongGermanMobileNumber() {

        final PhoneNumber phoneNumber = new PhoneNumber("0171123456789123");

        assertEquals("+49 171 1234 5678 9123", phoneNumber.getInternationalFormatOfPhoneNumber().get());
    }


    @Test
    public void ensureGettingCorrectCountryForPhoneNumber() throws PhoneNumberFormattingException {

        final PhoneNumber phoneNumber = new PhoneNumber("00124278364736");

        assertEquals("+1", phoneNumber.getCountry().getCountryCallingCode());
        assertEquals("BS", phoneNumber.getCountry().getCountryCode());
    }


    @Test
    public void ensureGettingCorrectRegionCodeForPhoneNumber() throws PhoneNumberFormattingException {

        final PhoneNumber phoneNumber = new PhoneNumber("00124278364736");

        assertEquals("24278364736", phoneNumber.getNationalSignificantNumber());
    }


    @Test
    public void ensureGermanNumberToInternationalPhoneNumber() {

        final PhoneNumber phoneNumber = new PhoneNumber("02342323523423");

        assertEquals("+49 234 2323 5234 23", phoneNumber.getInternationalFormatOfPhoneNumber().get());
    }


    @Test
    public void ensureGettingCorrectNumberWithPhoneExtension() {

        final PhoneNumber phoneNumber = new PhoneNumber("02342323523423");
        phoneNumber.setPhoneExtension("56");

        assertEquals("+49 234 2323 5234 2356", phoneNumber.getInternationalFormatOfPhoneNumber().get());
    }
}

@@ -880,62 +880,10 @@
 nput
-Throws()%0A        throws PhoneNumberFormattingException
+()
  %7B%0A%0A
@@ -990,55 +990,21 @@
 ber.
-getInternationalFormatOfPhoneNumber().isPresent
+isPhoneNumber
 ());
@@ -1845,24 +1845,123 @@
 tryCode());%0A
+        assertEquals(%22+1 242 7836 4736%22, phoneNumber.getInternationalFormatOfPhoneNumber().get());%0A
     %7D%0A%0A%0A    
@@ -2691,24 +2691,24 @@
 ion(%2256%22);%0A%0A
-
         asse
@@ -2806,10 +2806,887 @@
 ;%0A    %7D%0A
+%0A%0A    @Test%0A    public void ensureGettingCorrectFormattedGermanNumber() %7B%0A%0A        final PhoneNumber phoneNumber = new PhoneNumber(%22072183478374%22);%0A%0A        assertEquals(%22+49 721 8347 8374%22, phoneNumber.getInternationalFormatOfPhoneNumber().get());%0A    %7D%0A%0A%0A    @Test%0A    public void ensureGetFormattingPhoneNumberFromMixedPhoneNumber() %7B%0A%0A        /*%0A         * currently phoneNumber lib converts mixed numbers to correct phoneNumbers%0A         * falsehoods of phone numbers says that 'numbers' can contain letters%0A         * if there are too many problems think about excluding letters in a custom contargo phone number validator%0A         */%0A        final PhoneNumber phoneNumber = new PhoneNumber(%22a234svljshdf034%22);%0A%0A        assertEquals(%22+49 234 7585 7433 034%22, phoneNumber.getInternationalFormatOfPhoneNumber().get());%0A        assertTrue(phoneNumber.isPhoneNumber());%0A    %7D%0A
 %7D%0A

add more tests for phone numbers