package com.braintree.converters;

import com.braintree.enums.BrainTreeCardType;
import com.braintree.hybris.data.BrainTreePaymentInfoData;
import com.braintree.model.BrainTreePaymentInfoModel;
import de.hybris.bootstrap.annotations.UnitTest;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String PAYMENT_PROVIDER = "paymentProvider";
    private static final String FUNDING_SOURCE = "fundingSource";
    private static final String CARD_NUMBER = "cardNumber";
    private static final String PAYER = "payer";
    private static final String MONTH = "01";
    private static final String YEAR = "2024";
    private static final String IMAGE_SOURCE = "imageSource";
    private static final String CARD_HOLDER_NAME = "cardHolderName";
    private static final String VISA = "Visa";
    private static final String LAST_4 = "last4";
    @Mock
    private BrainTreePaymentInfoModel model;

    @InjectMocks
    @Spy
    private BrainTreePaymentInfoDataConverter unit;

    @Test
    public void shouldConvertBrainTreePaymentInfoModelToData() {
        BrainTreePaymentInfoData paymentData = new BrainTreePaymentInfoData();

        when(model.getPaymentProvider()).thenReturn(PAYMENT_PROVIDER);
        when(model.getPaypalFundingSource()).thenReturn(FUNDING_SOURCE);
        when(model.getCardNumber()).thenReturn(CARD_NUMBER);
        when(model.getPayer()).thenReturn(PAYER);
        when(model.getExpirationMonth()).thenReturn(MONTH);
        when(model.getExpirationYear()).thenReturn(YEAR);
        when(model.getImageSource()).thenReturn(IMAGE_SOURCE);

        BrainTreePaymentInfoData result = unit.convert(model, paymentData);

        assertEquals(paymentData, result);
        assertEquals(PAYMENT_PROVIDER, result.getPaymentType());
        assertEquals(FUNDING_SOURCE, result.getPaypalFundingSource());
        assertEquals(CARD_NUMBER, result.getCardNumber());
        assertEquals(PAYER, result.getEmail());
        assertEquals(MONTH, result.getExpirationMonth());
        assertEquals(YEAR, result.getExpirationYear());
        assertEquals(IMAGE_SOURCE, result.getImageSource());
    }

    @Test
    public void shouldSetCardHolderName() {
        BrainTreePaymentInfoData paymentData = new BrainTreePaymentInfoData();

        when(model.getCardholderName()).thenReturn(CARD_HOLDER_NAME);

        BrainTreePaymentInfoData result = unit.convert(model, paymentData);

        assertEquals(paymentData, result);
        assertEquals(CARD_HOLDER_NAME, result.getCardholderName());
    }

    @Test
    public void shouldSetCardType() {
        BrainTreePaymentInfoData paymentData = new BrainTreePaymentInfoData();

        when(model.getCardType()).thenReturn(BrainTreeCardType.VISA);

        BrainTreePaymentInfoData result = unit.convert(model, paymentData);

        assertEquals(paymentData, result);
        assertEquals(VISA, result.getCardType());
    }

    @Test
    public void shouldSetLast4() {
        BrainTreePaymentInfoData paymentData = new BrainTreePaymentInfoData();

        when(model.getLast4()).thenReturn(LAST_4);

        BrainTreePaymentInfoData result = unit.convert(model, paymentData);

        assertEquals(paymentData, result);
        assertEquals(LAST_4, result.getLast4());
    }

    @Test
    public void shouldCreateNewDataObject() {
        BrainTreePaymentInfoData paymentData = new BrainTreePaymentInfoData();

        BrainTreePaymentInfoData result = unit.convert(model);

        verify(unit).convert(eq(model), any(BrainTreePaymentInfoData.class));
    }
}
