Search in sources :

Example 11 with FieldGroup

use of io.atlasmap.v2.FieldGroup 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 12 with FieldGroup

use of io.atlasmap.v2.FieldGroup 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 13 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class JavaFieldReaderTest method testReadTopmostArrayComplex.

@Test
public void testReadTopmostArrayComplex() throws Exception {
    TargetTestClass[] complexArray = new TargetTestClass[] { new TargetTestClass(), new TargetTestClass(), new TargetTestClass() };
    complexArray[0].setAddress(new TargetAddress());
    complexArray[0].getAddress().setAddressLine1("123 any street");
    complexArray[2].setAddress(new TargetAddress());
    complexArray[2].getAddress().setAddressLine1("1234 any street");
    reader.setDocument(complexArray);
    read("/[0]/address/addressLine1", FieldType.STRING);
    assertEquals(0, audits.size());
    assertEquals("123 any street", field.getValue());
    assertEquals(0, audits.size());
    readGroup("/[]/address/addressLine1", FieldType.STRING);
    assertEquals(0, audits.size());
    assertNotNull(fieldGroup);
    assertEquals(2, fieldGroup.getField().size());
    Field f = fieldGroup.getField().get(0);
    assertEquals("/[0]/address/addressLine1", f.getPath());
    assertEquals("123 any street", f.getValue());
    f = fieldGroup.getField().get(1);
    assertEquals("/[2]/address/addressLine1", f.getPath());
    assertEquals("1234 any street", f.getValue());
    assertEquals(0, audits.size());
}
Also used : Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) TargetAddress(io.atlasmap.java.test.TargetAddress) TargetTestClass(io.atlasmap.java.test.TargetTestClass) Test(org.junit.jupiter.api.Test)

Example 14 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class JavaFieldReaderTest method testReadParent.

@Test
public void testReadParent() throws Exception {
    TargetTestClass source = new TargetTestClass();
    LinkedList<TargetContact> contactList = new LinkedList<>();
    for (int i = 0; i < 5; i++) {
        TargetContact c = new TargetContact();
        c.setFirstName("f" + i);
        c.setLastName("l" + i);
        c.setPhoneNumber("p" + i);
        c.setZipCode("z" + i);
        contactList.add(c);
    }
    source.setContactList(contactList);
    reader.setDocument(source);
    FieldGroup contactListGroup = new FieldGroup();
    contactListGroup.setDocId("TargetTestClass");
    contactListGroup.setPath("/contactList<>");
    contactListGroup.setFieldType(FieldType.COMPLEX);
    contactListGroup.setCollectionType(CollectionType.LIST);
    Field firstNameField = new JavaField();
    firstNameField.setDocId("TargetTestClass");
    firstNameField.setPath("/contactList<>/firstName");
    firstNameField.setFieldType(FieldType.STRING);
    contactListGroup.getField().add(firstNameField);
    fieldGroup = (FieldGroup) read(contactListGroup);
    assertEquals(0, audits.size());
    assertEquals("/contactList<>", fieldGroup.getPath());
    assertEquals(FieldType.COMPLEX, fieldGroup.getFieldType());
    assertEquals(CollectionType.LIST, fieldGroup.getCollectionType());
    assertEquals(5, fieldGroup.getField().size());
    Field contact0 = fieldGroup.getField().get(0);
    assertEquals(FieldGroup.class, contact0.getClass());
    FieldGroup contact0Group = (FieldGroup) contact0;
    assertEquals(1, contact0Group.getField().size());
    assertEquals("/contactList<0>", contact0Group.getPath());
    assertEquals(FieldType.COMPLEX, contact0Group.getFieldType());
    Field firstName0 = contact0Group.getField().get(0);
    assertEquals("/contactList<0>/firstName", firstName0.getPath());
    assertEquals(FieldType.STRING, firstName0.getFieldType());
    assertEquals("f0", firstName0.getValue());
    contactListGroup.setPath("/contactList<1>");
    firstNameField.setPath("/contactList<1>/firstName");
    fieldGroup = (FieldGroup) read(contactListGroup);
    assertEquals(0, audits.size());
    assertEquals(1, fieldGroup.getField().size());
    Field firstName1 = fieldGroup.getField().get(0);
    assertEquals("/contactList<1>/firstName", firstName1.getPath());
    assertEquals(FieldType.STRING, firstName1.getFieldType());
    assertEquals("f1", firstName1.getValue());
}
Also used : Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) JavaField(io.atlasmap.java.v2.JavaField) FieldGroup(io.atlasmap.v2.FieldGroup) TargetContact(io.atlasmap.java.test.TargetContact) TargetTestClass(io.atlasmap.java.test.TargetTestClass) LinkedList(java.util.LinkedList) Test(org.junit.jupiter.api.Test)

Example 15 with FieldGroup

use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.

the class JavaFieldWriter method commitWriting.

/**
 * Commits queued write tasks.
 * @param session session
 * @throws AtlasException unexpected error
 */
public void commitWriting(AtlasInternalSession session) throws AtlasException {
    Field targetField = session.head().getTargetField();
    try {
        if (targetField instanceof FieldGroup) {
            FieldGroup targetFieldGroup = (FieldGroup) targetField;
            if (targetFieldGroup.getField().size() > 0) {
                for (Field f : targetFieldGroup.getField()) {
                    session.head().setTargetField(f);
                    doCommitWriting(session);
                }
            } else {
                AtlasPath path = new AtlasPath(targetField.getPath());
                if (path.getLastSegment().getCollectionType() != CollectionType.NONE) {
                    // create an empty collection
                    doCommitWriting(session);
                }
            }
            return;
        }
        doCommitWriting(session);
    } finally {
        if (!this.pathParentQueue.isEmpty()) {
            LOG.warn("Discarding uncommitted entry in the queue: {}", this.pathParentQueue);
            this.pathParentQueue.clear();
        }
    }
}
Also used : JavaEnumField(io.atlasmap.java.v2.JavaEnumField) Field(io.atlasmap.v2.Field) JavaField(io.atlasmap.java.v2.JavaField) FieldGroup(io.atlasmap.v2.FieldGroup) AtlasPath(io.atlasmap.core.AtlasPath)

Aggregations

FieldGroup (io.atlasmap.v2.FieldGroup)110 Field (io.atlasmap.v2.Field)89 Test (org.junit.jupiter.api.Test)48 SimpleField (io.atlasmap.v2.SimpleField)32 AtlasPath (io.atlasmap.core.AtlasPath)28 ArrayList (java.util.ArrayList)24 AtlasInternalSession (io.atlasmap.spi.AtlasInternalSession)17 CsvField (io.atlasmap.csv.v2.CsvField)16 AtlasException (io.atlasmap.api.AtlasException)15 Audits (io.atlasmap.v2.Audits)14 KafkaConnectField (io.atlasmap.kafkaconnect.v2.KafkaConnectField)13 ConstantField (io.atlasmap.v2.ConstantField)13 Head (io.atlasmap.spi.AtlasInternalSession.Head)12 JsonField (io.atlasmap.json.v2.JsonField)11 Mapping (io.atlasmap.v2.Mapping)11 PropertyField (io.atlasmap.v2.PropertyField)11 JavaField (io.atlasmap.java.v2.JavaField)10 XmlField (io.atlasmap.xml.v2.XmlField)9 SegmentContext (io.atlasmap.core.AtlasPath.SegmentContext)8 LinkedList (java.util.LinkedList)8