Search in sources :

Example 26 with Record

use of org.apache.avro.generic.GenericData.Record in project components by Talend.

the class MarketoOutputWriterTest method testWriteSyncLead.

@Test
public void testWriteSyncLead() throws Exception {
    props.outputOperation.setValue(OutputOperation.syncLead);
    props.dieOnError.setValue(false);
    props.batchSize.setValue(1);
    props.updateSchemaRelated();
    when(sink.getProperties()).thenReturn(props);
    doReturn(getFailedSyncResult(false)).when(client).syncLead(any(TMarketoOutputProperties.class), any(IndexedRecord.class));
    doReturn(getFailedSyncResult(false)).when(client).syncMultipleLeads(any(TMarketoOutputProperties.class), any(List.class));
    IndexedRecord record = new Record(MarketoConstants.getRESTOutputSchemaForSyncLead());
    record.put(0, 12345);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    doReturn(getSuccessSyncResult("created")).when(client).syncLead(any(TMarketoOutputProperties.class), any(IndexedRecord.class));
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    // 
    // 
    props.outputOperation.setValue(OutputOperation.syncMultipleLeads);
    props.batchSize.setValue(2);
    when(sink.getProperties()).thenReturn(props);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    doReturn(getSuccessSyncResult("created")).when(client).syncMultipleLeads(any(TMarketoOutputProperties.class), any(List.class));
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    // 
    props.schemaInput.schema.setValue(getLeadDynamicSchema());
    props.updateOutputSchemas();
    when(sink.getProperties()).thenReturn(props);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    // 
    props.connection.apiMode.setValue(APIMode.SOAP);
    props.updateSchemaRelated();
    when(sink.getProperties()).thenReturn(props);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoOutputProperties(org.talend.components.marketo.tmarketooutput.TMarketoOutputProperties) ArrayList(java.util.ArrayList) List(java.util.List) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 27 with Record

use of org.apache.avro.generic.GenericData.Record in project components by Talend.

the class MarketoOutputWriterTest method testWriteCustomObject.

@Test
public void testWriteCustomObject() throws Exception {
    props.outputOperation.setValue(OutputOperation.deleteCustomObjects);
    props.updateSchemaRelated();
    when(sink.getProperties()).thenReturn(props);
    doReturn(getSuccessSyncResult("deleted")).when(client).deleteCustomObjects(any(TMarketoOutputProperties.class), any(List.class));
    IndexedRecord record = new Record(MarketoConstants.getRESTOutputSchemaForSyncLead());
    record.put(0, 12345);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
    // 
    props.outputOperation.setValue(OutputOperation.syncCustomObjects);
    when(sink.getProperties()).thenReturn(props);
    doReturn(getSuccessSyncResult("updated")).when(client).syncCustomObjects(any(TMarketoOutputProperties.class), any(List.class));
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoOutputProperties(org.talend.components.marketo.tmarketooutput.TMarketoOutputProperties) ArrayList(java.util.ArrayList) List(java.util.List) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 28 with Record

use of org.apache.avro.generic.GenericData.Record in project components by Talend.

the class MarketoOutputWriterTest method testRetryOperationFailNonRecoverableErrror.

@Test
public void testRetryOperationFailNonRecoverableErrror() throws Exception {
    IndexedRecord record = new Record(MarketoConstants.getRESTOutputSchemaForSyncMultipleLeads());
    record.put(0, 12345);
    doReturn(false).when(client).isErrorRecoverable(any(List.class));
    doReturn(getFailedSyncResult("REST", "902", "Invalid operation")).when(client).syncMultipleLeads(any(TMarketoOutputProperties.class), any(List.class));
    props.dieOnError.setValue(false);
    props.outputOperation.setValue(OutputOperation.syncMultipleLeads);
    props.updateSchemaRelated();
    when(sink.getProperties()).thenReturn(props);
    writer.open("test");
    writer.write(record);
    MarketoResult result = (MarketoResult) writer.close();
    assertEquals(1, result.apiCalls);
    assertEquals(Collections.emptyList(), writer.getSuccessfulWrites());
    List<IndexedRecord> rejects = writer.getRejectedWrites();
    IndexedRecord reject = rejects.get(0);
    assertNotNull(reject);
    assertEquals("failed", reject.get(4));
    assertTrue(String.valueOf(reject.get(5)).contains("902"));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TMarketoOutputProperties(org.talend.components.marketo.tmarketooutput.TMarketoOutputProperties) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 29 with Record

use of org.apache.avro.generic.GenericData.Record in project components by Talend.

the class MarketoOutputWriterTest method testWriteDeleteLeads.

@Test
public void testWriteDeleteLeads() throws Exception {
    writer.open("test");
    writer.write(null);
    // deleteLeads
    doReturn(getFailedSyncResult(true)).when(client).deleteLeads(any(ArrayList.class));
    IndexedRecord record = new Record(MarketoConstants.getDeleteLeadsSchema());
    record.put(0, 12345);
    try {
        writer.write(record);
        fail("Should not be here");
    } catch (Exception e) {
    }
    props.dieOnError.setValue(false);
    when(sink.getProperties()).thenReturn(props);
    writer.open("test");
    writer.write(record);
    assertNotNull(writer.close());
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 30 with Record

use of org.apache.avro.generic.GenericData.Record in project components by Talend.

the class MarketoSOAPClient method convertLeadChangeRecords.

private List<IndexedRecord> convertLeadChangeRecords(List<LeadChangeRecord> value, Schema schema, Map<String, String> mappings) {
    List<IndexedRecord> results = new ArrayList<>();
    for (LeadChangeRecord input : value) {
        IndexedRecord record = new Record(schema);
        for (Field f : schema.getFields()) {
            // find matching marketo column name
            String col = mappings.get(f.name());
            if (col == null) {
                LOG.warn("[convertLeadChangeRecords] Couldn't find mapping for column {}.", f.name());
                continue;
            }
            switch(col) {
                case FIELD_ID:
                    record.put(f.pos(), input.getId().getValue());
                    break;
                case FIELD_MARKETO_GUID:
                    record.put(f.pos(), input.getMarketoGUID());
                    break;
                case FIELD_ACTIVITY_DATE_TIME:
                    record.put(f.pos(), input.getActivityDateTime() != null ? input.getActivityDateTime().toGregorianCalendar().getTimeInMillis() : null);
                    break;
                case FIELD_ACTIVITY_TYPE:
                    record.put(f.pos(), input.getActivityType());
                    break;
                case FIELD_MKTG_ASSET_NAME:
                    record.put(f.pos(), input.getMktgAssetName() != null ? input.getMktgAssetName().getValue() : null);
                    break;
                case FIELD_MKT_PERSON_ID:
                    record.put(f.pos(), input.getMktPersonId());
                    break;
                case FIELD_CAMPAIGN:
                    record.put(f.pos(), input.getCampaign());
                    break;
                default:
                    if (!input.getActivityAttributes().isNil()) {
                        for (Attribute attr : input.getActivityAttributes().getValue().getAttributes()) {
                            if (attr.getAttrName().equals(col)) {
                                record.put(f.pos(), attr.getAttrValue());
                            }
                        }
                    }
            }
        }
        results.add(record);
    }
    return results;
}
Also used : Field(org.apache.avro.Schema.Field) IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayOfAttribute(com.marketo.mktows.ArrayOfAttribute) Attribute(com.marketo.mktows.Attribute) ArrayList(java.util.ArrayList) Record(org.apache.avro.generic.GenericData.Record) ActivityRecord(com.marketo.mktows.ActivityRecord) LeadChangeRecord(com.marketo.mktows.LeadChangeRecord) ArrayOfLeadRecord(com.marketo.mktows.ArrayOfLeadRecord) IndexedRecord(org.apache.avro.generic.IndexedRecord) LeadRecord(com.marketo.mktows.LeadRecord) ArrayOfString(com.marketo.mktows.ArrayOfString) LeadChangeRecord(com.marketo.mktows.LeadChangeRecord)

Aggregations

Record (org.apache.avro.generic.GenericData.Record)96 Test (org.junit.Test)44 IndexedRecord (org.apache.avro.generic.IndexedRecord)43 Schema (org.apache.avro.Schema)33 ArrayList (java.util.ArrayList)24 GenericRecord (org.apache.avro.generic.GenericRecord)14 Field (org.apache.avro.Schema.Field)11 List (java.util.List)10 GenericData (org.apache.avro.generic.GenericData)10 TestRunner (org.apache.nifi.util.TestRunner)8 GenericRecordBuilder (org.apache.avro.generic.GenericRecordBuilder)7 JsonObject (com.google.gson.JsonObject)6 DataFileStream (org.apache.avro.file.DataFileStream)6 DataFileWriter (org.apache.avro.file.DataFileWriter)6 GenericDatumReader (org.apache.avro.generic.GenericDatumReader)6 Utf8 (org.apache.avro.util.Utf8)6 TMarketoOutputProperties (org.talend.components.marketo.tmarketooutput.TMarketoOutputProperties)6 ActivityRecord (com.marketo.mktows.ActivityRecord)5 ArrayOfLeadRecord (com.marketo.mktows.ArrayOfLeadRecord)5 LeadChangeRecord (com.marketo.mktows.LeadChangeRecord)5