Skip to content

Commit

Permalink
Add unit test for parseOptions of SINGLELINE data processor
Browse files Browse the repository at this point in the history
  • Loading branch information
chaochenq committed Mar 5, 2018
1 parent a2bae50 commit 30b10bb
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 9 deletions.
2 changes: 1 addition & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@

<target name="release" depends="clean, compile, build" />

</project>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -43,29 +43,29 @@ public class SingleLineDataConverter implements IDataConverter {

private final boolean noTrim;
private final boolean escapeNewLine;

public SingleLineDataConverter(Configuration config) {
List<String> parseOptions = config.readList("parseOptions", String.class, new ArrayList<String>());
noTrim = parseOptions.contains("NO_TRIM") ? true : false;
escapeNewLine = parseOptions.contains("ESCAPE_NEW_LINE") ? true : false;
}

@Override
public ByteBuffer convert(ByteBuffer data) throws DataConversionException {
String dataStr = ByteBuffers.toString(data, StandardCharsets.UTF_8);
String[] lines = dataStr.split(NEW_LINE);

if (!noTrim) {
for (int i = 0; i < lines.length; i++) {
lines[i] = lines[i].trim();
}
}

String delimiter = escapeNewLine ? "\\\\n" : "";
String dataRes = StringUtils.join(lines, delimiter) + NEW_LINE;
return ByteBuffer.wrap(dataRes.getBytes(StandardCharsets.UTF_8));
}

@Override
public String toString() {
return getClass().getSimpleName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ public void testBracketsDataConverter() throws Exception {

@Test
public void testSingleLineDataConverter() throws Exception {
final IDataConverter converter = new SingleLineDataConverter();
final Configuration config = new Configuration(new HashMap<String, Object>() {{
put("optionName", "SINGLELINE");
put("parseOptions", Arrays.asList("ESCAPE_NEW_LINE"));
}});
final IDataConverter converter = new SingleLineDataConverter(config);
final String dataStr = "{\n" +
" \"a\": {\n" +
" \"b\": 1,\n" +
Expand All @@ -38,7 +42,7 @@ public void testSingleLineDataConverter() throws Exception {
" \"g\": 2\n" +
" " +
"}\n";
final String expectedStr = "{\"a\": {\"b\": 1,\"c\": null,\"d\": true},\"e\": \"f\",\"g\": 2}\n";
final String expectedStr = "{\\\\n\"a\": {\\\\n\"b\": 1,\\\\n\"c\": null,\\\\n\"d\": true\\\\n},\\\\n\"e\": \"f\",\\\\n\"g\": 2\\\\n}\n";
verifyDataConversion(converter, dataStr.getBytes(), expectedStr.getBytes());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -532,11 +532,14 @@ public void testConvertingMultiLineDataFromRealLog() throws IOException {
final String testfileName = "pretty_printed_json";
final String expectedResultFileName = "parsed_singleline_json";
final String pattern = " \\{";
final Configuration config = new Configuration(new HashMap<String, Object>() {{
put("optionName", "SINGLELINE");
}});
flow = spy(flow);
// Skip the first two lines
when(flow.getSkipHeaderLines()).thenReturn(2);
when(flow.getRecordSplitter()).thenReturn(new RegexSplitter(pattern));
when(flow.getDataConverter()).thenReturn(new AgentDataConverterChain(new SingleLineDataConverter()));
when(flow.getDataConverter()).thenReturn(new AgentDataConverterChain(new SingleLineDataConverter(config)));
Path testFile = FileSystems.getDefault().getPath(this.getClass().getResource(testfileName).getFile());
Path resultFile = FileSystems.getDefault().getPath(this.getClass().getResource(expectedResultFileName).getFile());
P parser = buildParser();
Expand Down

0 comments on commit 30b10bb

Please sign in to comment.