package com.braintree.actions.consignment;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.basecommerce.enums.ConsignmentStatus;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.ordersplitting.model.ConsignmentProcessModel;
import de.hybris.platform.servicelayer.model.ModelService;
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.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 BrainTreeConfirmConsignmentPickupActionTest {

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

    @Mock
    private ModelService modelService;

    @InjectMocks
    private BrainTreeConfirmConsignmentPickupAction unit;

    @Mock
    private ConsignmentProcessModel process;
    @Mock
    private ConsignmentModel consignment;

    @Test
    public void shouldSetStatusPickUp() {
        //given
        when(process.getConsignment()).thenReturn(consignment);

        //when
        String result = unit.execute(process);

        //then
        assertEquals(BrainTreeConfirmConsignmentPickupAction.Transition.OK.toString(), result);
        verify(modelService).save(consignment);
        verify(consignment).setStatus(ConsignmentStatus.PICKUP_COMPLETE);
    }

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

        //then
        assertEquals(BrainTreeConfirmConsignmentPickupAction.Transition.ERROR.toString(), result);
        verify(modelService, never()).save(any(ConsignmentModel.class));
        verify(consignment, never()).setStatus(any(ConsignmentStatus.class));
    }

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

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

}
