Search in sources :

Example 11 with TSalesforceOutputBulkProperties

use of org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties in project components by Talend.

the class SalesforceBulkLoadTestIT method testUpdate.

@Test
public void testUpdate() throws Throwable {
    List<String> ids = util.createTestData();
    String id = ids.get(0);
    final List<Map<String, String>> testData = new ArrayList<Map<String, String>>();
    Map<String, String> datarow = new HashMap<String, String>();
    datarow.put("Id", id);
    datarow.put("FirstName", "Wei");
    datarow.put("LastName", "Wang");
    // update the field
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    datarow = new HashMap<String, String>();
    // should reject
    datarow.put("Id", "not_exist");
    datarow.put("FirstName", "Who");
    datarow.put("LastName", "Who");
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    String data_file = tempFolder.newFile("data.txt").getAbsolutePath();
    // outputbulk part
    TSalesforceOutputBulkDefinition definition = (TSalesforceOutputBulkDefinition) getComponentService().getComponentDefinition(TSalesforceOutputBulkDefinition.COMPONENT_NAME);
    TSalesforceOutputBulkProperties modelProperties = util.simulateUserBasicAction(definition, data_file, util.getTestSchema4());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema4(), testData);
    // bulkexec part
    TSalesforceBulkExecDefinition defin = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    TSalesforceBulkExecProperties modelProps = (TSalesforceBulkExecProperties) defin.createRuntimeProperties();
    Reader reader = util.initReader(defin, data_file, modelProps, util.getTestSchema4(), util.getTestSchema4());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.UPDATE);
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String phone = (String) record.get(3);
                Assert.assertEquals(id, resultid);
                Assert.assertEquals("010-89492686", phone);
            } catch (DataRejectException e) {
                java.util.Map<String, Object> info = e.getRejectInfo();
                Object data = info.get("talend_record");
                String err = (String) info.get("error");
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String firstname = (String) record.get(1);
                String lastname = (String) record.get(2);
                String phone = (String) record.get(3);
                Assert.assertNull(resultid);
                Assert.assertEquals("Who", firstname);
                Assert.assertEquals("Who", lastname);
                Assert.assertEquals("010-89492686", phone);
                Assert.assertTrue(err != null);
            }
        }
    } finally {
        try {
            reader.close();
        } finally {
            util.deleteTestData(ids);
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ArrayList(java.util.ArrayList) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) Reader(org.talend.components.api.component.runtime.Reader) TSalesforceBulkExecProperties(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties) TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) TSalesforceOutputBulkDefinition(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 12 with TSalesforceOutputBulkProperties

use of org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties in project components by Talend.

the class SalesforceBulkLoadTestIT method testUpsert.

@Test
public void testUpsert() throws Throwable {
    List<String> ids = util.createTestData();
    String id = ids.get(0);
    final List<Map<String, String>> testData = new ArrayList<Map<String, String>>();
    Map<String, String> datarow = new HashMap<String, String>();
    // should update
    datarow.put("Id", id);
    datarow.put("FirstName", "Wei");
    datarow.put("LastName", "Wang");
    // update the field
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    datarow = new HashMap<String, String>();
    // should insert
    datarow.put("Id", null);
    datarow.put("FirstName", "Who");
    datarow.put("LastName", "Who");
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    String data_file = tempFolder.newFile("data.txt").getAbsolutePath();
    // outputbulk part
    TSalesforceOutputBulkDefinition definition = (TSalesforceOutputBulkDefinition) getComponentService().getComponentDefinition(TSalesforceOutputBulkDefinition.COMPONENT_NAME);
    TSalesforceOutputBulkProperties modelProperties = util.simulateUserBasicAction(definition, data_file, util.getTestSchema4());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema4(), testData);
    // bulkexec part
    TSalesforceBulkExecDefinition defin = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    TSalesforceBulkExecProperties modelProps = (TSalesforceBulkExecProperties) defin.createRuntimeProperties();
    Reader reader = util.initReader(defin, data_file, modelProps, util.getTestSchema4(), util.getTestSchema4());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.UPSERT);
    modelProps.upsertKeyColumn.setValue("Id");
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        int index = -1;
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String phone = (String) record.get(3);
                index++;
                if (index == 0) {
                    Assert.assertEquals(id, resultid);
                    Assert.assertEquals("010-89492686", phone);
                } else if (index == 1) {
                    Assert.assertTrue(resultid != null);
                    Assert.assertEquals("010-89492686", phone);
                }
            } catch (DataRejectException e) {
                Assert.fail(e.getMessage());
            }
        }
    } finally {
        try {
            reader.close();
        } finally {
            util.deleteTestData(ids);
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ArrayList(java.util.ArrayList) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) Reader(org.talend.components.api.component.runtime.Reader) TSalesforceBulkExecProperties(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties) TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) TSalesforceOutputBulkDefinition(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 13 with TSalesforceOutputBulkProperties

use of org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties in project components by Talend.

the class SalesforceBulkFileWriterTestIT method testBasic.

@Test
public void testBasic() throws Exception {
    TSalesforceOutputBulkDefinition definition = (TSalesforceOutputBulkDefinition) getComponentService().getComponentDefinition(TSalesforceOutputBulkDefinition.COMPONENT_NAME);
    String data_file = tempFolder.newFile("data.txt").getAbsolutePath();
    TSalesforceOutputBulkProperties modelProperties = util.simulateUserBasicAction(definition, data_file, util.getTestSchema1());
    String[] expected = { "FirstName", "LastName", "Phone" };
    List<String> actual = (List<String>) modelProperties.upsertRelationTable.columnName.getPossibleValues();
    Assert.assertArrayEquals(expected, actual.toArray());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema1(), util.getTestData());
    String[] expected1 = { "FirstName,LastName,Phone", "Wei,Wang,010-11111111", "Jin,Zhao,010-11111112", "Wei,Yuan,#N/A" };
    util.compareFileContent(data_file, expected1);
    // test ignore null
    modelProperties.ignoreNull.setValue(true);
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema1(), util.getTestData());
    String[] expected2 = { "FirstName,LastName,Phone", "Wei,Wang,010-11111111", "Jin,Zhao,010-11111112", "Wei,Yuan," };
    util.compareFileContent(data_file, expected2);
    // test append
    modelProperties.append.setValue(true);
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema1(), util.getTestData());
    String[] expected3 = { "FirstName,LastName,Phone", "Wei,Wang,010-11111111", "Jin,Zhao,010-11111112", "Wei,Yuan,", "Wei,Wang,010-11111111", "Jin,Zhao,010-11111112", "Wei,Yuan," };
    util.compareFileContent(data_file, expected3);
}
Also used : TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) List(java.util.List) TSalesforceOutputBulkDefinition(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition) Test(org.junit.Test)

Example 14 with TSalesforceOutputBulkProperties

use of org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties in project components by Talend.

the class SalesforceRuntimeTestUtil method simulateUserBasicAction.

public TSalesforceOutputBulkProperties simulateUserBasicAction(TSalesforceOutputBulkDefinition definition, String data_file, Schema schema) {
    // simulate some ui action
    // user create component
    // setup
    TSalesforceOutputBulkProperties modelProperties = (TSalesforceOutputBulkProperties) definition.createRuntimeProperties();
    // the
    // properties,
    // trigger
    // the
    // setup
    // layout
    // and
    // refresh
    // layout
    // user set the schema and file path
    modelProperties.schema.schema.setValue(schema);
    modelProperties.bulkFilePath.setValue(data_file);
    // user switch the ui and trigger it
    modelProperties.beforeUpsertRelationTable();
    return modelProperties;
}
Also used : TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties)

Example 15 with TSalesforceOutputBulkProperties

use of org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties in project components by Talend.

the class SalesforceBulkFileWriter method getHeaders.

@Override
public String[] getHeaders(Schema schema) {
    TSalesforceOutputBulkProperties salesforceBulkProperties = getBulkProperties();
    List<String> headers = new ArrayList<String>();
    StringBuilder sbuilder = new StringBuilder();
    for (Schema.Field f : schema.getFields()) {
        String header = f.name();
        if (checkDeleteOption(header)) {
            continue;
        }
        String ref_module_name = f.getProp(SalesforceSchemaConstants.REF_MODULE_NAME);
        String ref_field_name = f.getProp(SalesforceSchemaConstants.REF_FIELD_NAME);
        if (ref_module_name != null) {
            header = sbuilder.append(ref_module_name).append(":").append(ref_field_name).append(".").append(f.name()).toString();
            sbuilder.setLength(0);
        } else {
            Object value = salesforceBulkProperties.upsertRelationTable.columnName.getValue();
            if (value != null && value instanceof List) {
                int index = getIndex((List<String>) value, header);
                if (index > -1) {
                    List<Boolean> polymorphics = salesforceBulkProperties.upsertRelationTable.polymorphic.getValue();
                    List<String> lookupFieldModuleNames = salesforceBulkProperties.upsertRelationTable.lookupFieldModuleName.getValue();
                    List<String> lookupRelationshipFieldNames = salesforceBulkProperties.upsertRelationTable.lookupRelationshipFieldName.getValue();
                    List<String> externalIdFromLookupFields = salesforceBulkProperties.upsertRelationTable.lookupFieldExternalIdName.getValue();
                    Object polymorphic = polymorphics.get(index);
                    boolean poly = false;
                    if (polymorphic != null && polymorphic instanceof Boolean) {
                        poly = (Boolean) polymorphic;
                    }
                    if (poly) {
                        sbuilder.append(lookupFieldModuleNames.get(index)).append(":");
                    }
                    sbuilder.append(lookupRelationshipFieldNames.get(index)).append(".").append(externalIdFromLookupFields.get(index));
                    header = sbuilder.toString();
                    sbuilder.setLength(0);
                }
            }
        }
        headers.add(header);
    }
    return headers.toArray(new String[headers.size()]);
}
Also used : Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) Field(org.apache.avro.Schema.Field) TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

TSalesforceOutputBulkProperties (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties)15 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)7 TSalesforceOutputBulkDefinition (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition)7 IndexedRecord (org.apache.avro.generic.IndexedRecord)5 TSalesforceBulkExecProperties (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Map (java.util.Map)4 Reader (org.talend.components.api.component.runtime.Reader)4 DataRejectException (org.talend.components.api.exception.DataRejectException)4 TSalesforceBulkExecDefinition (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition)4 SObject (com.sforce.soap.partner.sobject.SObject)2 Sink (org.talend.components.api.component.runtime.Sink)2 SourceOrSink (org.talend.components.api.component.runtime.SourceOrSink)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 Schema (org.apache.avro.Schema)1 Field (org.apache.avro.Schema.Field)1 Before (org.junit.Before)1 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)1