Search in sources :

Example 1 with Json

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

the class AtlasMappingServiceTest method testLoadMappingFileAtlasMappingFormatAtlasValidationException.

@Test(expected = AtlasValidationException.class)
public void testLoadMappingFileAtlasMappingFormatAtlasValidationException() throws AtlasValidationException {
    File file = Paths.get("src" + File.separator + "test" + File.separator + "resources" + File.separator + "atlasmapping2.json").toFile();
    AtlasMapping atlasMapping = atlasMappingService.loadMapping(file, AtlasMappingFormat.JSON);
    assertNotNull(atlasMapping);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) File(java.io.File) Test(org.junit.Test)

Example 2 with Json

use of io.atlasmap.v2.Json in project syndesis-qe by syndesisio.

the class AtlasMapperGenerator method getField.

/**
 * Gets the field with the path corresponding to the search string. It recursively goes through child fields if the field is a complex type.
 *
 * @param fields list of fields
 * @param searchString path search string
 * @return field
 */
private Field getField(List<Field> fields, String searchString) {
    Field f = null;
    for (Field field : fields) {
        if (field instanceof JsonComplexType || field instanceof XmlComplexType) {
            // Search child elements of the complex element, just create a new list because Json/XmlField is a child class of Field
            final List<Field> fieldList;
            if (field instanceof JsonComplexType) {
                fieldList = new ArrayList<>(((JsonComplexType) field).getJsonFields().getJsonField());
            } else {
                fieldList = new ArrayList<>(((XmlComplexType) field).getXmlFields().getXmlField());
            }
            f = getField(fieldList, searchString);
        } else {
            if (field.getPath().equals(searchString)) {
                return field;
            } else {
                continue;
            }
        }
        if (f != null) {
            break;
        }
    }
    return f;
}
Also used : Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) XmlComplexType(io.atlasmap.xml.v2.XmlComplexType) JsonComplexType(io.atlasmap.json.v2.JsonComplexType)

Example 3 with Json

use of io.atlasmap.v2.Json in project syndesis-qe by syndesisio.

the class AtlasMapperGenerator method processDataShapeIntoFields.

/**
 * The dataShapeSpecification needs to be separated into fields, which could then be used for generation of mapping
 * steps. This is different for the "Json", "Java" and also "Xml" data shape type. Java DS type is already the
 * specification of field types, Json needs to be send to be sent first to Json inspection endpoint, to generate the
 * fields.
 *
 * @return list of fields from given datashape
 */
private List<Field> processDataShapeIntoFields(String dataShapeSpecification, DataShapeKinds dsKind) {
    ObjectMapper mapper = new ObjectMapper();
    mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);
    List<Field> fields = null;
    if (dsKind.equals(DataShapeKinds.JAVA)) {
        try {
            JavaClass jClass = mapper.readValue(dataShapeSpecification, JavaClass.class);
            fields = getJavaFields(jClass).stream().map(this::replacePrimitiveWithObject).collect(Collectors.toList());
        } catch (IOException e) {
            log.error("error: ", e);
        }
    } else if (dsKind.equals(DataShapeKinds.JSON_SCHEMA) || dsKind.equals(DataShapeKinds.JSON_INSTANCE)) {
        JsonInspectionResponse inspectionResponse = atlasmapEndpoint.inspectJson(dataShapeSpecification, dsKind);
        try {
            log.debug("Inspection API response: " + mapper.writeValueAsString(inspectionResponse));
            fields = inspectionResponse.getJsonDocument().getFields().getField();
        } catch (JsonProcessingException e) {
            log.error("Unable to write inspection API response as string", e);
        }
    } else if (dsKind.equals(DataShapeKinds.XML_SCHEMA) || dsKind.equals(DataShapeKinds.XML_INSTANCE)) {
        XmlInspectionResponse inspectionResponse = atlasmapEndpoint.inspectXml(dataShapeSpecification, dsKind);
        try {
            log.debug("Inspection API response: " + mapper.writeValueAsString(inspectionResponse));
            fields = inspectionResponse.getXmlDocument().getFields().getField();
        } catch (JsonProcessingException e) {
            log.error("Unable to write inspection API response as string", e);
        }
    }
    return fields;
}
Also used : Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaClass(io.atlasmap.java.v2.JavaClass) JsonInspectionResponse(io.atlasmap.json.v2.JsonInspectionResponse) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) XmlInspectionResponse(io.atlasmap.xml.v2.XmlInspectionResponse)

Example 4 with Json

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

the class JsonMarshallerTest method testJsonMapXMLField.

@Test
public void testJsonMapXMLField() throws Exception {
    AtlasMapping atlasMapping = generateAtlasMapping();
    // Object to JSON in file
    mapper.writerWithDefaultPrettyPrinter().writeValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping.json"), atlasMapping);
    AtlasMapping uMapping = mapper.readValue(new File("target" + File.separator + "junit" + File.separator + testMethodName + File.separator + "atlasmapping.json"), AtlasMapping.class);
    assertNotNull(uMapping);
    validateAtlasMapping(uMapping);
}
Also used : AtlasMapping(io.atlasmap.v2.AtlasMapping) File(java.io.File) Test(org.junit.jupiter.api.Test)

Example 5 with Json

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

the class JsonModule method processPreValidation.

@Override
public void processPreValidation(AtlasInternalSession atlasSession) throws AtlasException {
    if (atlasSession == null || atlasSession.getMapping() == null) {
        throw new AtlasValidationException("Invalid session: Session and AtlasMapping must be specified");
    }
    Validations validations = atlasSession.getValidations();
    JsonValidationService jsonValidationService = new JsonValidationService(getConversionService(), getFieldActionService());
    jsonValidationService.setMode(getMode());
    jsonValidationService.setDocId(getDocId());
    List<Validation> jsonValidations = jsonValidationService.validateMapping(atlasSession.getMapping());
    if (jsonValidations != null && !jsonValidations.isEmpty()) {
        validations.getValidation().addAll(jsonValidations);
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Detected " + jsonValidations.size() + " json validation notices");
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("{}: processPreValidation completed", getDocId());
    }
}
Also used : Validation(io.atlasmap.v2.Validation) Validations(io.atlasmap.v2.Validations) AtlasValidationException(io.atlasmap.api.AtlasValidationException)

Aggregations

Test (org.junit.jupiter.api.Test)15 Field (io.atlasmap.v2.Field)12 AtlasMapping (io.atlasmap.v2.AtlasMapping)11 JsonField (io.atlasmap.json.v2.JsonField)7 File (java.io.File)7 FieldGroup (io.atlasmap.v2.FieldGroup)6 AtlasException (io.atlasmap.api.AtlasException)5 AtlasValidationException (io.atlasmap.api.AtlasValidationException)4 ProcessMappingResponse (io.atlasmap.v2.ProcessMappingResponse)4 Response (javax.ws.rs.core.Response)4 AtlasPath (io.atlasmap.core.AtlasPath)3 JavaField (io.atlasmap.java.v2.JavaField)3 JsonDataSource (io.atlasmap.json.v2.JsonDataSource)3 DataSource (io.atlasmap.v2.DataSource)3 XmlDataSource (io.atlasmap.xml.v2.XmlDataSource)3 Test (org.junit.Test)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 AtlasConversionException (io.atlasmap.api.AtlasConversionException)2 JsonDocument (io.atlasmap.json.v2.JsonDocument)2