@Test public void testWithHeaderRowOfEmptyColumns() { final Parser<String, Object> parser = parserFactory.get(format, true, 0); parser.startFileFromBeginning(); final String[] body = new String[]{ concat(format, "time", "", "value2", ""), concat(format, "hello", "world", "foo", "bar") }; Assert.assertNull(parser.parseToMap(body[0])); final Map<String, Object> jsonMap = parser.parseToMap(body[1]); Assert.assertEquals( "jsonMap", ImmutableMap.of("time", "hello", "column_2", "world", "value2", "foo", "column_4", "bar"), jsonMap ); }
@Test public void testWithDifferentHeaderRows() { final Parser<String, Object> parser = parserFactory.get(format, true, 0); parser.startFileFromBeginning(); final String[] body = new String[]{ concat(format, "time", "value1", "value2"), concat(format, "hello", "world", "foo") }; Assert.assertNull(parser.parseToMap(body[0])); Map<String, Object> jsonMap = parser.parseToMap(body[1]); Assert.assertEquals( "jsonMap", ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"), jsonMap ); parser.startFileFromBeginning(); final String[] body2 = new String[]{ concat(format, "time", "value1", "value2", "value3"), concat(format, "hello", "world", "foo", "bar") }; Assert.assertNull(parser.parseToMap(body2[0])); jsonMap = parser.parseToMap(body2[1]); Assert.assertEquals( "jsonMap", ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo", "value3", "bar"), jsonMap ); }
@Test public void testWithSkipHeaderRows() { final int skipHeaderRows = 2; final Parser<String, Object> parser = parserFactory.get(format, false, skipHeaderRows); parser.startFileFromBeginning(); final String[] body = new String[]{ concat(format, "header", "line", "1"), concat(format, "header", "line", "2"), concat(format, "hello", "world", "foo") }; int index; for (index = 0; index < skipHeaderRows; index++) { Assert.assertNull(parser.parseToMap(body[index])); } final Map<String, Object> jsonMap = parser.parseToMap(body[index]); Assert.assertEquals( "jsonMap", ImmutableMap.of("column_1", "hello", "column_2", "world", "column_3", "foo"), jsonMap ); }
@Test public void testWithHeaderRow() { final Parser<String, Object> parser = parserFactory.get(format, true, 0); parser.startFileFromBeginning(); final String[] body = new String[]{ concat(format, "time", "value1", "value2"), concat(format, "hello", "world", "foo") }; Assert.assertNull(parser.parseToMap(body[0])); final Map<String, Object> jsonMap = parser.parseToMap(body[1]); Assert.assertEquals( "jsonMap", ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"), jsonMap ); }
@Test public void testWithoutStartFileFromBeginning() { expectedException.expect(UnsupportedOperationException.class); expectedException.expectMessage( "hasHeaderRow or maxSkipHeaderRows is not supported. Please check the indexTask supports these options." ); final int skipHeaderRows = 2; final Parser<String, Object> parser = parserFactory.get(format, false, skipHeaderRows); final String[] body = new String[]{ concat(format, "header", "line", "1"), concat(format, "header", "line", "2"), concat(format, "hello", "world", "foo") }; parser.parseToMap(body[0]); }
public Parser<String, Object> get(FlatTextFormat format) { return get(format, false, 0); }
@Test public void testDuplicatedColumnName() { final String header = concat(format, "time", "value1", "value2", "value2"); expectedException.expect(ParseException.class); expectedException.expectMessage(StringUtils.format("Unable to parse header [%s]", header)); parserFactory.get(format, header); }
@Test public void testWithHeader() { final String header = concat(format, "time", "value1", "value2"); final Parser<String, Object> parser = parserFactory.get(format, header); final String body = concat(format, "hello", "world", "foo"); final Map<String, Object> jsonMap = parser.parseToMap(body); Assert.assertEquals( "jsonMap", ImmutableMap.of("time", "hello", "value1", "world", "value2", "foo"), jsonMap ); }
@Test public void testWithoutHeader() { final Parser<String, Object> parser = parserFactory.get(format); final String body = concat(format, "hello", "world", "foo"); final Map<String, Object> jsonMap = parser.parseToMap(body); Assert.assertEquals( "jsonMap", ImmutableMap.of("column_1", "hello", "column_2", "world", "column_3", "foo"), jsonMap ); }
@Test public void testValidHeader() { final String header = concat(format, "time", "value1", "value2"); final Parser<String, Object> parser = parserFactory.get(format, header); Assert.assertEquals(ImmutableList.of("time", "value1", "value2"), parser.getFieldNames()); }