Search in sources :

Example 26 with ConstraintViolation

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));
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 27 with ConstraintViolation

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());
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 28 with ConstraintViolation

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());
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) DynamicEntity(org.molgenis.data.support.DynamicEntity) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 29 with ConstraintViolation

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);
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) Test(org.junit.jupiter.api.Test) AbstractMockitoTest(org.molgenis.test.AbstractMockitoTest) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 30 with ConstraintViolation

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());
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) DynamicEntity(org.molgenis.data.support.DynamicEntity) Entity(org.molgenis.data.Entity) Attribute(org.molgenis.data.meta.model.Attribute) ConstraintViolation(org.molgenis.validation.ConstraintViolation) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

ConstraintViolation (org.molgenis.validation.ConstraintViolation)36 Entity (org.molgenis.data.Entity)22 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)14 Attribute (org.molgenis.data.meta.model.Attribute)14 DynamicEntity (org.molgenis.data.support.DynamicEntity)13 Test (org.junit.jupiter.api.Test)12 EntityType (org.molgenis.data.meta.model.EntityType)11 MolgenisValidationException (org.molgenis.data.validation.MolgenisValidationException)10 MethodSource (org.junit.jupiter.params.provider.MethodSource)6 AbstractMockitoTest (org.molgenis.test.AbstractMockitoTest)6 AttributeType (org.molgenis.data.meta.AttributeType)4 UnexpectedEnumException (org.molgenis.util.UnexpectedEnumException)4 Collectors.toList (java.util.stream.Collectors.toList)3 MolgenisDataException (org.molgenis.data.MolgenisDataException)3 Lists.newArrayList (com.google.common.collect.Lists.newArrayList)2 Streams.stream (com.google.common.collect.Streams.stream)2 String.format (java.lang.String.format)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Objects.requireNonNull (java.util.Objects.requireNonNull)2