Search in sources :

Example 6 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class EntityAttributesValidatorTest method checkMrefNullValue.

@ParameterizedTest
@MethodSource("checkMrefValidProvider")
void checkMrefNullValue(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 mrefAttrName = "mref";
    Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
    when(idAttr.getDataType()).thenReturn(STRING);
    Attribute mrefAttr = when(mock(Attribute.class).getName()).thenReturn(mrefAttrName).getMock();
    when(mrefAttr.getDataType()).thenReturn(attrType);
    when(mrefAttr.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("entity");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getAtomicAttributes()).thenReturn(asList(idAttr, mrefAttr));
    Entity refEntity0 = when(mock(Entity.class).getEntityType()).thenReturn(refEntityType).getMock();
    when(refEntity0.getIdValue()).thenReturn("refId0");
    Entity refEntity1 = when(mock(Entity.class).getEntityType()).thenReturn(refEntityType).getMock();
    when(refEntity1.getIdValue()).thenReturn("refId1");
    Entity entity0 = when(mock(Entity.class).getEntityType()).thenReturn(entityType).getMock();
    when(entity0.getIdValue()).thenReturn("id0");
    when(entity0.getEntities(mrefAttrName)).thenReturn(asList(refEntity0, null, refEntity1));
    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)

Example 7 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class EntityAttributesValidatorTest method checkMrefValid.

@ParameterizedTest
@MethodSource("checkMrefValidProvider")
void checkMrefValid(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 mrefAttrName = "mref";
    Attribute idAttr = when(mock(Attribute.class).getName()).thenReturn(idAttrName).getMock();
    when(idAttr.getDataType()).thenReturn(STRING);
    Attribute mrefAttr = when(mock(Attribute.class).getName()).thenReturn(mrefAttrName).getMock();
    when(mrefAttr.getDataType()).thenReturn(attrType);
    when(mrefAttr.getRefEntity()).thenReturn(refEntityType);
    EntityType entityType = mock(EntityType.class);
    when(entityType.getId()).thenReturn("entity");
    when(entityType.getIdAttribute()).thenReturn(idAttr);
    when(entityType.getAtomicAttributes()).thenReturn(asList(idAttr, mrefAttr));
    Entity refEntity0 = when(mock(Entity.class).getEntityType()).thenReturn(refEntityType).getMock();
    when(refEntity0.getIdValue()).thenReturn("refId0");
    Entity refEntity1 = when(mock(Entity.class).getEntityType()).thenReturn(refEntityType).getMock();
    when(refEntity1.getIdValue()).thenReturn("refId1");
    Entity entity0 = when(mock(Entity.class).getEntityType()).thenReturn(entityType).getMock();
    when(entity0.getIdValue()).thenReturn("id0");
    when(entity0.getEntities(mrefAttrName)).thenReturn(asList(refEntity0, refEntity1));
    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 8 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class QueryValidator method toQueryRuleValue.

private Object toQueryRuleValue(Object queryRuleValue, Attribute attr) {
    Object value;
    AttributeType attrType = attr.getDataType();
    switch(attrType) {
        case BOOL:
            value = convertBool(attr, queryRuleValue);
            break;
        case EMAIL:
        case HTML:
        case HYPERLINK:
        case SCRIPT:
        case STRING:
        case TEXT:
            value = convertString(queryRuleValue);
            break;
        case ENUM:
            value = convertEnum(attr, queryRuleValue);
            break;
        case CATEGORICAL:
        case XREF:
        case CATEGORICAL_MREF:
        case MREF:
        case ONE_TO_MANY:
            value = convertRef(attr, queryRuleValue);
            break;
        case DATE:
            value = convertDate(attr, queryRuleValue);
            break;
        case DATE_TIME:
            value = convertDateTime(attr, queryRuleValue);
            break;
        case DECIMAL:
            value = convertDecimal(attr, queryRuleValue);
            break;
        case FILE:
            value = convertFile(attr, queryRuleValue);
            break;
        case INT:
            value = convertInt(attr, queryRuleValue);
            break;
        case LONG:
            value = convertLong(attr, queryRuleValue);
            break;
        case COMPOUND:
            throw new MolgenisValidationException(new ConstraintViolation(format("Attribute [%s] type [%s] is not allowed", attr.getName(), attrType.toString())));
        default:
            throw new UnexpectedEnumException(attrType);
    }
    return value;
}
Also used : UnexpectedEnumException(org.molgenis.util.UnexpectedEnumException) AttributeType(org.molgenis.data.meta.AttributeType) ConstraintViolation(org.molgenis.validation.ConstraintViolation)

Example 9 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class RepositoryValidationDecorator method validateEntityValueUniqueness.

private void validateEntityValueUniqueness(Entity entity, ValidationResource validationResource, ValidationMode validationMode) {
    validationResource.getUniqueAttrs().forEach(uniqueAttr -> {
        Object attrValue = entity.get(uniqueAttr.getName());
        if (attrValue != null) {
            if (isSingleReferenceType(uniqueAttr)) {
                attrValue = ((Entity) attrValue).getIdValue();
            }
            HugeMap<Object, Object> uniqueAttrValues = validationResource.getUniqueAttrsValues().get(uniqueAttr.getName());
            Object existingEntityId = uniqueAttrValues.get(attrValue);
            if ((validationMode == ValidationMode.ADD && existingEntityId != null) || (validationMode == ValidationMode.UPDATE && existingEntityId != null && !existingEntityId.equals(entity.getIdValue()))) {
                ConstraintViolation constraintViolation = new ConstraintViolation(format("Duplicate value '%s' for unique attribute '%s' from entity '%s'", attrValue, uniqueAttr.getName(), getName()), (long) validationResource.getRow());
                validationResource.addViolation(constraintViolation);
            } else {
                uniqueAttrValues.put(attrValue, entity.getIdValue());
            }
        }
    });
}
Also used : ConstraintViolation(org.molgenis.validation.ConstraintViolation)

Example 10 with ConstraintViolation

use of org.molgenis.validation.ConstraintViolation in project molgenis by molgenis.

the class RepositoryValidationDecorator method validateEntityValueReadOnly.

@SuppressWarnings("unchecked")
private void validateEntityValueReadOnly(Entity entity, ValidationResource validationResource) {
    if (validationResource.getReadonlyAttrs().isEmpty()) {
        return;
    }
    Entity entityToUpdate = findOneById(entity.getIdValue());
    validationResource.getReadonlyAttrs().forEach(readonlyAttr -> {
        Object value = entity.get(readonlyAttr.getName());
        Object existingValue = entityToUpdate.get(readonlyAttr.getName());
        if (isSingleReferenceType(readonlyAttr)) {
            if (value != null) {
                value = ((Entity) value).getIdValue();
            }
            if (existingValue != null) {
                existingValue = ((Entity) existingValue).getIdValue();
            }
        } else if (isMultipleReferenceType(readonlyAttr)) {
            value = stream(entity.getEntities(readonlyAttr.getName())).map(Entity::getIdValue).collect(toList());
            existingValue = stream(entityToUpdate.getEntities(readonlyAttr.getName())).map(Entity::getIdValue).collect(toList());
        }
        if (value != null && existingValue != null && !value.equals(existingValue)) {
            validationResource.addViolation(new ConstraintViolation(format("The attribute '%s' of entity '%s' can not be changed it is readonly.", readonlyAttr.getName(), getName()), (long) validationResource.getRow()));
        }
    });
}
Also used : Entity(org.molgenis.data.Entity) ConstraintViolation(org.molgenis.validation.ConstraintViolation)

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