From 0fed158d14d7c73d91d3516d2af1ea1acf902463 Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Tue, 19 Apr 2016 14:55:41 -0400 Subject: [PATCH] NIFI-1785 added NON_BLANK_VALIDATOR NIFI-1785 addressed PR comment NIFI-1785 fixed white spaces This closes #365. Signed-off-by: Bryan Bende --- .../nifi/processor/util/StandardValidators.java | 17 +++++++++++++++++ .../processor/util/TestStandardValidators.java | 15 +++++++++++++++ 2 files changed, 32 insertions(+) diff --git a/nifi-commons/nifi-processor-utilities/src/main/java/org/apache/nifi/processor/util/StandardValidators.java b/nifi-commons/nifi-processor-utilities/src/main/java/org/apache/nifi/processor/util/StandardValidators.java index 8255781d3a..2419c897e4 100644 --- a/nifi-commons/nifi-processor-utilities/src/main/java/org/apache/nifi/processor/util/StandardValidators.java +++ b/nifi-commons/nifi-processor-utilities/src/main/java/org/apache/nifi/processor/util/StandardValidators.java @@ -127,6 +127,9 @@ public class StandardValidators { public static final Validator PORT_VALIDATOR = createLongValidator(1, 65535, true); + /** + * {@link Validator} that ensures that value's length > 0 + */ public static final Validator NON_EMPTY_VALIDATOR = new Validator() { @Override public ValidationResult validate(final String subject, final String value, final ValidationContext context) { @@ -134,6 +137,20 @@ public class StandardValidators { } }; + /** + * {@link Validator} that ensures that value has 1+ non-whitespace + * characters + */ + public static final Validator NON_BLANK_VALIDATOR = new Validator() { + @Override + public ValidationResult validate(final String subject, final String value, final ValidationContext context) { + return new ValidationResult.Builder().subject(subject).input(value) + .valid(value != null && !value.trim().isEmpty()) + .explanation(subject + + " must contain at least one character that is not white space").build(); + } + }; + public static final Validator BOOLEAN_VALIDATOR = new Validator() { @Override public ValidationResult validate(final String subject, final String value, final ValidationContext context) { diff --git a/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/util/TestStandardValidators.java b/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/util/TestStandardValidators.java index 13d2f4f7cc..062bf5a89b 100644 --- a/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/util/TestStandardValidators.java +++ b/nifi-commons/nifi-processor-utilities/src/test/java/org/apache/nifi/processor/util/TestStandardValidators.java @@ -18,6 +18,7 @@ package org.apache.nifi.processor.util; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; import java.util.concurrent.TimeUnit; @@ -29,6 +30,20 @@ import org.mockito.Mockito; public class TestStandardValidators { + @Test + public void testNonBlankValidator() { + Validator val = StandardValidators.NON_BLANK_VALIDATOR; + ValidationContext vc = mock(ValidationContext.class); + ValidationResult vr = val.validate("foo", "", vc); + assertFalse(vr.isValid()); + + vr = val.validate("foo", " ", vc); + assertFalse(vr.isValid()); + + vr = val.validate("foo", " h", vc); + assertTrue(vr.isValid()); + } + @Test public void testTimePeriodValidator() { Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);