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());
}
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());
}
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;
}
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());
}
}
});
}
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()));
}
});
}
Aggregations