use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method validateNullableExpressionWithVisibility.
@ParameterizedTest
@MethodSource("nullableExpressionWithVisibility")
void validateNullableExpressionWithVisibility(Boolean nullable, Boolean visible, boolean error) {
String nullableExpression = "nullableExpression";
String visibleExpression = "visibleExpression";
Attribute attribute = createMockAttribute("attribute", STRING, nullableExpression);
when(attribute.getVisibleExpression()).thenReturn(visibleExpression);
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("entityType");
when(entityType.getAtomicAttributes()).thenReturn(List.of(attribute));
when(entityType.toString()).thenReturn("entityType");
Entity entity = mock(Entity.class);
when(entity.getIdValue()).thenReturn("MyEntityId");
when(entity.getLabelValue()).thenReturn("lbl-entity");
when(entity.getString("attribute")).thenReturn(null);
when(simpleExpressionValidator.resolveBooleanExpressions(asList(nullableExpression, visibleExpression), entity)).thenReturn(newArrayList(nullable, visible));
Set<ConstraintViolation> constraintViolations = entityAttributesValidator.validate(entity, entityType);
Set<ConstraintViolation> expectedConstraintViolations = newHashSet();
if (error) {
expectedConstraintViolations.add(new ConstraintViolation("Invalid [string] value [null] for attribute [lbl-attribute] of entity [lbl-entity] " + "with type [entityType]. Offended nullable expression: nullableExpression"));
}
assertEquals(expectedConstraintViolations, newHashSet(constraintViolations));
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkXrefValid.
@ParameterizedTest
@MethodSource("checkXrefValidProvider")
void checkXrefValid(AttributeType attrType) {
Attribute refIdAttr = when(mock(Attribute.class).getName()).thenReturn("refId").getMock();
when(refIdAttr.getDataType()).thenReturn(STRING);
EntityType refEntityType = mock(EntityType.class);
when(refEntityType.getId()).thenReturn("refEntity");
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(refEntityType.getAtomicAttributes()).thenReturn(asList(refIdAttr));
String idAttrName = "id";
String xrefAttrName = "xref";
Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
when(idAttr.getDataType()).thenReturn(STRING);
Attribute xrefAttr = when(mock(Attribute.class).getName()).thenReturn(xrefAttrName).getMock();
when(xrefAttr.getDataType()).thenReturn(attrType);
when(xrefAttr.getRefEntity()).thenReturn(refEntityType);
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("entity");
when(entityType.getIdAttribute()).thenReturn(idAttr);
when(entityType.getAtomicAttributes()).thenReturn(asList(idAttr, xrefAttr));
Entity refEntity0 = when(mock(Entity.class).getEntityType()).thenReturn(refEntityType).getMock();
when(refEntity0.getIdValue()).thenReturn("refId0");
Entity entity0 = when(mock(Entity.class).getEntityType()).thenReturn(entityType).getMock();
when(entity0.getIdValue()).thenReturn("id0");
when(entity0.getEntity(xrefAttrName)).thenReturn(refEntity0);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityType());
assertEquals(0, constraints.size());
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkRangeMinOnly.
@Test
void checkRangeMinOnly() {
Entity entity = new DynamicEntity(intRangeMinMeta);
entity.set("id", "123");
entity.set("intrangemin", 2);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity, intRangeMinMeta);
assertTrue(constraints.isEmpty());
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method testValidateIdValueNull.
@Test
void testValidateIdValueNull() {
Entity entity = mock(Entity.class);
when(entity.getLabelValue()).thenReturn("My entity");
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("MyEntityTypeId");
Attribute idAttribute = mock(Attribute.class);
when(idAttribute.getDataType()).thenReturn(STRING);
when(idAttribute.getLabel()).thenReturn("id");
when(entityType.getIdAttribute()).thenReturn(idAttribute);
Set<ConstraintViolation> constraintViolations = entityAttributesValidator.validate(entity, entityType);
assertEquals(singleton(new ConstraintViolation("Invalid [string] value [null] for attribute [id] of entity [My entity] with type [MyEntityTypeId].")), constraintViolations);
}
use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.
the class EntityAttributesValidatorTest method checkXrefEntityWrongType.
@ParameterizedTest
@MethodSource("checkXrefValidProvider")
void checkXrefEntityWrongType(AttributeType attrType) {
Attribute refIdAttr = when(mock(Attribute.class).getName()).thenReturn("refId").getMock();
when(refIdAttr.getDataType()).thenReturn(STRING);
EntityType refEntityType = mock(EntityType.class);
when(refEntityType.getId()).thenReturn("refEntity");
when(refEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(refEntityType.getAtomicAttributes()).thenReturn(asList(refIdAttr));
Attribute otherRefIdAttr = when(mock(Attribute.class).getName()).thenReturn("otherRefId").getMock();
when(otherRefIdAttr.getDataType()).thenReturn(STRING);
EntityType otherRefEntityType = mock(EntityType.class);
when(otherRefEntityType.getId()).thenReturn("otherRefEntity");
when(otherRefEntityType.getIdAttribute()).thenReturn(refIdAttr);
when(otherRefEntityType.getAtomicAttributes()).thenReturn(asList(otherRefIdAttr));
String idAttrName = "id";
String xrefAttrName = "xref";
Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
when(idAttr.getDataType()).thenReturn(STRING);
Attribute xrefAttr = when(mock(Attribute.class).getName()).thenReturn(xrefAttrName).getMock();
when(xrefAttr.getDataType()).thenReturn(attrType);
when(xrefAttr.getRefEntity()).thenReturn(refEntityType);
EntityType entityType = mock(EntityType.class);
when(entityType.getId()).thenReturn("entity");
when(entityType.getIdAttribute()).thenReturn(idAttr);
when(entityType.getAtomicAttributes()).thenReturn(asList(idAttr, xrefAttr));
Entity refEntity0 = // wrong
when(mock(Entity.class).getEntityType()).thenReturn(otherRefEntityType).getMock();
// intRangeMinMeta
when(refEntity0.getIdValue()).thenReturn("otherRefId0");
Entity entity0 = when(mock(Entity.class).getEntityType()).thenReturn(entityType).getMock();
when(entity0.getIdValue()).thenReturn("id0");
when(entity0.getEntity(xrefAttrName)).thenReturn(refEntity0);
Set<ConstraintViolation> constraints = entityAttributesValidator.validate(entity0, entity0.getEntityType());
assertEquals(1, constraints.size());
}
Aggregations