package com.braintree.actions.order;

import com.braintree.BrainTreeCheckOrderService;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
import de.hybris.platform.payment.enums.PaymentTransactionType;
import de.hybris.platform.payment.model.PaymentTransactionEntryModel;
import de.hybris.platform.payment.model.PaymentTransactionModel;
import de.hybris.platform.processengine.action.AbstractSimpleDecisionAction;
import de.hybris.platform.servicelayer.model.ModelService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;

import static com.braintree.constants.BraintreeConstants.FAKE_REQUEST_ID;
import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Mock
    private BrainTreeCheckOrderService checkOrderService;

    @Mock
    private ModelService modelService;

    @InjectMocks
    private BrainTreeCheckOrderAction unit;

    @Mock
    private OrderModel order;

    @Mock
    private OrderProcessModel process;

    @Mock
    private PaymentTransactionModel paymentTransactionModel;

    @Mock
    private PaymentTransactionEntryModel paymentTransactionEntryModel;

    @Before
    public void setUp() {
        when(process.getOrder()).thenReturn(order);
        when(order.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentTransactionEntryModel.getType()).thenReturn(PaymentTransactionType.CAPTURE);

    }

    @Test
    public void shouldDoNothingIfOrderIsNull() {
        //given
        when(process.getOrder()).thenReturn(null);

        //when
        AbstractSimpleDecisionAction.Transition transition = unit.executeAction(process);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, transition);
        verify(modelService, never()).save(order);
        verify(order, never()).setStatus(any(OrderStatus.class));
    }

    @Test
    public void shouldExecuteActionWhenLocalPaymentReversedOrder() {
        //given
        when(paymentTransactionEntryModel.getType()).thenReturn(PaymentTransactionType.REFUND_FOLLOW_ON);
        when(paymentTransactionEntryModel.getRequestId()).thenReturn(FAKE_REQUEST_ID);

        //when
        AbstractSimpleDecisionAction.Transition transition = unit.executeAction(process);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, transition);
        verify(modelService).save(order);
        verify(order).setStatus(OrderStatus.PAYMENT_REVERSED);
    }

    @Test
    public void shouldCheckValidOrder() {
        //given
        when(checkOrderService.check(order)).thenReturn(true);

        //when
        AbstractSimpleDecisionAction.Transition transition = unit.executeAction(process);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.OK, transition);
        verify(modelService).save(order);
        verify(order).setStatus(OrderStatus.CHECKED_VALID);
    }

    @Test
    public void shouldCheckInvalidOrder() {
        //given
        when(checkOrderService.check(order)).thenReturn(false);

        //when
        AbstractSimpleDecisionAction.Transition transition = unit.executeAction(process);

        //then
        assertEquals(AbstractSimpleDecisionAction.Transition.NOK, transition);
        verify(modelService).save(order);
        verify(order).setStatus(OrderStatus.CHECKED_INVALID);
    }

}
