mirror of https://github.com/apache/nifi.git
NIFI-2952 Fixed search value validation in ReplaceText
This closes: #1378 Signed-off-by: Andre F de Miranda <trixpan@users.noreply.github.com>
This commit is contained in:
parent
61c799d88b
commit
02d38b55bf
|
@ -24,6 +24,7 @@ import java.io.OutputStream;
|
|||
import java.io.OutputStreamWriter;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.HashMap;
|
||||
import java.util.HashSet;
|
||||
|
@ -44,6 +45,8 @@ import org.apache.nifi.annotation.documentation.CapabilityDescription;
|
|||
import org.apache.nifi.annotation.documentation.Tags;
|
||||
import org.apache.nifi.components.AllowableValue;
|
||||
import org.apache.nifi.components.PropertyDescriptor;
|
||||
import org.apache.nifi.components.ValidationContext;
|
||||
import org.apache.nifi.components.ValidationResult;
|
||||
import org.apache.nifi.components.Validator;
|
||||
import org.apache.nifi.expression.AttributeValueDecorator;
|
||||
import org.apache.nifi.flowfile.FlowFile;
|
||||
|
@ -110,7 +113,7 @@ public class ReplaceText extends AbstractProcessor {
|
|||
.displayName("Search Value")
|
||||
.description("The Search Value to search for in the FlowFile content. Only used for 'Literal Replace' and 'Regex Replace' matching strategies")
|
||||
.required(true)
|
||||
.addValidator(StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true))
|
||||
.addValidator(Validator.VALID)
|
||||
.expressionLanguageSupported(true)
|
||||
.defaultValue(DEFAULT_REGEX)
|
||||
.build();
|
||||
|
@ -201,6 +204,32 @@ public class ReplaceText extends AbstractProcessor {
|
|||
return relationships;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Collection<ValidationResult> customValidate(ValidationContext validationContext) {
|
||||
final List<ValidationResult> errors = new ArrayList<>(super.customValidate(validationContext));
|
||||
|
||||
switch (validationContext.getProperty(REPLACEMENT_STRATEGY).getValue()) {
|
||||
case literalReplaceValue:
|
||||
errors.add(StandardValidators.NON_EMPTY_VALIDATOR
|
||||
.validate(SEARCH_VALUE.getName(), validationContext.getProperty(SEARCH_VALUE).getValue(), validationContext));
|
||||
break;
|
||||
|
||||
case regexReplaceValue:
|
||||
errors.add(StandardValidators.createRegexValidator(0, Integer.MAX_VALUE, true)
|
||||
.validate(SEARCH_VALUE.getName(), validationContext.getProperty(SEARCH_VALUE).getValue(), validationContext));
|
||||
break;
|
||||
|
||||
case appendValue:
|
||||
case prependValue:
|
||||
case alwaysReplace:
|
||||
default:
|
||||
// nothing to check, search value is not used
|
||||
break;
|
||||
}
|
||||
|
||||
return errors;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
|
||||
final List<FlowFile> flowFiles = session.get(FlowFileFilters.newSizeBasedFilter(1, DataUnit.MB, 100));
|
||||
|
|
|
@ -1166,6 +1166,34 @@ public class TestReplaceText {
|
|||
runner.run();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testProcessorConfigurationRegexNotValid() throws IOException {
|
||||
final TestRunner runner = TestRunners.newTestRunner(new ReplaceText());
|
||||
runner.setProperty(ReplaceText.SEARCH_VALUE, "(?<!\\),*");
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_VALUE, "hello");
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.REGEX_REPLACE);
|
||||
runner.setProperty(ReplaceText.EVALUATION_MODE, ReplaceText.ENTIRE_TEXT);
|
||||
runner.assertNotValid();
|
||||
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.LITERAL_REPLACE);
|
||||
runner.assertValid();
|
||||
runner.enqueue("(?<!\\),*".getBytes());
|
||||
runner.run();
|
||||
runner.assertAllFlowFilesTransferred(ReplaceText.REL_SUCCESS, 1);
|
||||
final MockFlowFile out = runner.getFlowFilesForRelationship(ReplaceText.REL_SUCCESS).get(0);
|
||||
out.assertContentEquals("hello");
|
||||
|
||||
runner.setProperty(ReplaceText.SEARCH_VALUE, "");
|
||||
runner.assertNotValid();
|
||||
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.APPEND);
|
||||
runner.assertValid();
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.PREPEND);
|
||||
runner.assertValid();
|
||||
runner.setProperty(ReplaceText.REPLACEMENT_STRATEGY, ReplaceText.ALWAYS_REPLACE);
|
||||
runner.assertValid();
|
||||
}
|
||||
|
||||
private String translateNewLines(final File file) throws IOException {
|
||||
return translateNewLines(file.toPath());
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue