package com.braintreeocc.converters;

import com.braintree.hybris.data.ws.PayPalAddressWsDTO;
import com.braintree.paypal.validator.BraintreePayPalAddressDetailValidator;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commercefacades.user.data.AddressData;
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.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 BraintreePayPalAddressConverterForOCCTest {

    private static final String COUNTY_CODE = "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 FIRST_NAME = "Some";
    private static final String LAST_NAME = "Tester";
    private static final String STATE = "State";
    private static final String SOME_TESTER = "Some Tester";
    private static final String CITY = "City";

    @Mock
    private BraintreePayPalAddressDetailValidator payPalAddressDetailValidator;
    @Mock
    private PayPalAddressWsDTO payPalAddressWsDTO;

    @InjectMocks
    BraintreePayPalAddressConverterForOCC unit;

    @Before
    public void setUp() {
        payPalAddressWsDTO = Mockito.mock(PayPalAddressWsDTO.class);
        when(payPalAddressWsDTO.getCountryCode()).thenReturn(COUNTY_CODE);
        when(payPalAddressWsDTO.getState()).thenReturn(STATE);
        when(payPalAddressWsDTO.getLine1()).thenReturn(LINE_1);
        when(payPalAddressWsDTO.getLine2()).thenReturn(LINE_2);
        when(payPalAddressWsDTO.getCity()).thenReturn(CITY);
        when(payPalAddressWsDTO.getPostalCode()).thenReturn(POSTAL_CODE);
    }

    @Test
    public void convertAllReqRequiredFields(){
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(true);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, STATE)).thenReturn(true);
        when(payPalAddressWsDTO.getRecipientName()).thenReturn(SOME_TESTER);

        AddressData addressData = unit.convert(payPalAddressWsDTO);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, STATE);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(CITY, addressData.getTown());
        assertEquals(COUNTY_CODE, addressData.getCountry().getIsocode());
        assertEquals(COUNTY_CODE + "-" + STATE, addressData.getRegion().getIsocode());
        assertEquals(FIRST_NAME, addressData.getFirstName());
        assertEquals(LAST_NAME, addressData.getLastName());
    }

    @Test
    public void convertAllReqRequiredFieldsWithEmptyRecipientName(){
        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(false);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, STATE)).thenReturn(false);
        when(payPalAddressWsDTO.getRecipientName()).thenReturn(StringUtils.EMPTY);

        AddressData addressData = unit.convert(payPalAddressWsDTO);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, STATE);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(CITY, addressData.getTown());
        assertNull(addressData.getCountry());
        assertNull(addressData.getRegion());
        assertEquals(StringUtils.EMPTY, addressData.getFirstName());
        assertEquals(StringUtils.EMPTY, addressData.getLastName());
    }

    @Test
    public void convertAllReqRequiredFieldsWithInvalidCountryAndState(){

        when(payPalAddressDetailValidator.validatePayPalCountryCode(COUNTY_CODE)).thenReturn(true);
        when(payPalAddressDetailValidator.validatePayPalRegionCode(COUNTY_CODE, STATE)).thenReturn(true);
        when(payPalAddressWsDTO.getRecipientName()).thenReturn(FIRST_NAME);

        AddressData addressData = unit.convert(payPalAddressWsDTO);

        verify(payPalAddressDetailValidator).validatePayPalCountryCode(COUNTY_CODE);
        verify(payPalAddressDetailValidator).validatePayPalRegionCode(COUNTY_CODE, STATE);
        assertEquals(LINE_1, addressData.getLine1());
        assertEquals(LINE_2, addressData.getLine2());
        assertEquals(POSTAL_CODE, addressData.getPostalCode());
        assertEquals(CITY, addressData.getTown());
        assertEquals(COUNTY_CODE, addressData.getCountry().getIsocode());
        assertEquals(COUNTY_CODE + "-" + STATE, addressData.getRegion().getIsocode());
        assertEquals(FIRST_NAME, addressData.getFirstName());
        assertEquals(FIRST_NAME, addressData.getFirstName());
        assertEquals(StringUtils.EMPTY, addressData.getLastName());
    }

}
