Skip to content

Commit

Permalink
Merge pull request #387 from metafacture/382-recordPath
Browse files Browse the repository at this point in the history
Tweak `recordPath` code, add test cases
  • Loading branch information
fsteeg authored Sep 3, 2021
2 parents 5cbd698 + 5ffe4b5 commit aed6de7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* Decodes a record in JSON format.
Expand Down Expand Up @@ -132,21 +132,27 @@ public void resetRecordCount() {
@Override
public void process(final String json) {
assert !isClosed();
final List<String> records = recordPath.isEmpty() ? Arrays.asList(json)
: matches(JsonPath.read(json, recordPath));
records.forEach(record -> {
createParser(record);
try {
decode();
} catch (final IOException e) {
throw new MetafactureException(e);
} finally {
closeParser();
}
});
if (recordPath.isEmpty()) {
processRecord(json);
} else {
matches(JsonPath.read(json, recordPath)).forEach(record -> {
processRecord(record);
});
}
}

private void processRecord(String record) {
createParser(record);
try {
decode();
} catch (final IOException e) {
throw new MetafactureException(e);
} finally {
closeParser();
}
}

private List<String> matches(Object obj) {
private Stream<String> matches(Object obj) {
final List<?> records = (obj instanceof List<?>) ? ((List<?>) obj) : Arrays.asList(obj);
return records.stream().map(doc -> {
try {
Expand All @@ -155,7 +161,7 @@ private List<String> matches(Object obj) {
e.printStackTrace();
return doc.toString();
}
}).collect(Collectors.toList());
});
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ public void testShouldProcessConcatenatedRecords() {
}

@Test
public void testShouldProcessRecordsInArray() {
public void testShouldProcessRecordsInArrayField() {
jsonDecoder.setRecordPath("$.data");
jsonDecoder.process(
"{\"data\":[" + "{\"lit\": \"record 1\"}," +
Expand All @@ -167,6 +167,29 @@ public void testShouldProcessRecordsInArray() {
ordered.verify(receiver).endRecord();
}

@Test
public void testShouldProcessRecordsInArrayRoot() {
jsonDecoder.setRecordPath("$");
jsonDecoder.process(
"[" + "{\"lit\": \"record 1\"}," +
"{\"lit\": \"record 2\"}" + "]");

final InOrder ordered = inOrder(receiver);
ordered.verify(receiver).startRecord("1");
ordered.verify(receiver).literal("lit", "record 1");
ordered.verify(receiver).endRecord();
ordered.verify(receiver).startRecord("2");
ordered.verify(receiver).literal("lit", "record 2");
ordered.verify(receiver).endRecord();
}

@Test(expected=MetafactureException.class)
public void testRootArrayNoRecordPath() {
jsonDecoder.process(
"[" + "{\"lit\": \"record 1\"}," +
"{\"lit\": \"record 2\"}" + "]");
}

@Test
public void testShouldProcessMultipleRecords() {
jsonDecoder.process("{\"lit\": \"record 1\"}");
Expand Down

0 comments on commit aed6de7

Please sign in to comment.