package com.braintree.actions.consignment;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.enums.WarehouseConsignmentState;
import de.hybris.platform.ordersplitting.model.ConsignmentProcessModel;
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 java.util.Set;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    private static final String OK = "OK";
    private static final String CANCEL = "CANCEL";
    private static final String ERROR = "ERROR";

    @Mock
    private ModelService modelService;

    @InjectMocks
    private BrainTreeReceiveConsignmentStatusAction unit;

    @Mock
    private ConsignmentProcessModel process;

    @Before
    public void setUp() {
    }

    @Test
    public void shouldReturnTransitionErrorIfWarehouseConsignmentStateIsNull() {
        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeReceiveConsignmentStatusAction.Transition.ERROR.name(), result);
        verify(process).setWaitingForConsignment(false);
        verify(modelService).save(process);
    }

    @Test
    public void shouldReturnTransitionCancel() {
        //given
        when(process.getWarehouseConsignmentState()).thenReturn(WarehouseConsignmentState.CANCEL);
        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeReceiveConsignmentStatusAction.Transition.CANCEL.name(), result);
        verify(process).setWaitingForConsignment(false);
        verify(modelService).save(process);
    }

    @Test
    public void shouldReturnTransitionOk() {
        //given
        when(process.getWarehouseConsignmentState()).thenReturn(WarehouseConsignmentState.COMPLETE);
        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeReceiveConsignmentStatusAction.Transition.OK.name(), result);
        verify(process).setWaitingForConsignment(false);
        verify(modelService).save(process);
    }

    @Test
    public void shouldReturnTransitionErrorIfUnexpectedWarehouseConsignmentState() {
        //given
        when(process.getWarehouseConsignmentState()).thenReturn(WarehouseConsignmentState.PARTIAL);
        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeReceiveConsignmentStatusAction.Transition.ERROR.name(), result);
        verify(process).setWaitingForConsignment(false);
        verify(modelService).save(process);
    }

    @Test
    public void shouldGetTransition() {
        //when
        Set<String> transitions = unit.getTransitions();

        //then
        assertTrue(transitions.containsAll(List.of(OK, CANCEL, ERROR)));
    }

}
