Search in sources :

Example 31 with Json

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

the class JsonFieldReader method populateCollectionItems.

private FieldGroup populateCollectionItems(AtlasInternalSession session, JsonNode node, Field field) throws AtlasException {
    if (!node.isArray()) {
        throw new AtlasException(String.format("Couldn't find JSON array for field %s:%s", field.getDocId(), field.getPath()));
    }
    FieldGroup group = field instanceof FieldGroup ? (FieldGroup) field : AtlasModelFactory.createFieldGroupFrom(field, true);
    ArrayNode arrayNode = (ArrayNode) node;
    for (int i = 0; i < arrayNode.size(); i++) {
        AtlasPath itemPath = new AtlasPath(group.getPath());
        List<SegmentContext> segments = itemPath.getSegments(true);
        itemPath.setCollectionIndex(segments.size() - 1, i);
        if (field instanceof FieldGroup) {
            FieldGroup itemGroup = AtlasJsonModelFactory.cloneFieldGroup((FieldGroup) field);
            AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size(), i);
            populateChildFields(session, arrayNode.get(i), itemGroup, itemPath);
            group.getField().add(itemGroup);
        } else {
            JsonField itemField = AtlasJsonModelFactory.cloneField((JsonField) field, false);
            itemField.setPath(itemPath.toString());
            Object value = handleValueNode(session, arrayNode.get(i), itemField);
            itemField.setValue(value);
            group.getField().add(itemField);
        }
    }
    return group;
}
Also used : SegmentContext(io.atlasmap.core.AtlasPath.SegmentContext) JsonField(io.atlasmap.json.v2.JsonField) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath) ArrayNode(com.fasterxml.jackson.databind.node.ArrayNode) AtlasException(io.atlasmap.api.AtlasException)

Example 32 with Json

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

the class JsonFieldReaderTest method testReadParentCollectionEmpty.

@Test
public void testReadParentCollectionEmpty() throws Exception {
    final String document = new String(Files.readAllBytes(Paths.get(getClass().getClassLoader().getResource("complex-repeated-result-empty.json").toURI())));
    reader.setDocument(document);
    FieldGroup address = new FieldGroup();
    address.setFieldType(FieldType.COMPLEX);
    address.setDocId("json");
    address.setPath("/orders[]/address");
    JsonField addressLine1 = AtlasJsonModelFactory.createJsonField();
    addressLine1.setFieldType(FieldType.STRING);
    addressLine1.setDocId("json");
    addressLine1.setPath("/orders[]/address/addressLine1");
    address.getField().add(addressLine1);
    AtlasInternalSession session = mock(AtlasInternalSession.class);
    when(session.head()).thenReturn(mock(Head.class));
    when(session.head().getSourceField()).thenReturn(address);
    Field readField = reader.read(session);
    assertNotNull(readField);
    assertEquals(FieldGroup.class, readField.getClass());
    FieldGroup readGroup = FieldGroup.class.cast(readField);
    assertEquals(0, readGroup.getField().size());
}
Also used : JsonField(io.atlasmap.json.v2.JsonField) Field(io.atlasmap.v2.Field) JsonField(io.atlasmap.json.v2.JsonField) Head(io.atlasmap.spi.AtlasInternalSession.Head) AtlasInternalSession(io.atlasmap.spi.AtlasInternalSession) FieldGroup(io.atlasmap.v2.FieldGroup) Test(org.junit.jupiter.api.Test)

Example 33 with Json

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

the class JsonMarshallerTest method testJsonMapJavaField.

@Test
public void testJsonMapJavaField() 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 34 with Json

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

the class JsonMarshallerTest method testJsonSeparateJavaField.

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

Example 35 with Json

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

the class JsonMarshallerTest method testReferenceMapping.

@Test
public void testReferenceMapping() 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)

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