use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.
the class CsvFieldWriterTest method testWithNoMatchingHeader.
@Test
public void testWithNoMatchingHeader() throws Exception {
CsvConfig csvConfig = new CsvConfig();
csvConfig.setHeaders("FAMILYNAME,GIVENNAME");
CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
FieldGroup sourceField = new FieldGroup();
sourceField.setName("name");
sourceField.setPath("/<>/name");
CsvField sourceSubField0 = new CsvField();
sourceSubField0.setName("name");
sourceSubField0.setPath("/<0>/name");
sourceSubField0.setValue("Bob");
sourceField.getField().add(sourceSubField0);
CsvField sourceSubField1 = new CsvField();
sourceSubField1.setName("name");
sourceSubField1.setPath("/<1>/name");
sourceSubField1.setValue("Andrew");
sourceField.getField().add(sourceSubField1);
CsvField targetField = new CsvField();
targetField.setName("givenName");
targetField.setPath("/<>/givenName");
write(writer, sourceField, targetField);
String csv = writer.toCsv();
assertEquals("FAMILYNAME,GIVENNAME\r\n,\r\n,\r\n", csv);
}
use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.
the class CsvFieldWriterTest method testWithSimpleDocumentWithHeaderSpecified.
@Test
public void testWithSimpleDocumentWithHeaderSpecified() throws Exception {
CsvConfig csvConfig = new CsvConfig();
csvConfig.setHeaders("givenName");
CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
FieldGroup sourceField = new FieldGroup();
sourceField.setName("name");
sourceField.setPath("/<>/name");
CsvField sourceSubField0 = new CsvField();
sourceSubField0.setName("name");
sourceSubField0.setPath("/<0>/name");
sourceSubField0.setValue("Bob");
sourceField.getField().add(sourceSubField0);
CsvField sourceSubField1 = new CsvField();
sourceSubField1.setName("name");
sourceSubField1.setPath("/<1>/name");
sourceSubField1.setValue("Andrew");
sourceField.getField().add(sourceSubField1);
CsvField targetField = new CsvField();
targetField.setName("givenName");
targetField.setPath("/<>/givenName");
write(writer, sourceField, targetField);
String csv = writer.toCsv();
assertEquals("givenName\r\nBob\r\nAndrew\r\n", csv);
}
use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.
the class CsvFieldWriterTest method testWithSimpleDocumentWithTargetColumnsDefined.
@Test
public void testWithSimpleDocumentWithTargetColumnsDefined() throws Exception {
CsvConfig csvConfig = new CsvConfig();
CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
FieldGroup sourceField = new FieldGroup();
sourceField.setName("name");
sourceField.setPath("/<>/name");
CsvField sourceSubField0 = new CsvField();
sourceSubField0.setName("name");
sourceSubField0.setPath("/<0>/name");
sourceSubField0.setValue("Bob");
sourceField.getField().add(sourceSubField0);
CsvField sourceSubField1 = new CsvField();
sourceSubField1.setName("name");
sourceSubField1.setPath("/<1>/name");
sourceSubField1.setValue("Andrew");
sourceField.getField().add(sourceSubField1);
CsvField targetField = new CsvField();
targetField.setName("givenName");
targetField.setPath("/<>/givenName");
targetField.setColumn(1);
write(writer, sourceField, targetField);
FieldGroup secondSourceField = new FieldGroup();
CsvField secondSourceSubField = new CsvField();
secondSourceSubField.setName("familyName");
secondSourceSubField.setPath("/<0>/familyName");
secondSourceSubField.setValue("Smith");
secondSourceField.getField().add(secondSourceSubField);
CsvField secondSourceSubField1 = new CsvField();
secondSourceSubField1.setName("familyName");
secondSourceSubField1.setPath("/<1>/familyName");
secondSourceSubField1.setValue("Johnson");
secondSourceField.getField().add(secondSourceSubField1);
CsvField secondTargetField = new CsvField();
secondTargetField.setName("familyName");
secondTargetField.setPath("/<>/familyName");
secondTargetField.setColumn(0);
write(writer, secondSourceField, secondTargetField);
String csv = writer.toCsv();
assertEquals("familyName,givenName\r\nSmith,Bob\r\nJohnson,Andrew\r\n", csv);
}
use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.
the class CsvFieldWriterTest method testWithSimpleDocumentWithHeader.
@Test
public void testWithSimpleDocumentWithHeader() throws Exception {
CsvConfig csvConfig = new CsvConfig();
CsvFieldWriter writer = new CsvFieldWriter(csvConfig);
FieldGroup sourceField = new FieldGroup();
sourceField.setName("name");
sourceField.setPath("/<>/name");
CsvField sourceSubField0 = new CsvField();
sourceSubField0.setName("name");
sourceSubField0.setPath("/<0>/name");
sourceSubField0.setValue("Bob");
sourceField.getField().add(sourceSubField0);
CsvField sourceSubField1 = new CsvField();
sourceSubField1.setName("name");
sourceSubField1.setPath("/<1>/name");
sourceSubField1.setValue("Andrew");
sourceField.getField().add(sourceSubField1);
CsvField targetField = new CsvField();
targetField.setName("givenName");
targetField.setPath("/<>/givenName");
write(writer, sourceField, targetField);
String csv = writer.toCsv();
assertEquals("givenName\r\nBob\r\nAndrew\r\n", csv);
}
use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.
the class CsvFieldReader method read.
@Override
public Field read(AtlasInternalSession session) throws AtlasException {
Field field = session.head().getSourceField();
if (document == null) {
AtlasUtil.addAudit(session, field, String.format("Cannot read field '%s' of document '%s', document is null", field.getPath(), field.getDocId()), AuditStatus.ERROR, null);
return field;
}
if (field == null) {
throw new AtlasException(new IllegalArgumentException("Argument 'field' cannot be null"));
}
if (!(field instanceof CsvField) && !(field instanceof FieldGroup)) {
throw new AtlasException(String.format("Unsupported field type '%s'", field.getClass()));
}
if (field instanceof FieldGroup) {
// complex field
FieldGroup fieldGroup = (FieldGroup) field;
List<Field> fields = fieldGroup.getField();
FieldGroup readFieldGroup = AtlasModelFactory.copyFieldGroup(fieldGroup);
for (Field subField : fields) {
if (subField instanceof FieldGroup) {
// support only one level grouping
subField = ((FieldGroup) subField).getField().get(0);
}
if (subField instanceof CsvField) {
Field readSubField = readFields((CsvField) subField);
readFieldGroup.getField().add(readSubField);
}
}
session.head().setSourceField(readFieldGroup);
return readFieldGroup;
} else {
Field readField = readFields((CsvField) field);
session.head().setSourceField(readField);
return readField;
}
}
Aggregations