Search in sources :

Example 56 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class StandardValidators method createRegexValidator.

/**
 * Creates a @{link Validator} that ensure that a value is a valid Java
 * Regular Expression with at least <code>minCapturingGroups</code>
 * capturing groups and at most <code>maxCapturingGroups</code> capturing
 * groups. If <code>supportAttributeExpressionLanguage</code> is set to
 * <code>true</code>, the value may also include the Expression Language,
 * but the result of evaluating the Expression Language will be applied
 * before the Regular Expression is performed. In this case, the Expression
 * Language will not support FlowFile Attributes but only System/JVM
 * Properties
 *
 * @param minCapturingGroups minimum capturing groups allowed
 * @param maxCapturingGroups maximum capturing groups allowed
 * @param supportAttributeExpressionLanguage whether or not to support
 * expression language
 * @return validator
 */
public static Validator createRegexValidator(final int minCapturingGroups, final int maxCapturingGroups, final boolean supportAttributeExpressionLanguage) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String value, final ValidationContext context) {
            try {
                final String substituted;
                if (supportAttributeExpressionLanguage) {
                    try {
                        substituted = context.newPropertyValue(value).evaluateAttributeExpressions().getValue();
                    } catch (final Exception e) {
                        return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("Failed to evaluate the Attribute Expression Language due to " + e.toString()).build();
                    }
                } else {
                    substituted = value;
                }
                final Pattern pattern = Pattern.compile(substituted);
                final int numGroups = pattern.matcher("").groupCount();
                if (numGroups < minCapturingGroups || numGroups > maxCapturingGroups) {
                    return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("RegEx is required to have between " + minCapturingGroups + " and " + maxCapturingGroups + " Capturing Groups but has " + numGroups).build();
                }
                return new ValidationResult.Builder().subject(subject).input(value).valid(true).build();
            } catch (final Exception e) {
                return new ValidationResult.Builder().subject(subject).input(value).valid(false).explanation("Not a valid Java Regular Expression").build();
            }
        }
    };
}
Also used : Pattern(java.util.regex.Pattern) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) MalformedURLException(java.net.MalformedURLException) UnsupportedCharsetException(java.nio.charset.UnsupportedCharsetException) ParseException(java.text.ParseException) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 57 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class TestStandardValidators method testHostnamePortListValidator.

@Test
public void testHostnamePortListValidator() {
    Validator val = StandardValidators.HOSTNAME_PORT_LIST_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    Mockito.when(vc.isExpressionLanguageSupported("foo")).thenReturn(true);
    ValidationResult vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "localhost", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:0", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:65536", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:6666,localhost", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "test:65535", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "test:65535,localhost:666,127.0.0.1:8989", vc);
    assertTrue(vr.isValid());
    Mockito.when(vc.isExpressionLanguagePresent("${test}")).thenReturn(true);
    vr = val.validate("foo", "${test}", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "${test", vc);
    assertFalse(vr.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 58 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class TestStandardValidators method testCreateURLorFileValidator.

@Test
public void testCreateURLorFileValidator() {
    Validator val = StandardValidators.createURLorFileValidator();
    ValidationResult vr;
    final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
    vr = val.validate("URLorFile", null, validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "http://nifi.apache.org", validationContext);
    assertTrue(vr.isValid());
    vr = val.validate("URLorFile", "http//nifi.apache.org", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "nifi.apache.org", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("URLorFile", "src/test/resources/this_file_exists.txt", validationContext);
    assertTrue(vr.isValid());
    vr = val.validate("URLorFile", "src/test/resources/this_file_does_not_exist.txt", validationContext);
    assertFalse(vr.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 59 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class TestStandardValidators method testNonBlankValidator.

@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());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Example 60 with ValidationResult

use of org.apache.nifi.components.ValidationResult in project nifi by apache.

the class TestStandardValidators method testNonEmptyELValidator.

@Test
public void testNonEmptyELValidator() {
    Validator val = StandardValidators.NON_EMPTY_EL_VALIDATOR;
    ValidationContext vc = mock(ValidationContext.class);
    Mockito.when(vc.isExpressionLanguageSupported("foo")).thenReturn(true);
    ValidationResult vr = val.validate("foo", "", vc);
    assertFalse(vr.isValid());
    vr = val.validate("foo", "    h", vc);
    assertTrue(vr.isValid());
    Mockito.when(vc.isExpressionLanguagePresent("${test}")).thenReturn(true);
    vr = val.validate("foo", "${test}", vc);
    assertTrue(vr.isValid());
    vr = val.validate("foo", "${test", vc);
    assertTrue(vr.isValid());
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext) Test(org.junit.Test)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)179 Test (org.junit.Test)80 ArrayList (java.util.ArrayList)64 TestRunner (org.apache.nifi.util.TestRunner)46 ValidationContext (org.apache.nifi.components.ValidationContext)37 MockProcessContext (org.apache.nifi.util.MockProcessContext)26 TdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.api.TdchConnectionService)23 Validator (org.apache.nifi.components.Validator)21 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)20 DevTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DevTdchConnectionService)18 DummyTdchConnectionService (com.thinkbiganalytics.kylo.nifi.teradata.tdch.core.controllerservice.DummyTdchConnectionService)18 HashSet (java.util.HashSet)18 ProcessContext (org.apache.nifi.processor.ProcessContext)15 File (java.io.File)12 HashMap (java.util.HashMap)12 Collection (java.util.Collection)11 SSLContextService (org.apache.nifi.ssl.SSLContextService)11 Map (java.util.Map)10 List (java.util.List)9 ComponentLog (org.apache.nifi.logging.ComponentLog)9