@Test void testValidateEnumConstraint() { String attributeName = "attr"; Attribute attribute = when(mock(Attribute.class).getDataType()).thenReturn(ENUM).getMock(); List<String> enumOptions = asList("enum0", "enum1", "enum2"); when(attribute.getEnumOptions()).thenReturn(enumOptions); when(attribute.getName()).thenReturn(attributeName); EntityType entityType = when(mock(EntityType.class).getAtomicAttributes()) .thenReturn(singletonList(attribute)) .getMock(); String value = "invalidEnum"; Entity entity = when(mock(Entity.class).getEntityType()).thenReturn(entityType).getMock(); when(entity.getString(attribute)).thenReturn(value); Errors errors = mock(Errors.class); entityValidator.validate(entity, errors); verify(errors) .rejectValue( attributeName, "constraints.Enum", new Object[] {String.join(", ", enumOptions)}, null); }
private void validateEnumConstraint(Entity entity, Attribute attribute, Errors errors) { if (attribute.getDataType() != AttributeType.ENUM) { return; } String enumValue = entity.getString(attribute); if (enumValue == null) { return; } List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(enumValue)) { errors.rejectValue( attribute.getName(), ERROR_CODE_ENUM, new Object[] {String.join(", ", enumOptions)}, null); } }
EntityValidator implements Validator { private void validateEnumConstraint(Entity entity, Attribute attribute, Errors errors) { if (attribute.getDataType() != AttributeType.ENUM) { return; } String enumValue = entity.getString(attribute); if (enumValue == null) { return; } List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(enumValue)) { errors.rejectValue( attribute.getName(), ERROR_CODE_ENUM, new Object[] {String.join(", ", enumOptions)}, null); } } }
EntityValidator implements Validator { private void validateEnumConstraint(Entity entity, Attribute attribute, Errors errors) { if (attribute.getDataType() != AttributeType.ENUM) { return; } String enumValue = entity.getString(attribute); if (enumValue == null) { return; } List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(enumValue)) { errors.rejectValue( attribute.getName(), ERROR_CODE_ENUM, new Object[] {String.join(", ", enumOptions)}, null); } } EntityValidator(ExpressionValidator expressionValidator); }
EntityValidator implements Validator { private void validateEnumConstraint(Entity entity, Attribute attribute, Errors errors) { if (attribute.getDataType() != AttributeType.ENUM) { return; } String enumValue = entity.getString(attribute); if (enumValue == null) { return; } List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(enumValue)) { errors.rejectValue( attribute.getName(), ERROR_CODE_ENUM, new Object[] {String.join(", ", enumOptions)}, null); } } EntityValidator(ExpressionValidator expressionValidator); @Override boolean supports(@Nonnull Class<?> clazz); @Override void validate(@Nonnull Object target, @Nonnull Errors errors); }
EntityValidator implements Validator { private void validateEnumConstraint(Entity entity, Attribute attribute, Errors errors) { if (attribute.getDataType() != AttributeType.ENUM) { return; } String enumValue = entity.getString(attribute); if (enumValue == null) { return; } List<String> enumOptions = attribute.getEnumOptions(); if (!enumOptions.contains(enumValue)) { errors.rejectValue( attribute.getName(), ERROR_CODE_ENUM, new Object[] {String.join(", ", enumOptions)}, null); } } EntityValidator(ExpressionValidator expressionValidator); @Override boolean supports(@Nonnull Class<?> clazz); @Override void validate(@Nonnull Object target, @Nonnull Errors errors);  }