You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by alopresto <gi...@git.apache.org> on 2018/08/31 01:12:49 UTC
[GitHub] nifi pull request #2836: NIFI-5147 Calculate hash attribute redux
Github user alopresto commented on a diff in the pull request:
https://github.com/apache/nifi/pull/2836#discussion_r214222959
--- Diff: nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestCalculateAttributeHash.java ---
@@ -0,0 +1,178 @@
+/*
+ * 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.commons.codec.binary.Hex;
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.nifi.util.MockFlowFile;
+import org.apache.nifi.util.TestRunner;
+import org.apache.nifi.util.TestRunners;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+public class TestCalculateAttributeHash {
+
+ private static final Charset UTF8 = StandardCharsets.UTF_8;
+ @Test
+ public void testMD2() throws Exception {
+ testAllAlgorithm("MD2");
+ testParitalAlgorithm("MD2");
+ testMissingAlgorithm("MD2");
+ }
+
+ @Test
+ public void testMD5() throws Exception {
+ testAllAlgorithm("MD5");
+ testParitalAlgorithm("MD5");
+ testMissingAlgorithm("MD5");
+ }
+
+ @Test
+ public void testSHA1() throws Exception {
+ testAllAlgorithm("SHA-1");
+ testParitalAlgorithm("SHA-1");
+ testMissingAlgorithm("SHA-1");
+ }
+
+ @Test
+ public void testSHA256() throws Exception {
+ testAllAlgorithm("SHA-256");
+ testParitalAlgorithm("SHA-256");
+ testMissingAlgorithm("SHA-256");
+ }
+
+ @Test
+ public void testSHA384() throws Exception {
+ testAllAlgorithm("SHA-384");
+ testParitalAlgorithm("SHA-384");
+ testMissingAlgorithm("SHA-384");
+ }
+
+ @Test
+ public void testSHA512() throws Exception {
+ testAllAlgorithm("SHA-512");
+ testParitalAlgorithm("SHA-512");
+ testMissingAlgorithm("SHA-512");
+ }
+
+ public void testAllAlgorithm(String algorithm) {
+ final TestRunner runner = TestRunners.newTestRunner(new CalculateAttributeHash());
+ runner.setProperty(CalculateAttributeHash.HASH_ALGORITHM.getName(), algorithm);
+ runner.setProperty("name", String.format("%s_%s", "name", algorithm));
+ runner.setProperty("value", String.format("%s_%s", "value", algorithm));
+
+ final Map<String, String> attributeMap = new HashMap<>();
+ attributeMap.put("name", "abcdefg");
+ attributeMap.put("value", "hijklmnop");
+ runner.enqueue(new byte[0], attributeMap);
+
+ runner.run(1);
+
+ runner.assertTransferCount(HashAttribute.REL_FAILURE, 0);
+ runner.assertTransferCount(HashAttribute.REL_SUCCESS, 1);
+
+ final List<MockFlowFile> success = runner.getFlowFilesForRelationship(HashAttribute.REL_SUCCESS);
+
+ for (final MockFlowFile flowFile : success) {
+ Assert.assertEquals(Hex.encodeHexString(DigestUtils.getDigest(algorithm).digest("abcdefg".getBytes(UTF8))),
+ flowFile.getAttribute(String.format("%s_%s", "name", algorithm)));
+ Assert.assertEquals(Hex.encodeHexString(DigestUtils.getDigest(algorithm).digest("hijklmnop".getBytes(UTF8))),
+ flowFile.getAttribute(String.format("%s_%s", "value", algorithm)));
+ }
+ }
+
+ public void testParitalAlgorithm(String algorithm) {
--- End diff --
Typo: `testPartialAlgorithm`
---