package com.braintree.action;

import com.braintree.model.BrainTreePaymentInfoModel;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.b2b.process.approval.model.B2BApprovalProcessModel;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.order.payment.CreditCardPaymentInfoModel;
import de.hybris.platform.processengine.action.AbstractSimpleDecisionAction;
import de.hybris.platform.servicelayer.model.ModelService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static de.hybris.platform.b2bacceleratorservices.enums.CheckoutPaymentType.ACCOUNT;
import static de.hybris.platform.b2bacceleratorservices.enums.CheckoutPaymentType.CARD;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Mock
    protected ModelService modelService;

    @InjectMocks
    private BraintreeCheckCreditCartOrderAction unit;

    @Mock
    private B2BApprovalProcessModel process;
    @Mock
    private OrderModel order;
    @Mock
    private CreditCardPaymentInfoModel creditCardPaymentInfo;
    @Mock
    private BrainTreePaymentInfoModel brainTreePaymentInfo;

    @Test
    public void shouldReturnOkForCardPaymentTypeAndCreditCardPaymentInfo() {
        when(process.getOrder()).thenReturn(order);
        when(order.getPaymentInfo()).thenReturn(creditCardPaymentInfo);
        when(order.getPaymentType()).thenReturn(CARD);

        AbstractSimpleDecisionAction.Transition result = unit.executeAction(process);

        assertEquals(AbstractSimpleDecisionAction.Transition.OK, result);
    }

    @Test
    public void shouldReturnOkForBraintreePaymentInfo() {
        when(process.getOrder()).thenReturn(order);
        when(order.getPaymentInfo()).thenReturn(brainTreePaymentInfo);

        AbstractSimpleDecisionAction.Transition result = unit.executeAction(process);

        assertEquals(AbstractSimpleDecisionAction.Transition.OK, result);
    }

    @Test
    public void shouldReturnNokForAccountPaymentTypeAndCreditCardPaymentInfo() {
        when(process.getOrder()).thenReturn(order);
        when(order.getPaymentInfo()).thenReturn(creditCardPaymentInfo);
        when(order.getPaymentType()).thenReturn(ACCOUNT);

        AbstractSimpleDecisionAction.Transition result = unit.executeAction(process);

        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, result);
    }

    @Test
    public void shouldSetErrorStatusToOrderAndReturnNok() {
        when(process.getOrder()).thenReturn(order);
        when(order.getPaymentType()).thenThrow(new IllegalArgumentException());
        AbstractSimpleDecisionAction.Transition result = unit.executeAction(process);

        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, result);
        verify(order).setStatus(OrderStatus.B2B_PROCESSING_ERROR);
    }

}