Search in sources :

Example 11 with Project

use of io.crnk.validation.mock.models.Project in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testNestedPropertyNotNull.

@Test
public void testNestedPropertyNotNull() {
    ProjectData data = new ProjectData();
    // violation
    data.setValue(null);
    Project project = new Project();
    project.setId(1L);
    project.setName("test");
    project.setData(data);
    try {
        projectRepo.create(project);
    } catch (ConstraintViolationException e) {
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        Assert.assertEquals(1, violations.size());
        ConstraintViolationImpl violation = (ConstraintViolationImpl) violations.iterator().next();
        Assert.assertEquals("{javax.validation.constraints.NotNull.message}", violation.getMessageTemplate());
        Assert.assertEquals("data.value", violation.getPropertyPath().toString());
        Assert.assertEquals("/data/attributes/data/value", violation.getErrorData().getSourcePointer());
    }
}
Also used : Project(io.crnk.validation.mock.models.Project) Set(java.util.Set) HashSet(java.util.HashSet) ConstraintViolationImpl(io.crnk.validation.internal.ConstraintViolationImpl) ConstraintViolationException(javax.validation.ConstraintViolationException) ProjectData(io.crnk.validation.mock.models.ProjectData) Test(org.junit.Test)

Example 12 with Project

use of io.crnk.validation.mock.models.Project in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testMapElementAttributeNotNull.

@Test
public void testMapElementAttributeNotNull() {
    ProjectData data = new ProjectData();
    // violation
    data.setValue(null);
    Project project = new Project();
    project.setId(1L);
    project.setName("test");
    project.setDataMap(new LinkedHashMap());
    project.getDataMap().put("someKey", data);
    ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
    Validator validator = factory.getValidator();
    try {
        projectRepo.create(project);
    } catch (ConstraintViolationException e) {
        Set<ConstraintViolation<?>> violations = e.getConstraintViolations();
        Assert.assertEquals(1, violations.size());
        ConstraintViolationImpl violation = (ConstraintViolationImpl) violations.iterator().next();
        Assert.assertEquals("{javax.validation.constraints.NotNull.message}", violation.getMessageTemplate());
        Assert.assertEquals("dataMap[someKey].value", violation.getPropertyPath().toString());
        Assert.assertNotNull(violation.getMessage());
        Assert.assertEquals("/data/attributes/dataMap/someKey/value", violation.getErrorData().getSourcePointer());
    }
}
Also used : Project(io.crnk.validation.mock.models.Project) Set(java.util.Set) HashSet(java.util.HashSet) ValidatorFactory(javax.validation.ValidatorFactory) ConstraintViolationImpl(io.crnk.validation.internal.ConstraintViolationImpl) ConstraintViolationException(javax.validation.ConstraintViolationException) ProjectData(io.crnk.validation.mock.models.ProjectData) Validator(javax.validation.Validator) ComplexValidator(io.crnk.validation.mock.ComplexValidator) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Aggregations

Project (io.crnk.validation.mock.models.Project)12 Test (org.junit.Test)10 ConstraintViolationImpl (io.crnk.validation.internal.ConstraintViolationImpl)9 HashSet (java.util.HashSet)9 Set (java.util.Set)9 ConstraintViolationException (javax.validation.ConstraintViolationException)9 ProjectData (io.crnk.validation.mock.models.ProjectData)4 Task (io.crnk.validation.mock.models.Task)4 ComplexValidator (io.crnk.validation.mock.ComplexValidator)1 LinkedHashMap (java.util.LinkedHashMap)1 LinkedList (java.util.LinkedList)1 ValidationException (javax.validation.ValidationException)1 Validator (javax.validation.Validator)1 ValidatorFactory (javax.validation.ValidatorFactory)1