Search in sources :

Example 21 with TSalesforceOutputProperties

use of org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties in project components by Talend.

the class SalesforceWriterTestIT method cleanupAllRecords.

@AfterClass
public static void cleanupAllRecords() throws NoSuchElementException, IOException {
    List<IndexedRecord> recordsToClean = new ArrayList<>();
    String prefixToDelete = UNIQUE_NAME + "_" + UNIQUE_ID;
    // Get the list of records that match the prefix to delete.
    {
        TSalesforceInputProperties sfProps = getSalesforceInputProperties();
        SalesforceTestBase.setupProps(sfProps.connection, false);
        sfProps.module.setValue("moduleName", "Account");
        sfProps.module.main.schema.setValue(SCHEMA_UPDATE_ACCOUNT);
        DefaultComponentRuntimeContainerImpl container = new DefaultComponentRuntimeContainerImpl();
        // Initialize the Source and Reader
        SalesforceSource sfSource = new SalesforceSource();
        sfSource.initialize(container, sfProps);
        sfSource.validate(container);
        int nameIndex = -1;
        @SuppressWarnings("unchecked") Reader<IndexedRecord> sfReader = sfSource.createReader(container);
        if (sfReader.start()) {
            do {
                IndexedRecord r = sfReader.getCurrent();
                if (nameIndex == -1) {
                    nameIndex = r.getSchema().getField("Name").pos();
                }
                if (String.valueOf(r.get(nameIndex)).startsWith(prefixToDelete)) {
                    recordsToClean.add(r);
                }
            } while (sfReader.advance());
        }
    }
    // Delete those records.
    {
        ComponentDefinition sfDef = new TSalesforceOutputDefinition();
        TSalesforceOutputProperties sfProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
        SalesforceTestBase.setupProps(sfProps.connection, false);
        sfProps.outputAction.setValue(OutputAction.DELETE);
        sfProps.module.setValue("moduleName", "Account");
        sfProps.module.main.schema.setValue(SCHEMA_UPDATE_ACCOUNT);
        DefaultComponentRuntimeContainerImpl container = new DefaultComponentRuntimeContainerImpl();
        // Initialize the Sink, WriteOperation and Writer
        SalesforceSink sfSink = new SalesforceSink();
        sfSink.initialize(container, sfProps);
        sfSink.validate(container);
        SalesforceWriteOperation sfWriteOp = sfSink.createWriteOperation();
        sfWriteOp.initialize(container);
        Writer<Result> sfWriter = sfSink.createWriteOperation().createWriter(container);
        sfWriter.open("uid1");
        // Write one record.
        for (IndexedRecord r : recordsToClean) {
            sfWriter.write(r);
        }
        // Finish the Writer, WriteOperation and Sink.
        Result wr1 = sfWriter.close();
        sfWriteOp.finalize(Arrays.asList(wr1), container);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) ArrayList(java.util.ArrayList) Reader(org.talend.components.api.component.runtime.Reader) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) Result(org.talend.components.api.component.runtime.Result) TSalesforceOutputDefinition(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition) Writer(org.talend.components.api.component.runtime.Writer) ComponentDefinition(org.talend.components.api.component.ComponentDefinition) AfterClass(org.junit.AfterClass)

Example 22 with TSalesforceOutputProperties

use of org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties in project components by Talend.

the class SalesforceWriterTestIT method testSinkAllWithStringValue.

/*
     * With current API like date/datetime/int/.... string value can't be write to server side So we need convert the field
     * value type.
     */
@Test
public void testSinkAllWithStringValue() throws Exception {
    // Component framework objects.
    ComponentDefinition sfDef = new TSalesforceOutputDefinition();
    TSalesforceOutputProperties sfProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
    SalesforceTestBase.setupProps(sfProps.connection, false);
    sfProps.module.setValue("moduleName", "Event");
    sfProps.module.main.schema.setValue(SCHEMA_INSERT_EVENT);
    sfProps.ceaseForError.setValue(true);
    // Automatically generate the out schemas.
    sfProps.module.schemaListener.afterSchema();
    DefaultComponentRuntimeContainerImpl container = new DefaultComponentRuntimeContainerImpl();
    List<IndexedRecord> records = new ArrayList<>();
    String random = createNewRandom();
    IndexedRecord r1 = new GenericData.Record(SCHEMA_INSERT_EVENT);
    r1.put(0, "2011-02-02T02:02:02");
    r1.put(1, "2011-02-02T22:02:02.000Z");
    r1.put(2, "2011-02-02");
    r1.put(3, "1200");
    r1.put(4, "true");
    r1.put(5, random);
    // Rejected and successful writes are reset on the next record.
    IndexedRecord r2 = new GenericData.Record(SCHEMA_INSERT_EVENT);
    r2.put(0, "2016-02-02T02:02:02.000Z");
    r2.put(1, "2016-02-02T12:02:02");
    r2.put(2, "2016-02-02");
    r2.put(3, "600");
    r2.put(4, "0");
    r2.put(5, random);
    records.add(r1);
    records.add(r2);
    SalesforceSink salesforceSink = new SalesforceSink();
    salesforceSink.initialize(adaptor, sfProps);
    salesforceSink.validate(adaptor);
    Writer<Result> batchWriter = salesforceSink.createWriteOperation().createWriter(adaptor);
    writeRows(batchWriter, records);
    assertEquals(2, ((SalesforceWriter) batchWriter).getSuccessfulWrites().size());
    TSalesforceInputProperties sfInputProps = getSalesforceInputProperties();
    sfInputProps.copyValuesFrom(sfProps);
    sfInputProps.condition.setValue("Subject = '" + random + "' ORDER BY DurationInMinutes ASC");
    sfInputProps.module.main.schema.setValue(SCHEMA_INPUT_AND_DELETE_EVENT);
    List<IndexedRecord> inpuRecords = readRows(sfInputProps);
    try {
        assertEquals(2, inpuRecords.size());
        IndexedRecord inputRecords_1 = inpuRecords.get(0);
        IndexedRecord inputRecords_2 = inpuRecords.get(1);
        assertEquals(random, inputRecords_1.get(6));
        assertEquals(random, inputRecords_2.get(6));
        // we use containsInAnyOrder because we are not garanteed to have the same order every run.
        assertThat(Arrays.asList("2011-02-02T02:02:02.000Z", "2016-02-02T02:02:02.000Z"), containsInAnyOrder(inputRecords_1.get(1), inputRecords_2.get(1)));
        assertThat(Arrays.asList("2011-02-02T22:02:02.000Z", "2016-02-02T12:02:02.000Z"), containsInAnyOrder(inputRecords_1.get(2), inputRecords_2.get(2)));
        assertThat(Arrays.asList("2011-02-02", "2016-02-02"), containsInAnyOrder(inputRecords_1.get(3), inputRecords_2.get(3)));
        assertThat(Arrays.asList("1200", "600"), containsInAnyOrder(inputRecords_1.get(4), inputRecords_2.get(4)));
        assertThat(Arrays.asList("true", "false"), containsInAnyOrder(inputRecords_1.get(5), inputRecords_2.get(5)));
    } finally {
        deleteRows(inpuRecords, sfInputProps);
    }
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) ArrayList(java.util.ArrayList) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) Result(org.talend.components.api.component.runtime.Result) TSalesforceOutputDefinition(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) ComponentDefinition(org.talend.components.api.component.ComponentDefinition) Test(org.junit.Test)

Example 23 with TSalesforceOutputProperties

use of org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties in project components by Talend.

the class SalesforceWriterTestIT method testWriterOpenCloseWithEmptyData.

@Test
public void testWriterOpenCloseWithEmptyData() throws Throwable {
    TSalesforceOutputProperties props = createSalesforceoutputProperties(EXISTING_MODULE_NAME);
    Map<String, Object> resultMap;
    // this is mainly to check that open and close do not throw any exceptions.
    // insert
    props.outputAction.setValue(TSalesforceOutputProperties.OutputAction.INSERT);
    props.afterOutputAction();
    Writer<Result> saleforceWriter = createSalesforceOutputWriter(props);
    Result writeResult = writeRows(saleforceWriter, Collections.EMPTY_LIST);
    resultMap = getConsolidatedResults(writeResult, saleforceWriter);
    assertEquals(0, resultMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
    // deleted
    props.outputAction.setValue(TSalesforceOutputProperties.OutputAction.DELETE);
    props.afterOutputAction();
    saleforceWriter = createSalesforceOutputWriter(props);
    writeResult = writeRows(saleforceWriter, Collections.EMPTY_LIST);
    resultMap = getConsolidatedResults(writeResult, saleforceWriter);
    assertEquals(0, resultMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
    // update
    props.outputAction.setValue(TSalesforceOutputProperties.OutputAction.UPDATE);
    props.afterOutputAction();
    saleforceWriter = createSalesforceOutputWriter(props);
    writeResult = writeRows(saleforceWriter, Collections.EMPTY_LIST);
    resultMap = getConsolidatedResults(writeResult, saleforceWriter);
    assertEquals(0, resultMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
    // upsert
    props.outputAction.setValue(TSalesforceOutputProperties.OutputAction.UPSERT);
    props.afterOutputAction();
    saleforceWriter = createSalesforceOutputWriter(props);
    writeResult = writeRows(saleforceWriter, Collections.EMPTY_LIST);
    resultMap = getConsolidatedResults(writeResult, saleforceWriter);
    assertEquals(0, resultMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) Result(org.talend.components.api.component.runtime.Result) Test(org.junit.Test)

Example 24 with TSalesforceOutputProperties

use of org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties in project components by Talend.

the class SalesforceTestBase method deleteAllAccountTestRows.

public static void deleteAllAccountTestRows(String condition) throws Exception {
    TSalesforceInputProperties properties = (TSalesforceInputProperties) new TSalesforceInputProperties("foo").init();
    properties.condition.setValue("Name = '" + condition + "'");
    BoundedReader<?> salesforceInputReader = new SalesforceTestBase().createSalesforceInputReaderFromModule(EXISTING_MODULE_NAME, properties);
    // getting all rows
    List<IndexedRecord> rows = new ArrayList<>();
    try {
        salesforceInputReader.start();
        rows.add((IndexedRecord) salesforceInputReader.getCurrent());
        while (salesforceInputReader.advance()) {
            rows.add((IndexedRecord) salesforceInputReader.getCurrent());
        }
    } finally {
        salesforceInputReader.close();
    }
    // filtering rows
    TSalesforceOutputProperties salesforceoutputProperties = createSalesforceoutputProperties(EXISTING_MODULE_NAME);
    setupProps(salesforceoutputProperties.connection, !ADD_QUOTES);
    new SalesforceTestBase().deleteRows(rows, salesforceoutputProperties);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) ArrayList(java.util.ArrayList) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties)

Example 25 with TSalesforceOutputProperties

use of org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties in project components by Talend.

the class SalesforceTestBase method createSalesforceoutputProperties.

private static TSalesforceOutputProperties createSalesforceoutputProperties(String moduleName) throws Exception {
    TSalesforceOutputProperties props = (TSalesforceOutputProperties) new TSalesforceOutputProperties("foo").init();
    setupProps(props.connection, !ADD_QUOTES);
    props.module.moduleName.setValue(moduleName);
    // to setup schema.
    props.module.afterModuleName();
    return props;
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties)

Aggregations

TSalesforceOutputProperties (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties)32 IndexedRecord (org.apache.avro.generic.IndexedRecord)18 Test (org.junit.Test)16 ComponentDefinition (org.talend.components.api.component.ComponentDefinition)14 Result (org.talend.components.api.component.runtime.Result)13 TSalesforceOutputDefinition (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition)13 ArrayList (java.util.ArrayList)10 TSalesforceInputProperties (org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties)9 Schema (org.apache.avro.Schema)7 DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)7 File (java.io.File)3 IOException (java.io.IOException)3 List (java.util.List)3 GenericData (org.apache.avro.generic.GenericData)3 Ignore (org.junit.Ignore)2 LoginFault (com.sforce.soap.partner.fault.LoginFault)1 NoSuchElementException (java.util.NoSuchElementException)1 AfterClass (org.junit.AfterClass)1 Before (org.junit.Before)1 Connector (org.talend.components.api.component.Connector)1