Search in sources :

Example 1 with DefaultComponentRuntimeContainerImpl

use of org.talend.components.api.container.DefaultComponentRuntimeContainerImpl in project components by Talend.

the class SalesforceSourceOrSinkTestIT method prepareAndGetConnectionHolder.

private void prepareAndGetConnectionHolder(boolean isBulk) throws Exception {
    final SalesforceConnectionProperties connectionProperties = setupProps(null, false);
    connectionProperties.bulkConnection.setValue(isBulk);
    RuntimeContainer container = new DefaultComponentRuntimeContainerImpl() {

        @Override
        public String getCurrentComponentId() {
            return connectionProperties.getName();
        }
    };
    SalesforceSourceOrSink sourceOrSink = new SalesforceSourceOrSink();
    sourceOrSink.initialize(container, connectionProperties);
    // Creating and saving PartnerConnection + BulkConnection(holder) inside container.
    sourceOrSink.validate(container);
    PartnerConnection partnerConnection = (PartnerConnection) container.getComponentData(connectionProperties.getName(), SalesforceSourceOrSink.KEY_CONNECTION);
    TSalesforceInputProperties inputProperties = new TSalesforceInputProperties("input");
    inputProperties.connection.referencedComponent.componentInstanceId.setValue(connectionProperties.getName());
    sourceOrSink.initialize(container, inputProperties);
    ConnectionHolder actualHolder = sourceOrSink.connect(container);
    Assert.assertEquals(partnerConnection, actualHolder.connection);
    if (isBulk) {
        Assert.assertNotNull(actualHolder.bulkConnection);
    } else {
        // Check if bulk connection was not chosen but created.
        Assert.assertNull(actualHolder.bulkConnection);
    }
}
Also used : SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) PartnerConnection(com.sforce.soap.partner.PartnerConnection) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) ConnectionHolder(org.talend.components.salesforce.runtime.common.ConnectionHolder)

Example 2 with DefaultComponentRuntimeContainerImpl

use of org.talend.components.api.container.DefaultComponentRuntimeContainerImpl in project components by Talend.

the class SalesforceSessionReuseTestIT method testUseExistingConnection.

@Ignore("Need to solve test failed randomly")
@Test(timeout = 30_000)
public void testUseExistingConnection() throws Throwable {
    File sessionFolder = new File(tempFolder.getRoot().getPath() + "/tsalesforceconnection_1/");
    assertEquals(0, sessionFolder.getTotalSpace());
    LOGGER.debug("session folder: " + sessionFolder.getAbsolutePath());
    SalesforceConnectionProperties connProps = (SalesforceConnectionProperties) getComponentService().getComponentProperties(TSalesforceConnectionDefinition.COMPONENT_NAME);
    setupProps(connProps, !ADD_QUOTES);
    // setup session function
    connProps.reuseSession.setValue(true);
    connProps.sessionDirectory.setValue(sessionFolder.getAbsolutePath());
    final String currentComponentName = TSalesforceConnectionDefinition.COMPONENT_NAME + "_1";
    final java.util.Map<String, Object> globalMap = new java.util.HashMap<String, Object>();
    RuntimeContainer connContainer = new DefaultComponentRuntimeContainerImpl() {

        @Override
        public String getCurrentComponentId() {
            return currentComponentName;
        }

        @Override
        public Object getComponentData(String componentId, String key) {
            return globalMap.get(componentId + "_" + key);
        }

        @Override
        public void setComponentData(String componentId, String key, Object data) {
            globalMap.put(componentId + "_" + key, data);
        }

        @Override
        public Object getGlobalData(String key) {
            return globalMap.get(key);
        }
    };
    // 1. salesforce connection would save the session to a session file
    SalesforceSourceOrSink salesforceSourceOrSink = new SalesforceSourceOrSink();
    salesforceSourceOrSink.initialize(connContainer, connProps);
    assertEquals(ValidationResult.Result.OK, salesforceSourceOrSink.validate(connContainer).getStatus());
    // 2. set a wrong pwd to connection properties
    connProps.userPassword.password.setValue(WRONG_PWD);
    // Input component get connection from the tSalesforceConnection
    TSalesforceInputProperties inProps = (TSalesforceInputProperties) getComponentService().getComponentProperties(TSalesforceInputDefinition.COMPONENT_NAME);
    inProps.connection.referencedComponent.componentInstanceId.setValue(currentComponentName);
    inProps.connection.referencedComponent.setReference(connProps);
    checkAndAfter(inProps.connection.referencedComponent.getReference().getForm(Form.REFERENCE), "referencedComponent", inProps.connection);
    ComponentTestUtils.checkSerialize(inProps, errorCollector);
    assertEquals(2, inProps.getForms().size());
    Form f = inProps.module.getForm(Form.REFERENCE);
    assertTrue(f.getWidget("moduleName").isCallBeforeActivate());
    ComponentProperties moduleProps = (ComponentProperties) f.getProperties();
    try {
        // 3. input components would be get connection from connection session file
        moduleProps = (ComponentProperties) PropertiesTestUtils.checkAndBeforeActivate(getComponentService(), f, "moduleName", moduleProps);
        Object connection = connContainer.getComponentData(currentComponentName, SalesforceSourceOrSink.KEY_CONNECTION);
        assertNotNull(connection);
        ((PartnerConnection) connection).getConfig().setPassword(WRONG_PWD);
        // Check whether the session disable by other test.
        ((PartnerConnection) connection).getUserInfo();
        Property<?> prop = (Property<?>) f.getWidget("moduleName").getContent();
        assertTrue(prop.getPossibleValues().size() > 100);
        LOGGER.debug(moduleProps.getValidationResult().toString());
        assertEquals(ValidationResult.Result.OK, moduleProps.getValidationResult().getStatus());
        invalidSession(connProps, null);
        // This means that the session is disabled by current test
        // 4. invalid the session, then the session should be renew based on reference connection information(wrong pwd)
        // connect would be fail
        moduleProps = (ComponentProperties) PropertiesTestUtils.checkAndBeforeActivate(getComponentService(), f, "moduleName", moduleProps);
        assertEquals(ValidationResult.Result.ERROR, moduleProps.getValidationResult().getStatus());
        LOGGER.debug(moduleProps.getValidationResult().toString());
    } catch (ConnectionException e) {
        // Maybe get exception when same account run in parallel
        // The session maybe disabled by other test
        assertThat(e, instanceOf(LoginFault.class));
        assertEquals(ExceptionCode.INVALID_LOGIN, ((LoginFault) e).getExceptionCode());
        LOGGER.debug("session is disabled by other test!");
    }
    // 5.set correct pwd back
    setupProps(connProps, !ADD_QUOTES);
    moduleProps = (ComponentProperties) PropertiesTestUtils.checkAndBeforeActivate(getComponentService(), f, "moduleName", moduleProps);
    assertEquals(ValidationResult.Result.OK, moduleProps.getValidationResult().getStatus());
    LOGGER.debug(moduleProps.getValidationResult().toString());
}
Also used : ComponentProperties(org.talend.components.api.properties.ComponentProperties) SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) Form(org.talend.daikon.properties.presentation.Form) PartnerConnection(com.sforce.soap.partner.PartnerConnection) LoginFault(com.sforce.soap.partner.fault.LoginFault) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) File(java.io.File) Property(org.talend.daikon.properties.property.Property) ConnectionException(com.sforce.ws.ConnectionException) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with DefaultComponentRuntimeContainerImpl

use of org.talend.components.api.container.DefaultComponentRuntimeContainerImpl in project components by Talend.

the class SalesforceComponentTestIT method testUseExistingConnection.

@Test
public void testUseExistingConnection() throws Throwable {
    SalesforceConnectionProperties connProps = (SalesforceConnectionProperties) getComponentService().getComponentProperties(TSalesforceConnectionDefinition.COMPONENT_NAME);
    setupProps(connProps, !ADD_QUOTES);
    final String currentComponentName = TSalesforceConnectionDefinition.COMPONENT_NAME + "_1";
    RuntimeContainer connContainer = new DefaultComponentRuntimeContainerImpl() {

        @Override
        public String getCurrentComponentId() {
            return currentComponentName;
        }
    };
    SalesforceSourceOrSink salesforceSourceOrSink = new SalesforceSourceOrSink();
    salesforceSourceOrSink.initialize(connContainer, connProps);
    assertEquals(ValidationResult.Result.OK, salesforceSourceOrSink.validate(connContainer).getStatus());
    // Input component get connection from the tSalesforceConnection
    ComponentDefinition inputDefinition = getComponentService().getComponentDefinition(TSalesforceInputDefinition.COMPONENT_NAME);
    TSalesforceInputProperties inProps = (TSalesforceInputProperties) getComponentService().getComponentProperties(TSalesforceInputDefinition.COMPONENT_NAME);
    inProps.connection.referencedComponent.componentInstanceId.setValue(currentComponentName);
    SalesforceSourceOrSink salesforceInputSourceOrSink = new SalesforceSourceOrSink();
    salesforceInputSourceOrSink.initialize(connContainer, inProps);
    assertEquals(ValidationResult.Result.OK, salesforceInputSourceOrSink.validate(connContainer).getStatus());
}
Also used : SalesforceConnectionProperties(org.talend.components.salesforce.SalesforceConnectionProperties) SalesforceSourceOrSink(org.talend.components.salesforce.runtime.SalesforceSourceOrSink) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) TSalesforceInputProperties(org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties) RuntimeContainer(org.talend.components.api.container.RuntimeContainer) ComponentDefinition(org.talend.components.api.component.ComponentDefinition) Test(org.junit.Test)

Example 4 with DefaultComponentRuntimeContainerImpl

use of org.talend.components.api.container.DefaultComponentRuntimeContainerImpl in project components by Talend.

the class SalesforceWriterTestIT method testWriteDynamicWithFeedback.

/**
 * Test write dynamic with feedback
 */
@Test
public void testWriteDynamicWithFeedback() throws Exception {
    // Component framework objects.
    ComponentDefinition sfDef = new TSalesforceOutputDefinition();
    TSalesforceOutputProperties sfProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
    SalesforceTestBase.setupProps(sfProps.connection, false);
    sfProps.module.setValue("moduleName", "Account");
    Schema dynamicSchema = // 
    SchemaBuilder.builder().record("Schema").prop(SchemaConstants.INCLUDE_ALL_FIELDS, // 
    "true").prop("di.dynamic.column.position", "1").fields().name("Name").type().stringType().noDefault().endRecord();
    Schema dynamicRuntimeSchema = // 
    SchemaBuilder.builder().record("Schema").prop(SchemaConstants.INCLUDE_ALL_FIELDS, // 
    "true").prop("di.dynamic.column.position", "1").fields().name("Name").type().stringType().noDefault().name("BillingStreet").type().stringType().noDefault().name("BillingCity").type().stringType().noDefault().name("BillingState").type().stringType().noDefault().endRecord();
    sfProps.module.main.schema.setValue(dynamicSchema);
    sfProps.extendInsert.setValue(false);
    sfProps.ceaseForError.setValue(false);
    // Automatically generate the out schemas.
    sfProps.module.schemaListener.afterSchema();
    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);
    SalesforceWriter sfWriter = sfSink.createWriteOperation().createWriter(container);
    sfWriter.open("uid1");
    // Write one record, which should fail for missing name.
    IndexedRecord r1 = new GenericData.Record(dynamicRuntimeSchema);
    r1.put(0, "test");
    r1.put(1, "deleteme");
    r1.put(2, "deleteme");
    r1.put(3, "deleteme");
    sfWriter.write(r1);
    // Check success
    assertThat(sfWriter.getSuccessfulWrites(), hasSize(1));
    IndexedRecord success = sfWriter.getSuccessfulWrites().get(0);
    assertThat(success.getSchema().getFields(), hasSize(4));
    // Check the values copied from the incoming record.
    for (int i = 0; i < r1.getSchema().getFields().size(); i++) {
        assertThat(success.getSchema().getFields().get(i), is(r1.getSchema().getFields().get(i)));
        assertThat(success.get(i), is(r1.get(i)));
    }
    // The success fields.
    assertThat(success.getSchema().getFields().get(0).name(), is("Name"));
    assertThat(success.getSchema().getFields().get(1).name(), is("BillingStreet"));
    assertThat(success.getSchema().getFields().get(2).name(), is("BillingCity"));
    assertThat(success.getSchema().getFields().get(3).name(), is("BillingState"));
    assertThat(success.get(0), is((Object) "test"));
    assertThat(success.get(1), is((Object) "deleteme"));
    assertThat(success.get(2), is((Object) "deleteme"));
    assertThat(success.get(3), is((Object) "deleteme"));
    // Check reject
    IndexedRecord r2 = new GenericData.Record(dynamicRuntimeSchema);
    r2.put(1, "deleteme");
    r2.put(2, "deleteme");
    r2.put(3, "deleteme");
    sfWriter.write(r2);
    assertThat(sfWriter.getRejectedWrites(), hasSize(1));
    IndexedRecord rejected = sfWriter.getRejectedWrites().get(0);
    assertThat(rejected.getSchema().getFields(), hasSize(7));
    // The rejected fields.
    assertThat(rejected.getSchema().getFields().get(0).name(), is("Name"));
    assertThat(rejected.getSchema().getFields().get(1).name(), is("BillingStreet"));
    assertThat(rejected.getSchema().getFields().get(2).name(), is("BillingCity"));
    assertThat(rejected.getSchema().getFields().get(3).name(), is("BillingState"));
    assertThat(rejected.getSchema().getFields().get(4).name(), is(TSalesforceOutputProperties.FIELD_ERROR_CODE));
    assertThat(rejected.getSchema().getFields().get(5).name(), is(TSalesforceOutputProperties.FIELD_ERROR_FIELDS));
    assertThat(rejected.getSchema().getFields().get(6).name(), is(TSalesforceOutputProperties.FIELD_ERROR_MESSAGE));
    assertNull(rejected.get(0));
    assertThat(rejected.get(1), is((Object) "deleteme"));
    assertThat(rejected.get(2), is((Object) "deleteme"));
    assertThat(rejected.get(3), is((Object) "deleteme"));
    assertNotNull(rejected.get(4));
    assertNotNull(rejected.get(5));
    assertNotNull(rejected.get(6));
    // Finish the Writer, WriteOperation and Sink.
    Result wr1 = sfWriter.close();
    sfWriteOp.finalize(Arrays.asList(wr1), container);
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) Schema(org.apache.avro.Schema) 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 5 with DefaultComponentRuntimeContainerImpl

use of org.talend.components.api.container.DefaultComponentRuntimeContainerImpl in project components by Talend.

the class SalesforceWriterTestIT method testSinkWorkflow_insert.

/**
 * Basic test that shows how the {@link SalesforceSink} is meant to be used to write data.
 */
@Test
public void testSinkWorkflow_insert() throws Exception {
    // Component framework objects.
    ComponentDefinition sfDef = new TSalesforceOutputDefinition();
    TSalesforceOutputProperties sfProps = (TSalesforceOutputProperties) sfDef.createRuntimeProperties();
    SalesforceTestBase.setupProps(sfProps.connection, false);
    sfProps.module.setValue("moduleName", "Account");
    sfProps.module.main.schema.setValue(SCHEMA_INSERT_ACCOUNT);
    sfProps.ceaseForError.setValue(false);
    // Automatically generate the out schemas.
    sfProps.module.schemaListener.afterSchema();
    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);
    SalesforceWriter sfWriter = sfSink.createWriteOperation().createWriter(container);
    sfWriter.open("uid1");
    // Write one record.
    IndexedRecord r = new GenericData.Record(SCHEMA_INSERT_ACCOUNT);
    r.put(0, UNIQUE_NAME + "_" + UNIQUE_ID);
    r.put(1, "deleteme");
    r.put(2, "deleteme");
    r.put(3, "deleteme");
    sfWriter.write(r);
    sfWriter.close();
    assertThat(sfWriter.getRejectedWrites(), empty());
    assertThat(sfWriter.getSuccessfulWrites(), hasSize(1));
    assertThat(sfWriter.getSuccessfulWrites().get(0), is(r));
    sfWriter.cleanWrites();
    // Rejected and successful writes are reset on the next record.
    r = new GenericData.Record(SCHEMA_INSERT_ACCOUNT);
    r.put(0, UNIQUE_NAME + "_" + UNIQUE_ID);
    r.put(1, "deleteme2");
    r.put(2, "deleteme2");
    r.put(3, "deleteme2");
    sfWriter.write(r);
    sfWriter.close();
    assertThat(sfWriter.getRejectedWrites(), empty());
    assertThat(sfWriter.getSuccessfulWrites(), hasSize(1));
    assertThat(sfWriter.getSuccessfulWrites().get(0), is(r));
    // Finish the Writer, WriteOperation and Sink.
    Result wr1 = sfWriter.close();
    sfWriteOp.finalize(Arrays.asList(wr1), container);
}
Also used : TSalesforceOutputProperties(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties) IndexedRecord(org.apache.avro.generic.IndexedRecord) DefaultComponentRuntimeContainerImpl(org.talend.components.api.container.DefaultComponentRuntimeContainerImpl) TSalesforceOutputDefinition(org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) GenericData(org.apache.avro.generic.GenericData) ComponentDefinition(org.talend.components.api.component.ComponentDefinition) Result(org.talend.components.api.component.runtime.Result) Test(org.junit.Test)

Aggregations

DefaultComponentRuntimeContainerImpl (org.talend.components.api.container.DefaultComponentRuntimeContainerImpl)20 Test (org.junit.Test)14 IndexedRecord (org.apache.avro.generic.IndexedRecord)9 RuntimeContainer (org.talend.components.api.container.RuntimeContainer)9 ComponentDefinition (org.talend.components.api.component.ComponentDefinition)8 Result (org.talend.components.api.component.runtime.Result)8 TSalesforceOutputDefinition (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputDefinition)7 TSalesforceOutputProperties (org.talend.components.salesforce.tsalesforceoutput.TSalesforceOutputProperties)7 TSalesforceInputProperties (org.talend.components.salesforce.tsalesforceinput.TSalesforceInputProperties)5 Schema (org.apache.avro.Schema)4 File (java.io.File)3 Field (org.apache.avro.Schema.Field)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 JDBCSource (org.talend.components.jdbc.runtime.JDBCSource)3 TJDBCInputDefinition (org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition)3 TJDBCInputProperties (org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties)3 SalesforceConnectionProperties (org.talend.components.salesforce.SalesforceConnectionProperties)3 SnowflakeConnectionProperties (org.talend.components.snowflake.SnowflakeConnectionProperties)3 SnowflakeSourceOrSink (org.talend.components.snowflake.runtime.SnowflakeSourceOrSink)3 PartnerConnection (com.sforce.soap.partner.PartnerConnection)2