use of org.apache.apex.malhar.contrib.parser.AbstractCsvParser.Field in project apex-malhar by apache.
the class CSVParserTest method TestParserWithHeader.
@Test
public void TestParserWithHeader() {
CsvToMapParser parser = new CsvToMapParser();
parser.setFieldDelimiter(',');
parser.setLineDelimiter("\n");
parser.setHasHeader(true);
ArrayList<CsvToMapParser.Field> fields = new ArrayList<CsvToMapParser.Field>();
Field field1 = new Field();
field1.setName("Eid");
field1.setType("INTEGER");
fields.add(field1);
Field field2 = new Field();
field2.setName("Name");
field2.setType("STRING");
fields.add(field2);
Field field3 = new Field();
field3.setName("Salary");
field3.setType("LONG");
fields.add(field3);
parser.setFields(fields);
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
parser.output.setSink(sink);
parser.setup(null);
String input = "Eid,Name,Salary\n123,xyz,567777\n321,abc,7777000\n456,pqr,5454545454";
parser.input.process(input.getBytes());
parser.teardown();
Assert.assertEquals("Tuples read should be same ", 6, sink.collectedTuples.size());
Assert.assertEquals("Eid", sink.collectedTuples.get(0));
Assert.assertEquals("Name", sink.collectedTuples.get(1));
Assert.assertEquals("Salary", sink.collectedTuples.get(2));
Assert.assertEquals("{Name=xyz, Salary=567777, Eid=123}", sink.collectedTuples.get(3).toString());
Assert.assertEquals("{Name=abc, Salary=7777000, Eid=321}", sink.collectedTuples.get(4).toString());
Assert.assertEquals("{Name=pqr, Salary=5454545454, Eid=456}", sink.collectedTuples.get(5).toString());
sink.clear();
}
use of org.apache.apex.malhar.contrib.parser.AbstractCsvParser.Field in project apex-malhar by apache.
the class CSVParserTest method TestParserWithoutHeader.
@Test
public void TestParserWithoutHeader() {
CsvToMapParser parser = new CsvToMapParser();
CollectorTestSink<Object> sink = new CollectorTestSink<Object>();
parser.output.setSink(sink);
parser.setFieldDelimiter(',');
parser.setLineDelimiter("\n");
ArrayList<CsvToMapParser.Field> fields = new ArrayList<CsvToMapParser.Field>();
Field field1 = new Field();
field1.setName("Eid");
field1.setType("INTEGER");
fields.add(field1);
Field field2 = new Field();
field2.setName("Name");
field2.setType("STRING");
fields.add(field2);
Field field3 = new Field();
field3.setName("Salary");
field3.setType("LONG");
fields.add(field3);
parser.setFields(fields);
parser.setHasHeader(false);
parser.setup(null);
String input = "123,xyz,567777\n321,abc,7777000\n456,pqr,5454545454";
parser.input.process(input.getBytes());
parser.teardown();
Assert.assertEquals("Tuples read should be same ", 3, sink.collectedTuples.size());
Assert.assertEquals("{Name=xyz, Salary=567777, Eid=123}", sink.collectedTuples.get(0).toString());
Assert.assertEquals("{Name=abc, Salary=7777000, Eid=321}", sink.collectedTuples.get(1).toString());
Assert.assertEquals("{Name=pqr, Salary=5454545454, Eid=456}", sink.collectedTuples.get(2).toString());
sink.clear();
}
Aggregations