package com.braintree.converters;

import com.braintree.hybris.data.UsBankAccountAddressData;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commercefacades.user.data.AddressData;
import de.hybris.platform.commercefacades.user.data.CountryData;
import de.hybris.platform.commercefacades.user.data.RegionData;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;

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

    private static final String BUSINESS_NAME = "businessName";
    private static final String LINE_1 = "line1";
    private static final String LINE_2 = "line2";
    private static final String CITY = "city";
    private static final String POSTAL_CODE = "postalCode";
    private static final String COUNTRY_CODE = "countryCode";
    private static final String STATE = "state";
    private static final String FIRST_NAME = "firstName";
    private static final String LAST_NAME = "lastName";

    @InjectMocks
    @Spy
    private BrainTreeUsBankAccountAddressDataConverter unit;

    @Test
    public void shouldConvertUsBankAccountAddressDataToAddressData() {
        UsBankAccountAddressData data = new UsBankAccountAddressData();

        data.setBusinessName(BUSINESS_NAME);
        data.setLine1(LINE_1);
        data.setLine2(LINE_2);
        data.setCity(CITY);
        data.setPostalCode(POSTAL_CODE);
        data.setCountryCode(COUNTRY_CODE);
        data.setState(STATE);

        AddressData result = unit.convert(data);
        CountryData resultCountry = result.getCountry();
        RegionData resultRegion = result.getRegion();

        verify(unit).convert(eq(data), any(AddressData.class));

        assertEquals(BUSINESS_NAME, result.getFirstName());
        assertEquals(LINE_1, result.getLine1());
        assertEquals(LINE_2, result.getLine2());
        assertEquals(CITY, result.getTown());
        assertEquals(POSTAL_CODE, result.getPostalCode());
        assertEquals(COUNTRY_CODE, resultCountry.getIsocode());
        assertEquals(String.format("%s-%s", COUNTRY_CODE, STATE), resultRegion.getIsocode());
    }

    @Test
    public void shouldConvertUsBankAccountAddressDataToAddressDataWithFirsAndLastName() {
        UsBankAccountAddressData data = new UsBankAccountAddressData();

        data.setFirstName(FIRST_NAME);
        data.setLastName(LAST_NAME);

        AddressData result = unit.convert(data);

        verify(unit).convert(eq(data), any(AddressData.class));

        assertEquals(FIRST_NAME, result.getFirstName());
        assertEquals(LAST_NAME, result.getLastName());
    }

}