Search in sources :

Example 11 with DataRejectException

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

the class JiraWritersTestIT method testInsertProject.

/**
 * Checks {@link JiraInsertWriter#write(Object)} creates project on Jira server
 *
 * @throws IOException
 */
public void testInsertProject() throws IOException {
    IndexedRecord insertProjectRecord = new GenericData.Record(INSERT_SCHEMA);
    String insertProject = "{\"key\":\"ITP\",\"name\":\"Integration Test Project\",\"projectTemplateKey\":" + "\"com.atlassian.jira-core-project-templates:jira-core-project-management\",\"lead\":\"UserID\"}";
    insertProject = insertProject.replace("UserID", USER);
    insertProjectRecord.put(0, insertProject);
    JiraWriter insertProjectWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.PROJECT, Action.INSERT);
    insertProjectWriter.open("insProj");
    try {
        insertProjectWriter.write(insertProjectRecord);
        insertProjectWriter.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 12 with DataRejectException

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

the class JiraWritersTestIT method testDeleteProject.

/**
 * Checks {@link JiraDeleteWriter#write(Object)} deletes project from Jira server
 *
 * @throws IOException
 */
public void testDeleteProject() throws IOException {
    IndexedRecord deleteProjectRecord = new GenericData.Record(DELETE_SCHEMA);
    deleteProjectRecord.put(0, "ITP");
    JiraWriter deleteProjectWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.PROJECT, Action.DELETE);
    deleteProjectWriter.open("delProj");
    try {
        deleteProjectWriter.write(deleteProjectRecord);
    } 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 13 with DataRejectException

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

the class SalesforceBulkLoadTestIT method testUpdate.

@Test
public void testUpdate() throws Throwable {
    List<String> ids = util.createTestData();
    String id = ids.get(0);
    final List<Map<String, String>> testData = new ArrayList<Map<String, String>>();
    Map<String, String> datarow = new HashMap<String, String>();
    datarow.put("Id", id);
    datarow.put("FirstName", "Wei");
    datarow.put("LastName", "Wang");
    // update the field
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    datarow = new HashMap<String, String>();
    // should reject
    datarow.put("Id", "not_exist");
    datarow.put("FirstName", "Who");
    datarow.put("LastName", "Who");
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    String data_file = tempFolder.newFile("data.txt").getAbsolutePath();
    // outputbulk part
    TSalesforceOutputBulkDefinition definition = (TSalesforceOutputBulkDefinition) getComponentService().getComponentDefinition(TSalesforceOutputBulkDefinition.COMPONENT_NAME);
    TSalesforceOutputBulkProperties modelProperties = util.simulateUserBasicAction(definition, data_file, util.getTestSchema4());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema4(), testData);
    // bulkexec part
    TSalesforceBulkExecDefinition defin = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    TSalesforceBulkExecProperties modelProps = (TSalesforceBulkExecProperties) defin.createRuntimeProperties();
    Reader reader = util.initReader(defin, data_file, modelProps, util.getTestSchema4(), util.getTestSchema4());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.UPDATE);
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String phone = (String) record.get(3);
                Assert.assertEquals(id, resultid);
                Assert.assertEquals("010-89492686", phone);
            } catch (DataRejectException e) {
                java.util.Map<String, Object> info = e.getRejectInfo();
                Object data = info.get("talend_record");
                String err = (String) info.get("error");
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String firstname = (String) record.get(1);
                String lastname = (String) record.get(2);
                String phone = (String) record.get(3);
                Assert.assertNull(resultid);
                Assert.assertEquals("Who", firstname);
                Assert.assertEquals("Who", lastname);
                Assert.assertEquals("010-89492686", phone);
                Assert.assertTrue(err != null);
            }
        }
    } finally {
        try {
            reader.close();
        } finally {
            util.deleteTestData(ids);
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ArrayList(java.util.ArrayList) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) Reader(org.talend.components.api.component.runtime.Reader) TSalesforceBulkExecProperties(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties) TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) TSalesforceOutputBulkDefinition(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 14 with DataRejectException

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

the class SalesforceBulkLoadTestIT method testUpsert.

@Test
public void testUpsert() throws Throwable {
    List<String> ids = util.createTestData();
    String id = ids.get(0);
    final List<Map<String, String>> testData = new ArrayList<Map<String, String>>();
    Map<String, String> datarow = new HashMap<String, String>();
    // should update
    datarow.put("Id", id);
    datarow.put("FirstName", "Wei");
    datarow.put("LastName", "Wang");
    // update the field
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    datarow = new HashMap<String, String>();
    // should insert
    datarow.put("Id", null);
    datarow.put("FirstName", "Who");
    datarow.put("LastName", "Who");
    datarow.put("Phone", "010-89492686");
    testData.add(datarow);
    String data_file = tempFolder.newFile("data.txt").getAbsolutePath();
    // outputbulk part
    TSalesforceOutputBulkDefinition definition = (TSalesforceOutputBulkDefinition) getComponentService().getComponentDefinition(TSalesforceOutputBulkDefinition.COMPONENT_NAME);
    TSalesforceOutputBulkProperties modelProperties = util.simulateUserBasicAction(definition, data_file, util.getTestSchema4());
    util.simulateRuntimeCaller(definition, modelProperties, util.getTestSchema4(), testData);
    // bulkexec part
    TSalesforceBulkExecDefinition defin = (TSalesforceBulkExecDefinition) getComponentService().getComponentDefinition(TSalesforceBulkExecDefinition.COMPONENT_NAME);
    TSalesforceBulkExecProperties modelProps = (TSalesforceBulkExecProperties) defin.createRuntimeProperties();
    Reader reader = util.initReader(defin, data_file, modelProps, util.getTestSchema4(), util.getTestSchema4());
    modelProps.outputAction.setValue(TSalesforceBulkExecProperties.OutputAction.UPSERT);
    modelProps.upsertKeyColumn.setValue("Id");
    try {
        IndexedRecordConverter<Object, ? extends IndexedRecord> factory = null;
        int index = -1;
        for (boolean available = reader.start(); available; available = reader.advance()) {
            try {
                Object data = reader.getCurrent();
                factory = initCurrentData(factory, data);
                IndexedRecord record = factory.convertToAvro(data);
                String resultid = (String) record.get(0);
                String phone = (String) record.get(3);
                index++;
                if (index == 0) {
                    Assert.assertEquals(id, resultid);
                    Assert.assertEquals("010-89492686", phone);
                } else if (index == 1) {
                    Assert.assertTrue(resultid != null);
                    Assert.assertEquals("010-89492686", phone);
                }
            } catch (DataRejectException e) {
                Assert.fail(e.getMessage());
            }
        }
    } finally {
        try {
            reader.close();
        } finally {
            util.deleteTestData(ids);
        }
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ArrayList(java.util.ArrayList) TSalesforceBulkExecDefinition(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecDefinition) Reader(org.talend.components.api.component.runtime.Reader) TSalesforceBulkExecProperties(org.talend.components.salesforce.tsalesforcebulkexec.TSalesforceBulkExecProperties) TSalesforceOutputBulkProperties(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkProperties) TSalesforceOutputBulkDefinition(org.talend.components.salesforce.tsalesforceoutputbulk.TSalesforceOutputBulkDefinition) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with DataRejectException

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

the class SalesforceBulkExecReader method getCurrent.

@Override
public IndexedRecord getCurrent() {
    BulkResult result = currentBatchResult.get(resultIndex);
    IndexedRecord record = null;
    try {
        record = ((BulkResultAdapterFactory) getFactory()).convertToAvro(result);
    } catch (IOException e) {
        throw new ComponentException(e);
    }
    if ("true".equalsIgnoreCase((String) result.getValue("Success"))) {
        return record;
    } else {
        Map<String, Object> resultMessage = new HashMap<String, Object>();
        String error = (String) result.getValue("Error");
        resultMessage.put("error", error);
        resultMessage.put("talend_record", record);
        throw new DataRejectException(resultMessage);
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) HashMap(java.util.HashMap) DataRejectException(org.talend.components.api.exception.DataRejectException) ComponentException(org.talend.components.api.exception.ComponentException) IOException(java.io.IOException)

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