use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class DefaultAtlasPreviewContextTest method testProcessPreviewExpressionNullValue.
@Test
public void testProcessPreviewExpressionNullValue() throws AtlasException {
Mapping m = new Mapping();
FieldGroup fg = new FieldGroup();
m.setInputFieldGroup(fg);
Field source = new SimpleField();
source.setDocId("source");
source.setFieldType(FieldType.DOUBLE);
source.setPath("/sourceDouble");
fg.getField().add(source);
Field source2 = new SimpleField();
source2.setDocId("source2");
source2.setFieldType(FieldType.DOUBLE);
source2.setPath("/sourceDouble");
source2.setValue(1.0);
fg.getField().add(source2);
m.setExpression("IF(ISEMPTY(${source:/sourceDouble}), ${source2:/sourceDouble}, ${source:/sourceDouble})");
Field target = new SimpleField();
target.setFieldType(FieldType.STRING);
m.getOutputField().add(target);
Audits audits = previewContext.processPreview(m);
assertEquals(0, audits.getAudit().size(), printAudit(audits));
target = m.getOutputField().get(0);
assertEquals("1.0", target.getValue());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class DefaultAtlasPreviewContextTest method testProcessPreviewConcatenate.
@Test
public void testProcessPreviewConcatenate() throws AtlasException {
Mapping m = new Mapping();
Field source1 = new SimpleField();
source1.setFieldType(FieldType.STRING);
source1.setPath("/one");
source1.setIndex(1);
source1.setValue("one");
Field source2 = new SimpleField();
source2.setFieldType(FieldType.STRING);
source2.setPath("/three");
source2.setIndex(3);
source2.setValue("three");
Field source3 = new SimpleField();
source3.setFieldType(FieldType.STRING);
source3.setPath("/five");
source3.setIndex(5);
source3.setValue("five");
FieldGroup group = new FieldGroup();
group.getField().add(source1);
group.getField().add(source2);
group.getField().add(source3);
Concatenate action = new Concatenate();
action.setDelimiter("-");
action.setDelimitingEmptyValues(true);
group.setActions(new ArrayList<>());
group.getActions().add(action);
m.setInputFieldGroup(group);
Field target = new SimpleField();
target.setFieldType(FieldType.STRING);
m.getOutputField().add(target);
previewContext.processPreview(m);
target = m.getOutputField().get(0);
assertEquals("-one--three--five", target.getValue());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class DefaultAtlasPreviewContextTest method testProcessPreviewSourceFieldGroup.
@Test
public void testProcessPreviewSourceFieldGroup() throws AtlasException {
Mapping m = new Mapping();
Field source1 = new SimpleField();
source1.setFieldType(FieldType.STRING);
source1.setPath("/one");
source1.setIndex(0);
source1.setValue("one");
Field source2 = new SimpleField();
source2.setFieldType(FieldType.STRING);
source2.setPath("/two");
source2.setIndex(1);
source2.setValue("two");
FieldGroup group = new FieldGroup();
group.getField().add(source1);
group.getField().add(source2);
Expression action = new Expression();
action.setExpression("${0} + ' and ' + ${1}");
group.setActions(new ArrayList<>());
group.getActions().add(action);
m.setInputFieldGroup(group);
Field target = new SimpleField();
target.setFieldType(FieldType.STRING);
m.getOutputField().add(target);
previewContext.processPreview(m);
target = m.getOutputField().get(0);
assertEquals("one and two", target.getValue());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class XmlFieldReader method getFieldsForPath.
private List<Field> getFieldsForPath(AtlasInternalSession session, Optional<XmlNamespaces> xmlNamespaces, Element node, Field field, XmlPath path, int depth) throws AtlasException {
List<Field> fields = new ArrayList<>();
List<XmlSegmentContext> segments = path.getXmlSegments(false);
if (segments.size() < depth) {
throw new AtlasException(String.format("depth '%s' exceeds segment size '%s'", depth, segments.size()));
}
if (segments.size() == depth) {
if (!(field instanceof XmlEnumField) && field.getFieldType() == FieldType.COMPLEX) {
FieldGroup group = (FieldGroup) field;
populateChildFields(session, xmlNamespaces, node, group, path);
fields.add(group);
} else {
XmlField xmlField = new XmlField();
AtlasXmlModelFactory.copyField(field, xmlField, true);
if (field instanceof XmlEnumField && xmlField.getFieldType() == FieldType.COMPLEX) {
// enum has COMPLEX by default
xmlField.setFieldType(FieldType.STRING);
}
copyValue(session, xmlNamespaces, segments.get(depth - 1), node, xmlField);
// reset index for subfields
xmlField.setIndex(null);
fields.add(xmlField);
}
return fields;
}
// segments.size() > depth
XmlSegmentContext segment = segments.get(depth);
if (LOG.isDebugEnabled()) {
LOG.debug("Now processing segment: " + segment.getName());
}
if (depth == 0) {
if (segment.getName().startsWith(XmlIOHelper.getNodeNameWithoutNamespaceAlias(node))) {
Optional<String> rootNamespace = Optional.empty();
if (segment.getNamespace() != null) {
rootNamespace = getNamespace(xmlNamespaces, segment.getNamespace());
}
if (!rootNamespace.isPresent() || rootNamespace.get().equals(node.getNamespaceURI())) {
// "/XOA/contact<>/firstName", skip.
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping root segment: " + segment);
}
if (segments.size() > 1) {
depth = 1;
segment = segments.get(depth);
}
}
}
}
if (segment.isAttribute() && segments.size() == depth + 1) {
// if last segment is attribute
List<Field> attrFields = getFieldsForPath(session, xmlNamespaces, node, field, path, depth + 1);
fields.addAll(attrFields);
return fields;
}
String fieldName = segment.getName();
String fieldNamespace = segment.getNamespace();
Optional<String> namespace = getNamespace(xmlNamespaces, fieldNamespace);
List<Element> children = XmlIOHelper.getChildrenWithNameStripAlias(fieldName, namespace, node);
if (children == null || children.isEmpty()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, couldn't find children with name '" + fieldName + "', for segment: " + segment);
}
return fields;
}
if (segment.getCollectionType() == CollectionType.NONE) {
List<Field> childFields = getFieldsForPath(session, xmlNamespaces, children.get(0), field, path, depth + 1);
fields.addAll(childFields);
return fields;
}
// collection
Integer index = segment.getCollectionIndex();
if (index != null) {
if (index < children.size()) {
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(index), field, path, depth + 1);
fields.addAll(arrayFields);
} else if (LOG.isDebugEnabled()) {
LOG.debug("Skipping source value set, children list can't fit index " + index + ", children list size: " + children.size());
}
} else {
// if index not included, iterate over all
for (int i = 0; i < children.size(); i++) {
Field itemField;
if (field instanceof FieldGroup) {
itemField = AtlasXmlModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively((FieldGroup) itemField, depth + 1, i);
} else {
itemField = AtlasXmlModelFactory.cloneField((XmlField) field, false);
AtlasPath itemPath = new AtlasPath(field.getPath());
itemPath.setCollectionIndex(depth + 1, i);
itemField.setPath(itemPath.toString());
}
List<Field> arrayFields = getFieldsForPath(session, xmlNamespaces, children.get(i), itemField, new XmlPath(itemField.getPath()), depth + 1);
fields.addAll(arrayFields);
}
}
return fields;
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class KafkaConnectFieldReader method populateCollectionItems.
private FieldGroup populateCollectionItems(AtlasInternalSession session, List<Object> values, Field field) throws AtlasException {
FieldGroup group = AtlasModelFactory.createFieldGroupFrom(field, true);
for (int i = 0; i < values.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 = AtlasKafkaConnectModelFactory.cloneFieldGroup((FieldGroup) field);
AtlasPath.setCollectionIndexRecursively(itemGroup, segments.size() - 1, i);
populateChildFields(session, (Struct) values.get(i), itemGroup);
group.getField().add(itemGroup);
} else {
KafkaConnectField itemField = AtlasKafkaConnectModelFactory.cloneField((KafkaConnectField) field, false);
itemField.setPath(itemPath.toString());
Object converted = conversionService.convertType(values.get(i), itemField.getFormat(), itemField.getFieldType(), null);
itemField.setValue(converted);
group.getField().add(itemField);
}
}
return group;
}
Aggregations