Search in sources :

Example 26 with SandboxedInstance

use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.

the class JdbcRowTestIT method test_use_preparedstatement_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_use_preparedstatement_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from " + tablename + " where id = ?");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(1));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            IndexedRecord row = (IndexedRecord) reader.getCurrent();
            ResultSet resultSet = (ResultSet) row.get(0);
            resultSet.next();
            Assert.assertEquals(1, resultSet.getInt(1));
            Assert.assertEquals("wangwei", resultSet.getString(2));
            resultSet.close();
            // only output one row when it works as a input component
            Assert.assertFalse(reader.advance());
            reader.close();
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ResultSet(java.sql.ResultSet) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 27 with SandboxedInstance

use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.

the class JdbcRowTestIT method test_die_on_error_no_connector.

@Test
public void test_die_on_error_no_connector() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(4, 'a too long value')");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.NONE), properties.getClass().getClassLoader())) {
        JDBCRowSourceOrSink sourceOrSink = (JDBCRowSourceOrSink) sandboxedInstance.getInstance();
        sourceOrSink.initialize(null, properties);
        ValidationResult result = sourceOrSink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.ERROR);
        Assert.assertNotNull(result.getMessage());
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) JDBCRowSourceOrSink(org.talend.components.jdbc.runtime.JDBCRowSourceOrSink) ValidationResult(org.talend.daikon.properties.ValidationResult) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 28 with SandboxedInstance

use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.

the class JdbcRowTestIT method test_reject_as_input.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_as_input() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema4(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("select id, name from notexists");
    properties.dieOnError.setValue(false);
    randomCommit(properties);
    // the field is the unique reason to use the component as a input
    properties.propagateQueryResultSet.setValue(true);
    // component
    properties.beforeUseColumn();
    properties.useColumn.setValue(properties.useColumn.getPossibleValues().get(0).toString());
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSource source = (JDBCRowSource) sandboxedInstance.getInstance();
        source.initialize(null, properties);
        ValidationResult result = source.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        Reader reader = source.createReader(null);
        try {
            reader.start();
            reader.getCurrent();
            // should go to the exception before current statement
            Assert.fail();
            reader.advance();
            reader.close();
        } catch (DataRejectException e) {
            Map<String, Object> info = e.getRejectInfo();
            IndexedRecord data = (IndexedRecord) info.get("talend_record");
            Assert.assertNull(data.get(0));
            Assert.assertNotNull(data.get(1));
            Assert.assertNotNull(data.get(2));
        } finally {
            reader.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) Schema(org.apache.avro.Schema) Reader(org.talend.components.api.component.runtime.Reader) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowSource(org.talend.components.jdbc.runtime.JDBCRowSource) Map(java.util.Map) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 29 with SandboxedInstance

use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.

the class TJDBCInputProperties method afterFetchSchemaFromQuery.

public ValidationResult afterFetchSchemaFromQuery(RuntimeContext runtimeContext) {
    Object mappingFileLocation = runtimeContext.getData(ComponentConstants.MAPPING_LOCATION);
    if (mappingFileLocation == null) {
        return new ValidationResult(ValidationResult.Result.ERROR, "can't find the mapping files directory");
    }
    JdbcRuntimeInfo jdbcRuntimeInfo = new JdbcRuntimeInfo(this, "org.talend.components.jdbc.runtime.JDBCSource");
    try (SandboxedInstance sandboxI = RuntimeUtil.createRuntimeClass(jdbcRuntimeInfo, connection.getClass().getClassLoader())) {
        JdbcRuntimeSourceOrSink ss = (JdbcRuntimeSourceOrSink) sandboxI.getInstance();
        ss.initialize(null, this);
        try {
            ss.setDBTypeMapping(CommonUtils.getMapping((String) mappingFileLocation, this.getRuntimeSetting(), null, dbMapping.getValue()));
            Schema schema = ss.getSchemaFromQuery(null, sql.getValue());
            main.schema.setValue(schema);
        } catch (Exception e) {
            LOG.error("failed to retrieve the schema :", e);
            return new ValidationResult(ValidationResult.Result.ERROR, CommonUtils.getClearExceptionInfo(e));
        }
    }
    return ValidationResult.OK;
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) JdbcRuntimeSourceOrSink(org.talend.components.jdbc.runtime.setting.JdbcRuntimeSourceOrSink) JdbcRuntimeInfo(org.talend.components.jdbc.JdbcRuntimeInfo) Schema(org.apache.avro.Schema) ValidationResult(org.talend.daikon.properties.ValidationResult)

Example 30 with SandboxedInstance

use of org.talend.daikon.sandbox.SandboxedInstance in project components by Talend.

the class JDBCConnectionWizardProperties method afterFormFinishMain.

public ValidationResult afterFormFinishMain(Repository<Properties> repo) throws Exception {
    JdbcRuntimeInfo jdbcRuntimeInfo = new JdbcRuntimeInfo(this, "org.talend.components.jdbc.runtime.JDBCSourceOrSink");
    try (SandboxedInstance sandboxI = RuntimeUtil.createRuntimeClass(jdbcRuntimeInfo, connection.getClass().getClassLoader())) {
        JdbcRuntimeSourceOrSink sourceOrSink = (JdbcRuntimeSourceOrSink) sandboxI.getInstance();
        sourceOrSink.initialize(null, this);
        ValidationResult vr = sourceOrSink.validate(null);
        if (vr.getStatus() != ValidationResult.Result.OK) {
            return vr;
        }
        repo.storeProperties(this, this.name.getValue(), repositoryLocation, null);
        // no need to store the schemas, tup will do it by the old way, so only need to store the connection properties
        return ValidationResult.OK;
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) JdbcRuntimeSourceOrSink(org.talend.components.jdbc.runtime.setting.JdbcRuntimeSourceOrSink) JdbcRuntimeInfo(org.talend.components.jdbc.JdbcRuntimeInfo) ValidationResult(org.talend.daikon.properties.ValidationResult)

Aggregations

SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)66 Test (org.junit.Test)30 ValidationResult (org.talend.daikon.properties.ValidationResult)30 RuntimeInfo (org.talend.daikon.runtime.RuntimeInfo)28 Schema (org.apache.avro.Schema)20 IndexedRecord (org.apache.avro.generic.IndexedRecord)18 ArrayList (java.util.ArrayList)12 ComponentException (org.talend.components.api.exception.ComponentException)12 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)10 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)10 NamedThing (org.talend.daikon.NamedThing)9 DatasetRuntime (org.talend.components.common.dataset.runtime.DatasetRuntime)8 SalesforceDefinition.getSandboxedInstance (org.talend.components.salesforce.SalesforceDefinition.getSandboxedInstance)8 SalesforceRuntimeSourceOrSink (org.talend.components.salesforce.common.SalesforceRuntimeSourceOrSink)8 ValidationResultMutable (org.talend.daikon.properties.ValidationResultMutable)8 TalendRuntimeException (org.talend.daikon.exception.TalendRuntimeException)7 IOException (java.io.IOException)6 MarketoComponentDefinition.getSandboxedInstance (org.talend.components.marketo.MarketoComponentDefinition.getSandboxedInstance)6 MarketoSourceOrSinkRuntime (org.talend.components.marketo.runtime.MarketoSourceOrSinkRuntime)6 WriteOperation (org.talend.components.api.component.runtime.WriteOperation)5