Search in sources :

Example 21 with TSalesforceInputProperties

use of org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties in project components by Talend.

the class SalesforceInputReaderTestIT method testAggregrateQueryWithIntTypeAndBasicQuery.

@Test
public void testAggregrateQueryWithIntTypeAndBasicQuery() throws Throwable {
    TSalesforceInputProperties props = createTSalesforceInputProperties(true, false);
    props.manualQuery.setValue(true);
    // alias is necessary and should be the
    props.query.setValue("SELECT COUNT(ID) VALUE FROM Account WHERE Name LIKE 'a%'");
    // same with schema
    props.module.main.schema.setValue(SCHEMA_INT);
    List<IndexedRecord> outputRows = readRows(props);
    assertEquals(1, outputRows.size());
    IndexedRecord record = outputRows.get(0);
    assertNotNull(record.getSchema());
    Object value = record.get(0);
    Assert.assertTrue(value != null && value instanceof Integer);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) Test(org.junit.Test)

Example 22 with TSalesforceInputProperties

use of org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties in project components by Talend.

the class SalesforceInputReaderTestIT method testManualQuery.

@Test
public void testManualQuery() throws Throwable {
    TSalesforceInputProperties props = createTSalesforceInputProperties(false, false);
    props.manualQuery.setValue(true);
    props.query.setValue("select Id from Account WHERE Name = '" + randomizedValue + "'");
    List<IndexedRecord> outputRows = readRows(props);
    assertEquals(100, outputRows.size());
    props.module.main.schema.setValue(// 
    SchemaBuilder.builder().record("MakeRowRecord").fields().name("Id").type().nullable().stringType().noDefault().name("Name").type().nullable().stringType().noDefault().name("Owner_Name").type().nullable().stringType().noDefault().name("Owner_Id").type().nullable().stringType().noDefault().endRecord());
    props.query.setValue("SELECT Id, Name, Owner.Name ,Owner.Id FROM Account WHERE Name = '" + randomizedValue + "'");
    List<IndexedRecord> rowsWithForeignKey = readRows(props);
    props.module.main.schema.setValue(// 
    SchemaBuilder.builder().record("MakeRowRecord").fields().name("Id").type().nullable().stringType().noDefault().name("Name").type().nullable().stringType().noDefault().name("OwnerId").type().nullable().stringType().noDefault().endRecord());
    props.query.setValue("SELECT Id, Name, OwnerId FROM Account WHERE Name = '" + randomizedValue + "'");
    outputRows = readRows(props);
    assertEquals(rowsWithForeignKey.size(), outputRows.size());
    assertEquals(100, rowsWithForeignKey.size());
    IndexedRecord fkRecord = rowsWithForeignKey.get(0);
    IndexedRecord commonRecord = outputRows.get(0);
    assertNotNull(fkRecord);
    assertNotNull(commonRecord);
    Schema schemaFK = fkRecord.getSchema();
    Schema schemaCommon = commonRecord.getSchema();
    assertNotNull(schemaFK);
    assertNotNull(schemaCommon);
    assertEquals(commonRecord.get(schemaCommon.getField("OwnerId").pos()), fkRecord.get(schemaFK.getField("Owner_Id").pos()));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) Test(org.junit.Test)

Example 23 with TSalesforceInputProperties

use of org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties in project components by Talend.

the class SalesforceInputReaderTestIT method checkRows.

protected List<IndexedRecord> checkRows(SalesforceConnectionModuleProperties props, String soql, int nbLine, boolean bulkQuery) throws IOException {
    TSalesforceInputProperties inputProps = (TSalesforceInputProperties) new TSalesforceInputProperties("bar").init();
    inputProps.connection = props.connection;
    inputProps.module = props.module;
    inputProps.batchSize.setValue(200);
    if (bulkQuery) {
        inputProps.queryMode.setValue(TSalesforceInputProperties.QueryMode.Bulk);
    } else {
        inputProps.queryMode.setValue(TSalesforceInputProperties.QueryMode.Query);
    }
    inputProps.manualQuery.setValue(true);
    inputProps.query.setValue(soql);
    List<IndexedRecord> inputRows = readRows(inputProps);
    SalesforceReader<IndexedRecord> reader = (SalesforceReader) createBoundedReader(inputProps);
    boolean hasRecord = reader.start();
    List<IndexedRecord> rows = new ArrayList<>();
    while (hasRecord) {
        org.apache.avro.generic.IndexedRecord unenforced = reader.getCurrent();
        rows.add(unenforced);
        hasRecord = reader.advance();
    }
    Map<String, Object> result = reader.getReturnValues();
    Object totalCount = result.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT);
    assertNotNull(totalCount);
    assertThat((int) totalCount, is(nbLine));
    return inputRows;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) ArrayList(java.util.ArrayList) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord)

Example 24 with TSalesforceInputProperties

use of org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties in project components by Talend.

the class SalesforceWriterTestIT method testNillableBase64Field.

@Test
public void testNillableBase64Field() throws Throwable {
    String moduleName = "StaticResource";
    ComponentDefinition sfDef = new TSalesforceOutputDefinition();
    TSalesforceOutputProperties sfProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
    SalesforceTestBase.setupProps(sfProps.connection, false);
    sfProps.module.setValue("moduleName", moduleName);
    sfProps.module.main.schema.setValue(SCHEMA_STATIC_RESOURCE);
    sfProps.ceaseForError.setValue(true);
    sfProps.module.schemaListener.afterSchema();
    List records = new ArrayList<IndexedRecord>();
    String randomName = getRandomString(10);
    IndexedRecord record = new GenericData.Record(SCHEMA_STATIC_RESOURCE);
    record.put(0, randomName);
    record.put(1, "text/plain");
    record.put(2, "dGhpcyBpcyBiYXNlNjQgIHRlc3QgZmlsZS4g");
    record.put(3, "this is base64 test file.");
    records.add(record);
    SalesforceSink salesforceSink = new SalesforceSink();
    salesforceSink.initialize(adaptor, sfProps);
    salesforceSink.validate(adaptor);
    Writer<Result> batchWriter = salesforceSink.createWriteOperation().createWriter(adaptor);
    writeRows(batchWriter, records);
    TSalesforceInputProperties sfInputProps = getSalesforceInputProperties();
    sfInputProps.copyValuesFrom(sfProps);
    sfInputProps.condition.setValue("Name = '" + randomName + "'");
    sfProps.module.setValue("moduleName", moduleName);
    sfProps.module.main.schema.setValue(SCHEMA_STATIC_RESOURCE);
    List<IndexedRecord> inpuRecords = readRows(sfInputProps);
    try {
        assertEquals(1, inpuRecords.size());
        IndexedRecord r = inpuRecords.get(0);
        assertEquals(randomName, r.get(0));
        assertEquals("text/plain", r.get(1));
        assertEquals("dGhpcyBpcyBiYXNlNjQgIHRlc3QgZmlsZS4g", r.get(2));
        assertEquals("this is base64 test file.", r.get(3));
        assertNotNull(r.get(4));
    } finally {
        deleteRows(inpuRecords, sfInputProps);
    }
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) 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) List(java.util.List) ArrayList(java.util.ArrayList) IndexedRecord(org.apache.avro.generic.IndexedRecord) ComponentDefinition(org.talend.components.api.component.ComponentDefinition) Test(org.junit.Test)

Example 25 with TSalesforceInputProperties

use of org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties in project components by Talend.

the class SalesforceWriterTestIT method testOutputUpsert.

@Ignore("Need to add some custom modules in salesforce account for this test")
@Test
public void testOutputUpsert() throws Throwable {
    Schema CUSTOM_LOOKUP_MODULE_SCHEMA = // 
    SchemaBuilder.builder().record("Schema").fields().name("ExternalID__c").type().stringType().noDefault().name("Name").type().stringType().noDefault().name("Id").type().stringType().noDefault().endRecord();
    Schema CUSTOM_TEST_MODULE_SCHEMA = // 
    SchemaBuilder.builder().record("Schema").fields().name("ExternalID__c").type().stringType().noDefault().name("LookupModuleExternalId").type().stringType().noDefault().name("Name").type().stringType().noDefault().name("Id").type().stringType().noDefault().endRecord();
    // Component framework objects.
    ComponentDefinition sfDef = new TSalesforceOutputDefinition();
    // Prepare the lookup module data
    TSalesforceOutputProperties sfLookupProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
    SalesforceTestBase.setupProps(sfLookupProps.connection, false);
    sfLookupProps.module.setValue("moduleName", "TestLookupModule__c");
    sfLookupProps.module.main.schema.setValue(CUSTOM_LOOKUP_MODULE_SCHEMA);
    sfLookupProps.ceaseForError.setValue(true);
    // Automatically generate the out schemas.
    sfLookupProps.module.schemaListener.afterSchema();
    List<IndexedRecord> records = new ArrayList<>();
    IndexedRecord r1 = new GenericData.Record(CUSTOM_LOOKUP_MODULE_SCHEMA);
    r1.put(0, "EXTERNAL_ID_" + UNIQUE_ID);
    r1.put(1, UNIQUE_NAME + "_" + UNIQUE_ID);
    records.add(r1);
    SalesforceSink salesforceSink = new SalesforceSink();
    salesforceSink.initialize(adaptor, sfLookupProps);
    salesforceSink.validate(adaptor);
    Writer<Result> batchWriter = salesforceSink.createWriteOperation().createWriter(adaptor);
    writeRows(batchWriter, records);
    List<IndexedRecord> successRecords = ((SalesforceWriter) batchWriter).getSuccessfulWrites();
    assertEquals(1, successRecords.size());
    // 2. Upsert "TestModule__c" with upsert relation table
    TSalesforceOutputProperties sfTestLookupProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties().init();
    SalesforceTestBase.setupProps(sfTestLookupProps.connection, false);
    sfTestLookupProps.module.setValue("moduleName", "TestModule__c");
    sfTestLookupProps.module.main.schema.setValue(CUSTOM_TEST_MODULE_SCHEMA);
    // Automatically generate the out schemas.
    sfTestLookupProps.module.schemaListener.afterSchema();
    sfTestLookupProps.outputAction.setValue(OutputAction.UPSERT);
    sfTestLookupProps.afterOutputAction();
    assertEquals(4, sfTestLookupProps.upsertKeyColumn.getPossibleValues().size());
    sfTestLookupProps.upsertKeyColumn.setValue("ExternalID__c");
    sfTestLookupProps.ceaseForError.setValue(true);
    // setup relation table
    sfTestLookupProps.upsertRelationTable.columnName.setValue(Arrays.asList("LookupModuleExternalId"));
    sfTestLookupProps.upsertRelationTable.lookupFieldName.setValue(Arrays.asList("TestLookupModule__c"));
    sfTestLookupProps.upsertRelationTable.lookupRelationshipFieldName.setValue(Arrays.asList("TestLookupModule__r"));
    sfTestLookupProps.upsertRelationTable.lookupFieldModuleName.setValue(Arrays.asList("TestLookupModule__c"));
    sfTestLookupProps.upsertRelationTable.lookupFieldExternalIdName.setValue(Arrays.asList("ExternalID__c"));
    records = new ArrayList<>();
    r1 = new GenericData.Record(CUSTOM_TEST_MODULE_SCHEMA);
    r1.put(0, "EXTERNAL_ID_" + UNIQUE_ID);
    r1.put(1, "EXTERNAL_ID_" + UNIQUE_ID);
    r1.put(2, UNIQUE_NAME + "_" + UNIQUE_ID);
    records.add(r1);
    salesforceSink = new SalesforceSink();
    salesforceSink.initialize(adaptor, sfTestLookupProps);
    salesforceSink.validate(adaptor);
    batchWriter = salesforceSink.createWriteOperation().createWriter(adaptor);
    writeRows(batchWriter, records);
    assertEquals(1, ((SalesforceWriter) batchWriter).getSuccessfulWrites().size());
    TSalesforceInputProperties sfInputProps = getSalesforceInputProperties();
    sfInputProps.copyValuesFrom(sfTestLookupProps);
    // "LookupModuleExternalId" is not the column of module. So "CUSTOM_LOOKUP_MODULE_SCHEMA" for query
    sfInputProps.module.main.schema.setValue(CUSTOM_LOOKUP_MODULE_SCHEMA);
    sfInputProps.condition.setValue("ExternalID__c = 'EXTERNAL_ID_" + UNIQUE_ID + "'");
    List<IndexedRecord> inpuRecords = readRows(sfInputProps);
    assertEquals(1, inpuRecords.size());
    LOGGER.debug("Upsert operation insert a record in module \"TestModule__c\" with ID: " + inpuRecords.get(0).get(2));
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) GenericData(org.apache.avro.generic.GenericData) 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) Ignore(org.junit.Ignore) Test(org.junit.Test)

Aggregations

TSalesforceInputProperties (org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties)55 Test (org.junit.Test)32 IndexedRecord (org.apache.avro.generic.IndexedRecord)30 Schema (org.apache.avro.Schema)11 ArrayList (java.util.ArrayList)10 ComponentDefinition (org.talend.components.api.component.ComponentDefinition)10 TSalesforceOutputProperties (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties)9 TSalesforceOutputDefinition (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition)7 Result (org.talend.components.api.component.runtime.Result)6 DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)5 IOException (java.io.IOException)4 SalesforceConnectionProperties (org.talend.components.salesforce.SalesforceConnectionProperties)4 Form (org.talend.daikon.properties.presentation.Form)4 PartnerConnection (com.sforce.soap.partner.PartnerConnection)3 LoginFault (com.sforce.soap.partner.fault.LoginFault)3 File (java.io.File)3 List (java.util.List)3 Ignore (org.junit.Ignore)3 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)3 TSalesforceInputDefinition (org.talend.components.salesforce.tsalesforceinput.TSalesforceInputDefinition)3