From 523b7e2c613db55fc13331c9cfd071804ec9cb3f Mon Sep 17 00:00:00 2001
From: svacas <svacas@gmail.com>
Date: Tue, 7 Jun 2022 14:33:24 -0300
Subject: [PATCH] Sanitize sub-flow names (#656)

---
 .../batch1/input/src/main/app/mule-config.xml  |  4 ++--
 .../e2e/batch/batch1/output/report/report.json |  8 ++++++--
 .../output/src/main/mule/mule-config.xml       |  4 ++--
 .../e2e/batch/batch2/output/report/report.json |  8 ++++++--
 .../output/report/report.json                  |  8 ++++++--
 .../library/mule/steps/core/SubFlow.java       | 18 +++++++++++-------
 .../tasks/MuleCoreComponentsMigrationTask.java |  2 ++
 7 files changed, 35 insertions(+), 17 deletions(-)

diff --git a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/input/src/main/app/mule-config.xml b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/input/src/main/app/mule-config.xml
index d6f74eef7..1486d5a39 100644
--- a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/input/src/main/app/mule-config.xml
+++ b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/input/src/main/app/mule-config.xml
@@ -23,7 +23,7 @@
                 </batch:commit>
             </batch:step>
             <batch:step name="secondStep">
-                <flow-ref name="upperCasePipe"/>
+                <flow-ref name="/upperCasePipe"/>
                 <logger message="something"/>
             </batch:step>
         </batch:process-records>
@@ -32,7 +32,7 @@
         </batch:on-complete>
     </batch:job>
 
-    <sub-flow name="upperCasePipe">
+    <sub-flow name="/upperCasePipe">
         <set-payload value="#[payload + '|' + payload.toUpperCase()]"/>
     </sub-flow>
 
diff --git a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/report/report.json b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/report/report.json
index 0a2a9ec5d..eb134b5d4 100644
--- a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/report/report.json
+++ b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/report/report.json
@@ -2,8 +2,8 @@
   "projectType": "MULE_THREE_APPLICATION",
   "projectName": "input",
   "connectorsMigrated": [],
-  "numberOfMuleComponents": 15,
-  "numberOfMuleComponentsMigrated": 15,
+  "numberOfMuleComponents": 16,
+  "numberOfMuleComponentsMigrated": 16,
   "componentDetails": {
     "mule": {
       "success": 1,
@@ -17,6 +17,10 @@
       "success": 2,
       "failure": 0
     },
+    "sub-flow": {
+      "success": 1,
+      "failure": 0
+    },
     "flow-ref": {
       "success": 2,
       "failure": 0
diff --git a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/src/main/mule/mule-config.xml b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/src/main/mule/mule-config.xml
index fa9339502..d1a7b2600 100644
--- a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/src/main/mule/mule-config.xml
+++ b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch1/output/src/main/mule/mule-config.xml
@@ -22,7 +22,7 @@
                     </batch:aggregator>
                 </batch:step>
                 <batch:step name="secondStep">
-                    <flow-ref name="upperCasePipe" />
+                    <flow-ref name="\upperCasePipe" />
                     <logger message="something" />
                 </batch:step>
             </batch:process-records>
@@ -33,7 +33,7 @@
 
     </flow>
 
-    <sub-flow name="upperCasePipe">
+    <sub-flow name="\upperCasePipe">
         <set-payload value="#[mel:payload + '|' + payload.toUpperCase()]">
             <!--Migration WARN: The MEL expression could not be migrated to a DataWeave expression.-->
             <!--    For more information refer to:-->
diff --git a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch2/output/report/report.json b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch2/output/report/report.json
index dc7633e7a..74895fe6e 100644
--- a/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch2/output/report/report.json
+++ b/mule-migration-tool-e2e-tests/src/test/resources/e2e/batch/batch2/output/report/report.json
@@ -2,8 +2,8 @@
   "projectType": "MULE_THREE_APPLICATION",
   "projectName": "input",
   "connectorsMigrated": [],
-  "numberOfMuleComponents": 28,
-  "numberOfMuleComponentsMigrated": 28,
+  "numberOfMuleComponents": 29,
+  "numberOfMuleComponentsMigrated": 29,
   "componentDetails": {
     "mule": {
       "success": 1,
@@ -17,6 +17,10 @@
       "success": 4,
       "failure": 0
     },
+    "sub-flow": {
+      "success": 1,
+      "failure": 0
+    },
     "flow-ref": {
       "success": 2,
       "failure": 0
diff --git a/mule-migration-tool-e2e-tests/src/test/resources/e2e/policy/client_id_enforcement_policy/output/report/report.json b/mule-migration-tool-e2e-tests/src/test/resources/e2e/policy/client_id_enforcement_policy/output/report/report.json
index f948cb6d8..97b4b58ee 100644
--- a/mule-migration-tool-e2e-tests/src/test/resources/e2e/policy/client_id_enforcement_policy/output/report/report.json
+++ b/mule-migration-tool-e2e-tests/src/test/resources/e2e/policy/client_id_enforcement_policy/output/report/report.json
@@ -2,9 +2,13 @@
   "projectType": "MULE_THREE_POLICY",
   "projectName": "input",
   "connectorsMigrated": [],
-  "numberOfMuleComponents": 11,
-  "numberOfMuleComponentsMigrated": 11,
+  "numberOfMuleComponents": 12,
+  "numberOfMuleComponentsMigrated": 12,
   "componentDetails": {
+    "mule:sub-flow": {
+      "success": 1,
+      "failure": 0
+    },
     "mule:set-payload": {
       "success": 2,
       "failure": 0
diff --git a/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/steps/core/SubFlow.java b/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/steps/core/SubFlow.java
index dbbfee778..e7032b66d 100644
--- a/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/steps/core/SubFlow.java
+++ b/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/steps/core/SubFlow.java
@@ -5,6 +5,9 @@
  */
 package com.mulesoft.tools.migration.library.mule.steps.core;
 
+import static com.mulesoft.tools.migration.library.mule.steps.core.Flow.migrateFlowName;
+import static com.mulesoft.tools.migration.step.util.XmlDslUtils.getTopLevelCoreXPathSelector;
+
 import com.mulesoft.tools.migration.step.AbstractApplicationModelMigrationStep;
 import com.mulesoft.tools.migration.step.category.MigrationReport;
 
@@ -18,7 +21,7 @@
  */
 public class SubFlow extends AbstractApplicationModelMigrationStep {
 
-  public static final String XPATH_SELECTOR = "/*/mule:sub-flow";
+  public static final String XPATH_SELECTOR = getTopLevelCoreXPathSelector("sub-flow");
 
   @Override
   public String getDescription() {
@@ -31,12 +34,13 @@ public SubFlow() {
 
   @Override
   public void execute(Element element, MigrationReport report) throws RuntimeException {
-    element.setAttribute("name", element.getAttributeValue("name")
-        .replaceAll("\\/", "\\\\")
-        .replaceAll("\\[|\\{", "(")
-        .replaceAll("\\]|\\}", ")")
-        .replaceAll("#", "_"));
-  }
+    String name = element.getAttributeValue("name");
 
+    // do not replace special chars for sub-flows in policy templates
+    if (!name.matches(".*\\{\\{\\w+}}.*")) {
+      element.setAttribute("name", migrateFlowName(name));
+    }
+
+  }
 
 }
diff --git a/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/tasks/MuleCoreComponentsMigrationTask.java b/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/tasks/MuleCoreComponentsMigrationTask.java
index 7341fac22..5d523ac66 100644
--- a/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/tasks/MuleCoreComponentsMigrationTask.java
+++ b/mule-migration-tool-library/src/main/java/com/mulesoft/tools/migration/library/mule/tasks/MuleCoreComponentsMigrationTask.java
@@ -36,6 +36,7 @@
 import com.mulesoft.tools.migration.library.mule.steps.core.SetPayload;
 import com.mulesoft.tools.migration.library.mule.steps.core.SetVariable;
 import com.mulesoft.tools.migration.library.mule.steps.core.SpringImport;
+import com.mulesoft.tools.migration.library.mule.steps.core.SubFlow;
 import com.mulesoft.tools.migration.library.mule.steps.core.TransactionalScope;
 import com.mulesoft.tools.migration.library.mule.steps.core.UntilSuccessful;
 import com.mulesoft.tools.migration.library.mule.steps.ee.CacheHttpCachingStrategy;
@@ -88,6 +89,7 @@ public List<MigrationStep> getSteps() {
                         new ScatterGather(),
                         new Enricher(),
                         new Flow(),
+                        new SubFlow(),
                         new FlowRef(),
                         new CompositeSource(),
                         new Async(),