use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class JavaFieldWriter method doCommitWriting.
private void doCommitWriting(AtlasInternalSession session) throws AtlasException {
Field targetField = session.head().getTargetField();
AtlasPath path = new AtlasPath(targetField.getPath());
Object parentObject = this.pathParentQueue.get(targetField.getPath());
this.pathParentQueue.remove(targetField.getPath());
converter.convertTargetValue(session, parentObject, targetField);
SegmentContext lastSegment = path.getLastSegment();
if (path.isRoot()) {
if (lastSegment.getCollectionType() == CollectionType.NONE) {
this.rootObject = targetField.getValue();
} else {
Object adjusted = writerUtil.adjustCollectionSize(this.rootObject, lastSegment);
if (adjusted != this.rootObject) {
this.rootObject = adjusted;
}
writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
}
return;
}
if (parentObject == null) {
return;
}
String targetClassName = null;
if (targetField instanceof JavaField) {
targetClassName = ((JavaField) targetField).getClassName();
} else if (targetField instanceof JavaEnumField) {
targetClassName = ((JavaEnumField) targetField).getClassName();
}
if (lastSegment.getCollectionType() == CollectionType.NONE) {
// Don't handle null for JavaEnumField complex type using complex child object
if (targetField.getFieldType() == FieldType.COMPLEX && !(targetField instanceof JavaEnumField) && targetField.getValue() == null) {
if (targetClassName != null && !targetClassName.isEmpty()) {
writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
} else {
writerUtil.createComplexChildObject(parentObject, lastSegment);
}
} else {
writerUtil.setChildObject(parentObject, targetField.getValue(), lastSegment);
}
} else {
Object collection = writerUtil.getChildObject(parentObject, lastSegment);
if (collection == null) {
collection = writerUtil.createComplexChildObject(parentObject, lastSegment);
}
if (lastSegment.getCollectionIndex() == null) {
// Collection field without index - just create collection object and keep it empty
return;
}
Object adjusted = writerUtil.adjustCollectionSize(collection, lastSegment);
if (adjusted != collection) {
writerUtil.setChildObject(parentObject, adjusted, lastSegment);
}
if (targetField.getFieldType() == FieldType.COMPLEX && targetField.getValue() == null) {
if (targetClassName != null && !targetClassName.isEmpty()) {
writerUtil.createComplexChildObject(parentObject, lastSegment, writerUtil.loadClass(targetClassName));
} else {
writerUtil.createComplexChildObject(parentObject, lastSegment);
}
} else {
writerUtil.setCollectionItem(adjusted, targetField.getValue(), lastSegment);
}
}
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class BaseMarshallerTest method generateCollectionMapping.
protected AtlasMapping generateCollectionMapping() {
AtlasMapping innerMapping1 = generateAtlasMapping();
AtlasMapping innerMapping2 = generateAtlasMapping();
Collection cMapping = (Collection) AtlasModelFactory.createMapping(MappingType.COLLECTION);
cMapping.setMappings(new Mappings());
cMapping.getMappings().getMapping().addAll(innerMapping1.getMappings().getMapping());
cMapping.getMappings().getMapping().addAll(innerMapping2.getMappings().getMapping());
cMapping.setCollectionType(CollectionType.LIST);
cMapping.setCollectionSize(new BigInteger("2"));
cMapping.setAlias("alias");
cMapping.setDescription("description");
AtlasMapping mapping = generateAtlasMapping();
mapping.getMappings().getMapping().clear();
mapping.getMappings().getMapping().add(cMapping);
return mapping;
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class DefaultAtlasContextCollectionExpansionTest method shouldNotExponentialyGrowExpandedCollectionMappings.
@Test
public void shouldNotExponentialyGrowExpandedCollectionMappings() throws Exception {
final AtlasMapping mapping = new AtlasMapping();
final DefaultAtlasContext context = new DefaultAtlasContext(DefaultAtlasContextFactory.getInstance(), mapping) {
protected void init() {
// hijack initialization
}
};
context.setSourceModules(Collections.singletonMap("source", new JsonModule()));
final DefaultAtlasSession session = (DefaultAtlasSession) context.createSession();
session.setSourceDocument("source", "{ \"array\": [ { \"property\": 1 }, { \"property\": 2 }, { \"property\": 3 } ] }");
final Collection baseMapping = new Collection();
baseMapping.setMappingType(MappingType.COLLECTION);
final Mappings mappings = new Mappings();
final Mapping singleMapping = new Mapping();
singleMapping.setMappingType(MappingType.MAP);
final JsonField nestedArrayField = new JsonField();
nestedArrayField.setDocId("source");
nestedArrayField.setPath("/array<>/property");
singleMapping.getInputField().add(nestedArrayField);
mappings.getMapping().add(singleMapping);
baseMapping.setMappings(mappings);
Method m = DefaultAtlasContext.class.getDeclaredMethod("unwrapCollectionMappings", new Class[] { DefaultAtlasSession.class, BaseMapping.class });
m.setAccessible(true);
assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
assertEquals(1, List.class.cast(m.invoke(context, new Object[] { session, baseMapping })).size());
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class NestedCollectionXmlTest method processXmlNestedCollection.
private String processXmlNestedCollection(List<String> mappingsToProcess, boolean assertNoWarnings) throws AtlasException, IOException, URISyntaxException {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-xml.json");
ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
admHandler.load(AtlasContextFactory.Format.JSON, in);
AtlasMapping mapping = admHandler.getMappingDefinition();
mapping.getMappings().getMapping().removeIf(m -> !mappingsToProcess.contains(((Mapping) m).getId()));
AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
AtlasSession session = context.createSession();
String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.xml").toURI())));
session.setSourceDocument("XMLInstanceNestedCollection", source);
context.process(session);
assertFalse(session.hasErrors(), TestHelper.printAudit(session));
if (assertNoWarnings) {
assertFalse(session.hasWarns(), TestHelper.printAudit(session));
}
Object output = session.getTargetDocument("XMLInstanceNestedCollection");
return (String) output;
}
use of io.atlasmap.v2.Collection in project atlasmap by atlasmap.
the class NestedCollectionJsonTest method testAsymmetricSingleTarget.
@Test
public void testAsymmetricSingleTarget() throws Exception {
InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream("mappings/atlasmapping-nested-collection-asymmetric.json");
ADMArchiveHandler admHandler = new ADMArchiveHandler(Thread.currentThread().getContextClassLoader());
admHandler.load(AtlasContextFactory.Format.JSON, in);
AtlasMapping mapping = admHandler.getMappingDefinition();
mapping.getMappings().getMapping().removeIf(m -> !"3-1".equals(((Mapping) m).getId()));
AtlasContext context = DefaultAtlasContextFactory.getInstance().createContext(mapping);
AtlasSession session = context.createSession();
String source = new String(Files.readAllBytes(Paths.get(Thread.currentThread().getContextClassLoader().getResource("mappings/document-nested-collection.json").toURI())));
session.setSourceDocument("JSONInstanceNestedCollection", source);
context.process(session);
assertFalse(session.hasErrors(), TestHelper.printAudit(session));
assertTrue(session.hasWarns(), TestHelper.printAudit(session));
Object output = session.getTargetDocument("JSONInstanceNestedCollection");
assertEquals(String.class, output.getClass());
JsonNode outputJson = mapper.readTree((String) output);
String prettyPrinted = mapper.writeValueAsString(outputJson);
ArrayNode firstArray = (ArrayNode) outputJson.get("firstArray");
assertEquals(10, firstArray.size(), prettyPrinted);
assertEquals("thirdArrayValue0-0-0", firstArray.get(0).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(0).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue0-0-1", firstArray.get(1).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(1).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue0-1-0", firstArray.get(2).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(2).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue0-1-1", firstArray.get(3).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(3).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue0-1-2", firstArray.get(4).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(4).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue1-0-0", firstArray.get(5).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(5).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue1-0-1", firstArray.get(6).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(6).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue1-0-2", firstArray.get(7).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(7).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue1-1-0", firstArray.get(8).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(8).get("secondArray"), prettyPrinted);
assertEquals("thirdArrayValue1-1-1", firstArray.get(9).get("value").asText(), prettyPrinted);
assertNull(firstArray.get(9).get("secondArray"), prettyPrinted);
}
Aggregations