@Test void validateNullableExpression() { String expression0 = "expression0"; String expression1 = "expression1"; String expression2 = "expression2"; Attribute attr0 = createMockAttribute("attr0", STRING, expression0); Attribute attr1 = createMockAttribute("attr1", STRING, expression1); Attribute attr2 = createMockAttribute("attr2", MREF, expression2); Attribute attr3 = createMockAttribute("attr2", INT, null); EntityType entityType = mock(EntityType.class); when(entityType.getId()).thenReturn("entityType"); when(entityType.getAtomicAttributes()).thenReturn(asList(attr0, attr1, attr2, attr3)); when(entityType.toString()).thenReturn("entityType"); Entity entity = mock(Entity.class); when(entity.getIdValue()).thenReturn("MyEntityId"); when(entity.getLabelValue()).thenReturn("lbl-entity"); when(entity.getString("attr0")).thenReturn(null); when(entity.getString("attr1")).thenReturn(null); when(entity.getEntities("attr2")).thenReturn(emptyList()); when(entity.getInt("attr3")).thenReturn(null); List<Boolean> expressionResults = asList(true, false, false); when(expressionValidator.resolveBooleanExpressions( asList(expression0, expression1, expression2), entity)) .thenReturn(expressionResults); Set<ConstraintViolation> constraintViolations = entityAttributesValidator.validate(entity, entityType); Set<ConstraintViolation> expectedConstraintViolations = newHashSet( new ConstraintViolation( "Invalid [string] value [null] for attribute [lbl-attr1] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression1"), new ConstraintViolation( "Invalid [mref] value [[]] for attribute [lbl-attr2] of entity [lbl-entity] with type [entityType]. Offended nullable expression: expression2")); assertEquals(expectedConstraintViolations, newHashSet(constraintViolations)); }
public Set<ConstraintViolation> validate(Entity entity, EntityType meta) { Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta); violations.addAll(checkValidationExpressions(entity, meta)); stream(meta.getAtomicAttributes()) .filter(this::isValidationAttribute) .forEach(attr -> validateAttribute(entity, meta, attr).ifPresent(violations::add)); if (entity.getIdValue() == null) { violations.add(createConstraintViolation(entity, meta.getIdAttribute(), meta, null)); } return violations; }
EntityAttributesValidator { public Set<ConstraintViolation> validate(Entity entity, EntityType meta) { Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta); violations.addAll(checkValidationExpressions(entity, meta)); stream(meta.getAtomicAttributes()) .filter(this::isValidationAttribute) .forEach(attr -> validateAttribute(entity, meta, attr).ifPresent(violations::add)); if (entity.getIdValue() == null) { violations.add(createConstraintViolation(entity, meta.getIdAttribute(), meta, null)); } return violations; } }
EntityAttributesValidator { public Set<ConstraintViolation> validate(Entity entity, EntityType meta) { Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta); violations.addAll(checkValidationExpressions(entity, meta)); stream(meta.getAtomicAttributes()) .filter(this::isValidationAttribute) .forEach(attr -> validateAttribute(entity, meta, attr).ifPresent(violations::add)); if (entity.getIdValue() == null) { violations.add(createConstraintViolation(entity, meta.getIdAttribute(), meta, null)); } return violations; } EntityAttributesValidator(ExpressionValidator expressionValidator); }
EntityAttributesValidator { public Set<ConstraintViolation> validate(Entity entity, EntityType meta) { Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta); violations.addAll(checkValidationExpressions(entity, meta)); stream(meta.getAtomicAttributes()) .filter(this::isValidationAttribute) .forEach(attr -> validateAttribute(entity, meta, attr).ifPresent(violations::add)); if (entity.getIdValue() == null) { violations.add(createConstraintViolation(entity, meta.getIdAttribute(), meta, null)); } return violations; } EntityAttributesValidator(ExpressionValidator expressionValidator); Set<ConstraintViolation> validate(Entity entity, EntityType meta); }
EntityAttributesValidator { public Set<ConstraintViolation> validate(Entity entity, EntityType meta) { Set<ConstraintViolation> violations = checkNullableExpressions(entity, meta); violations.addAll(checkValidationExpressions(entity, meta)); stream(meta.getAtomicAttributes()) .filter(this::isValidationAttribute) .forEach(attr -> validateAttribute(entity, meta, attr).ifPresent(violations::add)); if (entity.getIdValue() == null) { violations.add(createConstraintViolation(entity, meta.getIdAttribute(), meta, null)); } return violations; } EntityAttributesValidator(ExpressionValidator expressionValidator); Set<ConstraintViolation> validate(Entity entity, EntityType meta);  }