@Test void testDefaultXrefSkipEntityReferenceValidation() { String refEntityTypeId = "refEntityTypeId"; String refIdAttributeName = "refIdAttributeName"; Attribute refIdAttribute = mock(Attribute.class); when(refIdAttribute.getDataType()).thenReturn(STRING); when(refIdAttribute.getName()).thenReturn(refIdAttributeName); EntityType refEntityType = mock(EntityType.class); when(refEntityType.getId()).thenReturn(refEntityTypeId); when(refEntityType.getIdAttribute()).thenReturn(refIdAttribute); Attribute attr = mock(Attribute.class); when(attr.getDefaultValue()).thenReturn("invalidEntityId"); when(attr.getDataType()).thenReturn(AttributeType.XREF); when(attr.getRefEntity()).thenReturn(refEntityType); attributeValidator.validateDefaultValue(attr, false); }
void validateDefaultValue(Attribute attr, boolean validateEntityReferences) { String value = attr.getDefaultValue(); if (value != null) { if (attr.isUnique()) { throw new MolgenisDataException( "Unique attribute " + attr.getName() + " cannot have default value"); } if (attr.getExpression() != null) { throw new MolgenisDataException( "Computed attribute " + attr.getName() + " cannot have default value"); } AttributeType fieldType = attr.getDataType(); if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) { throw new MolgenisDataException( "Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name()); } if (fieldType == AttributeType.EMAIL) { checkEmail(value); } if (fieldType == AttributeType.HYPERLINK) { checkHyperlink(value); } if (fieldType == AttributeType.ENUM) { checkEnum(attr, value); } Object typedValue; try { typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Invalid default value [%s] for data type [%s]", value, attr.getDataType()))); } if (validateEntityReferences) { if (isSingleReferenceType(attr)) { Entity refEntity = (Entity) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .getMeta() .getRepository(refEntityType) .orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())) .query() .eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()) .count() == 0) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to an unknown entity", value))); } } else if (isMultipleReferenceType(attr)) { @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .query(refEntityType.getId()) .in( refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())) .count() < Iterables.size(refEntitiesValue)) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to one or more unknown entities", value))); } } } } }
AttributeValidator { void validateDefaultValue(Attribute attr, boolean validateEntityReferences) { String value = attr.getDefaultValue(); if (value != null) { if (attr.isUnique()) { throw new MolgenisDataException( "Unique attribute " + attr.getName() + " cannot have default value"); } if (attr.getExpression() != null) { throw new MolgenisDataException( "Computed attribute " + attr.getName() + " cannot have default value"); } AttributeType fieldType = attr.getDataType(); if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) { throw new MolgenisDataException( "Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name()); } if (fieldType == AttributeType.EMAIL) { checkEmail(value); } if (fieldType == AttributeType.HYPERLINK) { checkHyperlink(value); } if (fieldType == AttributeType.ENUM) { checkEnum(attr, value); } Object typedValue; try { typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Invalid default value [%s] for data type [%s]", value, attr.getDataType()))); } if (validateEntityReferences) { if (isSingleReferenceType(attr)) { Entity refEntity = (Entity) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .getMeta() .getRepository(refEntityType) .orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())) .query() .eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()) .count() == 0) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to an unknown entity", value))); } } else if (isMultipleReferenceType(attr)) { @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .query(refEntityType.getId()) .in( refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())) .count() < Iterables.size(refEntitiesValue)) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to one or more unknown entities", value))); } } } } } }
AttributeValidator { void validateDefaultValue(Attribute attr, boolean validateEntityReferences) { String value = attr.getDefaultValue(); if (value != null) { if (attr.isUnique()) { throw new MolgenisDataException( "Unique attribute " + attr.getName() + " cannot have default value"); } if (attr.getExpression() != null) { throw new MolgenisDataException( "Computed attribute " + attr.getName() + " cannot have default value"); } AttributeType fieldType = attr.getDataType(); if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) { throw new MolgenisDataException( "Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name()); } if (fieldType == AttributeType.EMAIL) { checkEmail(value); } if (fieldType == AttributeType.HYPERLINK) { checkHyperlink(value); } if (fieldType == AttributeType.ENUM) { checkEnum(attr, value); } Object typedValue; try { typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Invalid default value [%s] for data type [%s]", value, attr.getDataType()))); } if (validateEntityReferences) { if (isSingleReferenceType(attr)) { Entity refEntity = (Entity) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .getMeta() .getRepository(refEntityType) .orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())) .query() .eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()) .count() == 0) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to an unknown entity", value))); } } else if (isMultipleReferenceType(attr)) { @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .query(refEntityType.getId()) .in( refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())) .count() < Iterables.size(refEntitiesValue)) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to one or more unknown entities", value))); } } } } } AttributeValidator(DataService dataService, EntityManager entityManager); }
AttributeValidator { void validateDefaultValue(Attribute attr, boolean validateEntityReferences) { String value = attr.getDefaultValue(); if (value != null) { if (attr.isUnique()) { throw new MolgenisDataException( "Unique attribute " + attr.getName() + " cannot have default value"); } if (attr.getExpression() != null) { throw new MolgenisDataException( "Computed attribute " + attr.getName() + " cannot have default value"); } AttributeType fieldType = attr.getDataType(); if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) { throw new MolgenisDataException( "Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name()); } if (fieldType == AttributeType.EMAIL) { checkEmail(value); } if (fieldType == AttributeType.HYPERLINK) { checkHyperlink(value); } if (fieldType == AttributeType.ENUM) { checkEnum(attr, value); } Object typedValue; try { typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Invalid default value [%s] for data type [%s]", value, attr.getDataType()))); } if (validateEntityReferences) { if (isSingleReferenceType(attr)) { Entity refEntity = (Entity) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .getMeta() .getRepository(refEntityType) .orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())) .query() .eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()) .count() == 0) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to an unknown entity", value))); } } else if (isMultipleReferenceType(attr)) { @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .query(refEntityType.getId()) .in( refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())) .count() < Iterables.size(refEntitiesValue)) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to one or more unknown entities", value))); } } } } } AttributeValidator(DataService dataService, EntityManager entityManager); void validate(Attribute attr, ValidationMode validationMode); }
AttributeValidator { void validateDefaultValue(Attribute attr, boolean validateEntityReferences) { String value = attr.getDefaultValue(); if (value != null) { if (attr.isUnique()) { throw new MolgenisDataException( "Unique attribute " + attr.getName() + " cannot have default value"); } if (attr.getExpression() != null) { throw new MolgenisDataException( "Computed attribute " + attr.getName() + " cannot have default value"); } AttributeType fieldType = attr.getDataType(); if (fieldType.getMaxLength() != null && value.length() > fieldType.getMaxLength()) { throw new MolgenisDataException( "Default value for attribute [" + attr.getName() + "] exceeds the maximum length for datatype " + attr.getDataType().name()); } if (fieldType == AttributeType.EMAIL) { checkEmail(value); } if (fieldType == AttributeType.HYPERLINK) { checkHyperlink(value); } if (fieldType == AttributeType.ENUM) { checkEnum(attr, value); } Object typedValue; try { typedValue = EntityUtils.getTypedValue(value, attr, entityManager); } catch (NumberFormatException e) { throw new MolgenisValidationException( new ConstraintViolation( format( "Invalid default value [%s] for data type [%s]", value, attr.getDataType()))); } if (validateEntityReferences) { if (isSingleReferenceType(attr)) { Entity refEntity = (Entity) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .getMeta() .getRepository(refEntityType) .orElseThrow(() -> new UnknownRepositoryException(refEntityType.getId())) .query() .eq(refEntityType.getIdAttribute().getName(), refEntity.getIdValue()) .count() == 0) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to an unknown entity", value))); } } else if (isMultipleReferenceType(attr)) { @SuppressWarnings("unchecked") Iterable<Entity> refEntitiesValue = (Iterable<Entity>) typedValue; EntityType refEntityType = attr.getRefEntity(); if (dataService .query(refEntityType.getId()) .in( refEntityType.getIdAttribute().getName(), stream(refEntitiesValue).map(Entity::getIdValue).collect(toList())) .count() < Iterables.size(refEntitiesValue)) { throw new MolgenisValidationException( new ConstraintViolation( format("Default value [%s] refers to one or more unknown entities", value))); } } } } } AttributeValidator(DataService dataService, EntityManager entityManager); void validate(Attribute attr, ValidationMode validationMode);  }