package com.braintree.actions.order;

import com.braintree.constants.BraintreefulfilmentprocessConstants;
import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.core.enums.OrderStatus;
import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.orderprocessing.model.OrderProcessModel;
import de.hybris.platform.ordersplitting.OrderSplittingService;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.ordersplitting.model.ConsignmentProcessModel;
import de.hybris.platform.processengine.BusinessProcessService;
import de.hybris.platform.servicelayer.model.ModelService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Captor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.anyList;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Mock
    private OrderSplittingService orderSplittingService;
    @Mock
    private BusinessProcessService businessProcessService;
    @Mock
    private ModelService modelService;

    @InjectMocks
    private BrainTreeSplitOrderAction unit;

    @Mock
    private OrderProcessModel process;

    @Mock
    private OrderModel order;

    @Mock
    private AbstractOrderEntryModel entryModel;

    @Mock
    private ConsignmentModel consignmentModel;

    @Mock
    private ConsignmentProcessModel consignmentProcessModel;

    @Captor
    private ArgumentCaptor<List<AbstractOrderEntryModel>> entriesToSplit;

    @Before
    public void setUp() {

    }

    @Test
    public void shouldSplitOrderIfEntriesToSplitExist() throws Exception {
        //given
        when(process.getOrder()).thenReturn(order);
        when(order.getEntries()).thenReturn(List.of(entryModel));
        when(entryModel.getConsignmentEntries()).thenReturn(new HashSet<>());
        when(orderSplittingService.splitOrderForConsignment(eq(order), anyList())).thenReturn(List.of(consignmentModel));
        when(businessProcessService.createProcess(anyString(), eq(BraintreefulfilmentprocessConstants.CONSIGNMENT_SUBPROCESS_NAME)))
                .thenReturn(consignmentProcessModel);

        //when
        unit.executeAction(process);

        //then
        verify(orderSplittingService).splitOrderForConsignment(eq(order), entriesToSplit.capture());
        List<AbstractOrderEntryModel> entriesToSplitValue = entriesToSplit.getValue();
        assertNotNull(entriesToSplitValue);
        assertEquals(1, entriesToSplitValue.size());
        assertEquals(entryModel, entriesToSplitValue.get(0));

        verify(consignmentProcessModel).setParentProcess(process);
        verify(consignmentProcessModel).setConsignment(consignmentModel);
        verify(modelService).save(consignmentProcessModel);
        verify(businessProcessService).startProcess(consignmentProcessModel);
        verify(order).setStatus(OrderStatus.ORDER_SPLIT);
        verify(modelService).save(order);
    }

    @Test
    public void shouldSplitOrderIfEntriesToSplitNotExist() throws Exception {
        //given
        when(orderSplittingService.splitOrderForConsignment(eq(order), anyList())).thenReturn(Collections.emptyList());
        when(process.getOrder()).thenReturn(order);

        //when
        unit.executeAction(process);

        //then
        verify(orderSplittingService).splitOrderForConsignment(eq(order), entriesToSplit.capture());
        List<AbstractOrderEntryModel> entriesToSplitValue = entriesToSplit.getValue();
        assertNotNull(entriesToSplitValue);
        assertTrue(entriesToSplitValue.isEmpty());

        verify(businessProcessService, never()).startProcess(any(ConsignmentProcessModel.class));
        verify(order).setStatus(OrderStatus.ORDER_SPLIT);
        verify(modelService).save(order);
    }

}
