package com.braintreeocc.validators;

import com.braintree.hybris.data.ws.PayPalDetailsWsDTO;
import com.braintree.hybris.data.ws.PayPalPaymentRequestWsDTO;
import com.braintreeocc.constants.BraintreeoccWebConstants;
import com.braintreeocc.validators.impl.BraintreePayPalPaymentRequestValidator;
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 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 BraintreePayPalPaymentRequestValidatorTest {

    private static final String NONCE = "nonce";

    @Mock
    private PayPalPaymentRequestWsDTO target;
    @Mock
    private PayPalDetailsWsDTO payPalDetailsWsDTO;
    @Mock
    private Errors errors;

    @Spy
    @InjectMocks
    private BraintreePayPalPaymentRequestValidator unit;

    @Test
    public void shouldNotFailValidationWhenErrorsFieldIsNotNull() {
        when(target.getDetails()).thenReturn(payPalDetailsWsDTO);
        when(errors.getFieldValue(NONCE)).thenReturn(NONCE);

        unit.validate(target, errors);

        verify(target).getDetails();
        verify(errors, never()).rejectValue(eq(NONCE), eq(BraintreeoccWebConstants.FIELD_REQUIRED_MESSAGE_ID), any(), any());
    }

    @Test
    public void shouldFailValidationWhenErrorsFieldIsNull() {
        when(target.getDetails()).thenReturn(payPalDetailsWsDTO);

        unit.validate(target, errors);

        verify(target).getDetails();
        verify(errors).rejectValue(eq(NONCE), 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);
    }
}
