package com.braintree.customersupportbackoffice.services.impl;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.when;

import com.braintree.constants.BraintreeConstants;
import com.braintree.converters.utils.BraintreeTransactionConverterUtils;
import com.braintree.model.BrainTreeTransactionDetailModel;
import com.braintreegateway.Transaction;
import de.hybris.bootstrap.annotations.UnitTest;
import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

@UnitTest
@RunWith(MockitoJUnitRunner.class)
public class BrainTreeTransactionManagementActionServiceImplTest {
    @InjectMocks
    private BrainTreeTransactionManagementActionServiceImpl unit;
    @Mock
    private BrainTreeTransactionDetailModel transaction;

    @Test
    public void shouldIsVoidPossibleWhenStatusIsAuthorizedReturnTrue() {
        when(transaction.getStatus()).thenReturn(Transaction.Status.AUTHORIZED.toString());
        assertTrue(unit.isVoidPossible(transaction));
    }

    @Test
    public void shouldIsVoidPossibleWhenStatusIsNotAuthorizedReturnFalse() {
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        assertFalse(unit.isVoidPossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusAuthorizedReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.AUTHORIZED.toString());
        assertTrue(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusAuthorizationExpiredReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.AUTHORIZATION_EXPIRED.toString());
        assertTrue(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusVoidedReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.VOIDED.toString());
        assertTrue(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusSettledReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        assertTrue(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusSubmittedForSettlementReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SUBMITTED_FOR_SETTLEMENT.toString());
        assertTrue(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsClonePossibleWhenCreditCardPaymentTypeAndStatusFailedShouldReturnFalse() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.FAILED.toString());
        assertFalse(unit.isClonePossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeAndStatusIsSettledAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeAndStatusIsSettledAndRefundExistsReturnFalse() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(BraintreeTransactionConverterUtils.TRANSACTION_REFUND_SIGN);
        assertFalse(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeSettledAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeAndStatusSettlingAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLING.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeAndStatusSettlementPendingAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLEMENT_PENDING.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenPayPalPaymentTypeAndStatusSettledAndRefundExistsReturnFalse() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.PAYPAL_PAYMENT_TYPE_NAME);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(BraintreeTransactionConverterUtils.TRANSACTION_REFUND_SIGN);
        assertFalse(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenCreditCardPaymentTypeAndStatusSettledAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenCreditCardPaymentTypeAndStatusSettlingAndNoRefundReturnTrue() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLING.toString());
        when(transaction.getRefund()).thenReturn(StringUtils.EMPTY);
        assertTrue(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsRefundPossibleWhenCreditCardPaymentTypeAndStatusSettledAndRefundExistsReturnFalse() {
        when(transaction.getPaymentType()).thenReturn(BraintreeConstants.CREDIT_CARD_PAYMENT_TYPE);
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        when(transaction.getRefund()).thenReturn(BraintreeTransactionConverterUtils.TRANSACTION_REFUND_SIGN);
        assertFalse(unit.isRefundPossible(transaction));
    }

    @Test
    public void shouldIsSubmitForSettlementPossibleWhenStatusIsAuthorizedReturnTrue() {
        when(transaction.getStatus()).thenReturn(Transaction.Status.AUTHORIZED.toString());
        assertTrue(unit.isSubmitForSettlementPossible(transaction));
    }

    @Test
    public void shouldIsSubmitForSettlementPossibleWhenStatusIsNotAuthorizedReturnFalse() {
        when(transaction.getStatus()).thenReturn(Transaction.Status.SETTLED.toString());
        assertFalse(unit.isSubmitForSettlementPossible(transaction));
    }

}


