package com.atlassian.stash.internal.util;

import com.atlassian.bitbucket.util.ValidationUtils;
import com.google.common.collect.ImmutableSet;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import javax.validation.Validator;
import java.util.Collections;

import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.*;

@RunWith(MockitoJUnitRunner.class)
public class ValidationUtilsTest {

    @Mock
    private Validator validator;

    @Test
    public void testValidate() {
        Object object = new Object();

        when(validator.validate(same(object))).thenReturn(Collections.<ConstraintViolation<Object>>emptySet());

        assertSame(object, ValidationUtils.validate(validator, object));
    }

    @Test(expected = ConstraintViolationException.class)
    public void testValidateWithViolations() {
        @SuppressWarnings("unchecked")
        ConstraintViolation<Object> violation = mock(ConstraintViolation.class);

        Object object = new Object();

        when(validator.validate(same(object))).thenReturn(ImmutableSet.of(violation));

        ValidationUtils.validate(validator, object);
    }
}
