Search in sources :

Example 11 with Validator

use of org.apache.nifi.components.Validator 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)

Example 12 with Validator

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

the class TestStandardValidators method testTimePeriodValidator.

@Test
public void testTimePeriodValidator() {
    Validator val = StandardValidators.createTimePeriodValidator(1L, TimeUnit.SECONDS, Long.MAX_VALUE, TimeUnit.NANOSECONDS);
    ValidationResult vr;
    final ValidationContext validationContext = Mockito.mock(ValidationContext.class);
    vr = val.validate("TimePeriodTest", "0 sense made", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", null, validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "0 secs", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "999 millis", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "999999999 nanos", validationContext);
    assertFalse(vr.isValid());
    vr = val.validate("TimePeriodTest", "1 sec", validationContext);
    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 13 with Validator

use of org.apache.nifi.components.Validator in project kylo by Teradata.

the class AbstractHadoopProcessor method createMultipleFilesExistValidator.

/**
 * Validates that one or more files exist, as specified in a single property.
 *
 * @return a validator instance that validates the files given
 */
public static Validator createMultipleFilesExistValidator() {
    return new Validator() {

        @Override
        public ValidationResult validate(String subject, String input, ValidationContext context) {
            final String[] files = input.split(",");
            for (String filename : files) {
                try {
                    final File file = new File(filename.trim());
                    final boolean valid = file.exists() && file.isFile();
                    if (!valid) {
                        final String message = "File " + file + " does not exist or is not a file";
                        return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                    }
                } catch (SecurityException e) {
                    final String message = "Unable to access " + filename + " due to " + e.getMessage();
                    return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation(message).build();
                }
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) File(java.io.File) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Example 14 with Validator

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

the class RouteText method customValidate.

@Override
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
    Collection<ValidationResult> results = new ArrayList<>(super.customValidate(validationContext));
    boolean dynamicProperty = false;
    final String matchStrategy = validationContext.getProperty(MATCH_STRATEGY).getValue();
    final boolean compileRegex = matchStrategy.equals(matchesRegularExpressionValue) || matchStrategy.equals(containsRegularExpressionValue);
    final boolean requiresExpression = matchStrategy.equalsIgnoreCase(satisfiesExpression);
    Validator validator = null;
    if (compileRegex) {
        validator = StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true);
    }
    Map<PropertyDescriptor, String> allProperties = validationContext.getProperties();
    for (final PropertyDescriptor descriptor : allProperties.keySet()) {
        if (descriptor.isDynamic()) {
            dynamicProperty = true;
            final String propValue = validationContext.getProperty(descriptor).getValue();
            if (compileRegex) {
                ValidationResult validationResult = validator.validate(descriptor.getName(), propValue, validationContext);
                if (validationResult != null) {
                    results.add(validationResult);
                }
            } else if (requiresExpression) {
                try {
                    final ResultType resultType = validationContext.newExpressionLanguageCompiler().compile(propValue).getResultType();
                    if (resultType != ResultType.BOOLEAN) {
                        results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName()).explanation("expression returns type of " + resultType.name() + " but is required to return a Boolean value").build());
                    }
                } catch (final IllegalArgumentException iae) {
                    results.add(new ValidationResult.Builder().valid(false).input(propValue).subject(descriptor.getName()).explanation("input is not a valid Expression Language expression").build());
                }
            }
        }
    }
    if (!dynamicProperty) {
        results.add(new ValidationResult.Builder().subject("Dynamic Properties").explanation("In order to route text there must be dynamic properties to match against").valid(false).build());
    }
    return results;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) CacheBuilder(com.google.common.cache.CacheBuilder) ArrayList(java.util.ArrayList) ResultType(org.apache.nifi.expression.AttributeExpression.ResultType) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator)

Example 15 with Validator

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

the class StandardValidators method createDataSizeBoundsValidator.

public static Validator createDataSizeBoundsValidator(final long minBytesInclusive, final long maxBytesInclusive) {
    return new Validator() {

        @Override
        public ValidationResult validate(final String subject, final String input, final ValidationContext context) {
            if (context.isExpressionLanguageSupported(subject) && context.isExpressionLanguagePresent(input)) {
                return new ValidationResult.Builder().subject(subject).input(input).explanation("Expression Language Present").valid(true).build();
            }
            final ValidationResult vr = DATA_SIZE_VALIDATOR.validate(subject, input, context);
            if (!vr.isValid()) {
                return vr;
            }
            final long dataSizeBytes = DataUnit.parseDataSize(input, DataUnit.B).longValue();
            if (dataSizeBytes < minBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be smaller than " + minBytesInclusive + " bytes").build();
            }
            if (dataSizeBytes > maxBytesInclusive) {
                return new ValidationResult.Builder().subject(subject).input(input).valid(false).explanation("Cannot be larger than " + maxBytesInclusive + " bytes").build();
            }
            return new ValidationResult.Builder().subject(subject).input(input).valid(true).build();
        }
    };
}
Also used : ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator) ValidationContext(org.apache.nifi.components.ValidationContext)

Aggregations

ValidationResult (org.apache.nifi.components.ValidationResult)20 Validator (org.apache.nifi.components.Validator)20 ValidationContext (org.apache.nifi.components.ValidationContext)17 Test (org.junit.Test)11 File (java.io.File)3 ArrayList (java.util.ArrayList)3 PropertyDescriptor (org.apache.nifi.components.PropertyDescriptor)2 ResultType (org.apache.nifi.expression.AttributeExpression.ResultType)2 CacheBuilder (com.google.common.cache.CacheBuilder)1 MalformedURLException (java.net.MalformedURLException)1 UnsupportedCharsetException (java.nio.charset.UnsupportedCharsetException)1 ParseException (java.text.ParseException)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Pattern (java.util.regex.Pattern)1