Search in sources :

Example 6 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class JavaValidationServiceTest method testValidateMappingSourceToTargetRangeConcerns.

@Test
public void testValidateMappingSourceToTargetRangeConcerns() throws Exception {
    AtlasMapping mapping = mappingUtil.loadMapping("src/test/resources/mappings/HappyPathMapping.xml");
    assertNotNull(mapping);
    Mapping fieldMapping = (Mapping) mapping.getMappings().getMapping().get(0);
    JavaField in = (JavaField) fieldMapping.getInputField().get(0);
    in.setFieldType(FieldType.DOUBLE);
    in.setClassName("java.lang.Double");
    JavaField out = (JavaField) fieldMapping.getOutputField().get(0);
    out.setFieldType(FieldType.LONG);
    out.setClassName("java.lang.Long");
    validations.addAll(sourceValidationService.validateMapping(mapping));
    validations.addAll(targetValidationService.validateMapping(mapping));
    if (LOG.isDebugEnabled()) {
        debugErrors(validations);
    }
    assertFalse(validationHelper.hasErrors());
    assertTrue(validationHelper.hasWarnings());
    assertFalse(validationHelper.hasInfos());
    assertThat(1, is(validationHelper.getCount()));
    assertTrue(validations.stream().anyMatch(atlasMappingError -> atlasMappingError.getMessage().contains("range")));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) DefaultAtlasConversionService(io.atlasmap.core.DefaultAtlasConversionService) AtlasValidationTestHelper(io.atlasmap.validators.AtlasValidationTestHelper) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) DataSource(io.atlasmap.v2.DataSource) MappingType(io.atlasmap.v2.MappingType) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) AtlasJavaModelFactory(io.atlasmap.java.v2.AtlasJavaModelFactory) Assert.assertThat(org.junit.Assert.assertThat) AtlasModelFactory(io.atlasmap.v2.AtlasModelFactory) After(org.junit.After) AtlasModuleMode(io.atlasmap.spi.AtlasModuleMode) Collector(java.util.stream.Collector) Before(org.junit.Before) AtlasConstants(io.atlasmap.api.AtlasConstants) Logger(org.slf4j.Logger) Assert.assertNotNull(org.junit.Assert.assertNotNull) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) ValidationStatus(io.atlasmap.v2.ValidationStatus) Collectors(java.util.stream.Collectors) DataSourceType(io.atlasmap.v2.DataSourceType) AtlasMappingUtil(io.atlasmap.core.AtlasMappingUtil) Mapping(io.atlasmap.v2.Mapping) List(java.util.List) MockField(io.atlasmap.v2.MockField) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) Assert.assertFalse(org.junit.Assert.assertFalse) AtlasMapping(io.atlasmap.v2.AtlasMapping) JavaField(io.atlasmap.java.v2.JavaField) Assert.assertEquals(org.junit.Assert.assertEquals) AtlasMapping(io.atlasmap.v2.AtlasMapping) JavaField(io.atlasmap.java.v2.JavaField) Mapping(io.atlasmap.v2.Mapping) AtlasMapping(io.atlasmap.v2.AtlasMapping) Test(org.junit.Test)

Example 7 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class JavaValidationService method validateClassConversion.

private void validateClassConversion(String mappingId, JavaField inputField, JavaField outField, List<Validation> validations) {
    Optional<AtlasConverter<?>> atlasConverter = getConversionService().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.WARN);
        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(mappingId, conversionInfo, getFieldName(inputField), getFieldName(outField), validations);
        }
    }
}
Also used : Validation(io.atlasmap.v2.Validation) DataInputStream(java.io.DataInputStream) Arrays(java.util.Arrays) FieldDirection(io.atlasmap.spi.FieldDirection) ValidationScope(io.atlasmap.v2.ValidationScope) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AtlasValidator(io.atlasmap.spi.AtlasValidator) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) BaseModuleValidationService(io.atlasmap.core.BaseModuleValidationService) NonNullValidator(io.atlasmap.validators.NonNullValidator) FieldType(io.atlasmap.v2.FieldType) Validation(io.atlasmap.v2.Validation) Field(io.atlasmap.v2.Field) Map(java.util.Map) AtlasConversionService(io.atlasmap.api.AtlasConversionService) Method(java.lang.reflect.Method) AtlasConverter(io.atlasmap.api.AtlasConverter) Logger(org.slf4j.Logger) IOException(java.io.IOException) ValidationStatus(io.atlasmap.v2.ValidationStatus) List(java.util.List) AtlasModuleDetail(io.atlasmap.spi.AtlasModuleDetail) JavaField(io.atlasmap.java.v2.JavaField) Optional(java.util.Optional) InputStream(java.io.InputStream) AtlasConversionInfo(io.atlasmap.spi.AtlasConversionInfo) AtlasConverter(io.atlasmap.api.AtlasConverter) Method(java.lang.reflect.Method)

Example 8 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class MapTest method testInspectJavaMapField.

@Test
public void testInspectJavaMapField() {
    JavaClass c = classInspectionService.inspectClass(TestMapOrders.class);
    assertNotNull(c);
    assertEquals("io.atlasmap.java.test.TestMapOrders", c.getClassName());
    assertNotNull(c.getJavaFields().getJavaField());
    assertEquals(c.getJavaFields().getJavaField().size(), 1);
    JavaField field = c.getJavaFields().getJavaField().get(0);
    assertEquals(field.getCollectionType().value(), "Map");
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 9 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class TestListOrdersTest method testInspectJavaList.

@Test
public void testInspectJavaList() {
    JavaClass c = classInspectionService.inspectClass(TestListOrders.class);
    assertNotNull(c);
    assertNull(c.getAnnotations());
    assertNull(c.getArrayDimensions());
    assertEquals("io.atlasmap.java.test.TestListOrders", c.getClassName());
    assertNull(c.getCollectionClassName());
    assertNull(c.getCollectionType());
    assertNull(c.getGetMethod());
    assertNotNull(c.getJavaEnumFields());
    assertNotNull(c.getJavaEnumFields().getJavaEnumField());
    assertEquals(Integer.valueOf(0), Integer.valueOf(c.getJavaEnumFields().getJavaEnumField().size()));
    assertNotNull(c.getJavaFields());
    assertNotNull(c.getJavaFields().getJavaField());
    assertNull(c.getName());
    assertEquals("io.atlasmap.java.test", c.getPackageName());
    assertNull(c.getSetMethod());
    assertNull(c.getFieldType());
    assertNotNull(c.getUri());
    assertEquals(String.format(AtlasJavaModelFactory.URI_FORMAT, "io.atlasmap.java.test.TestListOrders"), c.getUri());
    assertNull(c.getValue());
    assertEquals(Integer.valueOf(2), new Integer(c.getJavaFields().getJavaField().size()));
    JavaField f1 = c.getJavaFields().getJavaField().get(0);
    assertNotNull(f1);
    assertTrue(f1 instanceof JavaClass);
    JavaClass c2 = (JavaClass) f1;
    assertNotNull(c2.getCollectionClassName());
    assertEquals("java.util.List", c2.getCollectionClassName());
    assertEquals(CollectionType.LIST, c2.getCollectionType());
    boolean foundAddress = false;
    boolean foundContact = false;
    boolean foundCreated = false;
    for (JavaField c2f : c2.getJavaFields().getJavaField()) {
        if (c2f instanceof JavaClass) {
            if ("io.atlasmap.java.test.BaseAddress".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateSimpleTestAddress(((JavaClass) c2f));
                foundAddress = true;
            } else if ("io.atlasmap.java.test.BaseContact".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateSimpleTestContact(((JavaClass) c2f));
                foundContact = true;
            } else if ("java.util.Date".equals(((JavaClass) c2f).getClassName())) {
                ClassValidationUtil.validateCreated(c2f);
                foundCreated = true;
            } else {
                fail("Unexpected class: " + ((JavaClass) c2f).getClassName());
            }
        } else if ("java.util.Date".equals(c2f.getClassName())) {
            ClassValidationUtil.validateCreated(c2f);
            foundCreated = true;
        }
    }
    assertTrue(foundAddress);
    assertTrue(foundContact);
    assertTrue(foundCreated);
}
Also used : JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) Test(org.junit.Test)

Example 10 with JavaField

use of io.atlasmap.java.v2.JavaField in project atlasmap by atlasmap.

the class DocumentJavaFieldReader method read.

@Override
public void read(AtlasInternalSession session) throws AtlasException {
    try {
        Field sourceField = session.head().getSourceField();
        Method getter = null;
        if (sourceField.getFieldType() == null && (sourceField instanceof JavaField || sourceField instanceof JavaEnumField)) {
            getter = resolveGetMethod(sourceDocument, sourceField);
            if (getter == null) {
                AtlasUtil.addAudit(session, sourceField.getDocId(), String.format("Unable to auto-detect sourceField type path=%s docId=%s", sourceField.getPath(), sourceField.getDocId()), sourceField.getPath(), AuditStatus.WARN, null);
                return;
            }
            Class<?> returnType = getter.getReturnType();
            sourceField.setFieldType(conversionService.fieldTypeFromClass(returnType));
            if (LOG.isTraceEnabled()) {
                LOG.trace("Auto-detected sourceField type p=" + sourceField.getPath() + " t=" + sourceField.getFieldType());
            }
        }
        populateSourceFieldValue(session, sourceField, sourceDocument, getter);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Processed input field sPath=" + sourceField.getPath() + " sV=" + sourceField.getValue() + " sT=" + sourceField.getFieldType() + " docId: " + sourceField.getDocId());
        }
    } catch (Exception e) {
        throw new AtlasException(e);
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaEnumField(io.atlasmap.java.v2.JavaEnumField) JavaField(io.atlasmap.java.v2.JavaField) Method(java.lang.reflect.Method) AtlasException(io.atlasmap.api.AtlasException) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

JavaField (io.atlasmap.java.v2.JavaField)53 Test (org.junit.Test)19 JavaClass (io.atlasmap.java.v2.JavaClass)16 Mapping (io.atlasmap.v2.Mapping)16 AtlasMapping (io.atlasmap.v2.AtlasMapping)15 Validation (io.atlasmap.v2.Validation)8 FieldType (io.atlasmap.v2.FieldType)7 Method (java.lang.reflect.Method)7 JavaEnumField (io.atlasmap.java.v2.JavaEnumField)6 AtlasException (io.atlasmap.api.AtlasException)5 DataSource (io.atlasmap.v2.DataSource)5 Field (io.atlasmap.v2.Field)5 AtlasModuleDetail (io.atlasmap.spi.AtlasModuleDetail)4 ValidationScope (io.atlasmap.v2.ValidationScope)4 ValidationStatus (io.atlasmap.v2.ValidationStatus)4 List (java.util.List)4 Logger (org.slf4j.Logger)4 AtlasConstants (io.atlasmap.api.AtlasConstants)3 AtlasMappingUtil (io.atlasmap.core.AtlasMappingUtil)3 AtlasPath (io.atlasmap.core.AtlasPath)3