package com.braintreeocc.converters.populators;

import com.braintree.hybris.data.ws.SrcAddressWsDTO;
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.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 BraintreeSrcAddressPopulatorTest {

    public static final String REGION = "Region";
    private static final String COUNTY_CODE = "countyCode";
    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 PHONE = "phone";
    private static final String STREET_ADDRESS = "Street Address";

    @Mock
    SrcAddressWsDTO srcAddressWsDTO;
    @Mock
    private BraintreePayPalAddressDetailValidator payPalAddressDetailValidator;

    @InjectMocks
    BraintreeSrcAddressPopulator unit;

    @Before
    public void setUp() {
        when(srcAddressWsDTO.getCountryCode()).thenReturn(COUNTY_CODE);
        when(srcAddressWsDTO.getRegion()).thenReturn(REGION);
        when(srcAddressWsDTO.getPhoneNumber()).thenReturn(PHONE);
        when(srcAddressWsDTO.getStreetAddress()).thenReturn(STREET_ADDRESS);
        when(srcAddressWsDTO.getPostalCode()).thenReturn(POSTAL_CODE);
        when(srcAddressWsDTO.getLocality()).thenReturn(LOCALITY);
        when(srcAddressWsDTO.getFirstName()).thenReturn(FIRST_NAME);
        when(srcAddressWsDTO.getLastName()).thenReturn(LAST_NAME);
    }

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

        unit.populate(srcAddressWsDTO, addressData);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, REGION);
        assertEquals(STREET_ADDRESS, addressData.getLine1());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(LOCALITY, addressData.getTown());
        assertEquals(COUNTY_CODE, addressData.getCountry().getIsocode());
        assertEquals(COUNTY_CODE + "-" + REGION, addressData.getRegion().getIsocode());
        assertEquals(FIRST_NAME, addressData.getFirstName());
        assertEquals(LAST_NAME, addressData.getLastName());
        assertEquals(PHONE, addressData.getPhone());
    }

    @Test
    public void populateShouldSetRequiredFieldsWithInvalidCountryAndRegion() {
        AddressData addressData = new AddressData();
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(false);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, REGION)).thenReturn(false);

        unit.populate(srcAddressWsDTO, addressData);

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

}
