Search in sources :

Example 1 with DataRejectException

use of org.talend.components.api.exception.DataRejectException 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());
    JDBCRowSource source = new JDBCRowSource();
    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 : 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 2 with DataRejectException

use of org.talend.components.api.exception.DataRejectException in project components by Talend.

the class JDBCRowReader method handleReject.

private void handleReject(SQLException e) {
    IndexedRecord reject = new GenericData.Record(rejectSchema);
    for (Schema.Field outField : reject.getSchema().getFields()) {
        Object outValue = null;
        if ("errorCode".equals(outField.name())) {
            outValue = e.getSQLState();
        } else if ("errorMessage".equals(outField.name())) {
            outValue = e.getMessage();
        }
        reject.put(outField.pos(), outValue);
    }
    Map<String, Object> resultMessage = new HashMap<String, Object>();
    resultMessage.put("error", e.getMessage());
    resultMessage.put("errorCode", e.getSQLState());
    resultMessage.put("errorMessage", e.getMessage() + " - Line: " + result.totalCount);
    resultMessage.put("talend_record", reject);
    throw new DataRejectException(resultMessage);
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) Schema(org.apache.avro.Schema) IndexedRecord(org.apache.avro.generic.IndexedRecord)

Example 3 with DataRejectException

use of org.talend.components.api.exception.DataRejectException 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 4 with DataRejectException

use of org.talend.components.api.exception.DataRejectException in project components by Talend.

the class JiraWritersTestIT method testUpdateIssues.

/**
 * Checks {@link JiraUpdateWriter#write(Object)} updates issues on Jira server
 *
 * @throws IOException
 */
public void testUpdateIssues() throws IOException {
    IndexedRecord updateIssueRecord1 = new GenericData.Record(UPDATE_SCHEMA);
    String updateIssue1 = "{\"fields\":{\"summary\":\"Updated test issue 1\"}}";
    updateIssueRecord1.put(0, "ITP-1");
    updateIssueRecord1.put(1, updateIssue1);
    IndexedRecord updateIssueRecord2 = new GenericData.Record(UPDATE_SCHEMA);
    String updateIssue2 = "{\"fields\":{\"summary\":\"Updated test issue 2\"}}";
    updateIssueRecord2.put(0, "ITP-2");
    updateIssueRecord2.put(1, updateIssue2);
    JiraWriter updateIssueWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.ISSUE, Action.UPDATE);
    updateIssueWriter.open("updIss");
    try {
        updateIssueWriter.write(updateIssueRecord1);
        updateIssueWriter.write(updateIssueRecord2);
        updateIssueWriter.close();
    } catch (DataRejectException e) {
        String rejectError = e.getRejectInfo().get("error").toString();
        LOG.error(rejectError);
        collector.addError(new Throwable(rejectError));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) IndexedRecord(org.apache.avro.generic.IndexedRecord)

Example 5 with DataRejectException

use of org.talend.components.api.exception.DataRejectException in project components by Talend.

the class JiraWritersTestIT method testDeleteIssues.

/**
 * Checks {@link JiraDeleteWriter#write(Object)} deletes issues from Jira server
 *
 * @throws IOException
 */
public void testDeleteIssues() throws IOException {
    IndexedRecord deleteIssueRecord1 = new GenericData.Record(DELETE_SCHEMA);
    deleteIssueRecord1.put(0, "ITP-1");
    IndexedRecord deleteIssueRecord2 = new GenericData.Record(DELETE_SCHEMA);
    deleteIssueRecord2.put(0, "ITP-2");
    JiraWriter deleteIssueWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.ISSUE, Action.DELETE);
    deleteIssueWriter.open("delIss");
    try {
        deleteIssueWriter.write(deleteIssueRecord1);
        deleteIssueWriter.write(deleteIssueRecord2);
    } catch (DataRejectException e) {
        String rejectError = e.getRejectInfo().get("error").toString();
        LOG.error(rejectError);
        collector.addError(new Throwable(rejectError));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) IndexedRecord(org.apache.avro.generic.IndexedRecord)

Aggregations

IndexedRecord (org.apache.avro.generic.IndexedRecord)15 DataRejectException (org.talend.components.api.exception.DataRejectException)15 Test (org.junit.Test)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 Reader (org.talend.components.api.component.runtime.Reader)4 ArrayList (java.util.ArrayList)3 Schema (org.apache.avro.Schema)3 JDBCRowSource (org.talend.components.jdbc.runtime.JDBCRowSource)2 TJDBCRowDefinition (org.talend.components.jdbc.tjdbcrow.TJDBCRowDefinition)2 TJDBCRowProperties (org.talend.components.jdbc.tjdbcrow.TJDBCRowProperties)2 TSalesforceBulkExecDefinition (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition)2 TSalesforceBulkExecProperties (org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties)2 TSalesforceOutputBulkDefinition (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition)2 TSalesforceOutputBulkProperties (org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties)2 ValidationResult (org.talend.daikon.properties.ValidationResult)2 IOException (java.io.IOException)1 ComponentException (org.talend.components.api.exception.ComponentException)1 SandboxedInstance (org.talend.daikon.sandbox.SandboxedInstance)1