Search in sources :

Example 1 with WriterWithFeedback

use of org.talend.components.api.component.runtime.WriterWithFeedback in project components by Talend.

the class TFilterRowTest method testTFilterRowRuntime.

@Test
public void testTFilterRowRuntime() throws Exception {
    TFilterRowProperties props = (TFilterRowProperties) getComponentService().getComponentProperties("tFilterRow_POC");
    props.schemaListener.afterSchema();
    assertThat(props.conditionsTable.isEditable(), is(false));
    Schema schema = SchemaBuilder.builder().record("testRecord").fields().name("field1").type().stringType().noDefault().endRecord();
    props.schemaMain.schema.setValue(schema);
    final List<Schema.Field> additionalRejectFields = new ArrayList<Schema.Field>();
    Schema.Field field = null;
    field = new Schema.Field(TFilterRowProperties.FIELD_ERROR_MESSAGE, Schema.create(Schema.Type.STRING), null, (Object) null);
    field.addProp(SchemaConstants.TALEND_IS_LOCKED, "false");
    field.addProp(SchemaConstants.TALEND_FIELD_GENERATED, "true");
    field.addProp(SchemaConstants.TALEND_COLUMN_DB_LENGTH, "255");
    additionalRejectFields.add(field);
    Schema rejectSchema = newSchema(schema, "rejectOutput", additionalRejectFields);
    props.schemaListener.afterSchema();
    assertThat(props.schemaReject.schema.getValue(), is((Schema) rejectSchema));
    assertThat(props.schemaFlow.schema.getValue(), is((Schema) schema));
    assertThat(props.conditionsTable.isEditable(), is(true));
    props.conditionsTable.columnName.setValue(Arrays.asList("field1"));
    props.conditionsTable.function.setValue(Arrays.asList(FunctionType.EMPTY));
    props.conditionsTable.operator.setValue(Arrays.asList(OperatorType.EQUALS));
    props.conditionsTable.value.setValue(Arrays.asList((Object) "test"));
    props.logicalOperator.setValue(LogicalOperator.Or);
    Sink sink = new TFilterRowSink();
    sink.initialize(null, props);
    WriteOperation<?> writeOperation = sink.createWriteOperation();
    writeOperation.initialize(null);
    WriterWithFeedback<?, ?, ?> writer = (WriterWithFeedback<?, ?, ?>) writeOperation.createWriter(null);
    writer.open("uId");
    IndexedRecord record = new GenericData.Record(schema);
    record.put(schema.getField("field1").pos(), "test");
    writer.write(record);
    Iterable<?> success = writer.getSuccessfulWrites();
    Iterator<?> successIterator = success.iterator();
    assertThat(successIterator.hasNext(), is(true));
    IndexedRecord successWrite = (IndexedRecord) successIterator.next();
    assertThat(successWrite.getSchema(), is(schema));
    assertThat(successWrite, is(record));
    writer.cleanWrites();
    record = new GenericData.Record(schema);
    record.put(schema.getField("field1").pos(), "test123");
    writer.write(record);
    success = writer.getSuccessfulWrites();
    successIterator = success.iterator();
    assertThat(successIterator.hasNext(), is(false));
    Iterable<?> reject = writer.getRejectedWrites();
    Iterator<?> rejectIterator = reject.iterator();
    assertThat(rejectIterator.hasNext(), is(true));
    IndexedRecord rejectWrite = (IndexedRecord) rejectIterator.next();
    assertThat(rejectWrite.getSchema(), is(rejectSchema));
    Result result = (Result) writer.close();
    assertThat(result.totalCount, is(2));
    assertThat(result.successCount, is(1));
    assertThat(result.rejectCount, is(1));
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) Schema(org.apache.avro.Schema) ArrayList(java.util.ArrayList) GenericData(org.apache.avro.generic.GenericData) Result(org.talend.components.api.component.runtime.Result) TFilterRowSink(org.talend.components.filterrow.runtime.TFilterRowSink) Sink(org.talend.components.api.component.runtime.Sink) IndexedRecord(org.apache.avro.generic.IndexedRecord) TFilterRowSink(org.talend.components.filterrow.runtime.TFilterRowSink) WriterWithFeedback(org.talend.components.api.component.runtime.WriterWithFeedback) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 Schema (org.apache.avro.Schema)1 GenericData (org.apache.avro.generic.GenericData)1 IndexedRecord (org.apache.avro.generic.IndexedRecord)1 Test (org.junit.Test)1 Result (org.talend.components.api.component.runtime.Result)1 Sink (org.talend.components.api.component.runtime.Sink)1 WriterWithFeedback (org.talend.components.api.component.runtime.WriterWithFeedback)1 TFilterRowSink (org.talend.components.filterrow.runtime.TFilterRowSink)1