Search in sources :

Example 1 with ConstraintViolationImpl

use of io.crnk.validation.internal.ConstraintViolationImpl in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testRelationProperty.

@Test
public void testRelationProperty() {
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    task.setName(ComplexValidator.INVALID_NAME);
    Project project = new Project();
    project.setName("test");
    project.setTask(task);
    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("{complex.message}", violation.getMessageTemplate());
        Assert.assertEquals("task", violation.getPropertyPath().toString());
        Assert.assertEquals("/data/relationships/task", violation.getErrorData().getSourcePointer());
    }
}
Also used : Project(io.crnk.validation.mock.models.Project) Task(io.crnk.validation.mock.models.Task) Set(java.util.Set) HashSet(java.util.HashSet) ConstraintViolationImpl(io.crnk.validation.internal.ConstraintViolationImpl) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.Test)

Example 2 with ConstraintViolationImpl

use of io.crnk.validation.internal.ConstraintViolationImpl in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testResourceObjectValidation.

@Test
public void testResourceObjectValidation() {
    Project project = new Project();
    project.setId(1L);
    project.setName(ComplexValidator.INVALID_NAME);
    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("{complex.message}", violation.getMessageTemplate());
        Assert.assertEquals("", violation.getPropertyPath().toString());
        Assert.assertEquals("", 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) Test(org.junit.Test)

Example 3 with ConstraintViolationImpl

use of io.crnk.validation.internal.ConstraintViolationImpl in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testListElementAttributeNotNull.

@Test
public void testListElementAttributeNotNull() {
    ProjectData data = new ProjectData();
    // violation
    data.setValue(null);
    Project project = new Project();
    project.setId(1L);
    project.setName("test");
    project.getDataList().add(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("dataList[0].value", violation.getPropertyPath().toString());
        Assert.assertNotNull(violation.getMessage());
        Assert.assertEquals("/data/attributes/dataList/0/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 4 with ConstraintViolationImpl

use of io.crnk.validation.internal.ConstraintViolationImpl in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testPropertyOnRelation.

@Test
public void testPropertyOnRelation() {
    Task task = new Task();
    task.setId(1L);
    task.setName("test");
    taskRepo.create(task);
    task.setName(null);
    Project project = new Project();
    project.setId(2L);
    project.setName("test");
    project.getTasks().add(task);
    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("tasks[0]", violation.getPropertyPath().toString());
        Assert.assertEquals("/data/relationships/tasks/0", violation.getErrorData().getSourcePointer());
    }
}
Also used : Project(io.crnk.validation.mock.models.Project) Task(io.crnk.validation.mock.models.Task) Set(java.util.Set) HashSet(java.util.HashSet) ConstraintViolationImpl(io.crnk.validation.internal.ConstraintViolationImpl) ConstraintViolationException(javax.validation.ConstraintViolationException) Test(org.junit.Test)

Example 5 with ConstraintViolationImpl

use of io.crnk.validation.internal.ConstraintViolationImpl in project crnk-framework by crnk-project.

the class ValidationEndToEndTest method testPropertyNotNull.

@Test
public void testPropertyNotNull() {
    Project project = new Project();
    project.setId(1L);
    // violation
    project.setName(null);
    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("name", violation.getPropertyPath().toString());
        Assert.assertNotNull(violation.getMessage());
        Assert.assertTrue(violation.getMessage().contains("null"));
        Assert.assertEquals("/data/attributes/name", 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) Test(org.junit.Test)

Aggregations

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