From 993b56685672f23e919adba01fe58b4eecf335b9 Mon Sep 17 00:00:00 2001 From: guoyuqi Date: Mon, 29 Aug 2022 16:48:56 +0800 Subject: [PATCH] =?UTF-8?q?feat(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5=EF=BC=8C=E5=A6=82=E6=9E=9C?= =?UTF-8?q?=E5=AF=BC=E5=85=A5=E7=9A=84=E6=8E=A5=E5=8F=A3=E6=B2=A1=E6=9C=89?= =?UTF-8?q?=E7=94=A8=E4=BE=8B=EF=BC=8C=E4=B8=94=E8=BF=99=E4=B8=AA=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E4=B8=BA=E6=96=B0=E5=BB=BA=E7=9A=84=EF=BC=8C=E9=82=A3?= =?UTF-8?q?=E9=9C=80=E8=A6=81=E4=B8=BA=E8=BF=99=E4=B8=AA=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=96=B0=E5=BB=BA=E4=B8=80=E4=B8=AA=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --user --- .../api/service/ApiDefinitionService.java | 111 ++++++++++-------- 1 file changed, 59 insertions(+), 52 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index d44fde0a5a..d62afae0ae 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -833,9 +833,8 @@ public class ApiDefinitionService { } else { apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion()); } - String requestStr = setImportHashTree(apiDefinition); + caseList = setRequestAndAddNewCase(apiDefinition, caseList, true); reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); - apiDefinition.setRequest(requestStr); batchMapper.insert(apiDefinition); importCase(apiDefinition, apiTestCaseMapper, caseList); extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId()); @@ -851,8 +850,24 @@ public class ApiDefinitionService { return apiDefinition; } + private List setRequestAndAddNewCase(ApiDefinitionWithBLOBs apiDefinition, List caseList, boolean newCreate) { + boolean createCase = false; + if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { + createCase = setImportHashTree(apiDefinition); + + } else if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { + createCase = setImportTCPHashTree(apiDefinition); + } + if (newCreate && createCase && CollectionUtils.isEmpty(caseList)) { + ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = addNewCase(apiDefinition); + caseList = new ArrayList<>(); + caseList.add(apiTestCaseWithBLOBs); + } + return caseList; + } + private void importCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, List caseList) { - if (caseList == null || caseList.isEmpty()) { + if (CollectionUtils.isEmpty(caseList)) { return; } for (int i = 0; i < caseList.size(); i++) { @@ -939,16 +954,8 @@ public class ApiDefinitionService { } reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); - if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { - String request = setImportHashTree(apiDefinition); - apiDefinition.setRequest(request); - batchMapper.insert(apiDefinition); - } else { - if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { - setImportTCPHashTree(apiDefinition); - } - batchMapper.insert(apiDefinition); - } + caseList = setRequestAndAddNewCase(apiDefinition, caseList, true); + batchMapper.insert(apiDefinition); importCase(apiDefinition, apiTestCaseMapper, caseList); extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId()); extApiDefinitionMapper.addLatestVersion(apiDefinition.getRefId()); @@ -997,7 +1004,7 @@ public class ApiDefinitionService { apiDefinition.setUserId(existApi.getUserId()); } //Check whether the content has changed, if not, do not change the creation time - if (apiDefinition.getProtocol().equals("HTTP")) { + if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { Boolean toChangeTime = checkIsSynchronize(existApi, apiDefinition); if (toChangeTime) { apiDefinition.setUpdateTime(System.currentTimeMillis()); @@ -1009,23 +1016,11 @@ public class ApiDefinitionService { if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) { apiDefinition.setTags(existApi.getTags()); // 其他格式 tag 不变,MS 格式替换 } - if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { - //如果存在则修改 - apiDefinition.setId(existApi.getId()); - String request = setImportHashTree(apiDefinition); - apiDefinition.setOrder(existApi.getOrder()); - apiDefinition.setRequest(request); - reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); - batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); - } else { - apiDefinition.setId(existApi.getId()); - if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { - setImportTCPHashTree(apiDefinition); - } - apiDefinition.setOrder(existApi.getOrder()); - reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); - batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); - } + apiDefinition.setId(existApi.getId()); + setRequestAndAddNewCase(apiDefinition, caseList, false); + apiDefinition.setOrder(existApi.getOrder()); + reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum()); + batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); importCase(apiDefinition, apiTestCaseMapper, caseList); } extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId()); @@ -1033,6 +1028,15 @@ public class ApiDefinitionService { } } + private ApiTestCaseWithBLOBs addNewCase(ApiDefinitionWithBLOBs apiDefinition) { + ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs(); + apiTestCase.setApiDefinitionId(apiDefinition.getId()); + apiTestCase.setProjectId(apiDefinition.getProjectId()); + apiTestCase.setName(apiDefinition.getName()); + apiTestCase.setRequest(apiDefinition.getRequest()); + return apiTestCase; + } + public Boolean checkIsSynchronize(ApiDefinitionWithBLOBs existApi, ApiDefinitionWithBLOBs apiDefinition) { ApiDefinition exApi; @@ -1176,23 +1180,6 @@ public class ApiDefinitionService { return false; } - /** - * 如果是MS格式,带用例导出,最后创建用例,重新设置接口id - * - * @param cases - * @param originId - * @param newId - */ - private void reSetImportCasesApiId(List cases, String originId, String newId) { - if (CollectionUtils.isNotEmpty(cases)) { - cases.forEach(item -> { - if (StringUtils.equals(item.getApiDefinitionId(), originId)) { - item.setApiDefinitionId(newId); - } - }); - } - } - private void reSetImportMocksApiId(List mocks, String originId, String newId, int apiNum) { if (CollectionUtils.isNotEmpty(mocks)) { int index = 1; @@ -1206,22 +1193,42 @@ public class ApiDefinitionService { } } - private String setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) { + private boolean setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) { String request = apiDefinition.getRequest(); MsHTTPSamplerProxy msHTTPSamplerProxy = JSONObject.parseObject(request, MsHTTPSamplerProxy.class, Feature.DisableSpecialKeyDetect); + boolean createCase = CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getHeaders()); + if (CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getArguments()) && !createCase) { + createCase = true; + } + if (msHTTPSamplerProxy.getBody() != null && !createCase) { + createCase = true; + } + if (CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getRest()) && !createCase) { + createCase = true; + } msHTTPSamplerProxy.setId(apiDefinition.getId()); msHTTPSamplerProxy.setHashTree(new LinkedList<>()); apiDefinition.setRequest(JSONObject.toJSONString(msHTTPSamplerProxy)); - return request; + return createCase; } - private String setImportTCPHashTree(ApiDefinitionWithBLOBs apiDefinition) { + private boolean setImportTCPHashTree(ApiDefinitionWithBLOBs apiDefinition) { String request = apiDefinition.getRequest(); MsTCPSampler tcpSampler = JSONObject.parseObject(request, MsTCPSampler.class, Feature.DisableSpecialKeyDetect); + boolean createCase = CollectionUtils.isNotEmpty(tcpSampler.getParameters()); + if (StringUtils.isNotBlank(tcpSampler.getJsonDataStruct()) && !createCase) { + createCase = true; + } + if (StringUtils.isNotBlank(tcpSampler.getRawDataStruct()) && !createCase) { + createCase = true; + } + if (CollectionUtils.isNotEmpty(tcpSampler.getXmlDataStruct()) && !createCase) { + createCase = true; + } tcpSampler.setId(apiDefinition.getId()); tcpSampler.setHashTree(new LinkedList<>()); apiDefinition.setRequest(JSONObject.toJSONString(tcpSampler)); - return request; + return createCase; } private void deleteFileByTestId(String apiId) {