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));
}
Aggregations