Search in sources :

Example 1 with Validator

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

the class ThriftConnectionPool method createMultipleFilesExistValidator.

/**
 * Validates that one or more files exist, as specified in a single property.
 *
 * @return a validator which describes the result of the validation
 */
public static final 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 2 with Validator

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

the class ControlRate method customValidate.

@Override
protected Collection<ValidationResult> customValidate(final ValidationContext context) {
    final List<ValidationResult> validationResults = new ArrayList<>(super.customValidate(context));
    final Validator rateValidator;
    switch(context.getProperty(RATE_CONTROL_CRITERIA).getValue().toLowerCase()) {
        case DATA_RATE:
            rateValidator = StandardValidators.DATA_SIZE_VALIDATOR;
            break;
        case ATTRIBUTE_RATE:
            rateValidator = StandardValidators.POSITIVE_LONG_VALIDATOR;
            final String rateAttr = context.getProperty(RATE_CONTROL_ATTRIBUTE_NAME).getValue();
            if (rateAttr == null) {
                validationResults.add(new ValidationResult.Builder().subject(RATE_CONTROL_ATTRIBUTE_NAME.getName()).explanation("<Rate Controlled Attribute> property must be set if using <Rate Control Criteria> of 'attribute value'").build());
            }
            break;
        case FLOWFILE_RATE:
        default:
            rateValidator = StandardValidators.POSITIVE_LONG_VALIDATOR;
            break;
    }
    final ValidationResult rateResult = rateValidator.validate("Maximum Rate", context.getProperty(MAX_RATE).getValue(), context);
    if (!rateResult.isValid()) {
        validationResults.add(rateResult);
    }
    return validationResults;
}
Also used : ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) Validator(org.apache.nifi.components.Validator)

Example 3 with Validator

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

the class ExtractText method customValidate.

@Override
protected Collection<ValidationResult> customValidate(final ValidationContext validationContext) {
    final List<ValidationResult> problems = new ArrayList<>(super.customValidate(validationContext));
    // If the capture group zero is not going to be included, each dynamic property must have at least one group
    final boolean includeCaptureGroupZero = validationContext.getProperty(INCLUDE_CAPTURE_GROUP_ZERO).getValue().equalsIgnoreCase("true");
    getLogger().debug("Include capture group zero is " + includeCaptureGroupZero);
    if (!includeCaptureGroupZero) {
        final Validator oneGroupMinimumValidator = StandardValidators.createRegexValidator(1, 40, true);
        for (Map.Entry<PropertyDescriptor, String> prop : validationContext.getProperties().entrySet()) {
            PropertyDescriptor pd = prop.getKey();
            if (pd.isDynamic()) {
                String value = validationContext.getProperty(pd).getValue();
                getLogger().debug("Evaluating dynamic property " + pd.getDisplayName() + " (" + pd.getName() + ") with value " + value);
                ValidationResult result = oneGroupMinimumValidator.validate(pd.getDisplayName(), value, validationContext);
                getLogger().debug("Validation result: " + result.toString());
                if (!result.isValid()) {
                    problems.add(result);
                }
            }
        }
    }
    return problems;
}
Also used : PropertyDescriptor(org.apache.nifi.components.PropertyDescriptor) ArrayList(java.util.ArrayList) ValidationResult(org.apache.nifi.components.ValidationResult) HashMap(java.util.HashMap) Map(java.util.Map) Validator(org.apache.nifi.components.Validator)

Example 4 with Validator

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

the class TestCSVValidators method testUnescapedSingleCharGoodChar.

@Test
public void testUnescapedSingleCharGoodChar() {
    Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("Delimiter", ",", mockContext);
    assertTrue(result.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 5 with Validator

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

the class TestCSVValidators method testUnescapedSingleCharUnicodeChar.

@Test
public void testUnescapedSingleCharUnicodeChar() {
    Validator validator = CSVValidators.UNESCAPED_SINGLE_CHAR_VALIDATOR;
    ValidationContext mockContext = Mockito.mock(ValidationContext.class);
    ValidationResult result = validator.validate("Delimiter", "\\u0001", mockContext);
    assertTrue(result.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)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