Adding a test for multiple attributes where neither evaluates to a found path.

This commit is contained in:
Aldrin Piri 2015-02-16 18:24:57 -05:00
parent 78ad0a3147
commit d4a94c37ee
2 changed files with 41 additions and 16 deletions

View File

@ -212,28 +212,31 @@ public class EvaluateJsonPath extends AbstractProcessor {
String jsonPathAttrKey = attributeJsonPathEntry.getKey();
JsonPath jsonPathExp = attributeJsonPathEntry.getValue();
final String evalResult = evaluatePathForContext(jsonPathExp, documentContext);
final ObjectHolder<String> resultHolder = new ObjectHolder<>("");
try {
switch (destination) {
case DESTINATION_ATTRIBUTE:
jsonPathResults.put(jsonPathAttrKey, evalResult);
break;
case DESTINATION_CONTENT:
flowFile = processSession.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
try (OutputStream outputStream = new BufferedOutputStream(out)) {
outputStream.write(evalResult.getBytes(StandardCharsets.UTF_8));
}
}
});
break;
}
resultHolder.set(evaluatePathForContext(jsonPathExp, documentContext));
} catch (PathNotFoundException e) {
logger.error("FlowFile {} could not find path {} for attribute key {}.", new Object[]{flowFile.getId(), jsonPathExp.getPath(), jsonPathAttrKey}, e);
jsonPathResults.put(jsonPathAttrKey, "");
}
switch (destination) {
case DESTINATION_ATTRIBUTE:
jsonPathResults.put(jsonPathAttrKey, resultHolder.get());
break;
case DESTINATION_CONTENT:
flowFile = processSession.write(flowFile, new OutputStreamCallback() {
@Override
public void process(final OutputStream out) throws IOException {
try (OutputStream outputStream = new BufferedOutputStream(out)) {
outputStream.write(resultHolder.get().getBytes(StandardCharsets.UTF_8));
}
}
});
break;
}
}
flowFile = processSession.putAllAttributes(flowFile, jsonPathResults);
processSession.transfer(flowFile, REL_MATCH);

View File

@ -117,6 +117,28 @@ public class TestEvaluateJsonPath {
Assert.assertEquals("Transferred flow file did not have the correct result for name attribute", "{\"first\":\"Shaffer\",\"last\":\"Pearson\"}", out.getAttribute(jsonPathNameAttrKey));
}
@Test
public void testExtractPath_destinationAttributes_twoPaths_notFound() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new EvaluateJsonPath());
testRunner.setProperty(EvaluateJsonPath.DESTINATION, EvaluateJsonPath.DESTINATION_ATTRIBUTE);
String jsonPathIdAttrKey = "evaluatejson.id";
String jsonPathNameAttrKey = "evaluatejson.name";
testRunner.setProperty(jsonPathIdAttrKey, "$[0]._id.nonexistent");
testRunner.setProperty(jsonPathNameAttrKey, "$[0].name.nonexistent");
testRunner.enqueue(JSON_SNIPPET);
testRunner.run();
Relationship expectedRel = EvaluateJsonPath.REL_MATCH;
testRunner.assertAllFlowFilesTransferred(expectedRel, 1);
final MockFlowFile out = testRunner.getFlowFilesForRelationship(expectedRel).get(0);
Assert.assertEquals("Transferred flow file did not have the correct result for id attribute", "", out.getAttribute(jsonPathIdAttrKey));
Assert.assertEquals("Transferred flow file did not have the correct result for name attribute", "", out.getAttribute(jsonPathNameAttrKey));
}
@Test
public void testExtractPath_destinationContent() throws Exception {
String jsonPathAttrKey = "JsonPath";