package com.atlassian.stash.internal.web.util;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public final class FilteredErrorsTest {

    private final FieldError error = new FieldError("object", "field", "msg");

    private FilteredErrors filteredErrors;

    @Mock
    private Errors errors;

    @Before
    public void setUp() throws Exception {
        when(errors.hasFieldErrors(anyString())).thenReturn(true);
        when(errors.getFieldErrorCount(anyString())).thenReturn(42);
        when(errors.getFieldError(anyString())).thenReturn(error);

        filteredErrors = new FilteredErrors(errors, "ignorefield");
    }

    @Test
    public void testNormalField() throws Exception {
        assertTrue(filteredErrors.hasFieldErrors("field"));
        assertEquals(42, filteredErrors.getFieldErrorCount("field"));
        assertEquals(error, filteredErrors.getFieldError("field"));
    }

    @Test
    public void testIgnoredField() throws Exception {
        assertFalse(filteredErrors.hasFieldErrors("ignorefield"));
        assertEquals(0, filteredErrors.getFieldErrorCount("ignorefield"));
        assertNull(filteredErrors.getFieldError("ignorefield"));
    }

}
