use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RestController method handleMolgenisValidationException.
@ExceptionHandler(MolgenisValidationException.class)
@ResponseStatus(BAD_REQUEST)
public ErrorMessageResponse handleMolgenisValidationException(MolgenisValidationException e) {
LOG.info("", e);
List<ErrorMessage> messages = Lists.newArrayList();
for (ConstraintViolation violation : e.getViolations()) {
messages.add(new ErrorMessage(violation.getMessage()));
}
return new ErrorMessageResponse(messages);
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class RepositoryValidationDecoratorTest method updateEntityAttributesValidationError.
@Test
void updateEntityAttributesValidationError() {
// entities
Entity entity0 = mock(Entity.class);
when(entity0.getIdValue()).thenReturn("id1");
when(entity0.getEntityType()).thenReturn(entityType);
when(entity0.getEntity(attrXrefName)).thenReturn(refEntity0);
when(entity0.getEntity(attrNillableXrefName)).thenReturn(null);
when(entity0.getEntities(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.getEntities(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.getString(attrUniqueStringName)).thenReturn("unique1");
when(entity0.getEntity(attrUniqueXrefName)).thenReturn(refEntity1);
when(entity0.get(attrIdName)).thenReturn("id1");
when(entity0.get(attrXrefName)).thenReturn(refEntity0);
when(entity0.get(attrNillableXrefName)).thenReturn(null);
when(entity0.get(attrMrefName)).thenReturn(Arrays.asList(refEntity0));
when(entity0.get(attrNillableMrefName)).thenReturn(emptyList());
when(entity0.get(attrUniqueStringName)).thenReturn("unique1");
when(entity0.get(attrUniqueXrefName)).thenReturn(refEntity1);
Set<ConstraintViolation> violations = singleton(new ConstraintViolation("violation", 2L));
when(entityAttributesValidator.validate(entity0, entityType)).thenReturn(violations);
// actual tests
try {
repositoryValidationDecorator.update(entity0);
throw new RuntimeException("Expected MolgenisValidationException instead of no exception");
} catch (MolgenisValidationException e) {
verify(entityAttributesValidator, times(1)).validate(entity0, entityType);
assertEquals(violations, e.getViolations());
}
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method validateNullableExpression.
@Test
void validateNullableExpression() {
String expression0 = "expression0";
String expression1 = "expression1";
String expression2 = "expression2";
Attribute attr0 = createMockAttribute("attr0", STRING, expression0);
Attribute attr1 = createMockAttribute("attr1", STRING, expression1);
Attribute attr2 = createMockAttribute("attr2", MREF, expression2);
Attribute attr3 = createMockAttribute("attr2", INT, null);
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("entityType");
when(entityType.getAtomicAttributes()).thenReturn(asList(attr0, attr1, attr2, attr3));
when(entityType.toString()).thenReturn("entityType");
Entity entity = mock(Entity.class);
when(entity.getIdValue()).thenReturn("MyEntityId");
when(entity.getLabelValue()).thenReturn("lbl-entity");
when(entity.getString("attr0")).thenReturn(null);
when(entity.getString("attr1")).thenReturn(null);
when(entity.getEntities("attr2")).thenReturn(emptyList());
when(entity.getInt("attr3")).thenReturn(null);
List<Boolean> expressionResults = asList(true, false, false);
when(simpleExpressionValidator.resolveBooleanExpressions(asList(expression0, expression1, expression2), entity)).thenReturn(expressionResults);
Set<ConstraintViolation> constraintViolations = entityAttributesValidator.validate(entity, entityType);
Set<ConstraintViolation> expectedConstraintViolations = newHashSet(new ConstraintViolation("Invalid [string] value [null] for attribute [lbl-attr1] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression1"), new ConstraintViolation("Invalid [mref] value [[]] for attribute [lbl-attr2] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression2"));
assertEquals(expectedConstraintViolations, newHashSet(constraintViolations));
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkRangeMinOnlyInvalid.
@Test
void checkRangeMinOnlyInvalid() {
Entity entity = new DynamicEntity(intRangeMinMeta);
entity.set("id", "123");
entity.set("intrangemin", -1);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMinMeta);
assertEquals(1, constraints.size());
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkRangeMaxOnly.
@Test
void checkRangeMaxOnly() {
Entity entity = new DynamicEntity(intRangeMaxMeta);
entity.set("id", "123");
entity.set("intrangemin", 0);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMaxMeta);
assertTrue(constraints.isEmpty());
}
Aggregations