From 8eb54a50193897cf564eb7d222aae35481168af4 Mon Sep 17 00:00:00 2001 From: Joe Trite Date: Wed, 22 Feb 2017 16:46:13 -0500 Subject: [PATCH] NIFI-3497 test cases for metadata updates Adding test cases to support metadata option update. --- .../standard/TestScanAttribute.java | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) diff --git a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestScanAttribute.java b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestScanAttribute.java index b4a41369f2..53aecf357c 100644 --- a/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestScanAttribute.java +++ b/nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestScanAttribute.java @@ -16,9 +16,12 @@ */ package org.apache.nifi.processors.standard; +import static org.junit.Assert.*; + import java.util.HashMap; import java.util.Map; +import org.apache.nifi.flowfile.FlowFile; import org.apache.nifi.util.TestRunner; import org.apache.nifi.util.TestRunners; @@ -145,4 +148,189 @@ public class TestScanAttribute { runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); runner.clearTransferState(); } + + @Test + public void testSingleMatchWithMeta() { + final TestRunner runner = TestRunners.newTestRunner(new ScanAttribute()); + FlowFile f; + + runner.setProperty(ScanAttribute.DICTIONARY_FILE, "src/test/resources/ScanAttribute/dictionary1_meta"); + runner.setProperty(ScanAttribute.DICTIONARY_ENTRY_METADATA_DEMARCATOR, ":"); + + final Map attributes = new HashMap<>(); + attributes.put("produce_name", "banana"); + + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"banana"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "yellow fruit"); + + runner.clearTransferState(); + + attributes.remove("produce_name"); + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + runner.clearTransferState(); + + attributes.put("produce_name", "cherry"); + runner.setProperty(ScanAttribute.ATTRIBUTE_PATTERN, "pro.*"); + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"cherry"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "red fruit"); + runner.clearTransferState(); + + runner.setProperty(ScanAttribute.ATTRIBUTE_PATTERN, "c.*"); + runner.enqueue(new byte[0], attributes); + runner.run(); + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + runner.clearTransferState(); + + } + + @Test + public void testAllMatchWithMeta() { + final TestRunner runner = TestRunners.newTestRunner(new ScanAttribute()); + FlowFile f; + + runner.setProperty(ScanAttribute.DICTIONARY_FILE, "src/test/resources/ScanAttribute/dictionary1_meta"); + runner.setProperty(ScanAttribute.MATCHING_CRITERIA, ScanAttribute.MATCH_CRITERIA_ALL); + runner.setProperty(ScanAttribute.ATTRIBUTE_PATTERN, "pro.*"); + runner.setProperty(ScanAttribute.DICTIONARY_ENTRY_METADATA_DEMARCATOR, ":"); + + final Map attributes = new HashMap<>(); + attributes.put("produce_name", "banana"); + + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"banana"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "yellow fruit"); + + runner.clearTransferState(); + + attributes.remove("produce_name"); + runner.enqueue(new byte[0], attributes); + runner.run(); + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + runner.clearTransferState(); + + attributes.put("produce_name", "banana"); + attributes.put("produce_name_2", "french fries"); + runner.enqueue(new byte[0], attributes); + runner.run(); + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + runner.clearTransferState(); + + attributes.put("produce_name", "corn"); + attributes.put("produce_name_2", "banana"); + runner.enqueue(new byte[0], attributes); + runner.run(); + + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"banana"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "yellow fruit"); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.2.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.2.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.2.term") ,"corn"); + assertEquals(f.getAttribute("dictionary.hit.2.metadata"), "yellow vegetable"); + } + + @Test + public void testWithEmptyEntriesWithMeta() { + final TestRunner runner = TestRunners.newTestRunner(new ScanAttribute()); + runner.setProperty(ScanAttribute.DICTIONARY_FILE, "src/test/resources/ScanAttribute/dictionary-with-empty-new-lines_meta"); + runner.setProperty(ScanAttribute.DICTIONARY_ENTRY_METADATA_DEMARCATOR, ":"); + + final Map attributes = new HashMap<>(); + attributes.put("produce_name", ""); + + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + runner.clearTransferState(); + + runner.setProperty(ScanAttribute.ATTRIBUTE_PATTERN, "pro.*"); + runner.enqueue(new byte[0], attributes); + runner.run(); + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + } + + @Test + public void testWithDictionaryFilterWithMeta() { + final TestRunner runner = TestRunners.newTestRunner(new ScanAttribute()); + FlowFile f; + + runner.setProperty(ScanAttribute.DICTIONARY_FILE, "src/test/resources/ScanAttribute/dictionary-with-extra-info_meta"); + runner.setProperty(ScanAttribute.DICTIONARY_FILTER, "(.*)"); + runner.setProperty(ScanAttribute.DICTIONARY_ENTRY_METADATA_DEMARCATOR, ":"); + + final Map attributes = new HashMap<>(); + attributes.put("produce_name", "banana"); + + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"banana"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "yellow fruit"); + runner.clearTransferState(); + + attributes.put("produce_name", "tomatoe"); + runner.enqueue(new byte[0], attributes); + runner.run(); + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_UNMATCHED, 1); + runner.clearTransferState(); + + runner.setProperty(ScanAttribute.DICTIONARY_FILTER, "(.*)<.*>"); + runner.enqueue(new byte[0], attributes); + runner.run(); + + runner.assertAllFlowFilesTransferred(ScanAttribute.REL_MATCHED, 1); + f = runner.getFlowFilesForRelationship(ScanAttribute.REL_MATCHED).get(0); + + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.term"); + runner.assertAllFlowFilesContainAttribute("dictionary.hit.1.metadata"); + + assertEquals(f.getAttribute("dictionary.hit.1.term") ,"tomatoe"); + assertEquals(f.getAttribute("dictionary.hit.1.metadata"), "red vegetable"); + + + runner.clearTransferState(); + } }