Search in sources :

Example 6 with DataRejectException

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

the class JiraWritersTestIT method testUpdateProject.

/**
 * Checks {@link JiraUpdateWriter#write(Object)} updates project on Jira server
 *
 * @throws IOException
 */
public void testUpdateProject() throws IOException {
    IndexedRecord updateProjectRecord = new GenericData.Record(UPDATE_SCHEMA);
    String updateProject = "{\"name\":\"Updated Integration Test Project\",\"assigneeType\":\"PROJECT_LEAD\"}";
    updateProjectRecord.put(0, "ITP");
    updateProjectRecord.put(1, updateProject);
    JiraWriter updateProjectWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.PROJECT, Action.UPDATE);
    updateProjectWriter.open("updProj");
    try {
        updateProjectWriter.write(updateProjectRecord);
        updateProjectWriter.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 7 with DataRejectException

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

the class JiraDeleteWriterTestIT method testWriteUnauthorized.

/**
 * Checks {@link JiraDeleteWriter#write()} throws {@link IOException} which message contains
 * "User is not authenticated. Record wasn't deleted"
 * in case server responses with 401 Unauthorized status code
 *
 * @throws IOException
 */
@Test
public void testWriteUnauthorized() throws IOException {
    String expectedError = "User is not authenticated. Record wasn't deleted";
    IndexedRecord badJsonRecord = new GenericData.Record(DELETE_SCHEMA);
    badJsonRecord.put(0, "TP");
    thrown.expect(IOException.class);
    thrown.expectMessage("Reason: user is not authenticated. Record wasn't deleted");
    thrown.expectMessage("Record: TP");
    thrown.expectMessage("Error: ");
    JiraWriter deleteProjectWriter = JiraTestsHelper.createWriter(HOST_PORT, WRONG_USER, PASS, Resource.PROJECT, Action.DELETE);
    deleteProjectWriter.open("del");
    try {
        deleteProjectWriter.write(badJsonRecord);
        fail();
    } catch (DataRejectException e) {
        String rejectError = e.getRejectInfo().get("error").toString();
        assertEquals(expectedError, rejectError);
    } finally {
        deleteProjectWriter.close();
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) IndexedRecord(org.apache.avro.generic.IndexedRecord) Test(org.junit.Test)

Example 8 with DataRejectException

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

the class FileDelimitedReaderTestIT method readAndCheck.

// Read the records from the file and check the records
private void readAndCheck(TFileInputDelimitedProperties inputProps, int count, boolean dieOnError) throws Throwable {
    FileDelimitedSource source = new FileDelimitedSource();
    source.initialize(null, inputProps);
    source.validate(null);
    BoundedReader<IndexedRecord> reader = source.createReader(null);
    boolean hasRecord = reader.start();
    while (hasRecord) {
        try {
            IndexedRecord currentRecord = reader.getCurrent();
            assertNotNull(currentRecord.getSchema());
            StringBuffer sb = new StringBuffer();
            int columnSize = currentRecord.getSchema().getFields().size();
            for (int i = 0; i < columnSize; i++) {
                // This convert exception maybe throw exception
                // DataRejectException or the real exception which based on "Die on error" check or not.
                sb.append(currentRecord.get(i));
                if (i != columnSize - 1) {
                    sb.append(" - ");
                }
            }
            LOGGER.debug("Valid row " + " :" + sb.toString());
            sb.delete(0, sb.length());
        } catch (DataRejectException e) {
            if (dieOnError) {
                throw e;
            } else {
                LOGGER.debug("Invalid row " + " :" + e.getRejectInfo().get(TFileInputDelimitedProperties.FIELD_ERROR_MESSAGE));
            }
        }
        hasRecord = reader.advance();
    }
    reader.close();
    Map<String, Object> returnMap = reader.getReturnValues();
    assertNotNull(returnMap);
    // Check the nb line with except value
    assertEquals(count, returnMap.get(ComponentDefinition.RETURN_TOTAL_RECORD_COUNT));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException)

Example 9 with DataRejectException

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

the class FileDelimitedTestBasic method printLogRecords.

// Return the records are not rejected
protected List<IndexedRecord> printLogRecords(List<IndexedRecord> records) {
    List<IndexedRecord> successRecords = new ArrayList<>();
    if (records != null) {
        StringBuffer sb = new StringBuffer();
        for (int index = 0; index < records.size(); index++) {
            try {
                IndexedRecord record = records.get(index);
                assertNotNull(record.getSchema());
                int columnSize = record.getSchema().getFields().size();
                for (int i = 0; i < columnSize; i++) {
                    sb.append(record.get(i));
                    if (i != columnSize - 1) {
                        sb.append(" - ");
                    }
                }
                LOGGER.debug("Row " + (index + 1) + " :" + sb.toString());
                successRecords.add(record);
            } catch (DataRejectException dre) {
                LOGGER.debug("Row " + (index + 1) + " :" + dre.getRejectInfo().get("errorMessage"));
            }
            sb.delete(0, sb.length());
        }
    } else {
        LOGGER.debug("Records list is empty!");
    }
    return successRecords;
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) DataRejectException(org.talend.components.api.exception.DataRejectException) ArrayList(java.util.ArrayList)

Example 10 with DataRejectException

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

the class JiraWritersTestIT method testInsertIssues.

/**
 * Checks {@link JiraInsertWriter#write(Object)} inserts issues on Jira server
 *
 * @throws IOException
 */
public void testInsertIssues() throws IOException {
    IndexedRecord insertIssueRecord1 = new GenericData.Record(INSERT_SCHEMA);
    String insertIssue1 = "{\"fields\":{\"project\":{\"key\":\"ITP\"},\"summary\":\"Integration test issue 1\",\"issuetype\":{\"id\":\"10000\"}}}";
    insertIssueRecord1.put(0, insertIssue1);
    IndexedRecord insertIssueRecord2 = new GenericData.Record(INSERT_SCHEMA);
    String insertIssue2 = "{\"fields\":{\"project\":{\"key\":\"ITP\"},\"summary\":\"Integration test issue 2\",\"issuetype\":{\"id\":\"10000\"}}}";
    insertIssueRecord2.put(0, insertIssue2);
    JiraWriter insertIssueWriter = JiraTestsHelper.createWriter(HOST_PORT, USER, PASS, Resource.ISSUE, Action.INSERT);
    insertIssueWriter.open("insIss");
    try {
        insertIssueWriter.write(insertIssueRecord1);
        insertIssueWriter.write(insertIssueRecord2);
        insertIssueWriter.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)

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