You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by jo...@apache.org on 2015/02/18 05:30:13 UTC

[11/29] incubator-nifi git commit: Adding a test for multiple attributes where neither evaluates to a found path.

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


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

Branch: refs/heads/NIFI-360
Commit: d4a94c37eea9c4a545e9f764c113a3d863257e57
Parents: 78ad0a3
Author: Aldrin Piri <al...@gmail.com>
Authored: Mon Feb 16 18:24:57 2015 -0500
Committer: Aldrin Piri <al...@gmail.com>
Committed: Mon Feb 16 18:24:57 2015 -0500

----------------------------------------------------------------------
 .../processors/standard/EvaluateJsonPath.java   | 35 +++++++++++---------
 .../standard/TestEvaluateJsonPath.java          | 22 ++++++++++++
 2 files changed, 41 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/d4a94c37/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java
----------------------------------------------------------------------
diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java
index e747838..13eb50b 100644
--- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java
+++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/EvaluateJsonPath.java
@@ -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);

http://git-wip-us.apache.org/repos/asf/incubator-nifi/blob/d4a94c37/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java
----------------------------------------------------------------------
diff --git a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java
index 60e19d9..6a1fbad 100644
--- a/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java
+++ b/nifi/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestEvaluateJsonPath.java
@@ -118,6 +118,28 @@ public class TestEvaluateJsonPath {
     }
 
     @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";