fix(接口测试): 修复场景导入系统请求报错的缺陷

--bug=1038978 --user=王孝刚 【接口测试】场景导入系统请求-切换项目导入场景报错
https://www.tapd.cn/55049933/s/1492667
--bug=1038386 --user=王孝刚
【接口测试】场景-创建场景-导入 系统请求-当前项目选择多页的场景-复制/引用失败
https://www.tapd.cn/55049933/s/1492801
This commit is contained in:
wxg0103 2024-04-10 14:22:07 +08:00 committed by 刘瑞斌
parent b744fc9935
commit 40284129ba
2 changed files with 191 additions and 145 deletions

View File

@ -247,6 +247,51 @@ public class ApiDefinitionImportUtilService {
currentApiCaseOrder.remove();
currentApiOrder.remove();
currentModuleOrder.remove();
//获取需要新增的模块
getNeedAddModule(modulePathMap, idModuleMap, differenceSet, addModuleList);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiDefinitionModuleMapper moduleMapper = sqlSession.getMapper(ApiDefinitionModuleMapper.class);
ApiDefinitionMapper apiMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiDefinitionBlobMapper apiBlobMapper = sqlSession.getMapper(ApiDefinitionBlobMapper.class);
//创建模块
insertModule(request, addModuleList, moduleMapper);
//取出需要更新的数据的id
List<String> updateModuleLists = updateModuleData.stream().map(ApiDefinitionImportDetail::getId).toList();
//更新模块数据
updateApiModule(modulePathMap, request, updateModuleData, apiMapper);
List<LogDTO> operationLogs = new ArrayList<>();
List<ApiDefinitionImportDetail> updateRequestData = apiDetailWithDataUpdate.getUpdateRequestData();
//更新接口请求数据
updateApiRequest(request, updateRequestData, updateModuleLists, apiMapper, apiBlobMapper);
Map<String, ApiDefinitionImportDetail> logData = apiDetailWithDataUpdate.getLogData();
Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
List<ApiDefinitionCaseDTO> updateLists = new ArrayList<>();
//获取更新的日志
getUpdateLog(request, logData, updateLists, project, operationLogs);
List<ApiDefinitionCaseDTO> createLists = new ArrayList<>();
//获取新增的数据和日志
addApiAndLog(modulePathMap, request, addModuleData, apiMapper, apiBlobMapper, project, operationLogs, createLists);
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
SubListUtils.dealForSubList(operationLogs, 500, operationLogService::batchAdd);
//发送通知
List<Map> createResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(createLists), Map.class));
User user = userMapper.selectByPrimaryKey(request.getUserId());
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CREATE, createResources, user, request.getProjectId());
List<Map> updateResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(updateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.UPDATE, updateResources, user, request.getProjectId());
}
private static void getNeedAddModule(Map<String, BaseTreeNode> modulePathMap, Map<String, BaseTreeNode> idModuleMap, Set<String> differenceSet, List<BaseTreeNode> addModuleList) {
differenceSet.forEach(item -> {
//解析modulePath 格式为/a/b/c
String[] split = item.split("/");
@ -271,82 +316,9 @@ public class ApiDefinitionImportUtilService {
}
}
});
}
//创建模块
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
ApiDefinitionModuleMapper moduleMapper = sqlSession.getMapper(ApiDefinitionModuleMapper.class);
ApiDefinitionMapper apiMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiDefinitionBlobMapper apiBlobMapper = sqlSession.getMapper(ApiDefinitionBlobMapper.class);
addModuleList.forEach(t -> {
ApiDefinitionModule module = new ApiDefinitionModule();
module.setId(t.getId());
module.setName(t.getName());
module.setParentId(t.getParentId());
module.setProjectId(request.getProjectId());
module.setCreateUser(request.getUserId());
module.setPos(getImportNextModuleOrder(request.getProjectId()));
module.setCreateTime(System.currentTimeMillis());
module.setUpdateUser(request.getUserId());
module.setUpdateTime(System.currentTimeMillis());
moduleMapper.insertSelective(module);
});
//取出需要更新的数据的id
List<String> updateModuleLists = updateModuleData.stream().map(ApiDefinitionImportDetail::getId).toList();
//更新模块数据
updateModuleData.forEach(t -> {
ApiDefinition apiDefinition = new ApiDefinition();
apiDefinition.setId(t.getId());
apiDefinition.setModuleId(modulePathMap.get(t.getModulePath()).getId());
apiDefinition.setUpdateUser(request.getUserId());
apiDefinition.setUpdateTime(System.currentTimeMillis());
apiMapper.updateByPrimaryKeySelective(apiDefinition);
});
List<LogDTO> operationLogs = new ArrayList<>();
List<ApiDefinitionImportDetail> updateRequestData = apiDetailWithDataUpdate.getUpdateRequestData();
updateRequestData.forEach(t -> {
ApiDefinition apiDefinition = new ApiDefinition();
if (CollectionUtils.isNotEmpty(updateModuleLists) && updateModuleLists.contains(t.getId())) {
apiDefinition.setId(t.getId());
apiDefinition.setUpdateUser(request.getUserId());
}
apiDefinition.setUpdateTime(System.currentTimeMillis());
apiMapper.updateByPrimaryKeySelective(apiDefinition);
//更新blob数据
ApiDefinitionBlob apiDefinitionBlob = new ApiDefinitionBlob();
apiDefinitionBlob.setId(t.getId());
apiDefinitionBlob.setRequest(JSON.toJSONBytes(t.getRequest()));
apiDefinitionBlob.setResponse(JSON.toJSONBytes(t.getResponse()));
apiBlobMapper.updateByPrimaryKeySelective(apiDefinitionBlob);
});
Map<String, ApiDefinitionImportDetail> logData = apiDetailWithDataUpdate.getLogData();
Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
List<ApiDefinitionCaseDTO> updateLists = new ArrayList<>();
if (MapUtils.isNotEmpty(logData)) {
logData.forEach((k, v) -> {
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
BeanUtils.copyBean(apiDefinitionDTO, v);
ApiDefinitionCaseDTO apiDefinitionCaseDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(apiDefinitionCaseDTO, v);
updateLists.add(apiDefinitionCaseDTO);
LogDTO dto = new LogDTO(
project.getId(),
project.getOrganizationId(),
v.getId(),
request.getUserId(),
OperationLogType.UPDATE.name(),
OperationLogModule.API_TEST_MANAGEMENT_DEFINITION,
v.getName());
dto.setHistory(true);
dto.setPath("/api/definition/import");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(apiDefinitionDTO));
operationLogs.add(dto);
});
}
List<ApiDefinitionCaseDTO> createLists = new ArrayList<>();
private void addApiAndLog(Map<String, BaseTreeNode> modulePathMap, ImportRequest request, List<ApiDefinitionImportDetail> addModuleData, ApiDefinitionMapper apiMapper, ApiDefinitionBlobMapper apiBlobMapper, Project project, List<LogDTO> operationLogs, List<ApiDefinitionCaseDTO> createLists) {
addModuleData.forEach(t -> {
ApiDefinition apiDefinition = new ApiDefinition();
BeanUtils.copyBean(apiDefinition, t);
@ -391,16 +363,76 @@ public class ApiDefinitionImportUtilService {
BeanUtils.copyBean(apiDefinitionCaseDTO, t);
createLists.add(apiDefinitionCaseDTO);
});
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
SubListUtils.dealForSubList(operationLogs, 500, operationLogService::batchAdd);
//发送通知
List<Map> createResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(createLists), Map.class));
User user = userMapper.selectByPrimaryKey(request.getUserId());
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.CREATE, createResources, user, request.getProjectId());
List<Map> updateResources = new ArrayList<>(JSON.parseArray(JSON.toJSONString(updateLists), Map.class));
commonNoticeSendService.sendNotice(NoticeConstants.TaskType.API_DEFINITION_TASK, NoticeConstants.Event.UPDATE, updateResources, user, request.getProjectId());
private static void getUpdateLog(ImportRequest request, Map<String, ApiDefinitionImportDetail> logData, List<ApiDefinitionCaseDTO> updateLists, Project project, List<LogDTO> operationLogs) {
if (MapUtils.isNotEmpty(logData)) {
logData.forEach((k, v) -> {
ApiDefinitionDTO apiDefinitionDTO = new ApiDefinitionDTO();
BeanUtils.copyBean(apiDefinitionDTO, v);
ApiDefinitionCaseDTO apiDefinitionCaseDTO = new ApiDefinitionCaseDTO();
BeanUtils.copyBean(apiDefinitionCaseDTO, v);
updateLists.add(apiDefinitionCaseDTO);
LogDTO dto = new LogDTO(
project.getId(),
project.getOrganizationId(),
v.getId(),
request.getUserId(),
OperationLogType.UPDATE.name(),
OperationLogModule.API_TEST_MANAGEMENT_DEFINITION,
v.getName());
dto.setHistory(true);
dto.setPath("/api/definition/import");
dto.setMethod(HttpMethodConstants.POST.name());
dto.setOriginalValue(JSON.toJSONBytes(apiDefinitionDTO));
operationLogs.add(dto);
});
}
}
private static void updateApiRequest(ImportRequest request, List<ApiDefinitionImportDetail> updateRequestData, List<String> updateModuleLists, ApiDefinitionMapper apiMapper, ApiDefinitionBlobMapper apiBlobMapper) {
updateRequestData.forEach(t -> {
ApiDefinition apiDefinition = new ApiDefinition();
if (CollectionUtils.isNotEmpty(updateModuleLists) && updateModuleLists.contains(t.getId())) {
apiDefinition.setId(t.getId());
apiDefinition.setUpdateUser(request.getUserId());
}
apiDefinition.setUpdateTime(System.currentTimeMillis());
apiMapper.updateByPrimaryKeySelective(apiDefinition);
//更新blob数据
ApiDefinitionBlob apiDefinitionBlob = new ApiDefinitionBlob();
apiDefinitionBlob.setId(t.getId());
apiDefinitionBlob.setRequest(JSON.toJSONBytes(t.getRequest()));
apiDefinitionBlob.setResponse(JSON.toJSONBytes(t.getResponse()));
apiBlobMapper.updateByPrimaryKeySelective(apiDefinitionBlob);
});
}
private static void updateApiModule(Map<String, BaseTreeNode> modulePathMap, ImportRequest request, List<ApiDefinitionImportDetail> updateModuleData, ApiDefinitionMapper apiMapper) {
updateModuleData.forEach(t -> {
ApiDefinition apiDefinition = new ApiDefinition();
apiDefinition.setId(t.getId());
apiDefinition.setModuleId(modulePathMap.get(t.getModulePath()).getId());
apiDefinition.setUpdateUser(request.getUserId());
apiDefinition.setUpdateTime(System.currentTimeMillis());
apiMapper.updateByPrimaryKeySelective(apiDefinition);
});
}
private void insertModule(ImportRequest request, List<BaseTreeNode> addModuleList, ApiDefinitionModuleMapper moduleMapper) {
addModuleList.forEach(t -> {
ApiDefinitionModule module = new ApiDefinitionModule();
module.setId(t.getId());
module.setName(t.getName());
module.setParentId(t.getParentId());
module.setProjectId(request.getProjectId());
module.setCreateUser(request.getUserId());
module.setPos(getImportNextModuleOrder(request.getProjectId()));
module.setCreateTime(System.currentTimeMillis());
module.setUpdateUser(request.getUserId());
module.setUpdateTime(System.currentTimeMillis());
moduleMapper.insertSelective(module);
});
}
private void getNeedUpdateData(ImportRequest request, ApiDetailWithData apiDetailWithData, ApiDetailWithDataUpdate apiDetailWithDataUpdate) {

View File

@ -2212,7 +2212,9 @@ public class ApiScenarioService extends MoveNodeService {
// config 转换成对象
steps.forEach(step -> {
if (step.getConfig() != null && StringUtils.isNotBlank(step.getConfig().toString())) {
step.setConfig(JSON.parseObject(step.getConfig().toString()));
if (step.getConfig() instanceof String configVal) {
step.setConfig(JSON.parseObject(configVal));
}
}
});
@ -2595,74 +2597,86 @@ public class ApiScenarioService extends MoveNodeService {
ScenarioSystemRequest scenarioRequest = request.getScenarioRequest();
if (ObjectUtils.isNotEmpty(apiRequest)) {
if (CollectionUtils.isNotEmpty(apiRequest.getModuleIds())) {
apiRequest.getSelectedIds().addAll(extApiDefinitionMapper.getIdsByModules(apiRequest));
}
apiRequest.getSelectedIds().removeAll(apiRequest.getUnselectedIds());
List<@NotBlank String> allApiIds = apiRequest.getSelectedIds().stream().distinct().toList();
SubListUtils.dealForSubList(allApiIds, 100, sublist -> {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andIdIn(sublist);
List<ApiDefinition> apiDefinitions = apiDefinitionMapper.selectByExample(example);
apiDefinitions.forEach(item -> {
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API.name());
step.setName(item.getName());
step.setResourceId(item.getId());
step.setRefType(request.getRefType());
step.setProjectId(item.getProjectId());
step.setResourceNum(item.getNum().toString());
step.setVersionId(item.getVersionId());
steps.add(step);
});
});
getApiStep(request, apiRequest, steps);
}
if (ObjectUtils.isNotEmpty(caseRequest)) {
if (CollectionUtils.isNotEmpty(caseRequest.getModuleIds())) {
caseRequest.getSelectedIds().addAll(extApiTestCaseMapper.getIdsByModules(caseRequest));
}
caseRequest.getSelectedIds().removeAll(caseRequest.getUnselectedIds());
List<@NotBlank String> allCaseIds = caseRequest.getSelectedIds().stream().distinct().toList();
SubListUtils.dealForSubList(allCaseIds, 100, sublist -> {
ApiTestCaseExample example = new ApiTestCaseExample();
example.createCriteria().andIdIn(sublist);
List<ApiTestCase> apiTestCases = apiTestCaseMapper.selectByExample(example);
apiTestCases.forEach(item -> {
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API_CASE.name());
step.setName(item.getName());
step.setResourceId(item.getId());
step.setRefType(request.getRefType());
step.setProjectId(item.getProjectId());
step.setResourceNum(item.getNum().toString());
step.setVersionId(item.getVersionId());
steps.add(step);
});
});
getCaseStep(request, caseRequest, steps);
}
if (ObjectUtils.isNotEmpty(scenarioRequest)) {
if (CollectionUtils.isNotEmpty(scenarioRequest.getModuleIds())) {
scenarioRequest.getSelectedIds().addAll(extApiScenarioMapper.getIdsByModules(scenarioRequest));
}
scenarioRequest.getSelectedIds().removeAll(scenarioRequest.getUnselectedIds());
List<@NotBlank String> allScenario = scenarioRequest.getSelectedIds().stream().distinct().toList();
allScenario.forEach(item -> {
ApiScenarioDetail apiScenarioDetail = get(item);
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API_SCENARIO.name());
step.setName(apiScenarioDetail.getName());
step.setResourceId(apiScenarioDetail.getId());
step.setRefType(request.getRefType());
step.setProjectId(apiScenarioDetail.getProjectId());
step.setResourceNum(apiScenarioDetail.getNum().toString());
step.setVersionId(apiScenarioDetail.getVersionId());
step.setChildren(apiScenarioDetail.getSteps());
steps.add(step);
});
getScenarioStep(request, scenarioRequest, steps);
}
return steps;
}
private void getScenarioStep(ApiScenarioSystemRequest request, ScenarioSystemRequest scenarioRequest, List<ApiScenarioStepDTO> steps) {
if (CollectionUtils.isNotEmpty(scenarioRequest.getModuleIds())) {
scenarioRequest.getSelectedIds().addAll(extApiScenarioMapper.getIdsByModules(scenarioRequest));
}
scenarioRequest.getSelectedIds().removeAll(scenarioRequest.getUnselectedIds());
List<@NotBlank String> allScenario = scenarioRequest.getSelectedIds().stream().distinct().toList();
allScenario.forEach(item -> {
ApiScenarioDetail apiScenarioDetail = get(item);
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API_SCENARIO.name());
step.setName(apiScenarioDetail.getName());
step.setResourceId(apiScenarioDetail.getId());
step.setRefType(request.getRefType());
step.setProjectId(apiScenarioDetail.getProjectId());
step.setResourceNum(apiScenarioDetail.getNum().toString());
step.setVersionId(apiScenarioDetail.getVersionId());
step.setChildren(apiScenarioDetail.getSteps());
steps.add(step);
});
}
private void getCaseStep(ApiScenarioSystemRequest request, ScenarioSystemRequest caseRequest, List<ApiScenarioStepDTO> steps) {
if (CollectionUtils.isNotEmpty(caseRequest.getModuleIds())) {
caseRequest.getSelectedIds().addAll(extApiTestCaseMapper.getIdsByModules(caseRequest));
}
caseRequest.getSelectedIds().removeAll(caseRequest.getUnselectedIds());
List<@NotBlank String> allCaseIds = caseRequest.getSelectedIds().stream().distinct().toList();
SubListUtils.dealForSubList(allCaseIds, 100, sublist -> {
ApiTestCaseExample example = new ApiTestCaseExample();
example.createCriteria().andIdIn(sublist);
List<ApiTestCase> apiTestCases = apiTestCaseMapper.selectByExample(example);
apiTestCases.forEach(item -> {
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API_CASE.name());
step.setName(item.getName());
step.setResourceId(item.getId());
step.setRefType(request.getRefType());
step.setProjectId(item.getProjectId());
step.setResourceNum(item.getNum().toString());
step.setVersionId(item.getVersionId());
steps.add(step);
});
});
}
private void getApiStep(ApiScenarioSystemRequest request, ScenarioSystemRequest apiRequest, List<ApiScenarioStepDTO> steps) {
if (CollectionUtils.isNotEmpty(apiRequest.getModuleIds())) {
apiRequest.getSelectedIds().addAll(extApiDefinitionMapper.getIdsByModules(apiRequest));
}
apiRequest.getSelectedIds().removeAll(apiRequest.getUnselectedIds());
List<@NotBlank String> allApiIds = apiRequest.getSelectedIds().stream().distinct().toList();
SubListUtils.dealForSubList(allApiIds, 100, sublist -> {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andIdIn(sublist);
List<ApiDefinition> apiDefinitions = apiDefinitionMapper.selectByExample(example);
apiDefinitions.forEach(item -> {
ApiScenarioStepDTO step = new ApiScenarioStepDTO();
step.setStepType(ApiScenarioStepType.API.name());
step.setName(item.getName());
step.setResourceId(item.getId());
step.setRefType(request.getRefType());
step.setProjectId(item.getProjectId());
step.setResourceNum(item.getNum().toString());
step.setVersionId(item.getVersionId());
steps.add(step);
});
});
}
@Override
public void updatePos(String id, long pos) {
extApiScenarioMapper.updatePos(id, pos);