fix: 接口定义导入order字段为空

This commit is contained in:
AnAngle 2021-09-14 14:34:39 +08:00 committed by jianxing
parent f6ddef4544
commit 07797ec3b5
1 changed files with 31 additions and 0 deletions

View File

@ -114,9 +114,14 @@ public class ApiDefinitionService {
private TestPlanMapper testPlanMapper; private TestPlanMapper testPlanMapper;
@Resource @Resource
private NoticeSendService noticeSendService; private NoticeSendService noticeSendService;
@Resource
private ExtApiTestCaseMapper extApiTestCaseMapper;
private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24); private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24);
private ThreadLocal<Long> currentApiOrder = new ThreadLocal<>();
private ThreadLocal<Long> currentApiCaseOrder = new ThreadLocal<>();
public List<ApiDefinitionResult> list(ApiDefinitionRequest request) { public List<ApiDefinitionResult> list(ApiDefinitionRequest request) {
request = this.initRequest(request, true, true); request = this.initRequest(request, true, true);
List<ApiDefinitionResult> resList = extApiDefinitionMapper.list(request); List<ApiDefinitionResult> resList = extApiDefinitionMapper.list(request);
@ -474,6 +479,7 @@ public class ApiDefinitionService {
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
//postman 可能含有前置脚本接口定义去掉脚本 //postman 可能含有前置脚本接口定义去掉脚本
String requestStr = setImportHashTree(apiDefinition); String requestStr = setImportHashTree(apiDefinition);
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
apiDefinition.setRequest(requestStr); apiDefinition.setRequest(requestStr);
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true);
@ -485,11 +491,32 @@ public class ApiDefinitionService {
return apiDefinition; return apiDefinition;
} }
private Long getImportNextOrder(String projectId) {
Long order = currentApiOrder.get();
if (order == null) {
order = ServiceUtils.getNextOrder(projectId, extApiDefinitionMapper::getLastOrder);
}
order = (order == null ? 0 : order) + 5000;
currentApiOrder.set(order);
return order;
}
private Long getImportNextCaseOrder(String projectId) {
Long order = currentApiCaseOrder.get();
if (order == null) {
order = ServiceUtils.getNextOrder(projectId, extApiTestCaseMapper::getLastOrder);
}
order = (order == null ? 0 : order) + 5000;
currentApiCaseOrder.set(order);
return order;
}
private void _importCreate(List<ApiDefinition> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition, private void _importCreate(List<ApiDefinition> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition,
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases) { ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases) {
if (CollectionUtils.isEmpty(sameRequest)) { if (CollectionUtils.isEmpty(sameRequest)) {
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) { if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
String request = setImportHashTree(apiDefinition); String request = setImportHashTree(apiDefinition);
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
apiDefinition.setRequest(request); apiDefinition.setRequest(request);
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true); importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true);
@ -497,6 +524,7 @@ public class ApiDefinitionService {
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) { if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
String request = setImportTCPHashTree(apiDefinition); String request = setImportTCPHashTree(apiDefinition);
} }
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
batchMapper.insert(apiDefinition); batchMapper.insert(apiDefinition);
} }
@ -603,6 +631,7 @@ public class ApiDefinitionService {
apiTestCase.setUpdateTime(System.currentTimeMillis()); apiTestCase.setUpdateTime(System.currentTimeMillis());
apiTestCase.setCreateUserId(SessionUtils.getUserId()); apiTestCase.setCreateUserId(SessionUtils.getUserId());
apiTestCase.setProjectId(SessionUtils.getCurrentProjectId()); apiTestCase.setProjectId(SessionUtils.getCurrentProjectId());
apiTestCase.setOrder(getImportNextCaseOrder(apiTestImportRequest.getProjectId()));
apiTestCaseMapper.insert(apiTestCase); apiTestCaseMapper.insert(apiTestCase);
} else if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) { } else if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
apiTestCase.setId(sameCase.getId()); apiTestCase.setId(sameCase.getId());
@ -837,6 +866,8 @@ public class ApiDefinitionService {
private void importApi(ApiTestImportRequest request, ApiDefinitionImport apiImport) { private void importApi(ApiTestImportRequest request, ApiDefinitionImport apiImport) {
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
currentApiCaseOrder.remove();
currentApiOrder.remove();
List<ApiDefinitionWithBLOBs> data = apiImport.getData(); List<ApiDefinitionWithBLOBs> data = apiImport.getData();
ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class); ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class); ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class);