package com.braintree.populators;

import com.braintree.command.result.BrainTreeBillingAddressResult;
import com.braintree.hybris.data.graphql.commands.response.BrainTreeAddress;
import de.hybris.bootstrap.annotations.UnitTest;
import org.apache.commons.lang.StringUtils;
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.when;

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

    private static final String COMPANY = "Company";
    private static final String COUNTRY_CODE = "CountryCode";
    private static final String ADDRESS_LINE_1 = "AddressLine1";
    private static final String ADMIN_AREA_1 = "AdminArea1";
    private static final String ADMIN_AREA_2 = "AdminArea2";
    private static final String POSTAL_CODE = "PostalCode";
    private static final String PHONE_NUMBER = "+888888888";
    private static final String ADDRESS_LINE_2 = "AddressLine2";
    private static final String FIRS_NAME = "User";
    private static final String LAST_NAME = "Tester";
    private static final String FULL_NAME_WITH_SPACE = String.format("%s %s", FIRS_NAME, LAST_NAME);

    @InjectMocks
    private BrainTreeGraphQLAddressToAddressResultPopulator unit;

    @Mock
    BrainTreeAddress brainTreeAddress;

    @Before
    public void setUp() {
        when(brainTreeAddress.getCompany()).thenReturn(COMPANY);
        when(brainTreeAddress.getCountryCode()).thenReturn(COUNTRY_CODE);
        when(brainTreeAddress.getAddressLine1()).thenReturn(ADDRESS_LINE_1);
        when(brainTreeAddress.getAdminArea1()).thenReturn(ADMIN_AREA_1);
        when(brainTreeAddress.getAdminArea2()).thenReturn(ADMIN_AREA_2);
        when(brainTreeAddress.getPostalCode()).thenReturn(POSTAL_CODE);
        when(brainTreeAddress.getPhoneNumber()).thenReturn(PHONE_NUMBER);
    }

    @Test
    public void shouldPopulateBrainTreeGraphQLAddressToAddressResultTest() {
        BrainTreeBillingAddressResult target = new BrainTreeBillingAddressResult();

        when(brainTreeAddress.getAddressLine2()).thenReturn(ADDRESS_LINE_2);
        when(brainTreeAddress.getFullName()).thenReturn(FULL_NAME_WITH_SPACE);

        unit.populate(brainTreeAddress, target);

        assertEquals(COMPANY, target.getCompany());
        assertEquals(COUNTRY_CODE, target.getCountyCode());
        assertEquals(ADDRESS_LINE_1, target.getStreetAddress());
        assertEquals(ADDRESS_LINE_2, target.getExtendedAddress());
        assertEquals(ADMIN_AREA_1, target.getRegion());
        assertEquals(ADMIN_AREA_2, target.getLocality());
        assertEquals(POSTAL_CODE, target.getPostalCode());
        assertEquals(PHONE_NUMBER, target.getPhone());
        assertEquals(FIRS_NAME, target.getFirstName());
        assertEquals(LAST_NAME, target.getLastName());
    }

    @Test
    public void shouldPopulateBrainTreeGraphQLAddressToAddressResultWithoutAddressLine2Test() {
        BrainTreeBillingAddressResult target = new BrainTreeBillingAddressResult();

        when(brainTreeAddress.getFullName()).thenReturn(LAST_NAME);

        unit.populate(brainTreeAddress, target);

        assertEquals(COMPANY, target.getCompany());
        assertEquals(COUNTRY_CODE, target.getCountyCode());
        assertEquals(ADDRESS_LINE_1, target.getStreetAddress());
        assertEquals(StringUtils.EMPTY, target.getExtendedAddress());
        assertEquals(ADMIN_AREA_1, target.getRegion());
        assertEquals(ADMIN_AREA_2, target.getLocality());
        assertEquals(POSTAL_CODE, target.getPostalCode());
        assertEquals(PHONE_NUMBER, target.getPhone());
        assertEquals(LAST_NAME, target.getFirstName());
        assertNull(target.getLastName());
    }
}