From f1c6e92df58bf24eb5199cdcb1784cbc438946db Mon Sep 17 00:00:00 2001 From: Otto Fowler Date: Thu, 23 Jan 2020 09:20:17 -0500 Subject: [PATCH] NIFI-7055 handle empty split evaluations, which contain only , add explict test for " , " updated with counting validator Signed-off-by: Matthew Burgess This closes #4012 --- .../processor/util/StandardValidators.java | 5 +++++ .../validator/TestStandardValidators.java | 19 +++++++++++++++++++ 2 files changed, 24 insertions(+) diff --git a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java index 6bf9aaac33..f2ca0a1237 100644 --- a/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java +++ b/nifi-commons/nifi-utils/src/main/java/org/apache/nifi/processor/util/StandardValidators.java @@ -575,7 +575,12 @@ public class StandardValidators { if (input == null) { return new ValidationResult.Builder().subject(subject).input(null).explanation("List must have at least one non-empty element").valid(false).build(); } + final String[] list = input.split(","); + if (list.length == 0) { + return new ValidationResult.Builder().subject(subject).input(null).explanation("List must have at least one non-empty element").valid(false).build(); + } + for (String item : list) { String itemToValidate = trimEntries ? item.trim() : item; if (!isEmpty(itemToValidate) || !excludeEmptyEntries) { diff --git a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java index e469955751..6c6bfe986d 100644 --- a/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java +++ b/nifi-commons/nifi-utils/src/test/java/org/apache/nifi/util/validator/TestStandardValidators.java @@ -184,6 +184,25 @@ public class TestStandardValidators { assertFalse(vr.isValid()); assertEquals(1, mockValidator.getValidateCallCount()); + // An empty list is the same as null, "" or " " + vr = val.validate("List", ",", validationContext); + assertFalse(vr.isValid()); + assertEquals(0, mockValidator.getValidateCallCount()); + + vr = val.validate("List", " , ", validationContext); + assertFalse(vr.isValid()); + assertEquals(1, mockValidator.getValidateCallCount()); + + // will evaluate to no entry + vr = val.validate("List", ",,,,", validationContext); + assertFalse(vr.isValid()); + assertEquals(0, mockValidator.getValidateCallCount()); + + // will evaluate to an empty element + vr = val.validate("List", ",foo", validationContext); + assertFalse(vr.isValid()); + assertEquals(1, mockValidator.getValidateCallCount()); + vr = val.validate("List", "1", validationContext); assertTrue(vr.isValid()); assertEquals(1, mockValidator.getValidateCallCount());