Search in sources :

Example 1 with CsvField

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);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 2 with CsvField

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);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 3 with CsvField

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);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 4 with CsvField

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);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 5 with CsvField

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;
    }
}
Also used : CsvField(io.atlasmap.csv.v2.CsvField) Field(io.atlasmap.v2.Field) FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) AtlasException(io.atlasmap.api.AtlasException)

Aggregations

CsvField (io.atlasmap.csv.v2.CsvField)21 FieldGroup (io.atlasmap.v2.FieldGroup)16 Test (org.junit.jupiter.api.Test)16 ByteArrayInputStream (java.io.ByteArrayInputStream)8 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)6 Audits (io.atlasmap.v2.Audits)6 AtlasException (io.atlasmap.api.AtlasException)4 CsvComplexType (io.atlasmap.csv.v2.CsvComplexType)4 Field (io.atlasmap.v2.Field)4 CsvInspectionResponse (io.atlasmap.csv.v2.CsvInspectionResponse)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Response (javax.ws.rs.core.Response)3 CSVFormat (org.apache.commons.csv.CSVFormat)3 InputStreamReader (java.io.InputStreamReader)2 CSVParser (org.apache.commons.csv.CSVParser)2 CSVRecord (org.apache.commons.csv.CSVRecord)2 AtlasPath (io.atlasmap.core.AtlasPath)1 CsvFields (io.atlasmap.csv.v2.CsvFields)1