Search in sources :

Example 16 with AtlasConversionInfo

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

the class IntegerConverterTest method checkAnnotations.

@Test
public void checkAnnotations() throws Exception {
    Class<?> aClass = IntegerConverter.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.INTEGER) == 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 17 with AtlasConversionInfo

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

the class LongConverterTest method checkAnnotations.

@Test
public void checkAnnotations() throws Exception {
    Class<?> aClass = LongConverter.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.LONG) == 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 18 with AtlasConversionInfo

use of io.atlasmap.spi.AtlasConversionInfo 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));
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Arrays(java.util.Arrays) FieldGroup(io.atlasmap.v2.FieldGroup) Logger(org.slf4j.Logger) AtlasException(io.atlasmap.api.AtlasException) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) ValidationStatus(io.atlasmap.v2.ValidationStatus) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern) ActionDetail(io.atlasmap.v2.ActionDetail) FieldType(io.atlasmap.v2.FieldType) AtlasConverter(io.atlasmap.spi.AtlasConverter) Validation(io.atlasmap.v2.Validation) List(java.util.List) Field(io.atlasmap.v2.Field) Action(io.atlasmap.v2.Action) Optional(java.util.Optional) Method(java.lang.reflect.Method) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.spi.AtlasConverter) Method(java.lang.reflect.Method) FieldType(io.atlasmap.v2.FieldType)

Example 19 with AtlasConversionInfo

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

the class BooleanConverterTest method checkAnnotations.

@Test
public void checkAnnotations() throws Exception {
    Class<?> aClass = BooleanConverter.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.BOOLEAN) == 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 20 with AtlasConversionInfo

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

the class CharSequenceConverter method toShort.

/**
 * Converts to {@link Short}.
 * @param value value
 * @return converted
 * @throws AtlasConversionException out of range or not a number
 */
@AtlasConversionInfo(sourceType = FieldType.STRING, targetType = FieldType.SHORT, concerns = { AtlasConversionConcern.FORMAT, AtlasConversionConcern.RANGE, AtlasConversionConcern.FRACTIONAL_PART })
public Short toShort(CharSequence value) throws AtlasConversionException {
    if (value == null) {
        return null;
    }
    String str = value.toString();
    Short shortty = null;
    try {
        shortty = Short.parseShort(str);
    } catch (NumberFormatException nfe) {
        try {
            BigDecimal bd = new BigDecimal(str);
            if (bd.compareTo(new BigDecimal(Short.MIN_VALUE)) < 0 || bd.compareTo(new BigDecimal(Short.MAX_VALUE)) > 0) {
                throw new AtlasConversionException(String.format("String %s is greater than Short.MAX_VALUE  or less than Short.MIN_VALUE", str));
            }
            shortty = bd.shortValue();
        } catch (NumberFormatException nfe2) {
            throw new AtlasConversionException(nfe2);
        }
    }
    return shortty;
}
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