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;
}
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());
}
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);
}
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);
}
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);
}
Aggregations