package com.braintree.order.strategy;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commerceservices.i18n.CommerceCommonI18NService;
import de.hybris.platform.commerceservices.order.CommerceCartCalculationStrategy;
import de.hybris.platform.commerceservices.service.data.CommerceCartParameter;
import de.hybris.platform.core.model.order.CartModel;
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 static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

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

    @Mock
    private CommerceCartCalculationStrategy commerceCartCalculationStrategy;
    @Mock
    private ModelService modelService;
    @Mock
    private CommerceCommonI18NService commerceCommonI18NService;

    @InjectMocks
    private DefaultBraintreeCommerceCartRestorationStrategy unit;

    @Mock
    private CartModel cart;

    @Test
    public void shouldRecalculateCart() {
        CommerceCartParameter parameter = new CommerceCartParameter();
        parameter.setIgnoreRecalculation(false);
        parameter.setCart(cart);

        unit.recalculateCartIfNotIgnored(parameter);

        verify(commerceCartCalculationStrategy).recalculateCart(parameter);
    }

    @Test
    public void shouldSkipRecalculateCart() {
        CommerceCartParameter parameter = new CommerceCartParameter();
        parameter.setIgnoreRecalculation(true);

        unit.recalculateCartIfNotIgnored(parameter);

        verify(commerceCartCalculationStrategy, never()).recalculateCart(parameter);
    }

    @Test
    public void shouldLogMessageIfRecalculationFailed() {
        CommerceCartParameter parameter = new CommerceCartParameter();
        parameter.setIgnoreRecalculation(false);
        parameter.setCart(cart);

        when(commerceCartCalculationStrategy.recalculateCart(parameter)).thenThrow(new IllegalStateException());

        unit.recalculateCartIfNotIgnored(parameter);

        verify(cart).getCode();
    }

}