Search in sources :

Example 6 with Project

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

the class TaskToProjectRepository method findOneTarget.

@Override
public Project findOneTarget(Long sourceId, String fieldName, QueryParams queryParams) {
    for (Relation<Task> relation : THREAD_LOCAL_REPOSITORY.keySet()) {
        if (relation.getSource().getId().equals(sourceId) && relation.getFieldName().equals(fieldName)) {
            Project project = new Project();
            project.setId((Long) relation.getTargetId());
            return project;
        }
    }
    return null;
}
Also used : Project(io.crnk.validation.mock.models.Project) Task(io.crnk.validation.mock.models.Task)

Example 7 with Project

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

the class TaskToProjectRepository method findManyTargets.

@Override
public Iterable<Project> findManyTargets(Long sourceId, String fieldName, QueryParams queryParams) {
    List<Project> projects = new LinkedList<>();
    for (Relation<Task> relation : THREAD_LOCAL_REPOSITORY.keySet()) {
        if (relation.getSource().getId().equals(sourceId) && relation.getFieldName().equals(fieldName)) {
            Project project = new Project();
            project.setId((Long) relation.getTargetId());
            projects.add(project);
        }
    }
    return projects;
}
Also used : Project(io.crnk.validation.mock.models.Project) Task(io.crnk.validation.mock.models.Task) LinkedList(java.util.LinkedList)

Example 8 with Project

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

Example 9 with Project

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

the class ValidationEndToEndTest method testListAttribute.

@Test
public void testListAttribute() {
    Project project = new Project();
    project.setId(1L);
    project.setName("test");
    project.getKeywords().add("1");
    project.getKeywords().add("2");
    project.getKeywords().add("3");
    project.getKeywords().add("4");
    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.Size.message}", violation.getMessageTemplate());
        Assert.assertEquals("keywords", violation.getPropertyPath().toString());
        // message depends on local
        Assert.assertNotNull(violation.getMessage());
        Assert.assertTrue(violation.getMessage().contains("0"));
        Assert.assertTrue(violation.getMessage().contains("3"));
        Assert.assertEquals("/data/attributes/keywords", 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 10 with Project

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

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