Search in sources :

Example 56 with Validation

use of io.atlasmap.v2.Validation 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 57 with Validation

use of io.atlasmap.v2.Validation in project atlasmap by atlasmap.

the class MappingFieldPairValidator method populateConversionConcerns.

/**
 * Parses the {@link AtlasConversionInfo} annotation and add {@link Validation} for each concerns.
 * @param validations a container to put the result {@link Validation}.
 * @param mappingId mapping ID
 * @param converterAnno annotation
 * @param sourceFieldName source field name
 * @param targetFieldName target field name
 */
public void populateConversionConcerns(List<Validation> validations, String mappingId, AtlasConversionInfo converterAnno, String sourceFieldName, String targetFieldName) {
    if (converterAnno == null || converterAnno.concerns() == null) {
        return;
    }
    for (AtlasConversionConcern atlasConversionConcern : converterAnno.concerns()) {
        String message = atlasConversionConcern.getMessage(converterAnno);
        if (AtlasConversionConcern.NONE.equals(atlasConversionConcern)) {
            // just supported - even INFO is verbose
            continue;
        } else if (atlasConversionConcern.equals(AtlasConversionConcern.RANGE) || atlasConversionConcern.equals(AtlasConversionConcern.FORMAT) || atlasConversionConcern.equals(AtlasConversionConcern.FRACTIONAL_PART) || atlasConversionConcern.equals(AtlasConversionConcern.TIMEZONE)) {
            Validation validation = new Validation();
            validation.setScope(ValidationScope.MAPPING);
            validation.setId(mappingId);
            validation.setMessage(message);
            validation.setStatus(ValidationStatus.WARN);
            validations.add(validation);
        } else if (atlasConversionConcern.equals(AtlasConversionConcern.UNSUPPORTED)) {
            Validation validation = new Validation();
            validation.setScope(ValidationScope.MAPPING);
            validation.setId(mappingId);
            validation.setMessage(message);
            validation.setStatus(ValidationStatus.ERROR);
            validations.add(validation);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) AtlasConversionConcern(io.atlasmap.spi.AtlasConversionConcern)

Example 58 with Validation

use of io.atlasmap.v2.Validation in project atlasmap by atlasmap.

the class MappingFieldPairValidator method validateFieldTypes.

/**
 * Validates field types by delegating to {@link #doValidateFieldTypes(List,String,Field,Field,FieldType)}.
 * @param validations a container to put the result {@link Validation}
 * @param mappingId mapping ID
 * @param sourceFields source fields
 * @param targetField target field
 */
public void validateFieldTypes(List<Validation> validations, String mappingId, List<Field> sourceFields, Field targetField) {
    for (Field sourceField : sourceFields) {
        if (!service.matchDocIdOrNull(sourceField.getDocId())) {
            return;
        }
        FieldType actionOutputType = getActionOutputFieldType(validations, mappingId, sourceField);
        doValidateFieldTypes(validations, mappingId, sourceField, targetField, actionOutputType != null ? actionOutputType : sourceField.getFieldType());
    }
}
Also used : Field(io.atlasmap.v2.Field) FieldType(io.atlasmap.v2.FieldType)

Example 59 with Validation

use of io.atlasmap.v2.Validation in project atlasmap by atlasmap.

the class LookupTableNameValidator method validate.

@Override
public void validate(Object target, List<Validation> validations, String id, ValidationStatus status) {
    LookupTables lookupTables = (LookupTables) target;
    List<LookupTable> tables = lookupTables.getLookupTable();
    List<LookupTable> deduped = Collections.unmodifiableList(tables).stream().filter(distinctByKey(LookupTable::getName)).collect(Collectors.toList());
    if (deduped.size() != tables.size()) {
        String dupedName = findDuplicatedName(tables);
        Validation validation = new Validation();
        validation.setScope(ValidationScope.LOOKUP_TABLE);
        validation.setId(id);
        validation.setMessage(String.format(violationMessage, dupedName));
        validation.setStatus(status);
        validations.add(validation);
    }
}
Also used : Validation(io.atlasmap.v2.Validation) LookupTables(io.atlasmap.v2.LookupTables) LookupTable(io.atlasmap.v2.LookupTable)

Example 60 with Validation

use of io.atlasmap.v2.Validation in project atlasmap by atlasmap.

the class NotEmptyValidator method validate.

@Override
public void validate(Object target, List<Validation> validations, String id, ValidationStatus status) {
    if (!supports(target)) {
        return;
    }
    if (((Collection<?>) target).isEmpty()) {
        Validation validation = new Validation();
        validation.setScope(scope);
        validation.setId(id);
        validation.setMessage(this.violationMessage);
        validation.setStatus(status);
        validations.add(validation);
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Collection(java.util.Collection)

Aggregations

Validation (io.atlasmap.v2.Validation)55 AtlasMapping (io.atlasmap.v2.AtlasMapping)18 Field (io.atlasmap.v2.Field)16 Test (org.junit.jupiter.api.Test)16 Mapping (io.atlasmap.v2.Mapping)14 Validations (io.atlasmap.v2.Validations)10 JavaField (io.atlasmap.java.v2.JavaField)6 BaseMapping (io.atlasmap.v2.BaseMapping)6 FieldType (io.atlasmap.v2.FieldType)6 AtlasException (io.atlasmap.api.AtlasException)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 AtlasConversionConcern (io.atlasmap.spi.AtlasConversionConcern)4 AtlasConversionInfo (io.atlasmap.spi.AtlasConversionInfo)4 FieldGroup (io.atlasmap.v2.FieldGroup)4 ValidationScope (io.atlasmap.v2.ValidationScope)4 ValidationStatus (io.atlasmap.v2.ValidationStatus)4 Method (java.lang.reflect.Method)4 Arrays (java.util.Arrays)4 Optional (java.util.Optional)4