
package com.braintreeocc.converters.populators;

import com.braintree.hybris.data.ws.GooglePayAddressWsDTO;
import com.braintree.paypal.validator.BraintreePayPalAddressDetailValidator;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commercefacades.user.data.AddressData;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BraintreeGooglePayAddressPopulatorTest {

    private static final String COUNTY_CODE = "countyCode";
    private static final String ADMINISTRATIVE_AREA = "countyCode";
    private static final String LINE_1 = "line1";
    private static final String LINE_2 = "line2";
    private static final String POSTAL_CODE = "postalCOde";
    private static final String LOCALITY = "Locality";
    private static final String FIRST_NAME = "Some";
    private static final String LAST_NAME = "Tester";
    private static final String FULL_NAME = "Tester Some";
    private static final String PHONE = "phone";

    @Mock
    private BraintreePayPalAddressDetailValidator payPalAddressDetailValidator;

    @InjectMocks
    private BraintreeGooglePayAddressPopulator unit;

    @Mock
    private GooglePayAddressWsDTO googlePayAddressWsDTO;

    @Before
    public void setUp() {

        googlePayAddressWsDTO = Mockito.mock(GooglePayAddressWsDTO.class);
        when(googlePayAddressWsDTO.getCountryCode()).thenReturn(COUNTY_CODE);
        when(googlePayAddressWsDTO.getAdministrativeArea()).thenReturn(ADMINISTRATIVE_AREA);
        when(googlePayAddressWsDTO.getPhoneNumber()).thenReturn(PHONE);
        when(googlePayAddressWsDTO.getAddress1()).thenReturn(LINE_1);
        when(googlePayAddressWsDTO.getAddress2()).thenReturn(LINE_2);
        when(googlePayAddressWsDTO.getPostalCode()).thenReturn(POSTAL_CODE);
        when(googlePayAddressWsDTO.getLocality()).thenReturn(LOCALITY);
    }

    @Test
    public void populateShouldSetRequiredFields() {
        AddressData addressData = new AddressData();
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(true);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA)).thenReturn(true);
        when(googlePayAddressWsDTO.getName()).thenReturn(LAST_NAME);

        unit.populate(googlePayAddressWsDTO, addressData);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(LOCALITY, addressData.getTown());
        assertEquals(COUNTY_CODE, addressData.getCountry().getIsocode());
        assertEquals(COUNTY_CODE + "-" + ADMINISTRATIVE_AREA, addressData.getRegion().getIsocode());
        assertEquals(LAST_NAME, addressData.getLastName());
        assertNull(addressData.getFirstName());
        assertEquals(PHONE, addressData.getPhone());
    }

    @Test
    public void populateShouldSetRequiredFieldsWithInvalidCountryAndAdminArea() {
        AddressData addressData = new AddressData();
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(false);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA)).thenReturn(false);
        when(googlePayAddressWsDTO.getName()).thenReturn(FULL_NAME);

        unit.populate(googlePayAddressWsDTO, addressData);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(LOCALITY, addressData.getTown());
        assertNull(addressData.getCountry());
        assertNull(addressData.getRegion());
        assertEquals(FIRST_NAME, addressData.getFirstName());
        assertEquals(LAST_NAME, addressData.getLastName());
        assertEquals(PHONE, addressData.getPhone());
    }

    @Test
    public void populateShouldSetRequiredFieldsWithEmptyName() {
        AddressData addressData = new AddressData();
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(true);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA)).thenReturn(true);
        when(googlePayAddressWsDTO.getName()).thenReturn(null);

        unit.populate(googlePayAddressWsDTO, addressData);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, ADMINISTRATIVE_AREA);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(LOCALITY, addressData.getTown());
        assertEquals(COUNTY_CODE, addressData.getCountry().getIsocode());
        assertEquals(COUNTY_CODE + "-" + ADMINISTRATIVE_AREA, addressData.getRegion().getIsocode());
        assertNull(addressData.getFirstName());
        assertNull(addressData.getLastName());
        assertEquals(PHONE, addressData.getPhone());
    }
}
