Kaynağa Gözat

#IB0SJ1 补充单元测试和优化代码

jay li 5 ay önce
ebeveyn
işleme
dba75efc3e

+ 47 - 100
liteflow-core/src/main/java/com/yomahub/liteflow/flow/instanceId/BaseNodeInstanceIdManageSpi.java

@@ -56,7 +56,7 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
 
         List<Condition> conditionList = chain.getConditionList();
 
-        return getNodeFromConditions(conditionList, nodeId, index, new HashMap<>());
+        return getNodeFromConditions(conditionList, nodeId, index);
     }
 
 
@@ -76,7 +76,7 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
             List<InstanceInfoDto> instanceInfos = parseList(instanceIdFile.get(i), InstanceInfoDto.class);
 
             for (InstanceInfoDto dto : instanceInfos) {
-                if (Objects.equals(dto.getInstanceId(), nodeId)) {
+                if (Objects.equals(dto.getNodeId(), nodeId)) {
                     instanceIds.add(dto.getInstanceId());
                 }
             }
@@ -95,25 +95,11 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
         }
 
         for (Condition condition : conditionList) {
-            List<Executable> executableList = condition.getExecutableList();
-            for (Executable executable : executableList) {
-                if (executable instanceof Node) {
-                    Node node = (Node) executable;
-                    if (Objects.equals(node.getInstanceId(), instanceId)) {
-                        return node;
-                    }
-                } else if (executable instanceof Condition) {
-                    Condition conditionTmp = (Condition) executable;
-                    List<Node> allNodeInCondition = conditionTmp.getAllNodeInCondition();
-
-                    for (Node node : allNodeInCondition) {
-                        if (Objects.equals(node.getInstanceId(), instanceId)) {
-                            return node;
-                        }
-                    }
-                } else if (executable instanceof Chain) {
-                    Chain chainTmp = (Chain) executable;
-                    return getNodeFromConditions(chainTmp.getConditionList(), instanceId);
+            List<Node> allNodeInCondition = condition.getAllNodeInCondition();
+
+            for (Node node : allNodeInCondition) {
+                if (Objects.equals(node.getInstanceId(), instanceId)) {
+                    return node;
                 }
             }
         }
@@ -123,34 +109,19 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
     /**
      * 根据nodeId和index获取node节点
      */
-    private Node getNodeFromConditions(List<Condition> conditionList, String nodeId,
-                                       Integer index, HashMap<String, Integer> idCntMap) {
+    private Node getNodeFromConditions(List<Condition> conditionList, String nodeId, Integer index) {
         if (CollUtil.isEmpty(conditionList)) {
             return null;
         }
 
+        Map<String, Integer> idCntMap = new HashMap<>();
         for (Condition condition : conditionList) {
-            List<Executable> executableList = condition.getExecutableList();
-            for (Executable executable : executableList) {
-                if (executable instanceof Node) {
-                    Node node = (Node) executable;
-                    idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
-                    if (Objects.equals(node.getId(), nodeId) && Objects.equals(idCntMap.get(node.getId()), index)) {
-                        return node;
-                    }
-                } else if (executable instanceof Condition) {
-                    Condition conditionTmp = (Condition) executable;
-                    List<Node> allNodeInCondition = conditionTmp.getAllNodeInCondition();
-
-                    for (Node node : allNodeInCondition) {
-                        idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
-                        if (Objects.equals(node.getId(), nodeId) && Objects.equals(idCntMap.get(node.getId()), index)) {
-                            return node;
-                        }
-                    }
-                } else if (executable instanceof Chain) {
-                    Chain chainTmp = (Chain) executable;
-                    return getNodeFromConditions(chainTmp.getConditionList(), nodeId, index, new HashMap<>());
+            List<Node> allNodeInCondition = condition.getAllNodeInCondition();
+
+            for (Node node : allNodeInCondition) {
+                idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
+                if (Objects.equals(node.getId(), nodeId) && Objects.equals(idCntMap.get(node.getId()), index)) {
+                    return node;
                 }
             }
         }
@@ -203,45 +174,31 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
                 instanceInfos = parseList(instanceIdFile.get(i), InstanceInfoDto.class);
             }
             List<InstanceInfoDto> finalInstanceInfos = instanceInfos;
-            Map<String, Integer> idCntMap = new HashMap<>();
 
-            setInstanceIdFromFile(finalInstanceInfos, chainId, condition.getExecutableGroup(), idCntMap);
+            setInstanceIdFromFile(finalInstanceInfos, chainId, condition.getAllNodeInCondition());
         }
     }
 
     /**
      * 从instanceIdFile里设置instanceId
      */
-    private void setInstanceIdFromFile(List<InstanceInfoDto> finalInstanceInfos, String chainId,
-                                       Map<String, List<Executable>> executableGroup, Map<String, Integer> idCntMap) {
-        if (CollUtil.isEmpty(executableGroup)) {
+    private void setInstanceIdFromFile(List<InstanceInfoDto> finalInstanceInfos, String chainId, List<Node> nodeList) {
+        if (CollUtil.isEmpty(nodeList)) {
             return;
         }
+        Map<String, Integer> idCntMap = new HashMap<>();
 
-        executableGroup.forEach((key, executables) -> {
-            executables.forEach(executable -> {
-                if (executable instanceof Node) {
-                    Node node = (Node) executable;
-                    idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
-
-                    for (InstanceInfoDto dto : finalInstanceInfos) {
-                        if (Objects.equals(dto.getNodeId(), node.getId())
-                                && Objects.equals(dto.getChainId(), chainId)
-                                && Objects.equals(dto.getIndex(), idCntMap.get(node.getId()))) {
-                            node.setInstanceId(dto.getInstanceId());
-                            break;
-                        }
-                    }
-                } else if (executable instanceof Condition) {
-                    Condition conditionTmp = (Condition) executable;
-                    setInstanceIdFromFile(finalInstanceInfos, chainId, conditionTmp.getExecutableGroup(), idCntMap);
-                } else if (executable instanceof Chain) {
-                    Chain chainTmp = (Chain) executable;
-                    List<Condition> conditionList = chainTmp.getConditionList();
-                    conditionList.forEach(condition ->
-                            setInstanceIdFromFile(finalInstanceInfos, chainId, condition.getExecutableGroup(), idCntMap));
+        nodeList.forEach(node -> {
+            idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
+
+            for (InstanceInfoDto dto : finalInstanceInfos) {
+                if (Objects.equals(dto.getNodeId(), node.getId())
+                        && Objects.equals(dto.getChainId(), chainId)
+                        && Objects.equals(dto.getIndex(), idCntMap.get(node.getId()))) {
+                    node.setInstanceId(dto.getInstanceId());
+                    break;
                 }
-            });
+            }
         });
     }
 
@@ -253,47 +210,37 @@ public abstract class BaseNodeInstanceIdManageSpi implements NodeInstanceIdManag
     private List<InstanceInfoDto> writeNodeInstanceId(Condition condition, String chainId) {
         ArrayList<InstanceInfoDto> instanceInfos = new ArrayList<>();
 
-        addInstanceIdFromExecutableGroup(instanceInfos, condition.getExecutableGroup(), chainId, new HashMap<>());
+        addInstanceIdFromExecutableGroup(instanceInfos, condition.getAllNodeInCondition(), chainId);
 
         return instanceInfos;
     }
 
     // 往instanceInfos里添加实例id
-    private void addInstanceIdFromExecutableGroup(List<InstanceInfoDto> instanceInfos, Map<String, List<Executable>> executableGroup,
-                                                  String chainId, Map<String, Integer> idCntMap) {
-        if (CollUtil.isEmpty(executableGroup)) {
+    private void addInstanceIdFromExecutableGroup(List<InstanceInfoDto> instanceInfos, List<Node> nodeList,
+                                                  String chainId) {
+        if (CollUtil.isEmpty(nodeList)) {
             return;
         }
-        executableGroup.forEach((key, executables) -> {
-            executables.forEach(executable -> {
-                if (executable instanceof Node) {
-                    Node node = (Node) executable;
-                    InstanceInfoDto instanceInfoDto = new InstanceInfoDto();
 
-                    instanceInfoDto.setChainId(chainId);
-                    instanceInfoDto.setNodeId(node.getId());
+        Map<String, Integer> idCntMap = new HashMap<>();
 
-                    String shortUUID = generateShortUUID();
+        nodeList.forEach(node -> {
+            InstanceInfoDto instanceInfoDto = new InstanceInfoDto();
 
-                    idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
+            instanceInfoDto.setChainId(chainId);
+            instanceInfoDto.setNodeId(node.getId());
 
-                    String instanceId = node.getId() + "_" + shortUUID + "_" + idCntMap.get(node.getId());
+            String shortUUID = generateShortUUID();
 
-                    node.setInstanceId(instanceId);
-                    instanceInfoDto.setInstanceId(instanceId);
-                    instanceInfoDto.setIndex(idCntMap.get(node.getId()));
+            idCntMap.put(node.getId(), idCntMap.getOrDefault(node.getId(), -1) + 1);
 
-                    instanceInfos.add(instanceInfoDto);
-                } else if (executable instanceof Condition) {
-                    Condition conditionTmp = (Condition) executable;
-                    addInstanceIdFromExecutableGroup(instanceInfos, conditionTmp.getExecutableGroup(), chainId, idCntMap);
-                } else if (executable instanceof Chain) {
-                    Chain chainTmp = (Chain) executable;
-                    List<Condition> conditionList = chainTmp.getConditionList();
-                    conditionList.forEach(condition -> addInstanceIdFromExecutableGroup(instanceInfos, condition.getExecutableGroup(), chainId, idCntMap));
-                }
-            });
+            String instanceId = node.getId() + "_" + shortUUID + "_" + idCntMap.get(node.getId());
+
+            node.setInstanceId(instanceId);
+            instanceInfoDto.setInstanceId(instanceId);
+            instanceInfoDto.setIndex(idCntMap.get(node.getId()));
+
+            instanceInfos.add(instanceInfoDto);
         });
     }
-
 }

+ 114 - 0
liteflow-testcase-el/liteflow-testcase-el-sql-springboot/src/test/java/com/yomahub/liteflow/test/sqlInstanceId/SQLWithXmlELInstanceIdSpringbootTest.java

@@ -2,6 +2,7 @@ package com.yomahub.liteflow.test.sqlInstanceId;
 
 import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.flow.LiteflowResponse;
+import com.yomahub.liteflow.flow.element.Node;
 import com.yomahub.liteflow.flow.entity.InstanceInfoDto;
 import com.yomahub.liteflow.flow.instanceId.NodeInstanceIdManageSpi;
 import com.yomahub.liteflow.flow.instanceId.NodeInstanceIdManageSpiHolder;
@@ -170,6 +171,65 @@ public class SQLWithXmlELInstanceIdSpringbootTest extends BaseTest {
         Assertions.assertEquals(Sets.newHashSet(extractStrings).size(), 4);
     }
 
+
+    @Test
+    public void getNodeByIdAndInstanceIdTest() throws SQLException {
+        String[] chainIds = new String[]{"chain1", "chain2", "chain4","r_chain1","r_chain2","chain5"};
+        for (String chainId : chainIds) {
+            LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg");
+            String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId();
+
+            Map<String, String> instanceMap = extractKeyValuePairs(executeStepStrWithInstanceId);
+
+            NodeInstanceIdManageSpi nodeInstanceIdManageSpi = NodeInstanceIdManageSpiHolder.getInstance().getNodeInstanceIdManageSpi();
+            for (Map.Entry<String, String> entry : instanceMap.entrySet()) {
+                Node node = nodeInstanceIdManageSpi.getNodeByIdAndInstanceId(chainId, entry.getKey());
+                Assertions.assertEquals(node.getId(), entry.getValue());
+            }
+        }
+
+    }
+
+
+    @Test
+    public void getNodeByIdAndIndexTest() throws SQLException {
+        String[] chainIds = new String[]{"chain1", "chain2", "chain4","r_chain1","r_chain2","chain5"};
+        for (String chainId : chainIds) {
+            LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg");
+            String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId();
+
+            Map<String, String> instanceMap = extractKeyValuePairs(executeStepStrWithInstanceId);
+
+            Map<String, Integer> idCntMap = new HashMap<>();
+
+            NodeInstanceIdManageSpi nodeInstanceIdManageSpi = NodeInstanceIdManageSpiHolder.getInstance().getNodeInstanceIdManageSpi();
+            for (Map.Entry<String, String> entry : instanceMap.entrySet()) {
+                idCntMap.put(entry.getValue(), idCntMap.getOrDefault(entry.getValue(), -1) + 1);
+                Node node = nodeInstanceIdManageSpi.getNodeByIdAndIndex(chainId, entry.getValue(), idCntMap.get(entry.getValue()));
+                Assertions.assertEquals(node.getId(), entry.getValue());
+            }
+        }
+    }
+
+    @Test
+    public void getNodeInstanceIdsTest() throws SQLException {
+        String[] chainIds = new String[]{"chain1", "chain2", "chain4","r_chain1","r_chain2","chain5"};
+        for (String chainId : chainIds) {
+            LiteflowResponse response = flowExecutor.execute2Resp(chainId, "arg");
+            String executeStepStrWithInstanceId = response.getExecuteStepStrWithInstanceId();
+
+            Map<String, List<String>> instanceMap = extractKeyValues(executeStepStrWithInstanceId);
+
+            NodeInstanceIdManageSpi nodeInstanceIdManageSpi = NodeInstanceIdManageSpiHolder.getInstance().getNodeInstanceIdManageSpi();
+            for (Map.Entry<String, List<String>> entry : instanceMap.entrySet()) {
+                Assertions.assertEquals(entry.getValue(), nodeInstanceIdManageSpi.getNodeInstanceIds(chainId, entry.getKey()));
+            }
+        }
+
+    }
+
+
+
     private String constructInstancePath(String executeStepStr, String chainId) throws SQLException {
         Map<String, InstanceInfoDto> instanceMap = queryInstanceMapByChainId(chainId);
         String[] nodes = executeStepStr.split("==>");
@@ -246,6 +306,60 @@ public class SQLWithXmlELInstanceIdSpringbootTest extends BaseTest {
         return values;
     }
 
+    /**
+     * key 为 InstanceId  value 为 nodeId
+     */
+    private Map<String, String> extractKeyValuePairs(String input) {
+        String[] parts = input.split("==>");
+
+        // 创建一个 Map 来存储结果
+        Map<String, String> resultMap = new HashMap<>();
+
+        for (String part : parts) {
+            // 去掉前后括号
+            int startIndex = part.indexOf('[');
+            int endIndex = part.lastIndexOf(']');
+
+            if (startIndex != -1 && endIndex != -1) {
+                String value = part.substring(0, startIndex).trim();
+                String key = part.substring(startIndex + 1, endIndex).trim();
+
+                // 将键值对放入 Map
+                resultMap.put(key, value);
+            }
+        }
+        return resultMap;
+    }
+
+
+
+    /**
+     * key 为 nodeId value 为 list InstanceId
+     */
+    private Map<String, List<String>> extractKeyValues(String input) {
+        String[] parts = input.split("==>");
+
+        // 创建一个 Map 来存储结果
+        Map<String, List<String>> resultMap = new HashMap<>();
+
+        for (String part : parts) {
+            // 去掉前后括号
+            int startIndex = part.indexOf('[');
+            int endIndex = part.lastIndexOf(']');
+
+            if (startIndex != -1 && endIndex != -1) {
+                String key = part.substring(0, startIndex).trim();
+                String value = part.substring(startIndex + 1, endIndex).trim();
+
+                List<String> mapOrDefault = resultMap.getOrDefault(key, new ArrayList<>());
+                mapOrDefault.add(value);
+                resultMap.put(key, mapOrDefault);
+            }
+        }
+        return resultMap;
+    }
+
+
     public String queryInstanceIdInfo(String chainId) throws SQLException {
         LiteflowConfig liteflowConfig = LiteflowConfigGetter.get();
         SQLParserVO sqlParserVO = JsonUtil.parseObject(liteflowConfig.getRuleSourceExtData(), SQLParserVO.class);