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());
}
}
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");
}
}
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());
}
}
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());
}
}
Aggregations