You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ex...@apache.org on 2023/05/17 14:19:35 UTC

[nifi] branch support/nifi-1.x updated: NIFI-11550 Refactored QueryGroovyTest to TestQuery

This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch support/nifi-1.x
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/support/nifi-1.x by this push:
     new 0923454ad6 NIFI-11550 Refactored QueryGroovyTest to TestQuery
0923454ad6 is described below

commit 0923454ad6e3306682336c0c6b364f4e9e4bfa12
Author: dan-s1 <ds...@gmail.com>
AuthorDate: Mon May 15 23:37:41 2023 +0000

    NIFI-11550 Refactored QueryGroovyTest to TestQuery
    
    This closes #7250
    
    Signed-off-by: David Handermann <ex...@apache.org>
    (cherry picked from commit c382ab6f069fbb244c55ff9fdba2be160e3651a8)
---
 .../expression/language/QueryGroovyTest.groovy     | 212 ---------------------
 .../attribute/expression/language/TestQuery.java   |  99 ++++++++++
 2 files changed, 99 insertions(+), 212 deletions(-)

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<String, String> 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<String, String> 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<String, String> 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<String, String> 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..734cf2f5ec 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;
@@ -47,6 +48,7 @@ import java.util.Collections;
 import java.util.Date;
 import java.util.HashMap;
 import java.util.HashSet;
+import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -61,6 +63,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 +1180,102 @@ public class TestQuery {
         verifyEquals("${attr:replaceAll('.*?(l+).*', '$1')}", attributes, "ll");
     }
 
+    @Test
+    public void testReplaceShouldReplaceAllLiteralMatches() {
+        int n = 3;
+        final String originalValue = "Hello World";
+        final Map<String, String> attributes = new LinkedHashMap<>();
+        attributes.put("single", originalValue);
+        attributes.put("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<String, String> attributes = new LinkedHashMap<>();
+        attributes.put("single", originalValue);
+        attributes.put("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<String, String> attributes = new LinkedHashMap<>();
+        attributes.put("single", originalValue);
+        attributes.put("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<String, String> attributes = new LinkedHashMap<>();
+        attributes.put("single", originalValue);
+        attributes.put("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<String, String> attributes = new HashMap<>();