SchemaElementDefinitionValidator { protected ValidationResult validateFunctionArgumentTypes( final ElementFilter filter, final SchemaElementDefinition schemaElDef) { final ValidationResult result = new ValidationResult(); if (null != filter && null != filter.getComponents()) { for (final TupleAdaptedPredicate<String, ?> adaptedPredicate : filter.getComponents()) { if (null == adaptedPredicate.getPredicate()) { result.addError(filter.getClass().getSimpleName() + " contains a null function."); } else { final Signature inputSig = Signature.getInputSignature(adaptedPredicate.getPredicate()); result.add(inputSig.assignable(getTypeClasses(adaptedPredicate.getSelection(), schemaElDef))); } } } return result; }  ValidationResult validate(final SchemaElementDefinition elementDef);  }
@Test public void shouldValidateFunctionSelectionsAndReturnFalseWhenAFunctionIsNull() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final ElementFilter elementFilter = new ElementFilter.Builder() .select("selection") .execute(null) .build(); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef); assertFalse(result.isValid()); assertTrue(result.getErrorString().contains("null function")); }
@Test public void shouldValidateFunctionSelectionsAndReturnTrueWhenNoFunctionsSet() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final ElementFilter elementFilter = new ElementFilter(); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef); assertTrue(result.isValid()); }
@Test public void shouldValidateFunctionSelectionsAndReturnTrueWhenElementFilterIsNull() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final ElementFilter elementFilter = null; final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef); assertTrue(result.isValid()); }
@Test public void shouldValidateFunctionSelectionsAndReturnFalseWhenFunctionTypeDoesNotEqualSelectionType() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); given(elementDef.getPropertyClass("selection")).willReturn((Class) String.class); final IsMoreThan function = new IsMoreThan(5); final ElementFilter elementFilter = new ElementFilter.Builder() .select("selection") .execute(function) .build(); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef); assertFalse(result.isValid()); assertEquals("Validation errors: \nControl value class java.lang.Integer is not compatible" + " with the input type: class java.lang.String", result.getErrorString()); }
@Test public void shouldValidateFunctionSelectionsAndReturnTrueWhenAllFunctionsAreValid() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); given(elementDef.getPropertyClass("selectionStr")).willReturn((Class) String.class); given(elementDef.getPropertyClass("selectionInt")).willReturn((Class) Integer.class); final Predicate<String> function1 = a -> a.contains("true"); final Predicate<Integer> function2 = a -> a > 0; final ElementFilter elementFilter = new ElementFilter.Builder() .select("selectionStr") .execute(function1) .select("selectionInt") .execute(function2) .build(); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final ValidationResult result = validator.validateFunctionArgumentTypes(elementFilter, elementDef); assertTrue(result.isValid()); }