package com.braintree.populators;

import com.braintree.hybris.data.graphql.commands.request.BrainTreeAddressInput;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.payment.dto.BillingInfo;
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.mockito.Mockito.when;

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

    private static final String FIRST_NAME = "User";
    private static final String LAST_NAME = "Tester";
    private static final String COUNTRY = "Country";
    private static final String STATE = "State";
    private static final String STREET_1 = "Street1";
    private static final String STREET_2 = "Street2";
    private static final String CITY = "City";
    private static final String POSTAL_CODE = "PostalCode";
    private static final String REGION = "Region";

    @InjectMocks
    private BrainTreeBillingInfoToGraphQLAddressInputPopulator unit;

    @Mock
    private BillingInfo billingAddress;

    @Before
    public void setUp() {
        when(billingAddress.getCountry()).thenReturn(COUNTRY);
        when(billingAddress.getFirstName()).thenReturn(FIRST_NAME);
        when(billingAddress.getLastName()).thenReturn(LAST_NAME);
        when(billingAddress.getStreet1()).thenReturn(STREET_1);
        when(billingAddress.getStreet2()).thenReturn(STREET_2);
        when(billingAddress.getCity()).thenReturn(CITY);
        when(billingAddress.getPostalCode()).thenReturn(POSTAL_CODE);
    }

    @Test
    public void shouldPopulateBillingInfoToBrainTreeAddressInputTest() {
        BrainTreeAddressInput target = new BrainTreeAddressInput();
        when(billingAddress.getRegion()).thenReturn(REGION);
        unit.populate(billingAddress, target);
        assertEquals(REGION, target.getRegion());
        verifyAddress(target);
    }

    @Test
    public void shouldPopulateBillingInfoToBrainTreeAddressInputWithStateTest() {
        BrainTreeAddressInput target = new BrainTreeAddressInput();
        when(billingAddress.getState()).thenReturn(STATE);
        unit.populate(billingAddress, target);
        assertEquals(STATE, target.getRegion());
        verifyAddress(target);
    }

    private void verifyAddress(BrainTreeAddressInput target) {
        assertEquals(COUNTRY, target.getCountryCode());
        assertEquals(FIRST_NAME, target.getFirstName());
        assertEquals(LAST_NAME, target.getLastName());
        assertEquals(STREET_1, target.getStreetAddress());
        assertEquals(STREET_2, target.getExtendedAddress());
        assertEquals(CITY, target.getLocality());
        assertEquals(POSTAL_CODE, target.getPostalCode());
    }
}