use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.
the class DefaultAtlasConversionServiceTest method findMatchingConverterByFieldTypes.
@Test
public void findMatchingConverterByFieldTypes() {
assertNotNull(service);
Optional<AtlasConverter<?>> atlasConverter = service.findMatchingConverter(FieldType.STRING, FieldType.BOOLEAN);
assertTrue(atlasConverter.isPresent());
assertNotNull(atlasConverter);
assertEquals(StringConverter.class, atlasConverter.get().getClass());
StringConverter stringConverter = (StringConverter) atlasConverter.get();
assertNotNull(stringConverter);
assertEquals("io.atlasmap.converters.StringConverter", atlasConverter.get().getClass().getCanonicalName());
Boolean t = stringConverter.toBoolean("T", null, null);
assertNotNull(t);
assertTrue(t);
Boolean f = stringConverter.toBoolean("F", null, null);
assertNotNull(f);
assertFalse(f);
service.findMatchingConverter(null, FieldType.BOOLEAN);
service.findMatchingConverter(FieldType.STRING, null);
FieldType fieldType = null;
service.findMatchingConverter(fieldType, fieldType);
}
use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.
the class JavaMappingFieldPairValidator method validateClassConversion.
private void validateClassConversion(String mappingId, JavaField inputField, JavaField outField, List<Validation> validations) {
// skip converter check for COMPLEX source field (possible for conditional mapping)
if (inputField.getFieldType() == FieldType.COMPLEX) {
return;
}
Optional<AtlasConverter<?>> atlasConverter = conversionService.findMatchingConverter(inputField.getClassName(), outField.getClassName());
if (!atlasConverter.isPresent()) {
Validation validation = new Validation();
validation.setScope(ValidationScope.MAPPING);
validation.setId(mappingId);
validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", inputField.getClassName(), outField.getClassName()));
validation.setStatus(ValidationStatus.ERROR);
validations.add(validation);
} else {
AtlasConversionInfo conversionInfo;
// find the method that does the conversion
Method[] methods = atlasConverter.get().getClass().getMethods();
conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> atlasConversionInfo.sourceType().compareTo(inputField.getFieldType()) == 0 && atlasConversionInfo.targetType().compareTo(outField.getFieldType()) == 0).findFirst().orElse(null);
if (conversionInfo != null) {
populateConversionConcerns(validations, mappingId, conversionInfo, service.getModuleFieldName(inputField), service.getModuleFieldName(outField));
}
}
}
use of io.atlasmap.spi.AtlasConverter in project atlasmap by atlasmap.
the class MappingFieldPairValidator method doValidateFieldTypes.
/**
* Validates field types.
* @param validations a container to put the result {@link Validation}.
* @param mappingId mapping ID
* @param sourceField source field
* @param targetField target field
* @param sourceFieldType source field type
*/
protected void doValidateFieldTypes(List<Validation> validations, String mappingId, Field sourceField, Field targetField, FieldType sourceFieldType) {
if (sourceField == null && targetField == null || sourceField.getFieldType() == targetField.getFieldType()) {
return;
}
FieldType targetFieldType = targetField.getFieldType();
if (sourceFieldType == null || targetFieldType == null) {
return;
}
if (sourceFieldType == FieldType.ANY || targetFieldType == FieldType.ANY) {
return;
}
// skip converter check for COMPLEX source field (possible for conditional mapping)
if (sourceField.getFieldType() == FieldType.COMPLEX) {
return;
}
Optional<AtlasConverter<?>> atlasConverter = service.getConversionService().findMatchingConverter(sourceFieldType, targetFieldType);
if (!atlasConverter.isPresent()) {
Validation validation = new Validation();
validation.setScope(ValidationScope.MAPPING);
validation.setId(mappingId);
validation.setMessage(String.format("Conversion from '%s' to '%s' is required but no converter is available", sourceField.getFieldType(), targetField.getFieldType()));
validation.setStatus(ValidationStatus.ERROR);
validations.add(validation);
} else {
AtlasConversionInfo conversionInfo;
// find the method that does the conversion
FieldType sft = sourceFieldType;
Method[] methods = atlasConverter.get().getClass().getMethods();
conversionInfo = Arrays.stream(methods).map(method -> method.getAnnotation(AtlasConversionInfo.class)).filter(atlasConversionInfo -> atlasConversionInfo != null).filter(atlasConversionInfo -> (atlasConversionInfo.sourceType().compareTo(sft) == 0 && atlasConversionInfo.targetType().compareTo(targetFieldType) == 0)).findFirst().orElse(null);
if (conversionInfo != null) {
populateConversionConcerns(validations, mappingId, conversionInfo, service.getFieldName(sourceField), service.getFieldName(targetField));
}
}
}
Aggregations