Search in sources :

Example 6 with FixedInputProperties

use of org.talend.components.localio.fixed.FixedInputProperties in project components by Talend.

the class FixedInputRuntimeTest method testCsvSource.

@Test
public void testCsvSource() {
    // The component properties to test.
    FixedInputProperties props = createComponentProperties();
    props.getDatasetProperties().format.setValue(FixedDatasetProperties.RecordFormat.CSV);
    props.getDatasetProperties().csvSchema.setValue("id;name");
    props.getDatasetProperties().values.setValue("1;one\n2;two");
    FixedInputRuntime runtime = new FixedInputRuntime();
    runtime.initialize(null, props);
    PCollection<IndexedRecord> indexRecords = pipeline.apply(runtime);
    try (DirectCollector<IndexedRecord> collector = DirectCollector.of()) {
        indexRecords.apply(collector);
        // Run the pipeline to fill the collectors.
        pipeline.run().waitUntilFinish();
        // Validate the contents of the collected outputs.
        List<IndexedRecord> outputs = collector.getRecords();
        assertThat(outputs, hasSize(2));
        IndexedRecord r1 = outputs.get(0);
        IndexedRecord r2 = outputs.get(1);
        // Output records can be in any order, so swap them if 2 comes before 1
        if (r1.toString().contains("2")) {
            IndexedRecord tmp = r2;
            r2 = r1;
            r1 = tmp;
        }
        // Check the schema and contents.
        assertThat(r1.getSchema().getFields(), hasSize(2));
        assertThat(r1.getSchema().getFields().get(0).name(), is("id"));
        assertThat(r1.getSchema().getFields().get(0).schema().getType(), is(Schema.Type.STRING));
        assertThat(r1.getSchema().getFields().get(1).name(), is("name"));
        assertThat(r1.getSchema().getFields().get(1).schema().getType(), is(Schema.Type.STRING));
        assertThat(r1.get(0).toString(), is("1"));
        assertThat(r1.get(1).toString(), is("one"));
        assertThat(r2.getSchema(), is(r1.getSchema()));
        assertThat(r2.get(0).toString(), is("2"));
        assertThat(r2.get(1).toString(), is("two"));
    }
}
Also used : IndexedRecord(org.apache.avro.generic.IndexedRecord) FixedInputProperties(org.talend.components.localio.fixed.FixedInputProperties) Test(org.junit.Test)

Aggregations

FixedInputProperties (org.talend.components.localio.fixed.FixedInputProperties)6 IndexedRecord (org.apache.avro.generic.IndexedRecord)4 Test (org.junit.Test)4 FixedDatasetProperties (org.talend.components.localio.fixed.FixedDatasetProperties)2 FixedDatastoreProperties (org.talend.components.localio.fixed.FixedDatastoreProperties)2 ArrayList (java.util.ArrayList)1 GenericRecord (org.apache.avro.generic.GenericRecord)1