/java/

JSR 303: Bean Validation

2013-08-23 23:56:25

ValidatorService.java - v.2

package jery.serive;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

public class ValidatorService

<t> {

    public boolean isValidate(T obj) {
        return (getConstraintViolationFor(obj).size() &gt; 0) ? false : true;
    }

    public Map<string, string=""> getValidatorErrors(T obj) {

        Map<string, string=""> errors = new HashMap<string, string="">();

        for (ConstraintViolation<t> item : getConstraintViolationFor(obj)) {
            errors.put(item.getPropertyPath().toString(), item.getMessage());
        }

        return errors;
    }

    private Set<constraintviolation<t>&gt; getConstraintViolationFor(T obj) {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        Validator validator = factory.getValidator();

        Set<constraintviolation<t>&gt; validatorError = validator.validate(obj);

        return validatorError;
    }
}

ValidatorService.java - v.1

package jery.serive;

import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

public class ValidatorService<t> {

    private ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    private Validator validator = factory.getValidator();

    public boolean isValidate(T obj) {
        return getValidatorErrors(obj).size() &gt; 0 ? false : true;
    }

    public Map<string, string=""> getValidatorErrors(T obj) {

        Set<constraintviolation<t>&gt; validatorError = validator.validate(obj);

        Map<string, string=""> errors = new HashMap<string, string="">();

        for (ConstraintViolation<t> item : validatorError) {
            errors.put(item.getPropertyPath().toString(), item.getMessage());
        }

        return errors;
    }
}

PostServiceTest.java

package jery.serive;

import java.util.AbstractMap;
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;

import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.startsWith;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import jery.dto.PostDto;

import org.junit.Test;

public class PostServiceTest {

    ValidatorService<postdto> validatorService = new ValidatorService<postdto>();
    PostDto post = new PostDto(1L, "lorem");
    PostDto postNull = new PostDto(null, null);

    @Test
    public void shouldBeValid() {
        boolean validatePost = validatorService.isValidate(post);
        assertThat(validatePost).isTrue();
    }

    @Test
    public void shouldBeNoValid() {
        boolean validatePost = validatorService.isValidate(postNull);
        assertThat(validatePost).isFalse();
    }

    @Test
    public void shouldMapContainTwoItemWitError() {
        Map<string, string=""> validatorErrors = validatorService.getValidatorErrors(postNull);
        assertThat(validatorErrors).hasSize(2);
    }

}

PostDto.java

package jery.dto;

import javax.validation.constraints.NotNull;

import org.hibernate.validator.constraints.NotEmpty;

public class PostDto {

    @NotNull(message="Id == null")
    private Long id;

    @NotEmpty(message="Name == null")
    private String name;

    public PostDto(Long id, String name) {
        this.id = id;
        this.name = name;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }   
}

pom.xml

<dependency>
<groupid>org.hibernate</groupid>
<artifactid>hibernate-validator</artifactid>
<version>4.2.0.Final</version>
</dependency>

Eclipse test template

@${testType:newType(org.junit.Test)}
public void should${cursor}() {

}