MikeThomsen commented on a change in pull request #4646:
URL: https://github.com/apache/nifi/pull/4646#discussion_r817742148



##########
File path: 
nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestDeduplicateRecords.java
##########
@@ -0,0 +1,318 @@
+/*
+ * 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.processors.standard;
+
+import org.apache.nifi.controller.AbstractControllerService;
+import org.apache.nifi.distributed.cache.client.Deserializer;
+import org.apache.nifi.distributed.cache.client.DistributedMapCacheClient;
+import org.apache.nifi.distributed.cache.client.Serializer;
+import org.apache.nifi.reporting.InitializationException;
+import org.apache.nifi.serialization.record.MockRecordParser;
+import org.apache.nifi.serialization.record.MockRecordWriter;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.junit.Assert.assertEquals;
+
+public class TestDeduplicateRecords {
+
+    private TestRunner runner;
+    private MockRecordParser reader;
+    private MockRecordWriter writer;
+
+    @BeforeClass
+    public static void beforeClass() {
+        System.setProperty("org.slf4j.simpleLogger.defaultLogLevel", "info");
+        System.setProperty("org.slf4j.simpleLogger.showDateTime", "true");
+        System.setProperty("org.slf4j.simpleLogger.log.nifi.io.nio", "debug");
+        
System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.DeduplicateRecords",
 "debug");
+        
System.setProperty("org.slf4j.simpleLogger.log.nifi.processors.standard.TestDeduplicateRecords",
 "debug");
+    }
+
+    @Before
+    public void setup() throws InitializationException {
+        runner = TestRunners.newTestRunner(DeduplicateRecords.class);
+
+        // RECORD_READER, RECORD_WRITER
+        reader = new MockRecordParser();
+        writer = new MockRecordWriter("header", false);
+
+        runner.addControllerService("reader", reader);
+        runner.enableControllerService(reader);
+        runner.addControllerService("writer", writer);
+        runner.enableControllerService(writer);
+
+        runner.setProperty(DeduplicateRecords.RECORD_READER, "reader");
+        runner.setProperty(DeduplicateRecords.RECORD_WRITER, "writer");
+        runner.setProperty(DeduplicateRecords.RECORD_HASHING_ALGORITHM, 
DeduplicateRecords.SHA1_ALGORITHM_VALUE);
+
+        reader.addSchemaField("firstName", RecordFieldType.STRING);
+        reader.addSchemaField("middleName", RecordFieldType.STRING);
+        reader.addSchemaField("lastName", RecordFieldType.STRING);
+
+        // INCLUDE_ZERO_RECORD_FLOWFILES
+        runner.setProperty(DeduplicateRecords.INCLUDE_ZERO_RECORD_FLOWFILES, 
"true");
+
+        runner.assertValid();
+    }
+
+    void commonEnqueue() {
+        final Map<String, String> props = new HashMap<>();
+        props.put("hash.value", "1000");
+        runner.enqueue(new byte[]{}, props);
+    }
+
+    @Test
+    public void testDetectDuplicatesHashSet() {
+        commonEnqueue();
+
+        runner.setProperty(DeduplicateRecords.FILTER_TYPE, 
DeduplicateRecords.HASH_SET_VALUE);
+        runner.setProperty("/middleName", "${field.value}");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 2, 1);
+    }
+
+    @Test
+    public void testDetectDuplicatesBloomFilter() {
+        commonEnqueue();
+        runner.setProperty(DeduplicateRecords.FILTER_TYPE, 
DeduplicateRecords.BLOOM_FILTER_VALUE);
+        runner.setProperty(DeduplicateRecords.BLOOM_FILTER_FPP, "0.10");
+        runner.setProperty("/middleName", "${field.value}");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 2, 1);
+    }
+
+    @Test
+    public void testNoDuplicatesHashSet() {
+        commonEnqueue();
+        runner.setProperty(DeduplicateRecords.FILTER_TYPE, 
DeduplicateRecords.HASH_SET_VALUE);
+        runner.setProperty("/middleName", "${field.value}");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 3, 0);
+    }
+
+    @Test
+    public void testNoDuplicatesBloomFilter() {
+        commonEnqueue();
+        runner.setProperty(DeduplicateRecords.FILTER_TYPE, 
DeduplicateRecords.BLOOM_FILTER_VALUE);
+        runner.setProperty(DeduplicateRecords.BLOOM_FILTER_FPP, "0.10");
+        runner.setProperty("/middleName", "${field.value}");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 3, 0);
+    }
+
+    @Test
+    public void testAllDuplicates() {
+        commonEnqueue();
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("John", "Q", "Smith");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 1, 2);
+    }
+
+    @Test
+    public void testAllUnique() {
+        commonEnqueue();
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 3, 0);
+    }
+
+    @Test
+    public void testCacheValueFromRecordPath() {
+        commonEnqueue();
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jack", "Z", "Brown");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 2, 1);
+    }
+
+    /*
+     * These are all related to NIFI-6014
+     */
+
+    @Test
+    public void testMultipleFileDeduplicationRequiresDMC() {
+        runner.setProperty(DeduplicateRecords.DEDUPLICATION_STRATEGY, 
DeduplicateRecords.OPTION_MULTIPLE_FILES.getValue());
+        runner.assertNotValid();
+    }
+
+    public static final String FIRST_KEY = 
"2875ba79836587028a920875a18ee5dceb837587";
+    public static final String SECOND_KEY = 
"6eeba6ecf9d263582f463890be339dbecbaf23c8";
+
+    @Test
+    public void testDeduplicateWithDMC() throws Exception {
+        DistributedMapCacheClient dmc = new MockCacheService<>();
+        runner.addControllerService("dmc", dmc);
+        runner.setProperty(DeduplicateRecords.DISTRIBUTED_MAP_CACHE, "dmc");
+        runner.setProperty(DeduplicateRecords.DEDUPLICATION_STRATEGY, 
DeduplicateRecords.OPTION_MULTIPLE_FILES.getValue());
+        runner.enableControllerService(dmc);
+        runner.assertValid();
+
+        dmc.put(FIRST_KEY, true, null, null);
+        dmc.put(SECOND_KEY, true, null, null);
+
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jane", "X", "Doe");
+
+        runner.enqueue("");
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 1, 3);
+    }
+
+    @Test
+    public void testDeduplicateWithDMCAndCacheIdentifier() throws Exception {
+        DistributedMapCacheClient dmc = new MockCacheService<>();
+        runner.addControllerService("dmc", dmc);
+        runner.setProperty(DeduplicateRecords.DISTRIBUTED_MAP_CACHE, "dmc");
+        runner.setProperty(DeduplicateRecords.DEDUPLICATION_STRATEGY, 
DeduplicateRecords.OPTION_MULTIPLE_FILES.getValue());
+        runner.setProperty(DeduplicateRecords.CACHE_IDENTIFIER, 
"concat('${user.name}', '${record.hash.value}')");
+        runner.enableControllerService(dmc);
+        runner.assertValid();
+
+        dmc.put(String.format("john.smith-%s", FIRST_KEY), true, null, null);
+        dmc.put(String.format("john.smith-%s", SECOND_KEY), true, null, null);
+
+        reader.addRecord("John", "Q", "Smith");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jack", "Z", "Brown");
+        reader.addRecord("Jane", "X", "Doe");
+
+        Map<String, String> attrs = new HashMap<>();
+        attrs.put("user.name", "john.smith-");
+
+        runner.enqueue("", attrs);
+        runner.run();
+
+        doCountTests(0, 1, 1, 1, 1, 3);
+    }
+
+    void doCountTests(int failure, int original, int duplicates, int 
notDuplicates, int notDupeCount, int dupeCount) {
+        runner.assertTransferCount(DeduplicateRecords.REL_DUPLICATE, 
duplicates);
+        runner.assertTransferCount(DeduplicateRecords.REL_NON_DUPLICATE, 
notDuplicates);
+        runner.assertTransferCount(DeduplicateRecords.REL_ORIGINAL, original);
+        runner.assertTransferCount(DeduplicateRecords.REL_FAILURE, failure);
+
+        List<MockFlowFile> duplicateFlowFile = 
runner.getFlowFilesForRelationship(DeduplicateRecords.REL_DUPLICATE);
+        if (duplicateFlowFile != null) {
+            assertEquals(String.valueOf(dupeCount), 
duplicateFlowFile.get(0).getAttribute("record.count"));
+        }
+
+        List<MockFlowFile> nonDuplicateFlowFile = 
runner.getFlowFilesForRelationship(DeduplicateRecords.REL_NON_DUPLICATE);
+        if (nonDuplicateFlowFile != null) {
+            assertEquals(String.valueOf(notDupeCount), 
nonDuplicateFlowFile.get(0).getAttribute("record.count"));
+        }
+    }
+
+    private static final class MockCacheService<K, V> extends 
AbstractControllerService implements DistributedMapCacheClient {

Review comment:
       I'll add one, but it should only show the transaction rolling back and 
the flowfile going to failure.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to