package com.braintree.strategy.impl;

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

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.stock.CommerceStockService;
import de.hybris.platform.core.model.order.AbstractOrderEntryModel;
import de.hybris.platform.core.model.order.OrderModel;
import de.hybris.platform.core.model.product.ProductModel;
import de.hybris.platform.store.BaseStoreModel;
import de.hybris.platform.storelocator.model.PointOfServiceModel;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.runners.MockitoJUnitRunner;

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

    private static final Long ZERO = 0L;

    @Mock
    private CommerceStockService commerceStockService;

    @InjectMocks
    private BrainTreeSplitByAvailableCount unit;

    @Test
    public void testGetGroupingObjectWithoutDeliveryPointOfService() {
        final AbstractOrderEntryModel orderEntryModel = Mockito.mock(AbstractOrderEntryModel.class);
        final OrderModel orderModel = Mockito.mock(OrderModel.class);
        final BaseStoreModel baseStoreModel = Mockito.mock(BaseStoreModel.class);

        when(orderModel.getStore()).thenReturn(baseStoreModel);
        when(orderEntryModel.getQuantity()).thenReturn(ZERO);
        when(orderEntryModel.getOrder()).thenReturn(orderModel);
        unit.getGroupingObject(orderEntryModel);

        verify(commerceStockService).getStockLevelForProductAndBaseStore(orderEntryModel.getProduct(), orderEntryModel.getOrder().getStore());

    }

    @Test
    public void testGetGroupingObjectWithDeliveryPointOfService() {
        final AbstractOrderEntryModel orderEntryModel = Mockito.mock(AbstractOrderEntryModel.class);
        final PointOfServiceModel serviceModel = Mockito.mock(PointOfServiceModel.class);
        when(orderEntryModel.getDeliveryPointOfService()).thenReturn(serviceModel);

        unit.getGroupingObject(orderEntryModel);
    }

    @Test
    public void testGetGroupingObjectWithoutDeliveryPointOfServiceAndWithoutOrderStore() {
        final AbstractOrderEntryModel orderEntryModel = Mockito.mock(AbstractOrderEntryModel.class);
        final OrderModel orderModel = Mockito.mock(OrderModel.class);
        final BaseStoreModel baseStoreModel = Mockito.mock(BaseStoreModel.class);

        when(orderModel.getStore()).thenReturn(baseStoreModel);
        when(orderEntryModel.getQuantity()).thenReturn(ZERO);
        when(orderEntryModel.getOrder()).thenReturn(orderModel);
        when(orderModel.getStore()).thenReturn(null);

        unit.getGroupingObject(orderEntryModel);

        verify(commerceStockService, never()).getStockLevelForProductAndBaseStore(orderEntryModel.getProduct(), orderEntryModel.getOrder().getStore());
    }
}
