From 4847926a4b70d4d02660c7f1cedbf57388b4c3f5 Mon Sep 17 00:00:00 2001 From: Pierre Villard Date: Thu, 6 Jan 2022 18:58:57 +0100 Subject: [PATCH] NIFI-9544 - LookupRecord - fixed behavior when no matching value in the LRS Signed-off-by: Joe Gresock This closes #5638. --- .../nifi-standard-processors/pom.xml | 2 ++ .../processors/standard/LookupRecord.java | 13 +++++---- .../processors/standard/TestLookupRecord.java | 6 ++-- .../lookup-array-input-unmatched.json | 29 +++++++++++++++++++ .../lookup-array-output-unmatched.json | 1 + 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json create mode 100644 nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml index 72d760d7cb..84fffaf243 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/pom.xml @@ -624,7 +624,9 @@ src/test/resources/TestValidateRecord/timestamp.avsc src/test/resources/TestValidateRecord/timestamp.json src/test/resources/TestLookupRecord/lookup-array-input.json + src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json src/test/resources/TestLookupRecord/lookup-array-output.json + src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json src/test/resources/TestValidateRecord/int-maps-schema.avsc src/test/resources/TestValidateRecord/int-maps-data.json src/test/resources/TestValidateRecord/array-and-map-with-null-element.avro diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java index 1f873a5703..e25077dc38 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/main/java/org/apache/nifi/processors/standard/LookupRecord.java @@ -309,6 +309,7 @@ public class LookupRecord extends AbstractRouteRecord recordPaths = flowFileContext.getKey(); final Map lookupCoordinates = new HashMap<>(recordPaths.size()); final String coordinateKey = lookupService.getRequiredKeys().iterator().next(); + boolean hasUnmatchedValue = false; for (final Map.Entry entry : recordPaths.entrySet()) { final RecordPath recordPath = entry.getValue(); @@ -338,20 +339,22 @@ public class LookupRecord extends AbstractRouteRecord rels = routeToMatchedUnmatched ? UNMATCHED_COLLECTION : SUCCESS_COLLECTION; - return rels; + hasUnmatchedValue = true; + continue; } final Object lookupValue = lookupValueOption.get(); final DataType inferredDataType = DataTypeUtils.inferDataType(lookupValue, RecordFieldType.STRING.getDataType()); fieldValue.updateValue(lookupValue, inferredDataType); - } } - final Set rels = routeToMatchedUnmatched ? MATCHED_COLLECTION : SUCCESS_COLLECTION; - return rels; + if (hasUnmatchedValue) { + return routeToMatchedUnmatched ? UNMATCHED_COLLECTION : SUCCESS_COLLECTION; + } else { + return routeToMatchedUnmatched ? MATCHED_COLLECTION : SUCCESS_COLLECTION; + } } private Set doResultPathReplacement(Record record, FlowFile flowFile, ProcessContext context, Tuple, RecordPath> flowFileContext) { diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java index 48200ea8a6..251a24d9f5 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestLookupRecord.java @@ -539,14 +539,16 @@ public class TestLookupRecord { runner.setProperty("lookupFoo", "/foo/foo"); lookupService.addValue("FR", "France"); - lookupService.addValue("CA", "Canada"); lookupService.addValue("fr", "French"); lookupService.addValue("badkey", "value"); - runner.enqueue(new File("src/test/resources/TestLookupRecord/lookup-array-input.json").toPath()); + runner.enqueue(new File("src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json").toPath()); runner.run(); runner.assertAllFlowFilesTransferred(LookupRecord.REL_UNMATCHED); + final MockFlowFile out = runner.getFlowFilesForRelationship(LookupRecord.REL_UNMATCHED).get(0); + System.out.println(out.getContent()); + out.assertContentEquals(new File("src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json").toPath()); } private static class MapLookup extends AbstractControllerService implements StringLookupService { diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json new file mode 100644 index 0000000000..3df6f365a6 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-input-unmatched.json @@ -0,0 +1,29 @@ +[ + { + "foo" : { + "foo" : "key" + }, + "locales": [ + { + "language" : "en", + "region" : "CA" + }, { + "language" : "fr", + "region" : "FR" + } + ] + }, { + "foo" : { + "foo" : "key" + }, + "locales": [ + { + "language" : "fr", + "region" : "CA" + }, { + "language" : "fr", + "region" : "FR" + } + ] + } +] diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json new file mode 100644 index 0000000000..a7f8786d72 --- /dev/null +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/resources/TestLookupRecord/lookup-array-output-unmatched.json @@ -0,0 +1 @@ +[{"foo":{"foo":"key"},"locales":[{"language":"en","region":"CA"},{"language":"French","region":"France"}]},{"foo":{"foo":"key"},"locales":[{"language":"French","region":"CA"},{"language":"French","region":"France"}]}] \ No newline at end of file