From e0c8a5ddf6e599bfca9b7c119afc4bccb64e8711 Mon Sep 17 00:00:00 2001 From: Jianguo-Genius Date: Tue, 29 Oct 2024 14:41:52 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=9C=BA=E6=99=AF=E5=AF=BC=E5=85=A5=E5=9C=A8?= =?UTF-8?q?=E6=9F=90=E4=BA=9B=E6=83=85=E5=86=B5=E4=B8=8B=E4=BC=9A=E9=87=8D?= =?UTF-8?q?=E5=A4=8D=E5=88=9B=E5=BB=BAapi=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1048286 --user=宋天阳 【场景导入】-原场景中引用和复制相同的CASE作为步骤,导出这个场景然后导入到其他项目,会在项目下创建两个API https://www.tapd.cn/55049933/s/1600281 --- .../ApiScenarioDataTransferService.java | 29 ++++++++++--------- .../api/utils/ApiScenarioImportUtils.java | 11 +++++-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioDataTransferService.java b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioDataTransferService.java index 60548b0b82..807320aa18 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioDataTransferService.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/service/ApiScenarioDataTransferService.java @@ -958,23 +958,23 @@ public class ApiScenarioDataTransferService { Map> apiIdMap = protocolList.stream().collect(Collectors.groupingBy(ApiTestCaseDTO::getApiDefinitionId)); for (Map.Entry> apiIdEntry : apiIdMap.entrySet()) { - ApiDefinitionDetail replaceApiDefinition = returnResource.getApi(apiIdEntry.getKey()) == null ? null : returnResource.getApi(apiIdEntry.getKey()); + ApiDefinitionDetail replacedApiDefinition = returnResource.getApi(apiIdEntry.getKey()) == null ? null : returnResource.getApi(apiIdEntry.getKey()); List testCaseList = apiIdEntry.getValue(); - if (replaceApiDefinition != null) { + if (replacedApiDefinition == null) { // 用例对应的接口在上述步骤中有没有处理 - boolean apiExistence = ApiScenarioImportUtils.isApiExistence( + ApiDefinitionDetail existenceApi = ApiScenarioImportUtils.isApiExistence( protocol, testCaseList.getFirst().getMethod(), testCaseList.getFirst().getPath(), testCaseList.getFirst().getModulePath(), testCaseList.getFirst().getApiDefinitionName(), existenceApiDefinitionList); - if (!apiExistence && !StringUtils.equalsIgnoreCase(targetProjectId, projectId)) { + if (existenceApi == null && !StringUtils.equalsIgnoreCase(targetProjectId, projectId)) { // 如果用例所在项目与当前导入项目不同,且在原项目中不存在时,判断是否在当前项目中存在 - apiExistence = ApiScenarioImportUtils.isApiExistence( + existenceApi = ApiScenarioImportUtils.isApiExistence( protocol, testCaseList.getFirst().getMethod(), testCaseList.getFirst().getPath(), testCaseList.getFirst().getModulePath(), testCaseList.getFirst().getApiDefinitionName(), thisProjectExistenceApiDefinitionList); } - if (apiExistence) { - Map existenceApiCaseNumMap = extApiTestCaseMapper.selectBaseInfoByProjectIdAndApiId(targetProjectId, apiIdEntry.getKey()) + if (existenceApi != null) { + Map existenceApiCaseNumMap = extApiTestCaseMapper.selectBaseInfoByProjectIdAndApiId(targetProjectId, existenceApi.getId()) .stream().collect(Collectors.toMap(ApiTestCaseDTO::getNum, Function.identity(), (k1, k2) -> k1)); for (ApiTestCaseDTO apiTestCaseDTO : apiIdEntry.getValue()) { if (existenceApiCaseNumMap.containsKey(apiTestCaseDTO.getNum())) { @@ -982,9 +982,10 @@ public class ApiScenarioDataTransferService { } else { String oldId = apiTestCaseDTO.getId(); apiTestCaseDTO.setId(IDGenerator.nextStr()); - apiTestCaseDTO.setProjectId(replaceApiDefinition.getProjectId()); - apiTestCaseDTO.setApiDefinitionId(replaceApiDefinition.getId()); - returnResource.putApiTestCase(oldId, apiTestCaseDTO); + apiTestCaseDTO.setName(apiTestCaseDTO.getName() + "_" + System.currentTimeMillis()); + apiTestCaseDTO.setProjectId(existenceApi.getProjectId()); + apiTestCaseDTO.setApiDefinitionId(existenceApi.getId()); + returnResource.putApiTestCase(apiTestCaseDTO.getId(), apiTestCaseDTO); analysisResult.setApiTestCase(apiTestCaseDTO); } } @@ -1006,6 +1007,7 @@ public class ApiScenarioDataTransferService { for (ApiTestCaseDTO apiTestCaseDTO : testCaseList) { String oldId = apiTestCaseDTO.getId(); apiTestCaseDTO.setId(IDGenerator.nextStr()); + apiTestCaseDTO.setName(apiTestCaseDTO.getName() + "_" + System.currentTimeMillis()); apiTestCaseDTO.setProjectId(apiDefinitionDetail.getProjectId()); apiTestCaseDTO.setApiDefinitionId(apiDefinitionDetail.getId()); returnResource.putApiTestCase(oldId, apiTestCaseDTO); @@ -1014,17 +1016,18 @@ public class ApiScenarioDataTransferService { } } else { - Map existenceApiCaseNumMap = extApiTestCaseMapper.selectBaseInfoByProjectIdAndApiId(targetProjectId, replaceApiDefinition.getId()) + Map existenceApiCaseNumMap = extApiTestCaseMapper.selectBaseInfoByProjectIdAndApiId(targetProjectId, replacedApiDefinition.getId()) .stream().collect(Collectors.toMap(ApiTestCaseDTO::getNum, Function.identity(), (k1, k2) -> k1)); for (ApiTestCaseDTO apiTestCaseDTO : testCaseList) { - apiTestCaseDTO.setApiDefinitionId(replaceApiDefinition.getId()); - apiTestCaseDTO.setProjectId(replaceApiDefinition.getProjectId()); + apiTestCaseDTO.setApiDefinitionId(replacedApiDefinition.getId()); + apiTestCaseDTO.setProjectId(replacedApiDefinition.getProjectId()); if (existenceApiCaseNumMap.containsKey(apiTestCaseDTO.getNum())) { returnResource.putApiTestCase(apiTestCaseDTO.getId(), existenceApiCaseNumMap.get(apiTestCaseDTO.getNum())); } else { apiTestCaseDTO.setProjectId(targetProjectId); returnResource.putApiTestCase(apiTestCaseDTO.getId(), apiTestCaseDTO); apiTestCaseDTO.setId(IDGenerator.nextStr()); + apiTestCaseDTO.setName(apiTestCaseDTO.getName() + "_" + System.currentTimeMillis()); analysisResult.setApiTestCase(apiTestCaseDTO); } } diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiScenarioImportUtils.java b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiScenarioImportUtils.java index b5c66f0ede..d2837f813c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiScenarioImportUtils.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/utils/ApiScenarioImportUtils.java @@ -108,14 +108,19 @@ public class ApiScenarioImportUtils { return apiIdDic; } - public static boolean isApiExistence(String protocol, String method, String path, String modulePath, String apiDefinitionName, List existenceApiDefinitionList) { + public static ApiDefinitionDetail isApiExistence(String protocol, String method, String path, String modulePath, String apiDefinitionName, List existenceApiDefinitionList) { Map existenceMap = null; if (StringUtils.equalsIgnoreCase(protocol, ApiConstants.HTTP_PROTOCOL)) { existenceMap = existenceApiDefinitionList.stream().collect(Collectors.toMap(t -> t.getMethod() + t.getPath(), t -> t, (oldValue, newValue) -> newValue)); - return existenceMap.containsKey(method + path); + if (existenceMap.containsKey(method + path)) { + return existenceMap.get(method + path); + } } else { existenceMap = existenceApiDefinitionList.stream().collect(Collectors.toMap(t -> t.getModulePath() + t.getName(), t -> t, (oldValue, newValue) -> newValue)); - return existenceMap.containsKey(modulePath + apiDefinitionName); + if (existenceMap.containsKey(modulePath + apiDefinitionName)) { + return existenceMap.get(modulePath + apiDefinitionName); + } } + return null; } }