Search in sources :

Example 1 with TSalesforceBulkExecDefinition

use of org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition in project components by Talend.

the class SalesforceBulkLoadTestIT method testInsert.

@Test
public void testInsert() throws Throwable {
    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.getTestSchema1());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema1(), util.getTestData());
    // bulkexec part
    TSalesforceBulkExecDefinition defin = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    TSalesforceBulkExecProperties modelProps = (TSalesforceBulkExecProperties) defin.createRuntimeProperties();
    Reader reader = util.initReader(defin, data_file, modelProps, util.getTestSchema1(), util.getTestSchema2());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.INSERT);
    List<String> ids = new ArrayList<String>();
    List<String> sids = new ArrayList<String>();
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        final List<Map<String, String>> result = new ArrayList<Map<String, String>>();
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String id = (String) record.get(0);
                ids.add(id);
                String firstname = (String) record.get(1);
                String lasttname = (String) record.get(2);
                String phone = (String) record.get(3);
                String salesforce_id = (String) record.get(4);
                sids.add(salesforce_id);
                Map<String, String> row = new HashMap<String, String>();
                row.put("FirstName", firstname);
                row.put("LastName", lasttname);
                row.put("Phone", phone);
                result.add(row);
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }
        Assert.assertEquals(ids, sids);
        Assert.assertEquals(util.getTestData(), result);
    } finally {
        try {
            reader.close();
        } finally {
            util.deleteTestData(ids);
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) ArrayList(java.util.ArrayList) Reader(org.talend.components.api.component.runtime.Reader) DataRejectException(org.talend.components.api.exception.DataRejectException) 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 2 with TSalesforceBulkExecDefinition

use of org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition in project components by Talend.

the class SalesforceBulkLoadTestIT method testDelete.

@Test
public void testDelete() throws Throwable {
    List<String> ids = util.createTestData();
    final List<Map<String, String>> testData = new ArrayList<Map<String, String>>();
    for (String id : ids) {
        Map<String, String> row = new HashMap<String, String>();
        row.put("Id", id);
        testData.add(row);
    }
    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.getTestSchema3());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema3(), 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.getTestSchema1(), util.getTestSchema3());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.DELETE);
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        List<String> resultIds = new ArrayList<String>();
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String id = (String) record.get(0);
                resultIds.add(id);
            } catch (Exception e) {
                Assert.fail(e.getMessage());
            }
        }
        Assert.assertEquals(ids, resultIds);
    } finally {
        try {
            reader.close();
        } finally {
        // do nothing
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) Reader(org.talend.components.api.component.runtime.Reader) DataRejectException(org.talend.components.api.exception.DataRejectException) 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 3 with TSalesforceBulkExecDefinition

use of org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition in project components by Talend.

the class SalesforceBulkExecReaderTestIT method executeBulkInsert.

/**
 * Test runtime of tSalesforceOutputBulk
 */
private void executeBulkInsert(TSalesforceBulkExecProperties bulkExecProperties, String random, int count) throws Throwable {
    TSalesforceBulkExecDefinition definition = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    RuntimeInfo runtimeInfo = definition.getRuntimeInfo(ExecutionEngine.DI, bulkExecProperties, ConnectorTopology.OUTGOING);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(runtimeInfo, definition.getClass().getClassLoader())) {
        SalesforceSource boundedSource = (SalesforceSource) sandboxedInstance.getInstance();
        boundedSource.initialize(null, bulkExecProperties);
        BoundedReader boundedReader = boundedSource.createReader(null);
        try {
            boolean hasRecord = boundedReader.start();
            List<IndexedRecord> rows = new ArrayList<>();
            while (hasRecord) {
                rows.add((IndexedRecord) boundedReader.getCurrent());
                hasRecord = boundedReader.advance();
            }
            checkRows(random, rows, count);
        } finally {
            boundedReader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) IndexedRecord(org.apache.avro.generic.IndexedRecord) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) ArrayList(java.util.ArrayList) BoundedReader(org.talend.components.api.component.runtime.BoundedReader)

Example 4 with TSalesforceBulkExecDefinition

use of org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition 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 5 with TSalesforceBulkExecDefinition

use of org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition 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)

Aggregations

TSalesforceBulkExecDefinition (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition)6 ArrayList (java.util.ArrayList)5 IndexedRecord (org.apache.avro.generic.IndexedRecord)5 Test (org.junit.Test)5 TSalesforceBulkExecProperties (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Reader (org.talend.components.api.component.runtime.Reader)4 DataRejectException (org.talend.components.api.exception.DataRejectException)4 TSalesforceOutputBulkDefinition (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition)4 TSalesforceOutputBulkProperties (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties)4 BoundedReader (org.talend.components.api.component.runtime.BoundedReader)1 RuntimeInfo (org.talend.daikon.runtime.RuntimeInfo)1 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)1