Search in sources :

Example 1 with LeadActivityRecord

use of org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord in project components by Talend.

the class MarketoLeadClient method convertLeadActivityRecords.

public List<IndexedRecord> convertLeadActivityRecords(List<LeadActivityRecord> recordList, Schema schema, Map<String, String> mappings) {
    List<IndexedRecord> results = new ArrayList<>();
    for (LeadActivityRecord input : recordList) {
        IndexedRecord record = new Record(schema);
        for (Field f : schema.getFields()) {
            String col = mappings.get(f.name());
            switch(col) {
                case FIELD_ID:
                    record.put(f.pos(), input.getId());
                    break;
                case FIELD_MARKETO_GUID:
                    record.put(f.pos(), input.getMarketoGUID());
                    break;
                case FIELD_LEAD_ID:
                    record.put(f.pos(), input.getLeadId());
                    break;
                case FIELD_ACTIVITY_DATE:
                    record.put(f.pos(), input.getActivityDate().getTime());
                    break;
                case FIELD_ACTIVITY_TYPE_ID:
                    record.put(f.pos(), input.getActivityTypeId());
                    break;
                case FIELD_ACTIVITY_TYPE_VALUE:
                    record.put(f.pos(), getActivityTypeNameById(input.getActivityTypeId()));
                    break;
                case FIELD_PRIMARY_ATTRIBUTE_VALUE_ID:
                    record.put(f.pos(), input.getPrimaryAttributeValueId());
                    break;
                case FIELD_PRIMARY_ATTRIBUTE_VALUE:
                    record.put(f.pos(), input.getPrimaryAttributeValue());
                    break;
                case FIELD_CAMPAIGN_ID:
                    record.put(f.pos(), input.getCampaignId());
                    break;
                default:
                    String attr = input.getMktoAttributes().get(col);
                    if (attr != null) {
                        record.put(f.pos(), attr);
                    }
            }
        }
        results.add(record);
    }
    return results;
}
Also used : Field(org.apache.avro.Schema.Field) IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) Record(org.apache.avro.generic.GenericData.Record) IndexedRecord(org.apache.avro.generic.IndexedRecord) LeadChangeRecord(org.talend.components.marketo.runtime.client.rest.type.LeadChangeRecord) LeadActivityRecord(org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord) LeadActivityRecord(org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord)

Example 2 with LeadActivityRecord

use of org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord in project components by Talend.

the class MarketoLeadClientTest method testGetLeadActivity.

@Test
public void testGetLeadActivity() throws Exception {
    iprops.inputOperation.setValue(InputOperation.getLeadActivity);
    iprops.leadKeyTypeREST.setValue(LeadKeyTypeREST.id);
    iprops.leadKeyValues.setValue("12345");
    iprops.includeTypes.type.setValue(Arrays.asList(IncludeExcludeFieldsREST.AddToList.name()));
    iprops.afterInputOperation();
    List<Field> moreFields = new ArrayList<>();
    moreFields.add(new Field("attrName", AvroUtils._string(), "", null));
    moreFields.add(new Field("campaignId", AvroUtils._int(), "", null));
    iprops.schemaInput.schema.setValue(MarketoUtils.newSchema(iprops.schemaInput.schema.getValue(), "test", moreFields));
    iprops.beforeMappingInput();
    // 
    doThrow(new MarketoException("REST", "error")).when(client).executeGetRequest(LeadActivitiesResult.class);
    mktoRR = client.getLeadActivity(iprops, null);
    assertFalse(mktoRR.isSuccess());
    assertFalse(mktoRR.getErrorsString().isEmpty());
    // 
    doReturn(new LeadActivitiesResult()).when(client).executeGetRequest(LeadActivitiesResult.class);
    mktoRR = client.getLeadActivity(iprops, null);
    assertFalse(mktoRR.isSuccess());
    // 
    LeadActivitiesResult lar = new LeadActivitiesResult();
    lar.setSuccess(true);
    List<LeadActivityRecord> lars = new ArrayList<>();
    LeadActivityRecord larecord = new LeadActivityRecord();
    larecord.setId(123456);
    larecord.setMarketoGUID("ABC-123-DEF");
    larecord.setLeadId(12345);
    larecord.setActivityTypeId(1);
    larecord.setActivityTypeValue("Visit Webpage");
    larecord.setPrimaryAttributeValue("changed");
    larecord.setPrimaryAttributeValueId(123);
    larecord.setActivityDate(new Date());
    larecord.setCampaignId(456);
    List<Map<String, Object>> attributes = new ArrayList<>();
    Map<String, Object> attribute = new HashMap<>();
    attribute.put("name", "attrName");
    attribute.put("value", "attrValue");
    attributes.add(attribute);
    larecord.setAttributes(attributes);
    lars.add(larecord);
    lar.setResult(lars);
    doReturn(lar).when(client).executeGetRequest(LeadActivitiesResult.class);
    mktoRR = client.getLeadActivity(iprops, null);
    assertTrue(mktoRR.isSuccess());
    List<IndexedRecord> records = mktoRR.getRecords();
    assertNotNull(records);
    IndexedRecord record = records.get(0);
    assertNotNull(record);
    Schema refSchema = iprops.schemaInput.schema.getValue();
    assertEquals(refSchema, record.getSchema());
    assertEquals(123456, record.get(refSchema.getField("id").pos()));
    assertEquals("ABC-123-DEF", record.get(refSchema.getField("marketoGUID").pos()));
    assertEquals(12345, record.get(refSchema.getField("leadId").pos()));
    assertEquals(1, record.get(refSchema.getField("activityTypeId").pos()));
    assertEquals("Visit Webpage", record.get(refSchema.getField("activityTypeValue").pos()));
    assertEquals(123, record.get(refSchema.getField("primaryAttributeValueId").pos()));
    assertEquals(456, record.get(refSchema.getField("campaignId").pos()));
    assertEquals("changed", record.get(refSchema.getField("primaryAttributeValue").pos()));
    assertTrue(record.get(refSchema.getField("activityDate").pos()) instanceof Long);
    assertEquals("attrValue", record.get(refSchema.getField("attrName").pos()));
}
Also used : LeadActivitiesResult(org.talend.components.marketo.runtime.client.rest.response.LeadActivitiesResult) IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) MarketoException(org.talend.components.marketo.runtime.client.type.MarketoException) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Matchers.anyString(org.mockito.Matchers.anyString) Date(java.util.Date) Field(org.apache.avro.Schema.Field) JsonObject(com.google.gson.JsonObject) LeadActivityRecord(org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord) Map(java.util.Map) HashMap(java.util.HashMap) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)2 Field (org.apache.avro.Schema.Field)2 IndexedRecord (org.apache.avro.generic.IndexedRecord)2 LeadActivityRecord (org.talend.components.marketo.runtime.client.rest.type.LeadActivityRecord)2 JsonObject (com.google.gson.JsonObject)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Schema (org.apache.avro.Schema)1 Record (org.apache.avro.generic.GenericData.Record)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1 LeadActivitiesResult (org.talend.components.marketo.runtime.client.rest.response.LeadActivitiesResult)1 LeadChangeRecord (org.talend.components.marketo.runtime.client.rest.type.LeadChangeRecord)1 MarketoException (org.talend.components.marketo.runtime.client.type.MarketoException)1