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());
}
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());
}
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();
}
};
}
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;
}
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();
}
};
}
Aggregations