package com.braintree.helpers;

import com.braintree.configuration.service.BrainTreeConfigService;
import com.braintree.constants.BraintreeConstants;
import com.braintree.enums.BrainTreePaymentMethod;
import de.hybris.bootstrap.annotations.UnitTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;

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

    @Mock
    private BrainTreeConfigService brainTreeConfigService;

    @Spy
    @InjectMocks
    private SubmitForSettlementHelper unit;

    @Test
    public void shouldReturnFalseForAllConditionsForIsAvailableSubmitForSettlement() {
        final String paymentType = BrainTreePaymentMethod.PAYPAL.getCode();

        doReturn(false).when(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        doReturn(false).when(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);

        assertFalse(unit.isAvailableSubmitForSettlement(paymentType));

        verify(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        verify(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);
    }

    @Test
    public void shouldReturnTrueForFirstConditionForIsAvailableSubmitForSettlement() {
        final String paymentType = BrainTreePaymentMethod.PAYPAL.getCode();

        doReturn(true).when(unit).isAvailableSubmitForSettlementByIntent(paymentType);

        assertTrue(unit.isAvailableSubmitForSettlement(paymentType));

        verify(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        verify(unit, never()).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);
    }

    @Test
    public void shouldReturnTrueForSecondConditionForIsAvailableSubmitForSettlement() {
        final String paymentType = BrainTreePaymentMethod.PAYPAL.getCode();

        doReturn(false).when(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        doReturn(true).when(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);

        assertTrue(unit.isAvailableSubmitForSettlement(paymentType));

        verify(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        verify(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);
    }

    @Test
    public void shouldReturnTrueForThirdConditionForIsAvailableSubmitForSettlement() {
        final String paymentType = BrainTreePaymentMethod.USBANKACCOUNT.getCode();

        doReturn(false).when(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        doReturn(false).when(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);

        assertTrue(unit.isAvailableSubmitForSettlement(paymentType));

        verify(unit).isAvailableSubmitForSettlementByIntent(paymentType);
        verify(unit).isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType);
    }

    @Test
    public void shouldReturnFalseForAllConditionsForIsAvailableSubmitForSettlementByIntent() {
        final String paymentType = BrainTreePaymentMethod.USBANKACCOUNT.getCode();

        assertFalse(unit.isAvailableSubmitForSettlementByIntent(paymentType));

        verify(brainTreeConfigService, never()).getIntent();
    }

    @Test
    public void shouldReturnTrueForFirstConditionForIsAvailableSubmitForSettlementByIntent() {
        final String paymentType = BrainTreePaymentMethod.PAYPALACCOUNT.getCode();
        final String intent = BraintreeConstants.PayPalIntent.PAYPAL_INTENT_AUTHORIZE;

        when(brainTreeConfigService.getIntent()).thenReturn(intent);

        assertFalse(unit.isAvailableSubmitForSettlementByIntent(paymentType));
        verify(brainTreeConfigService).getIntent();
    }

    @Test
    public void shouldReturnTrueForAllConditionsForIsAvailableSubmitForSettlementByIntent() {
        final String paymentType = BrainTreePaymentMethod.PAYPAL.getCode();
        final String intent = BraintreeConstants.PayPalIntent.PAYPAL_INTENT_SALE;

        when(brainTreeConfigService.getIntent()).thenReturn(intent);

        assertTrue(unit.isAvailableSubmitForSettlementByIntent(paymentType));
        verify(brainTreeConfigService).getIntent();
    }

    @Test
    public void shouldReturnFalseForAllConditionsForIsAvailableSubmitForSettlementBySettlementConfigParameter() {
        final String paymentType = BrainTreePaymentMethod.PAYPAL.getCode();

        assertFalse(unit.isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType));

        verify(brainTreeConfigService, never()).isSubmitForSettlement();
    }

    @Test
    public void shouldReturnTrueForFirstConditionForIsAvailableSubmitForSettlementBySettlementConfigParameter() {
        final String paymentType = BrainTreePaymentMethod.USBANKACCOUNT.getCode();

        when(brainTreeConfigService.isSubmitForSettlement()).thenReturn(false);

        assertFalse(unit.isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType));

        verify(brainTreeConfigService).isSubmitForSettlement();
    }

    @Test
    public void shouldReturnTrueForAllConditionsForIsAvailableSubmitForSettlementBySettlementConfigParameter() {
        final String paymentType = BrainTreePaymentMethod.USBANKACCOUNT.getCode();

        when(brainTreeConfigService.isSubmitForSettlement()).thenReturn(true);

        assertTrue(unit.isAvailableSubmitForSettlementBySettlementConfigParameter(paymentType));

        verify(brainTreeConfigService).isSubmitForSettlement();
    }

}
