package com.braintree.strategy.impl;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.delivery.dao.PickupDeliveryModeDao;
import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.AbstractOrderModel;
import de.hybris.platform.core.model.order.delivery.DeliveryModeModel;
import de.hybris.platform.ordersplitting.model.ConsignmentModel;
import de.hybris.platform.storelocator.model.PointOfServiceModel;
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 static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Mock
    private PickupDeliveryModeDao pickupDeliveryModeDao;

    @InjectMocks
    private BrainTreeSplitByDeliveryMode unit;

    @Mock
    private AbstractOrderEntryModel orderEntry;
    @Mock
    private DeliveryModeModel deliveryModeModel;
    @Mock
    private PointOfServiceModel pointOfServiceModel;
    @Mock
    private AbstractOrderModel orderModel;
    @Mock
    private ConsignmentModel consignmentModel;

    @Test
    public void shouldGetDeliveryMode() {
        //given
        when(orderEntry.getDeliveryMode()).thenReturn(deliveryModeModel);

        //when
        Object result = unit.getGroupingObject(orderEntry);

        //then
        assertEquals(deliveryModeModel, result);
    }

    @Test
    public void shouldGetDeliveryModeFromOrderModel() {
        //given
        when(orderEntry.getOrder()).thenReturn(orderModel);
        when(orderModel.getDeliveryMode()).thenReturn(deliveryModeModel);

        //when
        Object result = unit.getGroupingObject(orderEntry);

        //then
        assertEquals(deliveryModeModel, result);
    }

    @Test
    public void shouldGetDeliveryModeFromPickupDeliveryModeDao() {
        //given
        when(orderEntry.getDeliveryPointOfService()).thenReturn(pointOfServiceModel);
        when(orderEntry.getOrder()).thenReturn(orderModel);
        when(pickupDeliveryModeDao.findPickupDeliveryModesForAbstractOrder(orderModel)).thenReturn(List.of(deliveryModeModel));

        //when
        Object result = unit.getGroupingObject(orderEntry);

        //then
        assertEquals(deliveryModeModel, result);
    }

    @Test
    public void shouldExecuteAfterSplitting() {
        //when
        unit.afterSplitting(deliveryModeModel, consignmentModel);

        //then
        verify(consignmentModel).setDeliveryMode(deliveryModeModel);
    }

}
