Search in sources :

Example 1 with Project

use of io.crnk.validation.mock.models.Project 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 Project

use of io.crnk.validation.mock.models.Project 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 Project

use of io.crnk.validation.mock.models.Project 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 Project

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

the class ValidationEndToEndTest method testValidationException.

@Test
public void testValidationException() {
    Project project = new Project();
    project.setId(1L);
    // trigger ValidationException
    project.setName(ValidationException.class.getSimpleName());
    try {
        projectRepo.create(project);
    } catch (ValidationException e) {
        Assert.assertEquals("messageKey", e.getMessage());
    }
}
Also used : Project(io.crnk.validation.mock.models.Project) ValidationException(javax.validation.ValidationException) Test(org.junit.Test)

Example 5 with Project

use of io.crnk.validation.mock.models.Project 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)

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