package org.ovirt.engine.core.common.utils;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.ovirt.engine.core.common.validation.annotation.ValidName;

@RunWith(Parameterized.class)
public class ValidNameValidatorTest {

    private Validator validator;
    private String name;
    private boolean expectedResult;

    public ValidNameValidatorTest(String name, boolean expectedResult) {
        this.name = name;
        this.expectedResult = expectedResult;
    }

    @Before
    public void setup() throws Exception {
        validator = ValidationUtils.getValidator();
    }

    @Test
    public void checkName() {
        Set<ConstraintViolation<ValidNameContainer>> result = validator.validate(new ValidNameContainer(name));
        assertEquals(expectedResult, result.isEmpty());
    }

    @Parameterized.Parameters
    public static Collection<Object[]> namesParams() {
        return Arrays.asList(new Object[][] {
                { "abc", true },
                { "123", true },
                { "abc123", true },
                { "123abc", true },
                { null, true },
                { " ", false },
                { "", false },
                { "abc ", false },
                { " abc", false },
                { "abc cde", false },
                { "abc*", false }
        });
    }

    private class ValidNameContainer {

        @ValidName
        private String name;

        public ValidNameContainer(String name) {
            this.name = name;
        }
    }
}

Add test for ValidName annotation
engine: Add test for ValidName annotation

Change-Id: Ica4ef1fd43a1c6144bb722b57a036a160d713c57
Signed-off-by: Moti Asayag <da1debb83a8e12b6e8822edf2c275f55cc51b720@redhat.com>
