package com.braintree.actions.order;

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.PaymentService;
import de.hybris.platform.payment.dto.TransactionStatus;
import de.hybris.platform.payment.model.PaymentTransactionEntryModel;
import de.hybris.platform.payment.model.PaymentTransactionModel;
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.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.List;

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

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

    @Mock
    private PaymentService paymentService;

    @Mock
    private ModelService modelService;

    @Spy
    @InjectMocks
    private BrainTreeCancelWholeOrderAuthorizationAction unit;

    @Mock
    private OrderProcessModel process;

    @Mock
    private OrderModel order;

    @Mock
    private PaymentTransactionModel paymentTransactionModel;

    @Mock
    private PaymentTransactionEntryModel paymentTransactionEntryModel;

    @Mock
    private PaymentTransactionEntryModel txnResultEntry;

    @Before
    public void setUp() {
        when(process.getOrder()).thenReturn(order);
    }

    @Test
    public void shouldSetProcessingErrorIfPaymentTransactionsAreEmpty() {
        //when
        unit.executeAction(process);

        //then
        verify(order).getPaymentTransactions();
        verify(order).setStatus(OrderStatus.PROCESSING_ERROR);
        verify(modelService).save(order);
        verify(paymentTransactionModel, never()).getEntries();
    }

    @Test
    public void shouldSetProcessingErrorIfPaymentTransactionEntriesAreEmpty() {
        //given
        when(order.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));

        //when
        unit.executeAction(process);

        //then
        verify(order).getPaymentTransactions();
        verify(order).setStatus(OrderStatus.PROCESSING_ERROR);
        verify(modelService).save(order);
        verify(paymentTransactionModel).getEntries();
    }

    @Test
    public void shouldCancelAuthorizationTransaction() {
        //given
        when(order.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentService.cancel(paymentTransactionEntryModel)).thenReturn(txnResultEntry);
        when(txnResultEntry.getTransactionStatus()).thenReturn(TransactionStatus.ACCEPTED.name());

        //when
        unit.executeAction(process);

        //then
        verify(order).getPaymentTransactions();
        verify(paymentService).cancel(paymentTransactionEntryModel);
        verify(txnResultEntry).getTransactionStatus();
        verify(order).setStatus(OrderStatus.CANCELLED);
        verify(modelService).save(order);
        verify(paymentTransactionModel).getEntries();
    }

    @Test
    public void shouldSetProcessingErrorIfCancelAuthorizationTransactionFails() {
        //given
        when(order.getPaymentTransactions()).thenReturn(List.of(paymentTransactionModel));
        when(paymentTransactionModel.getEntries()).thenReturn(List.of(paymentTransactionEntryModel));
        when(paymentService.cancel(paymentTransactionEntryModel)).thenReturn(txnResultEntry);
        when(txnResultEntry.getTransactionStatus()).thenReturn(TransactionStatus.ERROR.name());

        //when
        unit.executeAction(process);

        //then
        verify(order).getPaymentTransactions();
        verify(paymentService).cancel(paymentTransactionEntryModel);
        verify(txnResultEntry).getTransactionStatus();
        verify(order).setStatus(OrderStatus.PROCESSING_ERROR);
        verify(modelService).save(order);
        verify(paymentTransactionModel).getEntries();
    }


}
