exceptionfactory commented on code in PR #6054:
URL: https://github.com/apache/nifi/pull/6054#discussion_r890368647


##########
nifi-commons/nifi-flow-encryptor/src/test/java/org/apache/nifi/flow/encryptor/StandardFlowEncryptorTest.java:
##########
@@ -78,18 +84,110 @@ public void testProcessEncrypted() {
 
     @Test
     public void testProcessNoEncrypted() {
-        final String property = String.format("%s%n", 
StandardFlowEncryptorTest.class.getSimpleName());
+        final String property = String.format("<?xml version=\"1.0\" 
encoding=\"UTF-8\" standalone=\"no\"?>%n" +
+                "<test>%s</test>", 
StandardFlowEncryptorTest.class.getSimpleName());
 
         final InputStream inputStream = new 
ByteArrayInputStream(property.getBytes(StandardCharsets.UTF_8));
         final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 
         flowEncryptor.processFlow(inputStream, outputStream, inputEncryptor, 
outputEncryptor);
 
         final String outputProperty = new String(outputStream.toByteArray());
-        assertEquals(property, outputProperty);
+        assertEquals(removeXmlRootTag(property).trim(), 
removeXmlRootTag(outputProperty).trim());
+    }
+
+    @Test
+    public void testProcessJson() {
+        final String password = 
StandardFlowEncryptorTest.class.getSimpleName();
+        final String encryptedPassword = String.format(ENCRYPTED_FORMAT, 
inputEncryptor.encrypt(password));
+
+        final String sampleFlowJson = getSampleFlowJson(encryptedPassword);
+
+        try (final InputStream inputStream = new 
ByteArrayInputStream(sampleFlowJson.getBytes(StandardCharsets.UTF_8));) {
+            try (final ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream();) {
+                flowEncryptor.processFlow(inputStream, outputStream, 
inputEncryptor, outputEncryptor);
+
+                final String outputFlowJson = new 
String(outputStream.toByteArray());
+
+                compareFlow(sampleFlowJson.trim(), outputFlowJson.trim());
+            }
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
+    }
+
+    @Test
+    public void testProcessXml() {
+        final String password = 
StandardFlowEncryptorTest.class.getSimpleName();
+        final String encryptedPassword = String.format(ENCRYPTED_FORMAT, 
inputEncryptor.encrypt(password));
+        final String sampleFlowXml = getSampleFlowXml(encryptedPassword);
+        try (final InputStream inputStream = new 
ByteArrayInputStream(sampleFlowXml.getBytes(StandardCharsets.UTF_8))) {
+            try (final ByteArrayOutputStream outputStream = new 
ByteArrayOutputStream()) {
+                flowEncryptor.processFlow(inputStream, outputStream, 
inputEncryptor, outputEncryptor);
+                final String outputXml = new 
String(outputStream.toByteArray());
+
+                compareFlow(removeXmlRootTag(sampleFlowXml).trim(), 
removeXmlRootTag(outputXml).trim());
+            }
+        } catch (IOException e) {
+            throw new UncheckedIOException(e);
+        }
     }
 
     private PropertyEncryptor getPropertyEncryptor(final String propertiesKey, 
final String propertiesAlgorithm) {
         return new 
PropertyEncryptorBuilder(propertiesKey).setAlgorithm(propertiesAlgorithm).build();
     }
+
+    private void compareFlow(final String sampleFlow, final String outputFlow) 
{
+        final Matcher inputMatcher = PATTERN.matcher(sampleFlow);
+        final Matcher outputMatcher = PATTERN.matcher(outputFlow);
+        assertTrue(inputMatcher.find() && outputMatcher.find());
+        assertEquals(inputEncryptor.decrypt(inputMatcher.group(1)), 
outputEncryptor.decrypt(outputMatcher.group(1)));
+
+        assertEquals(sampleFlow.replaceAll(PATTERN_REGEX, ""), 
outputFlow.replaceAll(PATTERN_REGEX, ""));
+    }
+
+    private String getSampleFlowJson(final String password) {
+        Objects.requireNonNull(password);
+        return 
String.format("{\"properties\":{\"username\":\"sample_username\",\"password\":\"%s\"}}",
 password);
+    }
+
+    private String getSampleFlowXml(final String password) {
+        Objects.requireNonNull(password);
+        final String flowXml = String.format("<?xml version=\"1.0\" 
encoding=\"UTF-8\" standalone=\"no\"?>%n" +
+                "<processor>%n" +
+                "\t<property>%n" +
+                "\t\t<name>Username</name>%n" +
+                "\t\t<value>SAMPLE_USERNAME</value>%n" +
+                "\t</property>%n" +
+                "\t<property>%n" +
+                "\t\t<name>Password</name>%n" +
+                "\t\t<value>%s</value>%n" +
+                "\t</property>%n" +
+                "</processor>", password);
+
+        return getPlatformSpecificFlowXml(flowXml);
+    }
+
+    private String getPlatformSpecificFlowXml(final String flowXml) {
+        final PropertyEncryptor passthroughEncryptor = new PropertyEncryptor() 
{
+            @Override
+            public String encrypt(String property) {
+                return property;
+            }
+
+            @Override
+            public String decrypt(String encryptedProperty) {
+                return encryptedProperty;
+            }
+        };
+        final InputStream inputStream = new 
ByteArrayInputStream(flowXml.getBytes(StandardCharsets.UTF_8));
+        final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
+        flowEncryptor.processFlow(inputStream, outputStream, 
passthroughEncryptor, passthroughEncryptor);
+        return outputStream.toString();
+    }
+
+    private String removeXmlRootTag(final String xmlFlow) {

Review Comment:
   Recommend renaming this to `removeXmlDeclaration()`, since that is different 
than the root tag. Instead of using a split, it seems better to use a regular 
expression pattern to look for the XML Declaration, something along the lines 
of:
   ```<\\?xml.+?>```



-- 
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