package com.braintree.customersupportbackoffice.search;

import com.hybris.cockpitng.dataaccess.facades.type.DataAttribute;
import com.hybris.cockpitng.search.data.ValueComparisonOperator;
import de.hybris.bootstrap.annotations.UnitTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;

import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;

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

    private static final String CUSTOMER_ID = "braintreeTransactionCustomerID";
    private static final String EMAIL = "braintreeTransactionCustomerEmail";
    private static final String UNKNOWN = "unknownAttribute";

    @Mock
    private DataAttribute dataAttribute;
    @Spy
    @InjectMocks
    private BrainTreeAdvancedSearchOperatorService advancedSearchOperatorService;


    @Test
    public void testGetAvailableOperatorsForTransactionID() {
        when(dataAttribute.getQualifier()).thenReturn(CUSTOMER_ID);

        Collection<ValueComparisonOperator> operators = advancedSearchOperatorService.getAvailableOperators(dataAttribute);
        assertEquals(1, operators.size());
        assertEquals(ValueComparisonOperator.EQUALS, operators.iterator().next());
    }

    @Test
    public void testGetAvailableOperatorsForTransactionEmail() {
        when(dataAttribute.getQualifier()).thenReturn(EMAIL);

        Collection<ValueComparisonOperator> operators = advancedSearchOperatorService.getAvailableOperators(dataAttribute);

        assertEquals(4, operators.size());
        assertEquals(true, operators.contains(ValueComparisonOperator.CONTAINS));
        assertEquals(true, operators.contains(ValueComparisonOperator.EQUALS));
        assertEquals(true, operators.contains(ValueComparisonOperator.STARTS_WITH));
        assertEquals(true, operators.contains(ValueComparisonOperator.ENDS_WITH));
    }

    @Test
    public void testGetAvailableOperatorsForUnknownAttribute() {
        when(dataAttribute.getQualifier()).thenReturn(UNKNOWN);
        doReturn(Arrays.asList(ValueComparisonOperator.NOT_EXISTS)).when(advancedSearchOperatorService).getParentAvailableOperators(dataAttribute);

        Collection<ValueComparisonOperator> operators = advancedSearchOperatorService.getAvailableOperators(dataAttribute);
        Iterator<ValueComparisonOperator> iterator = operators.iterator();

        assertFalse(operators.isEmpty());
        assertEquals(iterator.next(), ValueComparisonOperator.NOT_EXISTS);
    }
}
