package com.braintreeocc.validators;

import com.braintree.hybris.data.ws.GooglePayAddressWsDTO;
import com.braintree.hybris.data.ws.PayPalDetailsWsDTO;
import com.braintreeocc.constants.BraintreeoccWebConstants;
import com.braintreeocc.validators.impl.BraintreePayPalDetailsValidator;
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 org.springframework.validation.Errors;
import org.springframework.validation.Validator;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;

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

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

    private static final String FIRST_NAME = "firstName";
    private static final String LAST_NAME = "lastName";
    private static final String EMAIL = "email";

    @Mock
    private PayPalDetailsWsDTO target;
    @Mock
    private Validator paymentAddressValidator;
    @Mock
    private GooglePayAddressWsDTO googlePayAddressWsDTO;
    @Mock
    private Errors errors;

    @Spy
    @InjectMocks
    private BraintreePayPalDetailsValidator unit;

    @Test
    public void shouldNotFailValidationWhenErrorsFieldIsNotNull() {
        when(errors.getFieldValue(FIRST_NAME)).thenReturn(FIRST_NAME);
        when(errors.getFieldValue(LAST_NAME)).thenReturn(LAST_NAME);
        when(errors.getFieldValue(EMAIL)).thenReturn(EMAIL);

        unit.validate(target, errors);

        verify(errors, never()).rejectValue(eq(FIRST_NAME), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
        verify(errors, never()).rejectValue(eq(LAST_NAME), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
        verify(errors, never()).rejectValue(eq(EMAIL), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
    }

    @Test
    public void shouldFailValidationWhenErrorsFieldIsNull() {
        unit.validate(target, errors);

        verify(errors).rejectValue(eq(FIRST_NAME), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
        verify(errors).rejectValue(eq(LAST_NAME), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
        verify(errors).rejectValue(eq(EMAIL), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
    }

    @Test
    public void shouldReturnTrueIfPassedSupportedClass() {
        boolean isSupported = unit.supports(target.getClass());

        assertTrue(isSupported);
    }

    @Test
    public void shouldReturnFalseIfPassedUnsupportedClass() {
        boolean isSupported = unit.supports(Object.class);

        assertFalse(isSupported);
    }
}
