Search in sources :

Example 1 with ProjectData

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

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

the class ValidationEndToEndTest method testSetElementAttributeNotNull.

@Test
public void testSetElementAttributeNotNull() {
    Project project = new Project();
    project.setId(1L);
    project.setName("test");
    // ProjectData corrupedElement = null;
    for (int i = 0; i < 11; i++) {
        ProjectData data = new ProjectData();
        if (i != 3) {
            data.setValue(Integer.toString(i));
        // corrupedElement = data;
        }
        project.getDataSet().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.assertTrue(violation.getPropertyPath().toString().startsWith("dataSet["));
        Assert.assertTrue(violation.getPropertyPath().toString().endsWith("].value"));
        Assert.assertTrue(violation.getErrorData().getSourcePointer().startsWith("/data/attributes/dataSet/"));
    // TODO attempt to preserver order in Crnk by comparing incoming request, sourcePointer and server Set
    // or use of order preserving sets
    // List<ProjectData> list = new ArrayList<>(project.getDataSet());
    // int index = list.indexOf(corrupedElement);
    // Assert.assertEquals(violation.getErrorData().getSourcePointer(), "/data/attributes/dataSet/" + index +
    // "/value");
    }
}
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 3 with ProjectData

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

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

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