NIFI-1785 added NON_BLANK_VALIDATOR

NIFI-1785 addressed PR comment

NIFI-1785 fixed white spaces

This closes #365.

Signed-off-by: Bryan Bende <bbende@apache.org>
This commit is contained in:
Oleg Zhurakousky 2016-04-19 14:55:41 -04:00 committed by Bryan Bende
parent e12a79ea92
commit 0fed158d14
2 changed files with 32 additions and 0 deletions

View File

@ -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) {

View File

@ -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);