use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class CsvFieldWriterTest method testWithSimpleDocumentWithHeaderAndDelimiterSpecified.
@Test
public void testWithSimpleDocumentWithHeaderAndDelimiterSpecified() throws Exception {
CsvConfig csvConfig = new CsvConfig();
csvConfig.setDelimiter(';');
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);
sourceField = new FieldGroup();
sourceField.setName("family");
sourceField.setPath("/<>/family");
sourceSubField0 = new CsvField();
sourceSubField0.setName("family");
sourceSubField0.setPath("/<0>/family");
sourceSubField0.setValue("Smith");
sourceField.getField().add(sourceSubField0);
sourceSubField1 = new CsvField();
sourceSubField1.setName("family");
sourceSubField1.setPath("/<1>/family");
sourceSubField1.setValue("Johnson");
sourceField.getField().add(sourceSubField1);
targetField = new CsvField();
targetField.setName("familyName");
targetField.setPath("/<>/familyName");
write(writer, sourceField, targetField);
String csv = writer.toCsv();
assertEquals("familyName;givenName\r\nSmith;Bob\r\nJohnson;Andrew\r\n", csv);
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class CsvFieldReaderTest method testWithSimpleDocumentWithHeaderAndDelimiterSpecified.
@Test
public void testWithSimpleDocumentWithHeaderAndDelimiterSpecified() throws Exception {
CsvConfig csvConfig = new CsvConfig();
csvConfig.setDelimiter(';');
csvConfig.setHeaders("givenName;familyName");
CsvFieldReader csvFieldReader = new CsvFieldReader(csvConfig);
csvFieldReader.setDocument(new ByteArrayInputStream("Bob;Smith\nAndrew;Johnson".getBytes()));
AtlasInternalSession session = mock(AtlasInternalSession.class);
when(session.head()).thenReturn(mock(AtlasInternalSession.Head.class));
CsvField csvField = new CsvField();
csvField.setName("familyName");
csvField.setPath("/<>/familyName");
when(session.head().getSourceField()).thenReturn(csvField);
Audits audits = new Audits();
when(session.getAudits()).thenReturn(audits);
FieldGroup field = (FieldGroup) csvFieldReader.read(session);
assertEquals(0, audits.getAudit().size());
assertEquals("Smith", field.getField().get(0).getValue());
assertEquals("/<0>/familyName", field.getField().get(0).getPath());
assertEquals("Johnson", field.getField().get(1).getValue());
assertEquals("/<1>/familyName", field.getField().get(1).getPath());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class JavaFieldReaderTest method testReadTopmostListComplex.
@Test
public void testReadTopmostListComplex() throws Exception {
List<TargetTestClass> complexList = Arrays.asList(new TargetTestClass[] { new TargetTestClass(), new TargetTestClass(), new TargetTestClass() });
complexList.get(0).setAddress(new TargetAddress());
complexList.get(0).getAddress().setAddressLine1("123 any street");
complexList.get(2).setAddress(new TargetAddress());
complexList.get(2).getAddress().setAddressLine1("1234 any street");
reader.setDocument(complexList);
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());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class JavaFieldReaderTest method testReadParentCollectionsChildren.
@Test
public void testReadParentCollectionsChildren() throws Exception {
TargetTestClass source = new TargetTestClass();
TargetOrderArray orderArray = new TargetOrderArray();
source.setOrderArray(orderArray);
TargetOrder[] orders = new TargetOrder[5];
orderArray.setOrders(orders);
for (int i = 0; i < 5; i++) {
TargetOrder order = new TargetOrder();
TargetContact c = new TargetContact();
c.setFirstName("f" + i);
c.setLastName("l" + i);
c.setPhoneNumber("p" + i);
c.setZipCode("z" + i);
order.setContact(c);
orders[i] = order;
}
reader.setDocument(source);
FieldGroup contactGroup = new FieldGroup();
contactGroup.setDocId("TargetTestClass");
contactGroup.setPath("/orderArray/orders[]/contact");
contactGroup.setFieldType(FieldType.COMPLEX);
Field firstNameField = new JavaField();
firstNameField.setDocId("TargetTestClass");
firstNameField.setPath("/orderArray/orders[]/contact/firstName");
firstNameField.setFieldType(FieldType.STRING);
contactGroup.getField().add(firstNameField);
fieldGroup = (FieldGroup) read(contactGroup);
assertEquals(0, audits.size());
assertEquals("/orderArray/orders[]/contact", fieldGroup.getPath());
assertEquals(FieldType.COMPLEX, fieldGroup.getFieldType());
assertNull(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("/orderArray/orders[0]/contact", contact0Group.getPath());
assertEquals(FieldType.COMPLEX, contact0Group.getFieldType());
assertNull(contact0Group.getCollectionType());
Field firstName0 = contact0Group.getField().get(0);
assertEquals("/orderArray/orders[0]/contact/firstName", firstName0.getPath());
assertEquals(FieldType.STRING, firstName0.getFieldType());
assertNull(firstName0.getCollectionType());
assertEquals("f0", firstName0.getValue());
contactGroup.setPath("/orderArray/orders[1]/contact");
firstNameField.setPath("/orderArray/orders[1]/contact/firstName");
fieldGroup = (FieldGroup) read(contactGroup);
assertEquals(0, audits.size());
assertEquals(1, fieldGroup.getField().size());
assertEquals("/orderArray/orders[1]/contact", fieldGroup.getPath());
assertEquals(FieldType.COMPLEX, fieldGroup.getFieldType());
Field firstName1 = fieldGroup.getField().get(0);
assertEquals("/orderArray/orders[1]/contact/firstName", firstName1.getPath());
assertEquals(FieldType.STRING, firstName1.getFieldType());
assertEquals("f1", firstName1.getValue());
}
use of io.atlasmap.v2.FieldGroup in project atlasmap by atlasmap.
the class XmlModule method populateTargetField.
@Override
public void populateTargetField(AtlasInternalSession session) throws AtlasException {
Field sourceField = session.head().getSourceField();
Field targetField = session.head().getTargetField();
XmlPath path = new XmlPath(targetField.getPath());
FieldGroup targetFieldGroup = null;
if (path.hasCollection() && !path.isIndexedCollection()) {
targetFieldGroup = AtlasModelFactory.createFieldGroupFrom(targetField, true);
session.head().setTargetField(targetFieldGroup);
}
// Attempt to Auto-detect field type based on input value
if (targetField.getFieldType() == null && sourceField.getValue() != null) {
targetField.setFieldType(getConversionService().fieldTypeFromClass(sourceField.getValue().getClass()));
}
if (targetFieldGroup == null) {
if (sourceField instanceof FieldGroup) {
List<Field> subFields = ((FieldGroup) sourceField).getField();
if (subFields != null && subFields.size() > 0) {
Integer index = targetField.getIndex();
if (index != null) {
if (subFields.size() > index) {
sourceField = subFields.get(index);
} else {
AtlasUtil.addAudit(session, getDocId(), String.format("The number of source fields (%s) is smaller than target index (%s) - ignoring", subFields.size(), index), AuditStatus.WARN, null);
return;
}
} else {
// The last one wins for compatibility
sourceField = subFields.get(subFields.size() - 1);
}
session.head().setSourceField(sourceField);
}
}
super.populateTargetField(session);
} else if (sourceField instanceof FieldGroup) {
Field previousTargetSubField = null;
for (int i = 0; i < ((FieldGroup) sourceField).getField().size(); i++) {
Field sourceSubField = ((FieldGroup) sourceField).getField().get(i);
XmlField targetSubField = new XmlField();
AtlasXmlModelFactory.copyField(targetField, targetSubField, false);
getCollectionHelper().copyCollectionIndexes(sourceField, sourceSubField, targetSubField, previousTargetSubField);
previousTargetSubField = targetSubField;
// Attempt to Auto-detect field type based on input value
if (targetSubField.getFieldType() == null && sourceSubField.getValue() != null) {
targetSubField.setFieldType(getConversionService().fieldTypeFromClass(sourceSubField.getValue().getClass()));
}
targetFieldGroup.getField().add(targetSubField);
session.head().setSourceField(sourceSubField);
session.head().setTargetField(targetSubField);
super.populateTargetField(session);
}
session.head().setSourceField(sourceField);
session.head().setTargetField(targetFieldGroup);
} else {
XmlField targetSubField = new XmlField();
AtlasXmlModelFactory.copyField(targetField, targetSubField, false);
path.setVacantCollectionIndex(0);
targetSubField.setPath(path.toString());
targetFieldGroup.getField().add(targetSubField);
session.head().setTargetField(targetSubField);
super.populateTargetField(session);
session.head().setTargetField(targetFieldGroup);
}
if (LOG.isDebugEnabled()) {
LOG.debug("{}: processTargetFieldMapping completed: SourceField:[docId={}, path={}, type={}, value={}], TargetField:[docId={}, path={}, type={}, value={}]", getDocId(), sourceField.getDocId(), sourceField.getPath(), sourceField.getFieldType(), sourceField.getValue(), targetField.getDocId(), targetField.getPath(), targetField.getFieldType(), targetField.getValue());
}
}
Aggregations