exceptionfactory commented on code in PR #6321: URL: https://github.com/apache/nifi/pull/6321#discussion_r951756610
########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CountTextTest.java: ########## @@ -0,0 +1,316 @@ +/* + * 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.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.util.MockComponentLog; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.Security; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +public class CountTextTest { + private static final String TLC = "text.line.count"; + private static final String TLNEC = "text.line.nonempty.count"; + private static final String TWC = "text.word.count"; + private static final String TCC = "text.character.count"; + + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCountAllMetrics() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(CountText.class); Review Comment: This line could be moved to a shared `setRunner` method annotated with `BeforeEach` to create the runner. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CountTextTest.java: ########## @@ -0,0 +1,316 @@ +/* + * 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.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.util.MockComponentLog; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.Security; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +public class CountTextTest { + private static final String TLC = "text.line.count"; + private static final String TLNEC = "text.line.nonempty.count"; + private static final String TWC = "text.word.count"; + private static final String TCC = "text.character.count"; + + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } Review Comment: This Security Provider configuration method is not necessary and should be removed. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CountTextTest.java: ########## @@ -0,0 +1,316 @@ +/* + * 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.components.PropertyDescriptor; +import org.apache.nifi.flowfile.FlowFile; +import org.apache.nifi.util.MockComponentLog; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.security.Security; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.spy; + +public class CountTextTest { + private static final String TLC = "text.line.count"; + private static final String TLNEC = "text.line.nonempty.count"; + private static final String TWC = "text.word.count"; + private static final String TCC = "text.character.count"; + + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCountAllMetrics() throws IOException { + final TestRunner runner = TestRunners.newTestRunner(CountText.class); + + runner.setProperty(CountText.TEXT_LINE_COUNT_PD, "true"); + runner.setProperty(CountText.TEXT_LINE_NONEMPTY_COUNT_PD, "true"); + runner.setProperty(CountText.TEXT_WORD_COUNT_PD, "true"); + runner.setProperty(CountText.TEXT_CHARACTER_COUNT_PD, "true"); + + final Path inputPath = Paths.get("src/test/resources/TestCountText/jabberwocky.txt"); + + final Map<String, String> expectedValues = new HashMap<>(); + expectedValues.put(TLC, "34"); + expectedValues.put(TLNEC, "28"); + expectedValues.put(TWC, "166"); + expectedValues.put(TCC, "900"); + + runner.enqueue(Files.readAllBytes(inputPath)); + + runner.run(); + + runner.assertAllFlowFilesTransferred(CountText.REL_SUCCESS, 1); + FlowFile flowFile = runner.getFlowFilesForRelationship(CountText.REL_SUCCESS).get(0); + for (final Map.Entry<String, String> entry: expectedValues.entrySet()) { + final String attribute = entry.getKey(); + final String expectedValue = entry.getValue(); + + assertEquals(expectedValue, flowFile.getAttribute(attribute)); + } Review Comment: This approach should be replaced with `MockFlowFile.assertAttributeEquals()`, which would also remove the need for the `Map` of expected values. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashAttributeTest.java: ########## @@ -0,0 +1,343 @@ +/* + * 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.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CryptographicHashAttributeTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentAttribute() { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); Review Comment: Declaring this local variable is unnecessary. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashContentTest.java: ########## @@ -0,0 +1,246 @@ +/* + * 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.lang3.StringUtils; +import org.apache.nifi.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class CryptographicHashContentTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentContent() throws IOException { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); + + // Generate some long content (90 KB) + final String LONG_CONTENT = StringUtils.repeat("apachenifi ", 8192); Review Comment: This variable should be lowercased to follow standard conventions. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashAttributeTest.java: ########## @@ -0,0 +1,343 @@ +/* + * 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.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CryptographicHashAttributeTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentAttribute() { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); + + final TestRunner runner = TestRunners.newTestRunner(new CryptographicHashAttribute()); Review Comment: This should be moved to a `setRunner()` method annotated with `BeforeEach`. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/SplitXmlTest.java: ########## @@ -0,0 +1,50 @@ +/* + * 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.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.file.Paths; + +public class SplitXmlTest { + @Test + void testShouldHandleXXEInTemplate() throws IOException { + final String XXE_TEMPLATE_FILEPATH = "src/test/resources/xxe_template.xml"; + final TestRunner runner = TestRunners.newTestRunner(new SplitXml()); + runner.setProperty(SplitXml.SPLIT_DEPTH, "3"); + runner.enqueue(Paths.get(XXE_TEMPLATE_FILEPATH)); + + runner.run(); + + runner.assertAllFlowFilesTransferred(SplitXml.REL_FAILURE); Review Comment: Recommend moving this shared code to a new private method named something like `assertExternalEntitiesFailure()` and changing both test methods to call the shared method. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/TestFlattenJson.java: ########## @@ -0,0 +1,520 @@ +/* + * 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 com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.junit.jupiter.api.Test; + +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertInstanceOf; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestFlattenJson { + private static final ObjectMapper mapper = new ObjectMapper(); + + @Test + void testFlatten() throws JsonProcessingException { + final TestRunner testRunner = TestRunners.newTestRunner(FlattenJson.class); + final String json = "{\n" + + " \"test\": {\n" + + " \"msg\": \"Hello, world\"\n" + + " },\n" + + " \"first\": {\n" + + " \"second\": {\n" + + " \"third\": [\n" + + " \"one\",\n" + + " \"two\",\n" + + " \"three\",\n" + + " \"four\",\n" + + " \"five\"\n" + + " ]\n" + + " }\n" + + " }\n" + + "}"; + final Map parsed = (Map) baseTest(testRunner, json, 2); + assertEquals(parsed.get("test.msg"), "Hello, world", "test.msg should exist, but doesn't"); + assertEquals(parsed.get("first.second.third"), + Arrays.asList("one", "two", "three", "four", "five"), + "Three level block doesn't exist."); + } + + private Object baseTest(TestRunner testRunner, String json, int keyCount) throws JsonProcessingException { + return baseTest(testRunner, json, Collections.emptyMap(), keyCount); + } + + private Object baseTest(TestRunner testRunner, String json, Map attrs, int keyCount) throws JsonProcessingException { + testRunner.enqueue(json, attrs); + testRunner.run(1, true); + testRunner.assertTransferCount(FlattenJson.REL_FAILURE, 0); + testRunner.assertTransferCount(FlattenJson.REL_SUCCESS, 1); + + final List<MockFlowFile> flowFiles = testRunner.getFlowFilesForRelationship(FlattenJson.REL_SUCCESS); + final byte[] content = testRunner.getContentAsByteArray(flowFiles.get(0)); + final String asJson = new String(content); + if (asJson.startsWith("[")) { + final List parsed; + parsed = mapper.readValue(asJson, List.class); + assertEquals(keyCount, parsed.size(), "Too many keys"); + return parsed; + } else { + final Map parsed; + parsed = mapper.readValue(asJson, Map.class); + assertEquals(keyCount, parsed.size(), "Too many keys"); + return parsed; + } + } Review Comment: All private methods should be moved to the end of the class, following standard conventions. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashAttributeTest.java: ########## @@ -0,0 +1,343 @@ +/* + * 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.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CryptographicHashAttributeTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentAttribute() { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); + + final TestRunner runner = TestRunners.newTestRunner(new CryptographicHashAttribute()); + + // Create attributes for username and date + + final Map<String, String> attributes = new HashMap<>(); + attributes.put("username", "alopresto"); + attributes.put("date", ZonedDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSS Z"))); + + final Set<String> attributeKeys = attributes.keySet(); + + for (final HashAlgorithm algorithm : algorithms) { Review Comment: ```suggestion for (final HashAlgorithm algorithm : HashAlgorithm.values()) { ``` ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashContentTest.java: ########## @@ -0,0 +1,246 @@ +/* + * 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.lang3.StringUtils; +import org.apache.nifi.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.util.Collections; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotEquals; + +public class CryptographicHashContentTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentContent() throws IOException { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); + + // Generate some long content (90 KB) + final String LONG_CONTENT = StringUtils.repeat("apachenifi ", 8192); + + final TestRunner runner = TestRunners.newTestRunner(new CryptographicHashContent()); Review Comment: Should be moved to a standard `BeforeEach` annotated method. ########## nifi-nar-bundles/nifi-standard-bundle/nifi-standard-processors/src/test/java/org/apache/nifi/processors/standard/CryptographicHashAttributeTest.java: ########## @@ -0,0 +1,343 @@ +/* + * 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.security.util.crypto.HashAlgorithm; +import org.apache.nifi.security.util.crypto.HashService; +import org.apache.nifi.util.MockFlowFile; +import org.apache.nifi.util.TestRunner; +import org.apache.nifi.util.TestRunners; +import org.bouncycastle.jce.provider.BouncyCastleProvider; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.security.Security; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class CryptographicHashAttributeTest { + @BeforeAll + static void setUpOnce() { + Security.addProvider(new BouncyCastleProvider()); + } + + @Test + void testShouldCalculateHashOfPresentAttribute() { + final HashAlgorithm[] algorithms = HashAlgorithm.values(); + + final TestRunner runner = TestRunners.newTestRunner(new CryptographicHashAttribute()); + + // Create attributes for username and date + + final Map<String, String> attributes = new HashMap<>(); + attributes.put("username", "alopresto"); + attributes.put("date", ZonedDateTime.now().format(DateTimeFormatter.ofPattern("YYYY-MM-dd HH:mm:ss.SSS Z"))); + + final Set<String> attributeKeys = attributes.keySet(); + + for (final HashAlgorithm algorithm : algorithms) { + final String EXPECTED_USERNAME_HASH = HashService.hashValue(algorithm, attributes.get("username")); + final String EXPECTED_DATE_HASH = HashService.hashValue(algorithm, attributes.get("date")); Review Comment: These variable names should be lowercased following standard conventions for local variables. -- 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]
