nifi-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Arun Manivannan <a...@arunma.com>
Subject ReplaceText - Decorators
Date Fri, 01 Sep 2017 08:43:07 GMT
Hi,

I am using regular expressions as values for both the search and replace
properties of the ReplaceText processor. When I am using literals as values
for these properties, it all works great. [image: Screen Shot 2017-09-01 at
4.30.46 PM.png]

However, when I try to use a FlowFile attribute (both properties support
expression language), it fails, falling back to the default by returning
the original flowfile.

Created a Testcase for ReplaceText and figured out that the Decorators -
quotedAttributeDecorator and escapeBackRefDecorator (one for search and
replace regex) -escapes the Regex after evaluating the EL itself. When I
remove them as parameters for evaluateAttributeExpressions, they work just
like literals.

Not an expert with handling Regex but can I please understand the context
behind escaping the values.  If escaping the EL isn't intended, I can make
a PR to bypass this selectively - if the property has an EL (via
the isExpressionLanguagePresent()) then avoid the quoting.

The testcase I am using is this :

@Test
public void testAlwaysReplaceLineByLineUsingExpressionLanguage() {
    final TestRunner runner = TestRunners.newTestRunner(new
ReplaceTextTestProcessor());
    runner.setValidateExpressionUsage(true);
    runner.setProperty(ReplaceTextTestProcessor.EVALUATION_MODE,
ReplaceTextTestProcessor.LINE_BY_LINE);
    runner.setProperty(ReplaceTextTestProcessor.REPLACEMENT_STRATEGY,
ReplaceTextTestProcessor.REGEX_REPLACE);
    //runner.setProperty(ReplaceText.SEARCH_VALUE, "(.{1})(.{2})");
    runner.setProperty(ReplaceTextTestProcessor.SEARCH_VALUE,
"${source.regex}");
    runner.setProperty(ReplaceTextTestProcessor.REPLACEMENT_VALUE,
"${target.regex}");

    final Map<String, String> attributes = new HashMap<>();
    attributes.put("source.regex", "(.{1})(.{2})(.*)");
    attributes.put("target.regex", "$1;$2");
    runner.enqueue("Hel\nWor\r\ntody\n".getBytes(), attributes);

    runner.run();

    runner.assertAllFlowFilesTransferred(ReplaceTextTestProcessor.REL_SUCCESS,
1);
    final MockFlowFile out =
runner.getFlowFilesForRelationship(ReplaceTextTestProcessor.REL_SUCCESS).get(0);
    out.assertContentEquals("H;el\nW;or\r\nt;od\n");
}


Thank you in advance.

Regards,
Arun

Mime
  • Unnamed multipart/related (inline, None, 0 bytes)
View raw message