Search in sources :

Example 11 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvFieldWriterTest method testWithSimpleDocumentWithoutHeader.

@Test
public void testWithSimpleDocumentWithoutHeader() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setSkipHeaderRecord(true);
    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("Bob\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 12 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvFieldWriterTest method testWithHeaderAndIgnoreHeaderCase.

@Test
public void testWithHeaderAndIgnoreHeaderCase() throws Exception {
    CsvConfig csvConfig = new CsvConfig();
    csvConfig.setHeaders("FAMILYNAME,GIVENNAME");
    csvConfig.setIgnoreHeaderCase(true);
    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,Bob\r\n,Andrew\r\n", csv);
}
Also used : FieldGroup(io.atlasmap.v2.FieldGroup) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 13 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvServiceTest method testSchemaNoParametersSpecified.

@Test
public void testSchemaNoParametersSpecified() throws Exception {
    final String source = "l1r1,l1r2,l1r3\n" + "l2r1,l2r2,l2r3\n" + "l3r1,l3r2,l3r3\n";
    InputStream inputStream = new ByteArrayInputStream(source.getBytes());
    Response res = csvService.inspect(inputStream, null, null, null, null, null, null, null, null, null, null, null, null, null, null);
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    CsvInspectionResponse csvInspectionResponse = Json.mapper().readValue((byte[]) entity, CsvInspectionResponse.class);
    CsvComplexType complexType = (CsvComplexType) csvInspectionResponse.getCsvDocument().getFields().getField().get(0);
    List<CsvField> fields = complexType.getCsvFields().getCsvField();
    assertEquals("0", fields.get(0).getName());
    assertEquals("1", fields.get(1).getName());
    assertEquals("2", fields.get(2).getName());
}
Also used : CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) Response(javax.ws.rs.core.Response) CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CsvComplexType(io.atlasmap.csv.v2.CsvComplexType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 14 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvServiceTest method testSchemaFile.

@Test
public void testSchemaFile() throws Exception {
    InputStream inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.csv");
    Response res = csvService.inspect(inputStream, null, ",", true, null, null, null, null, null, null, null, null, null, null, null);
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    CsvInspectionResponse csvInspectionResponse = Json.mapper().readValue((byte[]) entity, CsvInspectionResponse.class);
    CsvComplexType complexType = (CsvComplexType) csvInspectionResponse.getCsvDocument().getFields().getField().get(0);
    List<CsvField> fields = complexType.getCsvFields().getCsvField();
    assertEquals(5, fields.size());
    assertEquals("sourceCsvString", fields.get(0).getName());
    assertEquals("sourceCsvNumber", fields.get(1).getName());
    assertEquals("sourceCsvDecimal", fields.get(2).getName());
    assertEquals("sourceCsvDate", fields.get(3).getName());
    assertEquals("sourceCsvBoolean", fields.get(4).getName());
}
Also used : CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) Response(javax.ws.rs.core.Response) CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) CsvComplexType(io.atlasmap.csv.v2.CsvComplexType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

Example 15 with CsvField

use of io.atlasmap.csv.v2.CsvField in project atlasmap by atlasmap.

the class CsvServiceTest method testSchema.

@Test
public void testSchema() throws Exception {
    final String source = "header1,header2,header3\n" + "l1r1,l1r2,l1r3\n" + "l2r1,l2r2,l2r3\n" + "l3r1,l3r2,l3r3\n";
    InputStream inputStream = new ByteArrayInputStream(source.getBytes());
    Response res = csvService.inspect(inputStream, null, ",", true, null, null, null, null, null, null, null, null, null, null, null);
    Object entity = res.getEntity();
    assertEquals(byte[].class, entity.getClass());
    CsvInspectionResponse csvInspectionResponse = Json.mapper().readValue((byte[]) entity, CsvInspectionResponse.class);
    CsvComplexType complexType = (CsvComplexType) csvInspectionResponse.getCsvDocument().getFields().getField().get(0);
    List<CsvField> fields = complexType.getCsvFields().getCsvField();
    assertEquals("header1", fields.get(0).getName());
    assertEquals("header2", fields.get(1).getName());
    assertEquals("header3", fields.get(2).getName());
}
Also used : CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) Response(javax.ws.rs.core.Response) CsvInspectionResponse(io.atlasmap.csv.v2.CsvInspectionResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) CsvComplexType(io.atlasmap.csv.v2.CsvComplexType) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) CsvField(io.atlasmap.csv.v2.CsvField) Test(org.junit.jupiter.api.Test)

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