package com.braintreeocc.filters;

import de.hybris.bootstrap.annotations.UnitTest;
import de.hybris.platform.commercewebservicescommons.strategies.CartLoaderStrategy;
import org.apache.commons.lang.StringUtils;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

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

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

    private static final String CART_GUID = "cartGuid";
    private static final String REFRESH_CART_PARAM = "refreshCart";
    private static final String TRUE = "true";
    private static final String TEST_CART_GUID = "Test cart guid";
    private static final String FALSE = "false";

    @Mock
    private CartLoaderStrategy cartLoaderStrategy;
    @Mock
    private HttpServletRequest request;
    @Mock
    private HttpServletResponse response;
    @Mock
    private FilterChain filterChain;

    @InjectMocks
    private BraintreeCartMatchingFilter unit;


    @Test
    public void doFilterInternalTest() throws ServletException, IOException {
        when(request.getParameter(CART_GUID)).thenReturn(TEST_CART_GUID);
        when(request.getParameter(REFRESH_CART_PARAM)).thenReturn(TRUE);

        unit.doFilterInternal(request, response, filterChain);

        verify(cartLoaderStrategy).loadCart(TEST_CART_GUID, true);
        verify(filterChain).doFilter(request, response);
    }

    @Test
    public void doFilterInternalTestWithNullCartGuid() throws ServletException, IOException {
        when(request.getParameter(CART_GUID)).thenReturn(null);

        unit.doFilterInternal(request, response, filterChain);

        verify(cartLoaderStrategy, never()).loadCart(TEST_CART_GUID, false);
        verify(filterChain).doFilter(request, response);
    }

    @Test
    public void doFilterInternalTestWithEmptyCartGuid() throws ServletException, IOException {
        when(request.getParameter(CART_GUID)).thenReturn(StringUtils.EMPTY);

        unit.doFilterInternal(request, response, filterChain);

        verify(cartLoaderStrategy, never()).loadCart(TEST_CART_GUID, false);
        verify(filterChain).doFilter(request, response);
    }

    @Test
    public void doFilterInternalTestWithNullRefreshParam() throws ServletException, IOException {
        when(request.getParameter(CART_GUID)).thenReturn(TEST_CART_GUID);
        when(request.getParameter(REFRESH_CART_PARAM)).thenReturn(null);

        unit.doFilterInternal(request, response, filterChain);
        unit.setCartRefreshedByDefault(true);

        verify(cartLoaderStrategy).loadCart(TEST_CART_GUID, false);
        verify(filterChain).doFilter(request, response);
    }

}
