Adding a test case for SplitJson where the specified path cannot be found. Adjusting documentation for additional detail.

This commit is contained in:
Aldrin Piri 2015-02-17 15:50:42 -05:00
parent 2e05dcbbfd
commit 7a74dd9420
3 changed files with 42 additions and 7 deletions

View File

@ -18,6 +18,7 @@ package org.apache.nifi.processors.standard;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.PathNotFoundException;
import org.apache.nifi.annotation.behavior.EventDriven;
import org.apache.nifi.annotation.behavior.SideEffectFree;
import org.apache.nifi.annotation.behavior.SupportsBatching;
@ -105,10 +106,17 @@ public class SplitJson extends AbstractProcessor {
final List<FlowFile> segments = new ArrayList<>();
Object jsonPathResult = documentContext.read(jsonPath);
Object jsonPathResult;
try {
jsonPathResult = documentContext.read(jsonPath);
} catch (PathNotFoundException e) {
logger.warn("JsonPath {} could not be found for FlowFile {}", new Object[]{jsonPath.getPath(), original});
processSession.transfer(original, REL_FAILURE);
return;
}
if (!(jsonPathResult instanceof List)) {
logger.error("The evaluated value {} of {} was not an array compatible type and cannot be split.",
logger.error("The evaluated value {} of {} was not a JSON Array compatible type and cannot be split.",
new Object[]{jsonPathResult, jsonPath.getPath()});
processSession.transfer(original, REL_FAILURE);
return;

View File

@ -89,14 +89,23 @@ public class JsonUtils {
return contextHolder.get();
}
public static boolean isValidJson(InputStream inputStream) throws IOException {
boolean isValid = false;
/*
* JSONValue#isValidJson is permissive to the degree of the Smart JSON definition.
* Accordingly, a strict JSON approach is preferred in determining whether or not a document is valid.
*/
/**
* JSONValue#isValidJson is permissive to the degree of the Smart JSON definition, accordingly a strict JSON approach
* is preferred in determining whether or not a document is valid.
* Performs a validation of the provided stream according to RFC 4627 as implemented by {@link net.minidev.json.parser.JSONParser#MODE_RFC4627}
*
* @param inputStream of content to be validated as JSON
* @return true, if the content is valid within the bounds of the strictness specified; false otherwise
* @throws IOException
*/
public static boolean isValidJson(InputStream inputStream) throws IOException {
boolean isValid = false;
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream)) {
isValid = JSONValue.isValidJsonStrict(inputStreamReader);
}
@ -104,8 +113,15 @@ public class JsonUtils {
return isValid;
}
/**
* Determines the context by which JsonSmartJsonProvider would treat the value. {@link java.util.Map} and
* {@link java.util.List} objects can be rendered as JSON elements, everything else is treated as a scalar.
*
* @param obj item to be inspected if it is a scalar or a JSON element
* @return false, if the object is a supported type; true otherwise
*/
public static boolean isJsonScalar(Object obj) {
// For the default provider, a Map or List is able to be handled as a JSON entity
// For the default provider, JsonSmartJsonProvider, a Map or List is able to be handled as a JSON entity
return !(obj instanceof Map || obj instanceof List);
}

View File

@ -112,4 +112,15 @@ public class TestSplitJson {
testRunner.getFlowFilesForRelationship(SplitJson.REL_SPLIT).get(0).assertContentEquals("{\"first\":\"Shaffer\",\"last\":\"Pearson\"}");
}
@Test
public void testSplit_pathNotFound() throws Exception {
final TestRunner testRunner = TestRunners.newTestRunner(new SplitJson());
testRunner.setProperty(SplitJson.ARRAY_JSON_PATH_EXPRESSION, "$.nonexistent");
testRunner.enqueue(JSON_SNIPPET);
testRunner.run();
testRunner.assertTransferCount(SplitJson.REL_FAILURE, 1);
testRunner.getFlowFilesForRelationship(SplitJson.REL_FAILURE).get(0).assertContentEquals(JSON_SNIPPET);
}
}