From 2eed3193094ca67f1d1093dc8ca0c32c5df8330c Mon Sep 17 00:00:00 2001 From: chenjianxing Date: Mon, 16 Aug 2021 11:33:24 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=8E=A5=E5=8F=A3=E5=AF=BC=E5=85=A5?= =?UTF-8?q?=E9=80=89=E8=A6=86=E7=9B=96=E6=97=B6=E6=9C=AA=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E7=94=A8=E4=BE=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../parse/PostmanAbstractParserParser.java | 4 +- .../api/service/ApiDefinitionService.java | 62 ++++++++++--------- .../api/service/ApiTestCaseService.java | 14 ++++- 3 files changed, 45 insertions(+), 35 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java b/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java index 36a7b5dd22..bde77056ee 100644 --- a/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java +++ b/backend/src/main/java/io/metersphere/api/parse/PostmanAbstractParserParser.java @@ -30,7 +30,7 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa } requestDesc.getAuth(); // todo 认证方式等待优化 PostmanUrl url = requestDesc.getUrl(); - MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url.getRaw(), requestDesc.getMethod()); + MsHTTPSamplerProxy request = buildRequest(requestItem.getName(), url == null ? "" : url.getRaw(), requestDesc.getMethod()); if (StringUtils.isNotBlank(request.getPath())) { String path = request.getPath().split("\\?")[0]; path = parseVariable(path); @@ -39,7 +39,7 @@ public abstract class PostmanAbstractParserParser extends ApiImportAbstractPa request.setPath("/"); } parseBody(request.getBody(), requestDesc); - request.setArguments(parseKeyValue(url.getQuery())); + request.setArguments(parseKeyValue(url == null ? new ArrayList<>() : url.getQuery())); request.setHeaders(parseKeyValue(requestDesc.getHeader())); addBodyHeader(request); addPreScript(request, requestItem.getEvent()); 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 b9b9295c66..194a145688 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -545,31 +545,18 @@ public class ApiDefinitionService { return request; } - private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession, ApiTestCaseMapper apiTestCaseMapper) { + private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession, ApiTestCaseMapper apiTestCaseMapper, + ApiTestImportRequest request) { List cases = apiImport.getCases(); - SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest(); if (CollectionUtils.isNotEmpty(cases)) { int batchCount = 0; - int nextNum = 0; for (int i = 0; i < cases.size(); i++) { ApiTestCaseWithBLOBs item = cases.get(i); ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = apiDefinitionMapper.selectByPrimaryKey(item.getApiDefinitionId()); if (apiDefinitionWithBLOBs == null) { continue; } - nextNum = apiTestCaseService.getNextNum(item.getApiDefinitionId()); - checkRequest.setName(item.getName()); - checkRequest.setApiDefinitionId(item.getApiDefinitionId()); - if (!apiTestCaseService.hasSameCase(checkRequest)) { - item.setId(UUID.randomUUID().toString()); - item.setCreateTime(System.currentTimeMillis()); - item.setUpdateTime(System.currentTimeMillis()); - item.setCreateUserId(SessionUtils.getUserId()); - item.setUpdateUserId(SessionUtils.getUserId()); - item.setProjectId(SessionUtils.getCurrentProjectId()); - item.setNum(nextNum); - apiTestCaseMapper.insert(item); - } + insertOrUpdateImportCase(item, request); } if (batchCount % 300 == 0) { sqlSession.flushStatements(); @@ -587,30 +574,45 @@ public class ApiDefinitionService { if (StringUtils.equalsAnyIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Plugin.name(), ApiImportPlatform.Postman.name())) { ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs(); BeanUtils.copyBean(apiTestCase, apiDefinition); - apiTestCase.setId(UUID.randomUUID().toString()); apiTestCase.setApiDefinitionId(apiDefinition.getId()); - apiTestCase.setCreateTime(System.currentTimeMillis()); - apiTestCase.setUpdateTime(System.currentTimeMillis()); - apiTestCase.setCreateUserId(SessionUtils.getUserId()); - apiTestCase.setUpdateUserId(SessionUtils.getUserId()); - apiTestCase.setNum(getNextNum(apiTestCase.getApiDefinitionId())); apiTestCase.setPriority("P0"); if (apiTestCase.getName().length() > 255) { apiTestCase.setName(apiTestCase.getName().substring(0, 255)); } - /* if (!isInsert) { - apiTestCase.setName(apiTestCase.getName() + "_" + apiTestCase.getId().substring(0, 5)); - }*/ - ApiTestCaseExample example = new ApiTestCaseExample(); - example.createCriteria().andApiDefinitionIdEqualTo(apiDefinition.getId()); - apiTestCaseMapper.deleteByExample(example); - apiTestCaseMapper.insert(apiTestCase); + insertOrUpdateImportCase(apiTestCase, apiTestImportRequest); } } catch (Exception e) { LogUtil.error("导入创建用例异常", e); } } + private void insertOrUpdateImportCase(ApiTestCaseWithBLOBs apiTestCase, ApiTestImportRequest apiTestImportRequest) { + SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest(); + checkRequest.setName(apiTestCase.getName()); + checkRequest.setApiDefinitionId(apiTestCase.getApiDefinitionId()); + ApiTestCase sameCase = apiTestCaseService.getSameCase(checkRequest); + apiTestCase.setUpdateUserId(SessionUtils.getUserId()); + if (sameCase == null) { + apiTestCase.setId(UUID.randomUUID().toString()); + apiTestCase.setNum(getNextNum(apiTestCase.getApiDefinitionId())); + apiTestCase.setCreateTime(System.currentTimeMillis()); + apiTestCase.setUpdateTime(System.currentTimeMillis()); + apiTestCase.setCreateUserId(SessionUtils.getUserId()); + apiTestCase.setProjectId(SessionUtils.getCurrentProjectId()); + apiTestCaseMapper.insert(apiTestCase); + } else if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { + apiTestCase.setId(sameCase.getId()); + apiTestCase.setUpdateTime(System.currentTimeMillis()); + apiTestCase.setCreateTime(null); + apiTestCase.setPriority(sameCase.getPriority()); + apiTestCase.setCreateUserId(sameCase.getCreateUserId()); + apiTestCase.setNum(sameCase.getNum()); + apiTestCase.setProjectId(sameCase.getProjectId()); + apiTestCase.setVersion((sameCase.getVersion() == null ? 0 : sameCase.getVersion()) + 1); + apiTestCaseMapper.updateByPrimaryKeySelective(apiTestCase); + } + } + private void deleteFileByTestId(String apiId) { ApiTestFileExample apiTestFileExample = new ApiTestFileExample(); apiTestFileExample.createCriteria().andTestIdEqualTo(apiId); @@ -875,7 +877,7 @@ public class ApiDefinitionService { } if (!CollectionUtils.isEmpty(apiImport.getCases())) { - importMsCase(apiImport, sqlSession, apiTestCaseMapper); + importMsCase(apiImport, sqlSession, apiTestCaseMapper, request); } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 598f13a23c..de9a5146c3 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -265,16 +265,24 @@ public class ApiTestCaseService { } public Boolean hasSameCase(SaveApiTestCaseRequest request) { + if (getSameCase(request) != null) { + return true; + } + return false; + } + + public ApiTestCase getSameCase(SaveApiTestCaseRequest request) { ApiTestCaseExample example = new ApiTestCaseExample(); ApiTestCaseExample.Criteria criteria = example.createCriteria(); criteria.andNameEqualTo(request.getName()).andApiDefinitionIdEqualTo(request.getApiDefinitionId()); if (StringUtils.isNotBlank(request.getId())) { criteria.andIdNotEqualTo(request.getId()); } - if (apiTestCaseMapper.countByExample(example) > 0) { - return true; + List apiTestCases = apiTestCaseMapper.selectByExample(example); + if (CollectionUtils.isNotEmpty(apiTestCases)) { + return apiTestCases.get(0); } - return false; + return null; } private ApiTestCase updateTest(SaveApiTestCaseRequest request) {