Search in sources :

Example 46 with SandboxedInstance

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

the class SandboxedSimpleFileIODatasetRuntimeTest method testBasic.

@Test
public void testBasic() throws Exception {
    File input = folder.newFile("stuff.csv");
    try (FileWriter fw = new FileWriter(input)) {
        fw.write("1;one");
    }
    SimpleFileIODatasetProperties props = createDatasetProperties();
    props.path.setValue(input.toURI().toString());
    final List<IndexedRecord> consumed = new ArrayList<>();
    RuntimeInfo ri = def.getRuntimeInfo(props);
    try (SandboxedInstance si = RuntimeUtil.createRuntimeClass(ri, getClass().getClassLoader())) {
        DatasetRuntime runtime = (DatasetRuntime) si.getInstance();
        runtime.initialize(null, props);
        assertThat(runtime, not(nullValue()));
        Schema s = runtime.getSchema();
        assertThat(s, not(nullValue()));
        runtime.getSample(100, new Consumer<IndexedRecord>() {

            @Override
            public void accept(IndexedRecord ir) {
                consumed.add(ir);
            }
        });
    }
    assertThat(consumed, hasSize(1));
    assertThat(consumed.get(0).get(0), is((Object) "1"));
    assertThat(consumed.get(0).get(1), is((Object) "one"));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) DatasetRuntime(org.talend.components.common.dataset.runtime.DatasetRuntime) FileWriter(java.io.FileWriter) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) File(java.io.File) SimpleFileIODatasetProperties(org.talend.components.simplefileio.SimpleFileIODatasetProperties) Test(org.junit.Test)

Example 47 with SandboxedInstance

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

the class SimpleFileIODatasetRuntimeTest method testBasic.

@Test
public void testBasic() throws Exception {
    File input = folder.newFile("stuff.csv");
    try (FileWriter fw = new FileWriter(input)) {
        fw.write("1;one");
    }
    SimpleFileIODatasetProperties props = createDatasetProperties();
    props.path.setValue(input.toURI().toString());
    final List<IndexedRecord> consumed = new ArrayList<>();
    RuntimeInfo ri = def.getRuntimeInfo(props);
    try (SandboxedInstance si = RuntimeUtil.createRuntimeClass(ri, getClass().getClassLoader())) {
        DatasetRuntime runtime = (DatasetRuntime) si.getInstance();
        runtime.initialize(null, props);
        assertThat(runtime, not(nullValue()));
        Schema s = runtime.getSchema();
        assertThat(s, not(nullValue()));
        runtime.getSample(100, new Consumer<IndexedRecord>() {

            @Override
            public void accept(IndexedRecord ir) {
                consumed.add(ir);
            }
        });
    }
    assertThat(consumed, hasSize(1));
    assertThat(consumed.get(0).get(0), is((Object) "1"));
    assertThat(consumed.get(0).get(1), is((Object) "one"));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) DatasetRuntime(org.talend.components.common.dataset.runtime.DatasetRuntime) FileWriter(java.io.FileWriter) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) File(java.io.File) SimpleFileIODatasetProperties(org.talend.components.simplefileio.SimpleFileIODatasetProperties) Test(org.junit.Test)

Example 48 with SandboxedInstance

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

the class JDBCDatasetProperties method updateSchema.

public void updateSchema() {
    JDBCDatasetDefinition definition = new JDBCDatasetDefinition();
    RuntimeInfo runtimeInfo = definition.getRuntimeInfo(this);
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(runtimeInfo, getClass().getClassLoader())) {
        DatasetRuntime<JDBCDatasetProperties> runtime = (DatasetRuntime) sandboxedInstance.getInstance();
        runtime.initialize(null, this);
        Schema schema = runtime.getSchema();
        main.schema.setValue(schema);
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) RuntimeInfo(org.talend.daikon.runtime.RuntimeInfo) DatasetRuntime(org.talend.components.common.dataset.runtime.DatasetRuntime) Schema(org.apache.avro.Schema)

Example 49 with SandboxedInstance

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

the class JdbcRowTestIT method test_reject_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_reject_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(?,?)");
    properties.dieOnError.setValue(false);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "a too long value"));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.INCOMING_AND_OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSink sink = (JDBCRowSink) sandboxedInstance.getInstance();
        sink.initialize(null, properties);
        ValidationResult result = sink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        WriteOperation operation = sink.createWriteOperation();
        JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
        try {
            writer.open("wid");
            IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
            r1.put(0, 4);
            r1.put(1, "xiaoming");
            writer.write(r1);
            List<IndexedRecord> rejects = writer.getRejectedWrites();
            assertThat(rejects, hasSize(1));
            IndexedRecord reject = rejects.get(0);
            Assert.assertEquals(4, reject.get(0));
            Assert.assertEquals("xiaoming", reject.get(1));
            Assert.assertNotNull(reject.get(2));
            Assert.assertNotNull(reject.get(3));
            assertThat(writer.getSuccessfulWrites(), empty());
            writer.cleanWrites();
            IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
            r2.put(0, 5);
            r2.put(1, "xiaobai");
            writer.write(r2);
            rejects = writer.getRejectedWrites();
            assertThat(rejects, hasSize(1));
            reject = rejects.get(0);
            Assert.assertEquals(5, reject.get(0));
            Assert.assertEquals("xiaobai", reject.get(1));
            Assert.assertNotNull(reject.get(2));
            Assert.assertNotNull(reject.get(3));
            assertThat(writer.getSuccessfulWrites(), empty());
            writer.cleanWrites();
            writer.close();
        } finally {
            writer.close();
        }
    }
}
Also used : SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) IndexedRecord(org.apache.avro.generic.IndexedRecord) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

Example 50 with SandboxedInstance

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

the class JdbcRowTestIT method test_basic_as_output.

@SuppressWarnings("rawtypes")
@Test
public void test_basic_as_output() throws Exception {
    TJDBCRowDefinition definition = new TJDBCRowDefinition();
    TJDBCRowProperties properties = DBTestUtils.createCommonJDBCRowProperties(allSetting, definition);
    Schema schema = DBTestUtils.createTestSchema(tablename);
    properties.main.schema.setValue(schema);
    properties.updateOutputSchemas();
    properties.tableSelection.tablename.setValue(tablename);
    properties.sql.setValue("insert into " + tablename + " values(?,?)");
    properties.dieOnError.setValue(true);
    randomCommit(properties);
    properties.usePreparedStatement.setValue(true);
    properties.preparedStatementTable.indexs.setValue(Arrays.asList(1, 2));
    properties.preparedStatementTable.types.setValue(Arrays.asList(PreparedStatementTable.Type.Int.name(), PreparedStatementTable.Type.String.name()));
    properties.preparedStatementTable.values.setValue(Arrays.<Object>asList(4, "momo"));
    try (SandboxedInstance sandboxedInstance = RuntimeUtil.createRuntimeClass(definition.getRuntimeInfo(ExecutionEngine.DI, properties, ConnectorTopology.INCOMING_AND_OUTGOING), properties.getClass().getClassLoader())) {
        JDBCRowSink sink = (JDBCRowSink) sandboxedInstance.getInstance();
        sink.initialize(null, properties);
        ValidationResult result = sink.validate(null);
        Assert.assertTrue(result.getStatus() == ValidationResult.Result.OK);
        WriteOperation operation = sink.createWriteOperation();
        JDBCRowWriter writer = (JDBCRowWriter) operation.createWriter(null);
        try {
            writer.open("wid");
            IndexedRecord r1 = new GenericData.Record(properties.main.schema.getValue());
            r1.put(0, 4);
            r1.put(1, "xiaoming");
            writer.write(r1);
            DBTestUtils.assertSuccessRecord(writer, r1);
            IndexedRecord r2 = new GenericData.Record(properties.main.schema.getValue());
            r2.put(0, 5);
            r2.put(1, "xiaobai");
            writer.write(r2);
            DBTestUtils.assertSuccessRecord(writer, r2);
            writer.close();
        } finally {
            writer.close();
        }
    }
    TJDBCInputDefinition definition1 = new TJDBCInputDefinition();
    TJDBCInputProperties properties1 = DBTestUtils.createCommonJDBCInputProperties(allSetting, definition1);
    List<IndexedRecord> records = DBTestUtils.fetchDataByReaderFromTable(tablename, schema, definition1, properties1);
    assertThat(records, hasSize(5));
    Assert.assertEquals(new Integer(4), records.get(3).get(0));
    Assert.assertEquals("momo", records.get(3).get(1));
    Assert.assertEquals(new Integer(4), records.get(4).get(0));
    Assert.assertEquals("momo", records.get(4).get(1));
}
Also used : TJDBCRowDefinition(org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition) IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ValidationResult(org.talend.daikon.properties.ValidationResult) JDBCRowWriter(org.talend.components.jdbc.runtime.writer.JDBCRowWriter) SandboxedInstance(org.talend.daikon.sandbox.SandboxedInstance) WriteOperation(org.talend.components.api.component.runtime.WriteOperation) IndexedRecord(org.apache.avro.generic.IndexedRecord) TJDBCInputProperties(org.talend.components.jdbc.tjdbcinput.TJDBCInputProperties) JDBCRowSink(org.talend.components.jdbc.runtime.JDBCRowSink) TJDBCInputDefinition(org.talend.components.jdbc.tjdbcinput.TJDBCInputDefinition) TJDBCRowProperties(org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties) Test(org.junit.Test)

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