nifi-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mcgil...@apache.org
Subject [08/50] incubator-nifi git commit: Adding a test case for SplitJson where the specified path cannot be found. Adjusting documentation for additional detail.
Date Fri, 06 Mar 2015 04:21:22 GMT
Adding a test case for SplitJson where the specified path cannot be found.  Adjusting documentation
for additional detail.


Project: http://git-wip-us.apache.org/repos/asf/incubator-nifi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-nifi/commit/7a74dd94
Tree: http://git-wip-us.apache.org/repos/asf/incubator-nifi/tree/7a74dd94
Diff: http://git-wip-us.apache.org/repos/asf/incubator-nifi/diff/7a74dd94

Branch: refs/heads/NIFI-353
Commit: 7a74dd942008bf2178634746e70266f46657d4ef
Parents: 2e05dcb
Author: Aldrin Piri <aldrinpiri@gmail.com>
Authored: Tue Feb 17 15:50:42 2015 -0500
Committer: Aldrin Piri <aldrinpiri@gmail.com>
Committed: Tue Feb 17 15:50:42 2015 -0500

----------------------------------------------------------------------
 .../nifi/processors/standard/SplitJson.java     | 12 +++++++--
 .../processors/standard/util/JsonUtils.java     | 28 +++++++++++++++-----
 .../nifi/processors/standard/TestSplitJson.java | 11 ++++++++
 3 files changed, 43 insertions(+), 8 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/7a74dd94/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
----------------------------------------------------------------------
diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
index e589b48..3ca0ace 100644
--- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
+++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/SplitJson.java
@@ -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;

http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/7a74dd94/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JsonUtils.java
----------------------------------------------------------------------
diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JsonUtils.java
b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JsonUtils.java
index 0bf33dd..5fe2a56 100644
--- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JsonUtils.java
+++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/util/JsonUtils.java
@@ -89,14 +89,23 @@ public class JsonUtils {
         return contextHolder.get();
     }
 
-
+    /*
+     * 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;
 
-        /*
-         * 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.
-         */
         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);
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/7a74dd94/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
----------------------------------------------------------------------
diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
index dd6fc6d..f47467f 100644
--- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
+++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestSplitJson.java
@@ -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);
+    }
 }


Mime
View raw message