SchemaElementDefinitionValidator { protected ValidationResult validateComponentTypes(final SchemaElementDefinition elementDef) { final ValidationResult result = new ValidationResult(); for (final IdentifierType idType : elementDef.getIdentifiers()) { try { if (null == elementDef.getIdentifierClass(idType)) { result.addError("Class for " + idType + " could not be found."); } } catch (final IllegalArgumentException e) { result.addError("Class " + elementDef.getIdentifierTypeName(idType) + " for identifier " + idType + " could not be found"); } } for (final String propertyName : elementDef.getProperties()) { if (null != IdentifierType.fromName(propertyName)) { result.addError("Property name " + propertyName + " is a reserved word. Please use a different property name."); } else { try { if (null == elementDef.getPropertyClass(propertyName)) { result.addError("Class for " + propertyName + " could not be found."); } } catch (final IllegalArgumentException e) { result.addError("Class " + elementDef.getPropertyTypeName(propertyName) + " for property " + propertyName + " could not be found"); } } } return result; }  ValidationResult validate(final SchemaElementDefinition elementDef);  }
@Test public void shouldValidateComponentTypesAndReturnTrueWhenNoIdentifiersOrProperties() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); given(elementDef.getIdentifiers()).willReturn(new HashSet<>()); given(elementDef.getProperties()).willReturn(new HashSet<>()); final ValidationResult result = validator.validateComponentTypes(elementDef); assertTrue(result.isValid()); }
@Test public void shouldValidateComponentTypesAndErrorWhenPropertyNameIsAReservedWord() { for (final IdentifierType identifierType : IdentifierType.values()) { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); final Set<String> properties = new HashSet<>(); properties.add(TestPropertyNames.COUNT); properties.add(identifierType.name()); given(elementDef.getIdentifiers()).willReturn(new HashSet<>()); given(elementDef.getProperties()).willReturn(properties); final ValidationResult result = validator.validateComponentTypes(elementDef); assertFalse(result.isValid()); assertTrue(result.getErrorString().contains("reserved word")); } }
@Test public void shouldValidateComponentTypesAndReturnTrueWhenIdentifiersAndPropertiesHaveClasses() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); given(elementDef.getIdentifiers()).willReturn(Sets.newSet(IdentifierType.DESTINATION, IdentifierType.SOURCE)); given(elementDef.getProperties()).willReturn(Sets.newSet(TestPropertyNames.PROP_1, TestPropertyNames.PROP_2)); given(elementDef.getIdentifierClass(IdentifierType.DESTINATION)).willReturn((Class) Double.class); given(elementDef.getIdentifierClass(IdentifierType.SOURCE)).willReturn((Class) Long.class); given(elementDef.getPropertyClass(TestPropertyNames.PROP_1)).willReturn((Class) Integer.class); given(elementDef.getPropertyClass(TestPropertyNames.PROP_2)).willReturn((Class) String.class); final ValidationResult result = validator.validateComponentTypes(elementDef); assertTrue(result.isValid()); }
@Test public void shouldValidateComponentTypesAndReturnFalseForInvalidPropertyClass() { final SchemaElementDefinition elementDef = mock(SchemaElementDefinition.class); final SchemaElementDefinitionValidator validator = new SchemaElementDefinitionValidator(); given(elementDef.getIdentifiers()).willReturn(new HashSet<>()); given(elementDef.getProperties()).willReturn(Sets.newSet(TestPropertyNames.PROP_1)); given(elementDef.getPropertyClass(TestPropertyNames.PROP_1)).willThrow(new IllegalArgumentException()); final ValidationResult result = validator.validateComponentTypes(elementDef); assertFalse(result.isValid()); assertEquals("Validation errors: \nClass null for property property1 could not be found", result.getErrorString()); }