Search in sources :

Example 6 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class DoubleConverterTest method checkAnnotations.

@Test
public void checkAnnotations() throws Exception {
    Class<?> aClass = DoubleConverter.class;
    Method[] methods = aClass.getMethods();
    for (Method method : methods) {
        if (method.isSynthetic()) {
            // We are running in Eclipse or jacoco
            continue;
        }
        if (method.getName().startsWith("convert")) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            assertNotNull(annotations);
            assertTrue(annotations.length > 0);
            for (Annotation annotation : annotations) {
                assertTrue(AtlasConversionInfo.class.isAssignableFrom(annotation.annotationType()));
                AtlasConversionInfo atlasConversionInfo = (AtlasConversionInfo) annotation;
                assertNotNull(atlasConversionInfo.sourceType());
                assertTrue(atlasConversionInfo.sourceType().compareTo(FieldType.DOUBLE) == 0);
                assertNotNull(atlasConversionInfo.targetType());
                for (AtlasConversionConcern atlasConversionConcern : atlasConversionInfo.concerns()) {
                    assertNotNull(atlasConversionConcern.getMessage(atlasConversionInfo));
                    assertNotNull(atlasConversionConcern.value());
                }
            }
        }
    }
}
Also used : AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Test(org.junit.jupiter.api.Test)

Example 7 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo 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));
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) AtlasConversionService(io.atlasmap.spi.AtlasConversionService) ValidationScope(io.atlasmap.v2.ValidationScope) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) FieldType(io.atlasmap.v2.FieldType) AtlasConverter(io.atlasmap.spi.AtlasConverter) Validation(io.atlasmap.v2.Validation) List(java.util.List) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) Optional(java.util.Optional) MappingFieldPairValidator(io.atlasmap.core.validate.MappingFieldPairValidator) Method(java.lang.reflect.Method) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.spi.AtlasConverter) Method(java.lang.reflect.Method)

Example 8 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class FloatConverterTest method checkAnnotations.

@Test
public void checkAnnotations() throws Exception {
    Class<?> aClass = FloatConverter.class;
    Method[] methods = aClass.getMethods();
    for (Method method : methods) {
        if (method.isSynthetic()) {
            // We are running in Eclipse or jacoco
            continue;
        }
        if (method.getName().startsWith("convert")) {
            Annotation[] annotations = method.getDeclaredAnnotations();
            assertNotNull(annotations);
            assertTrue(annotations.length > 0);
            for (Annotation annotation : annotations) {
                assertTrue(AtlasConversionInfo.class.isAssignableFrom(annotation.annotationType()));
                AtlasConversionInfo atlasConversionInfo = (AtlasConversionInfo) annotation;
                assertNotNull(atlasConversionInfo.sourceType());
                assertTrue(atlasConversionInfo.sourceType().compareTo(FieldType.FLOAT) == 0);
                assertNotNull(atlasConversionInfo.targetType());
                for (AtlasConversionConcern atlasConversionConcern : atlasConversionInfo.concerns()) {
                    assertNotNull(atlasConversionConcern.getMessage(atlasConversionInfo));
                    assertNotNull(atlasConversionConcern.value());
                }
            }
        }
    }
}
Also used : AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) Method(java.lang.reflect.Method) Annotation(java.lang.annotation.Annotation) Test(org.junit.jupiter.api.Test)

Example 9 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class BaseModuleValidationService method validateFieldTypeConversion.

protected void validateFieldTypeConversion(String mappingId, Field inputField, Field outField, List<Validation> validations) {
    FieldType inFieldType = inputField.getFieldType();
    FieldType outFieldType = outField.getFieldType();
    Optional<AtlasConverter<?>> atlasConverter = conversionService.findMatchingConverter(inFieldType, outFieldType);
    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.getFieldType(), outField.getFieldType()));
        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(inFieldType) == 0 && atlasConversionInfo.targetType().compareTo(outFieldType) == 0)).findFirst().orElse(null);
        if (conversionInfo != null) {
            populateConversionConcerns(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) DataSource(io.atlasmap.v2.DataSource) MappingType(io.atlasmap.v2.MappingType) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) ArrayList(java.util.ArrayList) Mapping(io.atlasmap.v2.Mapping) List(java.util.List) Field(io.atlasmap.v2.Field) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) AtlasMapping(io.atlasmap.v2.AtlasMapping) AtlasModuleMode(io.atlasmap.spi.AtlasModuleMode) Optional(java.util.Optional) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) BaseMapping(io.atlasmap.v2.BaseMapping) AtlasConverter(io.atlasmap.api.AtlasConverter) AtlasValidationService(io.atlasmap.api.AtlasValidationService) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method) FieldType(io.atlasmap.v2.FieldType)

Example 10 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo in project atlasmap by atlasmap.

the class StringConverter method toLong.

@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.LONG, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE })
public Long toLong(String value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    BigDecimal bd = null;
    Long l = null;
    try {
        l = Long.parseLong(value);
    } catch (NumberFormatException nfe) {
        try {
            bd = new BigDecimal(value);
            l = bd.longValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe);
        }
    }
    if (bd != null && bd.compareTo(BigDecimal.valueOf(l)) != 0) {
        throw new AtlasConversionException(String.format("String %s is greater than Long.MAX_VALUE  or less than Long.MIN_VALUE", value));
    }
    return l;
}
Also used : AtlasConversionException(io.atlasmap.api.AtlasConversionException) BigDecimal(java.math.BigDecimal) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo)

Aggregations

AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)22 Method (java.lang.reflect.Method)14 AtlasConversionConcern (io.atlasmap.spi.AtlasConversionConcern)12 Annotation (java.lang.annotation.Annotation)10 Test (org.junit.jupiter.api.Test)9 AtlasConversionException (io.atlasmap.api.AtlasConversionException)7 BigDecimal (java.math.BigDecimal)5 Field (io.atlasmap.v2.Field)4 FieldType (io.atlasmap.v2.FieldType)4 Validation (io.atlasmap.v2.Validation)4 ValidationScope (io.atlasmap.v2.ValidationScope)4 ValidationStatus (io.atlasmap.v2.ValidationStatus)4 Arrays (java.util.Arrays)4 List (java.util.List)4 Optional (java.util.Optional)4 AtlasConversionService (io.atlasmap.api.AtlasConversionService)2 AtlasConverter (io.atlasmap.api.AtlasConverter)2 JavaField (io.atlasmap.java.v2.JavaField)2 AtlasConverter (io.atlasmap.spi.AtlasConverter)2 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)2