fix: 接口测试导入插件报错

This commit is contained in:
chenjianxing 2021-09-24 14:17:24 +08:00 committed by jianxing
parent e008aa65a0
commit dc570d4f57
2 changed files with 22 additions and 11 deletions

View File

@ -506,7 +506,7 @@ public class ApiDefinitionService {
String requestStr = setImportHashTree(apiDefinition); String requestStr = setImportHashTree(apiDefinition);
reSetImportCasesApiId(cases, originId, apiDefinition.getId()); reSetImportCasesApiId(cases, originId, apiDefinition.getId());
apiDefinition.setRequest(requestStr); apiDefinition.setRequest(requestStr);
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); importApiCase(apiDefinition, apiTestImportRequest);
} }
} else { } else {
_importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases); _importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases);
@ -546,7 +546,7 @@ public class ApiDefinitionService {
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
String request = setImportHashTree(apiDefinition); String request = setImportHashTree(apiDefinition);
apiDefinition.setRequest(request); apiDefinition.setRequest(request);
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); importApiCase(apiDefinition, apiTestImportRequest);
} else { } else {
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
setImportTCPHashTree(apiDefinition); setImportTCPHashTree(apiDefinition);
@ -566,7 +566,7 @@ public class ApiDefinitionService {
apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition); apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
apiDefinition.setRequest(request); apiDefinition.setRequest(request);
reSetImportCasesApiId(cases, originId, apiDefinition.getId()); reSetImportCasesApiId(cases, originId, apiDefinition.getId());
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, false); importApiCase(apiDefinition, apiTestImportRequest);
} else { } else {
apiDefinition.setId(sameRequest.get(0).getId()); apiDefinition.setId(sameRequest.get(0).getId());
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
@ -615,7 +615,7 @@ public class ApiDefinitionService {
return request; return request;
} }
private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession, ApiTestCaseMapper apiTestCaseMapper, private void importMsCase(ApiDefinitionImport apiImport, SqlSession sqlSession,
ApiTestImportRequest request) { ApiTestImportRequest request) {
List<ApiTestCaseWithBLOBs> cases = apiImport.getCases(); List<ApiTestCaseWithBLOBs> cases = apiImport.getCases();
if (CollectionUtils.isNotEmpty(cases)) { if (CollectionUtils.isNotEmpty(cases)) {
@ -626,7 +626,7 @@ public class ApiDefinitionService {
if (apiDefinitionWithBLOBs == null) { if (apiDefinitionWithBLOBs == null) {
continue; continue;
} }
insertOrUpdateImportCase(item, request); insertOrUpdateImportCase(item, request, apiDefinitionWithBLOBs);
} }
if (batchCount % 300 == 0) { if (batchCount % 300 == 0) {
sqlSession.flushStatements(); sqlSession.flushStatements();
@ -638,8 +638,7 @@ public class ApiDefinitionService {
* 导入是插件或者postman时创建用例 * 导入是插件或者postman时创建用例
* postman考虑是否有前置脚本 * postman考虑是否有前置脚本
*/ */
private void importApiCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, private void importApiCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest apiTestImportRequest) {
ApiTestImportRequest apiTestImportRequest, Boolean isInsert) {
try { try {
if (StringUtils.equalsAnyIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Plugin.name(), ApiImportPlatform.Postman.name())) { if (StringUtils.equalsAnyIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Plugin.name(), ApiImportPlatform.Postman.name())) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs(); ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
@ -649,14 +648,14 @@ public class ApiDefinitionService {
if (apiTestCase.getName().length() > 255) { if (apiTestCase.getName().length() > 255) {
apiTestCase.setName(apiTestCase.getName().substring(0, 255)); apiTestCase.setName(apiTestCase.getName().substring(0, 255));
} }
insertOrUpdateImportCase(apiTestCase, apiTestImportRequest); insertOrUpdateImportCase(apiTestCase, apiTestImportRequest, apiDefinition);
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error("导入创建用例异常", e); LogUtil.error("导入创建用例异常", e);
} }
} }
private void insertOrUpdateImportCase(ApiTestCaseWithBLOBs apiTestCase, ApiTestImportRequest apiTestImportRequest) { private void insertOrUpdateImportCase(ApiTestCaseWithBLOBs apiTestCase, ApiTestImportRequest apiTestImportRequest, ApiDefinitionWithBLOBs apiDefinition) {
SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest(); SaveApiTestCaseRequest checkRequest = new SaveApiTestCaseRequest();
checkRequest.setName(apiTestCase.getName()); checkRequest.setName(apiTestCase.getName());
checkRequest.setApiDefinitionId(apiTestCase.getApiDefinitionId()); checkRequest.setApiDefinitionId(apiTestCase.getApiDefinitionId());
@ -668,7 +667,7 @@ public class ApiDefinitionService {
apiTestCase.setUpdateUserId(SessionUtils.getUserId()); apiTestCase.setUpdateUserId(SessionUtils.getUserId());
if (sameCase == null) { if (sameCase == null) {
apiTestCase.setId(UUID.randomUUID().toString()); apiTestCase.setId(UUID.randomUUID().toString());
apiTestCase.setNum(apiTestCaseService.getNextNum(apiTestCase.getApiDefinitionId())); apiTestCase.setNum(apiTestCaseService.getNextNum(apiTestCase.getApiDefinitionId(), apiDefinition.getNum()));
apiTestCase.setCreateTime(System.currentTimeMillis()); apiTestCase.setCreateTime(System.currentTimeMillis());
apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setUpdateTime(System.currentTimeMillis());
apiTestCase.setCreateUserId(SessionUtils.getUserId()); apiTestCase.setCreateUserId(SessionUtils.getUserId());
@ -965,7 +964,7 @@ public class ApiDefinitionService {
} }
if (!CollectionUtils.isEmpty(apiImport.getCases())) { if (!CollectionUtils.isEmpty(apiImport.getCases())) {
importMsCase(apiImport, sqlSession, apiTestCaseMapper, request); importMsCase(apiImport, sqlSession, request);
} }
} }

View File

@ -396,6 +396,18 @@ public class ApiTestCaseService {
} }
} }
public int getNextNum(String definitionId, Integer definitionNum) {
ApiTestCase apiTestCase = extApiTestCaseMapper.getNextNum(definitionId);
if (apiTestCase == null) {
if (definitionNum == null) {
return apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum() * 1000 + 1;
}
return definitionNum * 1000 + 1;
} else {
return Optional.of(apiTestCase.getNum() + 1)
.orElse(apiDefinitionMapper.selectByPrimaryKey(definitionId).getNum() * 1000 + 1);
}
}
private void deleteFileByTestId(String testId) { private void deleteFileByTestId(String testId) {
ApiTestFileExample ApiTestFileExample = new ApiTestFileExample(); ApiTestFileExample ApiTestFileExample = new ApiTestFileExample();