From c382ab6f069fbb244c55ff9fdba2be160e3651a8 Mon Sep 17 00:00:00 2001 From: dan-s1 Date: Mon, 15 May 2023 23:37:41 +0000 Subject: [PATCH] NIFI-11150 Refactored QueryGroovyTest to TestQuery This closes #7250 Signed-off-by: David Handermann --- .../language/QueryGroovyTest.groovy | 212 ------------------ .../expression/language/TestQuery.java | 94 ++++++++ 2 files changed, 94 insertions(+), 212 deletions(-) delete mode 100644 nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy diff --git a/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy b/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy deleted file mode 100644 index fd4d063e0e..0000000000 --- a/nifi-commons/nifi-expression-language/src/test/groovy/org/apache/nifi/attribute/expression/language/QueryGroovyTest.groovy +++ /dev/null @@ -1,212 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.nifi.attribute.expression.language - -import org.apache.nifi.attribute.expression.language.evaluation.QueryResult -import org.apache.nifi.expression.AttributeExpression -import org.junit.jupiter.api.AfterEach -import org.junit.jupiter.api.BeforeAll -import org.junit.jupiter.api.BeforeEach -import org.junit.jupiter.api.Test -import org.slf4j.Logger -import org.slf4j.LoggerFactory - -import static org.junit.jupiter.api.Assertions.assertEquals -import static org.junit.jupiter.api.Assertions.assertNotEquals - -class QueryGroovyTest { - private static final Logger logger = LoggerFactory.getLogger(QueryGroovyTest.class) - - @BeforeAll - static void setUpOnce() throws Exception { - logger.metaClass.methodMissing = { String name, args -> - logger.info("[${name?.toUpperCase()}] ${(args as List).join(" ")}") - } - } - - @BeforeEach - void setUp() { - - } - - @AfterEach - void tearDown() { - Query.metaClass.static = null - - } - - @Test - void testReplaceShouldReplaceAllLiteralMatches() { - // Arrange - int n = 3 - final String ORIGINAL_VALUE = "Hello World" - final Map attributes = [ - single : ORIGINAL_VALUE, - repeating: [ORIGINAL_VALUE].multiply(n).join(" ")] - logger.info("Attributes: ${attributes}") - - final String REPLACEMENT_VALUE = "Goodbye Planet" - - final String EXPECTED_SINGLE_RESULT = REPLACEMENT_VALUE - final String EXPECTED_REPEATING_RESULT = [REPLACEMENT_VALUE].multiply(n).join(" ") - - final String REPLACE_LITERAL = ORIGINAL_VALUE - - final String REPLACE_SINGLE_EXPRESSION = "\${single:replace('${REPLACE_LITERAL}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace single | ${REPLACE_SINGLE_EXPRESSION}") - final String REPLACE_REPEATING_EXPRESSION = "\${repeating:replace('${REPLACE_LITERAL}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace repeating | ${REPLACE_REPEATING_EXPRESSION}") - - Query replaceSingleQuery = Query.compile(REPLACE_SINGLE_EXPRESSION) - Query replaceRepeatingQuery = Query.compile(REPLACE_REPEATING_EXPRESSION) - - // Act - QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace single result: ${replaceSingleResult.value}") - - QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace repeating result: ${replaceRepeatingResult.value}") - - // Assert - assertEquals(EXPECTED_SINGLE_RESULT, replaceSingleResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.resultType) - - assertEquals(EXPECTED_REPEATING_RESULT, replaceRepeatingResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.resultType) - } - - @Test - void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() { - // Arrange - int n = 3 - final String ORIGINAL_VALUE = "Hello World" - final Map attributes = [ - single : ORIGINAL_VALUE, - repeating: [ORIGINAL_VALUE].multiply(n).join(" ")] - logger.info("Attributes: ${attributes}") - - final String REPLACEMENT_VALUE = "Goodbye Planet" - - final String EXPECTED_SINGLE_RESULT = REPLACEMENT_VALUE - final String EXPECTED_REPEATING_RESULT = [REPLACEMENT_VALUE, [ORIGINAL_VALUE].multiply(n - 1)].flatten().join(" ") - - final String REPLACE_ONLY_FIRST_PATTERN = /\w+\s\w+\b??/ - - final String REPLACE_SINGLE_EXPRESSION = "\${single:replaceFirst('${REPLACE_ONLY_FIRST_PATTERN}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace single | ${REPLACE_SINGLE_EXPRESSION}") - final String REPLACE_REPEATING_EXPRESSION = "\${repeating:replaceFirst('${REPLACE_ONLY_FIRST_PATTERN}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace repeating | ${REPLACE_REPEATING_EXPRESSION}") - - Query replaceSingleQuery = Query.compile(REPLACE_SINGLE_EXPRESSION) - Query replaceRepeatingQuery = Query.compile(REPLACE_REPEATING_EXPRESSION) - - // Act - QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace single result: ${replaceSingleResult.value}") - - QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace repeating result: ${replaceRepeatingResult.value}") - - // Assert - assertEquals(EXPECTED_SINGLE_RESULT, replaceSingleResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.resultType) - - assertEquals(EXPECTED_REPEATING_RESULT, replaceRepeatingResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.resultType) - } - - @Test - void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() { - // Arrange - int n = 3 - final String ORIGINAL_VALUE = "Hello World" - final Map attributes = [ - single : ORIGINAL_VALUE, - repeating: [ORIGINAL_VALUE].multiply(n).join(" ")] - logger.info("Attributes: ${attributes}") - - final String REPLACEMENT_VALUE = "Goodbye Planet" - - final String EXPECTED_SINGLE_RESULT = REPLACEMENT_VALUE - final String EXPECTED_REPEATING_RESULT = [REPLACEMENT_VALUE, [ORIGINAL_VALUE].multiply(n - 1)].flatten().join(" ") - - final String REPLACE_LITERAL = ORIGINAL_VALUE - - final String REPLACE_SINGLE_EXPRESSION = "\${single:replaceFirst('${REPLACE_LITERAL}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace single | ${REPLACE_SINGLE_EXPRESSION}") - final String REPLACE_REPEATING_EXPRESSION = "\${repeating:replaceFirst('${REPLACE_LITERAL}', '${REPLACEMENT_VALUE}')}" - logger.expression("Replace repeating | ${REPLACE_REPEATING_EXPRESSION}") - - Query replaceSingleQuery = Query.compile(REPLACE_SINGLE_EXPRESSION) - Query replaceRepeatingQuery = Query.compile(REPLACE_REPEATING_EXPRESSION) - - // Act - QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace single result: ${replaceSingleResult.value}") - - QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)) - logger.info("Replace repeating result: ${replaceRepeatingResult.value}") - - // Assert - assertEquals(EXPECTED_SINGLE_RESULT, replaceSingleResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.resultType) - - assertEquals(EXPECTED_REPEATING_RESULT, replaceRepeatingResult.value) - assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.resultType) - } - - @Test - void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() { - // Arrange - int n = 3 - final String ORIGINAL_VALUE = "Hello World" - final Map attributes = [ - single : ORIGINAL_VALUE, - repeating: [ORIGINAL_VALUE].multiply(n).join(" ")] - logger.info("Attributes: ${attributes}") - - final String REPLACEMENT_VALUE = "Goodbye Planet" - - final String EXPECTED_SINGLE_RESULT = REPLACEMENT_VALUE - final String EXPECTED_REPEATING_RESULT = [REPLACEMENT_VALUE, [ORIGINAL_VALUE].multiply(n - 1)].flatten().join(" ") - - final String REPLACE_ONLY_FIRST_PATTERN = /\w+\s\w+\b??/ - - // Act - - // Execute on both single and repeating with String#replace() - String replaceSingleResult = attributes.single.replace(REPLACE_ONLY_FIRST_PATTERN, REPLACEMENT_VALUE) - logger.info("Replace single result: ${replaceSingleResult}") - - String replaceRepeatingResult = attributes.repeating.replace(REPLACE_ONLY_FIRST_PATTERN, REPLACEMENT_VALUE) - logger.info("Replace repeating result: ${replaceRepeatingResult}") - - // Execute on both single and repeating with String#replaceFirst() - String replaceFirstSingleResult = attributes.single.replaceFirst(REPLACE_ONLY_FIRST_PATTERN, REPLACEMENT_VALUE) - logger.info("Replace first single result: ${replaceFirstSingleResult}") - - String replaceFirstRepeatingResult = attributes.repeating.replaceFirst(REPLACE_ONLY_FIRST_PATTERN, REPLACEMENT_VALUE) - logger.info("Replace repeating result: ${replaceFirstRepeatingResult}") - - // Assert - assertNotEquals(EXPECTED_SINGLE_RESULT, replaceSingleResult) - assertNotEquals(EXPECTED_REPEATING_RESULT, replaceRepeatingResult) - - assertEquals(EXPECTED_SINGLE_RESULT, replaceFirstSingleResult) - assertEquals(EXPECTED_REPEATING_RESULT, replaceFirstRepeatingResult) - } -} \ No newline at end of file diff --git a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java index 55d252f7a8..23ea9fb359 100644 --- a/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java +++ b/nifi-commons/nifi-expression-language/src/test/java/org/apache/nifi/attribute/expression/language/TestQuery.java @@ -22,6 +22,7 @@ import org.apache.nifi.attribute.expression.language.evaluation.NumberQueryResul import org.apache.nifi.attribute.expression.language.evaluation.QueryResult; import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageException; import org.apache.nifi.attribute.expression.language.exception.AttributeExpressionLanguageParsingException; +import org.apache.nifi.expression.AttributeExpression; import org.apache.nifi.expression.AttributeExpression.ResultType; import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.parameter.Parameter; @@ -61,6 +62,7 @@ import static org.hamcrest.Matchers.greaterThan; import static org.junit.jupiter.api.Assertions.assertDoesNotThrow; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.eq; @@ -1177,6 +1179,98 @@ public class TestQuery { verifyEquals("${attr:replaceAll('.*?(l+).*', '$1')}", attributes, "ll"); } + @Test + public void testReplaceShouldReplaceAllLiteralMatches() { + int n = 3; + final String originalValue = "Hello World"; + final Map attributes = Map.of("single", originalValue, + "repeating", StringUtils.repeat(originalValue, " ", n)); + final String replacementValue = "Goodbye Planet"; + final String expectedRepeatingResult = StringUtils.repeat(replacementValue, " ", n); + final String replaceSingleExpression = "${single:replace('" + originalValue + "', '" + replacementValue + "')}"; + final String replaceRepeatingExpression = "${repeating:replace('" + originalValue + "', '" + replacementValue + "')}"; + Query replaceSingleQuery = Query.compile(replaceSingleExpression); + Query replaceRepeatingQuery = Query.compile(replaceRepeatingExpression); + + QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)); + QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)); + + assertEquals(replacementValue, replaceSingleResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.getResultType()); + assertEquals(expectedRepeatingResult, replaceRepeatingResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.getResultType()); + } + + @Test + public void testReplaceFirstShouldOnlyReplaceFirstRegexMatch() { + int n = 3; + final String originalValue = "Hello World"; + final Map attributes = Map.of("single", originalValue, + "repeating", StringUtils.repeat(originalValue, " ", n)); + + final String replacementValue = "Goodbye Planet"; + final String expectedRepeatingResult = replacementValue + " " + StringUtils.repeat(originalValue, " ", n -1); + final String replaceOnlyFirstPattern = "\\w+\\s\\w+\\b??"; + final String replaceSingleExpression = "${single:replaceFirst('" + replaceOnlyFirstPattern +"', '" + replacementValue + "')}"; + final String replaceRepeatingExpression = "${repeating:replaceFirst('" + replaceOnlyFirstPattern + "', '" + replacementValue + "')}"; + Query replaceSingleQuery = Query.compile(replaceSingleExpression); + Query replaceRepeatingQuery = Query.compile(replaceRepeatingExpression); + + QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)); + QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)); + + assertEquals(replacementValue, replaceSingleResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.getResultType()); + assertEquals(expectedRepeatingResult, replaceRepeatingResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.getResultType()); + } + + @Test + public void testReplaceFirstShouldOnlyReplaceFirstLiteralMatch() { + int n = 3; + final String originalValue = "Hello World"; + final Map attributes = Map.of("single", originalValue, + "repeating", StringUtils.repeat(originalValue, " ", n)); + final String replacementValue = "Goodbye Planet"; + final String expectedRepeatingResult = replacementValue + " " + StringUtils.repeat(originalValue, " ", n -1); + final String replaceSingleExpression = "${single:replaceFirst('" + originalValue + "', '" + replacementValue + "')}"; + final String replaceRepeatingExpression = "${repeating:replaceFirst('" + originalValue + "', '" + replacementValue + "')}"; + Query replaceSingleQuery = Query.compile(replaceSingleExpression); + Query replaceRepeatingQuery = Query.compile(replaceRepeatingExpression); + + QueryResult replaceSingleResult = replaceSingleQuery.evaluate(new StandardEvaluationContext(attributes)); + QueryResult replaceRepeatingResult = replaceRepeatingQuery.evaluate(new StandardEvaluationContext(attributes)); + + assertEquals(replacementValue, replaceSingleResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceSingleResult.getResultType()); + assertEquals(expectedRepeatingResult, replaceRepeatingResult.getValue()); + assertEquals(AttributeExpression.ResultType.STRING, replaceRepeatingResult.getResultType()); + } + + @Test + public void testShouldDemonstrateDifferenceBetweenStringReplaceAndStringReplaceFirst() { + int n = 3; + final String originalValue = "Hello World"; + final Map attributes = Map.of("single", originalValue, + "repeating", StringUtils.repeat(originalValue, " ", n)); + final String replacementValue = "Goodbye Planet"; + final String expectedRepeatingResult = replacementValue + " " + StringUtils.repeat(originalValue, " ", n -1); + final String replaceOnlyFirstPattern = "\\w+\\s\\w+\\b??"; + + // Execute on both single and repeating with String#replace() + String replaceSingleResult = attributes.get("single").replace(replaceOnlyFirstPattern, replacementValue); + String replaceRepeatingResult = attributes.get("repeating").replace(replaceOnlyFirstPattern, replacementValue); + + // Execute on both single and repeating with String#replaceFirst() + String replaceFirstSingleResult = attributes.get("single").replaceFirst(replaceOnlyFirstPattern, replacementValue); + String replaceFirstRepeatingResult = attributes.get("repeating").replaceFirst(replaceOnlyFirstPattern, replacementValue); + + assertNotEquals(replacementValue, replaceSingleResult); + assertNotEquals(expectedRepeatingResult, replaceRepeatingResult); + assertEquals(replacementValue, replaceFirstSingleResult); + assertEquals(expectedRepeatingResult, replaceFirstRepeatingResult); + } + @Test public void testMathWholeNumberOperations() { final Map attributes = new HashMap<>();