package com.braintree.warehouse;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.enums.WarehouseConsignmentState;
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 de.hybris.platform.warehouse.WarehouseConsignmentStatus;
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 java.util.Map;

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

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

    private static final String CODE = "CODE";
    private static final String CODE_WAIT_FOR_WAREHOUSE = "CODE_WaitForWarehouse";

    @Mock
    private Map<WarehouseConsignmentStatus, WarehouseConsignmentState> statusMap;
    @Mock
    private ModelService modelService;
    @Mock
    private BusinessProcessService businessProcessService;

    @InjectMocks
    private DefaultBrainTreeWarehouse2ProcessAdapter unit;

    @Mock
    private ConsignmentModel consignment;
    @Mock
    private ConsignmentProcessModel consignmentProcess;

    @Test
    public void shouldReceiveConsignmentStatus() {
        //given
        when(consignment.getConsignmentProcesses()).thenReturn(List.of(consignmentProcess));
        when(statusMap.get(WarehouseConsignmentStatus.COMPLETE)).thenReturn(WarehouseConsignmentState.COMPLETE);
        when(consignmentProcess.getCode()).thenReturn(CODE);

        //when
        unit.receiveConsignmentStatus(consignment, WarehouseConsignmentStatus.COMPLETE);

        //then
        verify(consignmentProcess).setWarehouseConsignmentState(WarehouseConsignmentState.COMPLETE);
        verify(modelService).save(consignmentProcess);
        verify(businessProcessService).triggerEvent(CODE_WAIT_FOR_WAREHOUSE);
    }

    @Test(expected = IllegalStateException.class)
    public void shouldThrowErrorWhenStateIsNull() {
        //given
        when(consignment.getConsignmentProcesses()).thenReturn(List.of(consignmentProcess));
        when(statusMap.get(WarehouseConsignmentStatus.COMPLETE)).thenReturn(null);

        //when
        unit.receiveConsignmentStatus(consignment, WarehouseConsignmentStatus.COMPLETE);
    }

}
