@Test void testUpdateIdAttribute() { String attributeId = "attributeId"; String attributeName = "attributeName"; Attribute currentAttribute = mock(Attribute.class); when(currentAttribute.getIdentifier()).thenReturn(attributeId); when(currentAttribute.getName()).thenReturn(attributeName); when(currentAttribute.isIdAttribute()).thenReturn(true); Attribute attribute = mock(Attribute.class); when(attribute.getIdentifier()).thenReturn(attributeId); when(attribute.getName()).thenReturn(attributeName); when(attribute.isIdAttribute()).thenReturn(false); when(dataService.findOneById(ATTRIBUTE_META_DATA, attributeId, Attribute.class)) .thenReturn(currentAttribute); Exception exception = assertThrows( MolgenisValidationException.class, () -> attributeValidator.validate(attribute, ValidationMode.UPDATE)); assertThat(exception.getMessage()).containsPattern("ID attribute update not allowed"); }
public void validate(Attribute attr, ValidationMode validationMode) { validateName(attr); validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE); validateParent(attr); validateChildren(attr); validateExpression(attr); switch (validationMode) { case ADD: case ADD_SKIP_ENTITY_VALIDATION: validateAdd(attr); break; case UPDATE: case UPDATE_SKIP_ENTITY_VALIDATION: Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class); if (currentAttr == null) { throw new UnknownEntityException(ATTRIBUTE_META_DATA, attr.getIdentifier()); } validateUpdate(attr, currentAttr); break; default: throw new UnexpectedEnumException(validationMode); } }
AttributeValidator { public void validate(Attribute attr, ValidationMode validationMode) { validateName(attr); validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE); validateParent(attr); validateChildren(attr); validateExpression(attr); switch (validationMode) { case ADD: case ADD_SKIP_ENTITY_VALIDATION: validateAdd(attr); break; case UPDATE: case UPDATE_SKIP_ENTITY_VALIDATION: Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class); if (currentAttr == null) { throw new UnknownEntityException(ATTRIBUTE_META_DATA, attr.getIdentifier()); } validateUpdate(attr, currentAttr); break; default: throw new UnexpectedEnumException(validationMode); } } }
AttributeValidator { public void validate(Attribute attr, ValidationMode validationMode) { validateName(attr); validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE); validateParent(attr); validateChildren(attr); validateExpression(attr); switch (validationMode) { case ADD: case ADD_SKIP_ENTITY_VALIDATION: validateAdd(attr); break; case UPDATE: case UPDATE_SKIP_ENTITY_VALIDATION: Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class); if (currentAttr == null) { throw new UnknownEntityException(ATTRIBUTE_META_DATA, attr.getIdentifier()); } validateUpdate(attr, currentAttr); break; default: throw new UnexpectedEnumException(validationMode); } } AttributeValidator(DataService dataService, EntityManager entityManager); }
AttributeValidator { public void validate(Attribute attr, ValidationMode validationMode) { validateName(attr); validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE); validateParent(attr); validateChildren(attr); validateExpression(attr); switch (validationMode) { case ADD: case ADD_SKIP_ENTITY_VALIDATION: validateAdd(attr); break; case UPDATE: case UPDATE_SKIP_ENTITY_VALIDATION: Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class); if (currentAttr == null) { throw new UnknownEntityException(ATTRIBUTE_META_DATA, attr.getIdentifier()); } validateUpdate(attr, currentAttr); break; default: throw new UnexpectedEnumException(validationMode); } } AttributeValidator(DataService dataService, EntityManager entityManager); void validate(Attribute attr, ValidationMode validationMode); }
AttributeValidator { public void validate(Attribute attr, ValidationMode validationMode) { validateName(attr); validateDefaultValue(attr, validationMode == ADD || validationMode == UPDATE); validateParent(attr); validateChildren(attr); validateExpression(attr); switch (validationMode) { case ADD: case ADD_SKIP_ENTITY_VALIDATION: validateAdd(attr); break; case UPDATE: case UPDATE_SKIP_ENTITY_VALIDATION: Attribute currentAttr = dataService.findOneById(ATTRIBUTE_META_DATA, attr.getIdentifier(), Attribute.class); if (currentAttr == null) { throw new UnknownEntityException(ATTRIBUTE_META_DATA, attr.getIdentifier()); } validateUpdate(attr, currentAttr); break; default: throw new UnexpectedEnumException(validationMode); } } AttributeValidator(DataService dataService, EntityManager entityManager); void validate(Attribute attr, ValidationMode validationMode);  }