From 706628627329d02bd639b9da6d906e4c6a401afc Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Thu, 23 Jun 2022 16:08:13 +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=A4=9A?= =?UTF-8?q?=E5=87=BA=E6=AD=A5=E9=AA=A4=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --bug=1013504 --user=赵勇 【接口测试】导入存在引用关系的接口定义和自动化场景json文件,额外创建了新的接口用例 https://www.tapd.cn/55049933/s/1189401 --- .../parse/ApiScenarioImportUtil.java | 98 +++++++++---------- 1 file changed, 47 insertions(+), 51 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java index 072d04f2f4..584173a95a 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/parse/ApiScenarioImportUtil.java @@ -6,8 +6,7 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.parser.Feature; import io.metersphere.api.dto.automation.ApiScenarioModuleDTO; import io.metersphere.api.dto.definition.ApiDefinitionResult; -import io.metersphere.api.dto.definition.ApiTestCaseDTO; -import io.metersphere.api.dto.definition.ApiTestCaseRequest; +import io.metersphere.api.dto.definition.SaveApiTestCaseRequest; import io.metersphere.api.dto.definition.parse.ms.NodeTree; import io.metersphere.api.dto.definition.response.HttpResponse; import io.metersphere.api.dto.scenario.Body; @@ -16,10 +15,7 @@ import io.metersphere.api.parse.ApiImportAbstractParser; import io.metersphere.api.service.ApiDefinitionService; import io.metersphere.api.service.ApiScenarioModuleService; import io.metersphere.api.service.ApiTestCaseService; -import io.metersphere.base.domain.ApiDefinition; -import io.metersphere.base.domain.ApiDefinitionExample; -import io.metersphere.base.domain.ApiScenarioModule; -import io.metersphere.base.domain.ApiTestCaseWithBLOBs; +import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionMapper; import io.metersphere.base.mapper.ApiTestCaseMapper; import io.metersphere.commons.constants.APITestStatus; @@ -108,6 +104,7 @@ public class ApiScenarioImportUtil { /** * 根据导出的模块树,创建新的模块树 + * * @param nodeTree * @param projectId */ @@ -127,10 +124,10 @@ public class ApiScenarioImportUtil { } } - private static ApiDefinition getApiDefinitionResult(JSONObject object, ApiDefinitionService apiDefinitionService,MapdefinitionMap,Set userRelatedProjectIds) { + private static ApiDefinition getApiDefinitionResult(JSONObject object, ApiDefinitionService apiDefinitionService, Map definitionMap, Set userRelatedProjectIds) { List projectIds = new ArrayList<>(userRelatedProjectIds); ApiDefinitionExample apiDefinitionExample = new ApiDefinitionExample(); - if(object.getString("path") != null && object.getString("method") != null){ + if (object.getString("path") != null && object.getString("method") != null) { apiDefinitionExample.createCriteria() .andPathEqualTo(object.getString("path")) .andMethodEqualTo(object.getString("method")) @@ -139,20 +136,20 @@ public class ApiScenarioImportUtil { .andStatusNotEqualTo("Trash"); } ApiDefinition apiDefinition = apiDefinitionService.getApiDefinition(apiDefinitionExample); - if(apiDefinition==null){ - if(MapUtils.isEmpty(definitionMap)){ + if (apiDefinition == null) { + if (MapUtils.isEmpty(definitionMap)) { return null; - }else { + } else { return definitionMap.get(object.getString("path") + object.getString("method") + object.getString("protocol")); } } else { - if(MapUtils.isEmpty(definitionMap)){ - definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"),apiDefinition); + if (MapUtils.isEmpty(definitionMap)) { + definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"), apiDefinition); return apiDefinition; - }else { + } else { ApiDefinition apiDefinition1 = definitionMap.get(object.getString("path") + object.getString("method") + object.getString("protocol")); if (apiDefinition1 == null) { - definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"),apiDefinition); + definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"), apiDefinition); return apiDefinition; } else { return apiDefinition1; @@ -161,65 +158,64 @@ public class ApiScenarioImportUtil { } } - public static void checkCase(int i,JSONObject object, String versionId, String projectId, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionMapper apiDefinitionMapper,MapdefinitionMap) { + public static void checkCase(int i, JSONObject object, String versionId, String projectId, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionMapper apiDefinitionMapper, Map definitionMap) { ApiTestCaseService testCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class); ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ApiTestCaseWithBLOBs bloBs = testCaseService.get(object.getString("id")); CheckPermissionService checkPermissionService = CommonBeanFactory.getBean(CheckPermissionService.class); Set userRelatedProjectIds = checkPermissionService.getUserRelatedProjectIds(); - if (bloBs == null||!userRelatedProjectIds.contains(bloBs.getProjectId())) { - ApiDefinition apiDefinition = getApiDefinitionResult(object,apiDefinitionService,definitionMap,userRelatedProjectIds); - if(apiDefinition!=null){ - if(MapUtils.isNotEmpty(definitionMap)||definitionMap.size()==0){ - ApiTestCaseRequest request = new ApiTestCaseRequest(); + if (bloBs == null || !userRelatedProjectIds.contains(bloBs.getProjectId())) { + ApiDefinition apiDefinition = getApiDefinitionResult(object, apiDefinitionService, definitionMap, userRelatedProjectIds); + if (apiDefinition != null) { + if (MapUtils.isNotEmpty(definitionMap) || definitionMap.size() == 0) { + SaveApiTestCaseRequest request = new SaveApiTestCaseRequest(); request.setApiDefinitionId(apiDefinition.getId()); request.setName(object.getString("name")); - List apiTestCaseDTOS = testCaseService.listSimple(request); - if (apiTestCaseDTOS ==null || apiTestCaseDTOS.isEmpty()) { - structureCaseByJson(i,object,testCaseService, apiDefinition,apiTestCaseMapper); + ApiTestCase sameCase = testCaseService.getSameCase(request); + if (sameCase == null) { + structureCaseByJson(i, object, testCaseService, apiDefinition, apiTestCaseMapper); } else { - ApiTestCaseDTO apiTestCase = apiTestCaseDTOS.get(0); - object.put("id", apiTestCase.getId()); - object.put("resourceId", apiTestCase.getId()); + object.put("id", sameCase.getId()); + object.put("resourceId", sameCase.getId()); object.put("projectId", projectId); - object.put("useEnvironment",""); - object.put("environmentId",""); + object.put("useEnvironment", ""); + object.put("environmentId", ""); } } - }else{ - ApiDefinitionResult apiDefinitionResult = structureApiDefinitionByJson(i,apiDefinitionService, object, versionId, projectId, apiDefinitionMapper,definitionMap); - structureCaseByJson(i,object,testCaseService, apiDefinitionResult,apiTestCaseMapper); + } else { + ApiDefinitionResult apiDefinitionResult = structureApiDefinitionByJson(i, apiDefinitionService, object, versionId, projectId, apiDefinitionMapper, definitionMap); + structureCaseByJson(i, object, testCaseService, apiDefinitionResult, apiTestCaseMapper); } } } - public static ApiDefinitionResult structureApiDefinitionByJson(int i,ApiDefinitionService apiDefinitionService,JSONObject object, String versionId, String projectId,ApiDefinitionMapper apiDefinitionMapper,MapdefinitionMap) { + public static ApiDefinitionResult structureApiDefinitionByJson(int i, ApiDefinitionService apiDefinitionService, JSONObject object, String versionId, String projectId, ApiDefinitionMapper apiDefinitionMapper, Map definitionMap) { ApiDefinitionResult test = new ApiDefinitionResult(); apiDefinitionService.checkQuota(projectId); String protocal = object.getString("protocal"); if (StringUtils.equals(protocal, "DUBBO")) { test.setMethod("dubbo://"); - }else{ + } else { test.setMethod(object.getString("method")); } test.setProtocol(object.getString("protocol")); - if(StringUtils.isNotBlank(object.getString("modulePath"))&&StringUtils.isNotBlank(object.getString("moduleId"))){ + if (StringUtils.isNotBlank(object.getString("modulePath")) && StringUtils.isNotBlank(object.getString("moduleId"))) { test.setModulePath(object.getString("modulePath")); test.setModuleId(object.getString("moduleId")); - }else{ - apiDefinitionService.initModulePathAndId(projectId,test); + } else { + apiDefinitionService.initModulePathAndId(projectId, test); } String id = UUID.randomUUID().toString(); test.setId(id); - test.setNum(apiDefinitionService.getNextNum(projectId)+i); + test.setNum(apiDefinitionService.getNextNum(projectId) + i); test.setName(object.getString("name")); - if(StringUtils.isBlank(object.getString("path"))){ - if(StringUtils.isNotBlank(object.getString("url"))){ + if (StringUtils.isBlank(object.getString("path"))) { + if (StringUtils.isNotBlank(object.getString("url"))) { ApiImportAbstractParser apiImportAbstractParser = CommonBeanFactory.getBean(ApiImportAbstractParser.class); String path = apiImportAbstractParser.formatPath(object.getString("url")); test.setPath(path); } - }else{ + } else { test.setPath(object.getString("path")); } test.setCreateUser(SessionUtils.getUserId()); @@ -233,15 +229,15 @@ public class ApiScenarioImportUtil { object.put("id", test.getId()); object.put("resourceId", test.getId()); object.put("projectId", projectId); - object.put("useEnvironment",""); - object.put("environmentId",""); - object.put("url",""); + object.put("useEnvironment", ""); + object.put("environmentId", ""); + object.put("url", ""); JSONObject objectNew = JSONObject.parseObject(object.toJSONString(), Feature.DisableSpecialKeyDetect); objectNew.remove("refType"); objectNew.remove("referenced"); test.setRequest(objectNew.toJSONString()); HttpResponse httpResponse = new HttpResponse(); - KeyValue keyValue = new KeyValue(); + KeyValue keyValue = new KeyValue(); List list = new ArrayList<>(); list.add(keyValue); httpResponse.setHeaders(list); @@ -252,11 +248,11 @@ public class ApiScenarioImportUtil { test.setLatest(true); test.setOrder(apiDefinitionService.getImportNextOrder(projectId)); apiDefinitionMapper.insert(test); - definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"),test); + definitionMap.put(object.getString("path") + object.getString("method") + object.getString("protocol"), test); return test; } - public static void structureCaseByJson(int i,JSONObject object,ApiTestCaseService testCaseService, ApiDefinition apiDefinition, ApiTestCaseMapper apiTestCaseMapper) { + public static void structureCaseByJson(int i, JSONObject object, ApiTestCaseService testCaseService, ApiDefinition apiDefinition, ApiTestCaseMapper apiTestCaseMapper) { String projectId = apiDefinition.getProjectId(); ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class); ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs(); @@ -272,13 +268,13 @@ public class ApiScenarioImportUtil { apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setVersionId(apiDefinition.getVersionId()); apiTestCase.setPriority("P0"); - apiTestCase.setNum(testCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum()+i)); + apiTestCase.setNum(testCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum() + i)); object.put("id", apiTestCase.getId()); object.put("resourceId", apiTestCase.getId()); object.put("projectId", projectId); - object.put("useEnvironment",""); - object.put("environmentId",""); - JSONObject objectNew = JSONObject.parseObject(object.toJSONString(),Feature.DisableSpecialKeyDetect); + object.put("useEnvironment", ""); + object.put("environmentId", ""); + JSONObject objectNew = JSONObject.parseObject(object.toJSONString(), Feature.DisableSpecialKeyDetect); objectNew.remove("refType"); objectNew.remove("referenced"); apiTestCase.setRequest(objectNew.toJSONString());