refactor(接口测试): 拆分接口和UI报告的删除逻辑

This commit is contained in:
chenjianxing 2022-06-22 19:50:11 +08:00 committed by jianxing
parent a5d57bcde0
commit dff8015412
1 changed files with 76 additions and 102 deletions

View File

@ -35,7 +35,6 @@ import io.metersphere.xmind.utils.FileUtil;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -616,32 +615,14 @@ public class ApiScenarioReportService {
public void delete(DeleteAPIReportRequest request) {
apiScenarioReportDetailMapper.deleteByPrimaryKey(request.getId());
ApiScenarioReportResultExample example = new ApiScenarioReportResultExample();
example.createCriteria().andReportIdEqualTo(request.getId());
apiScenarioReportResultMapper.deleteByExample(example);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdEqualTo(request.getId());
apiScenarioReportStructureMapper.deleteByExample(structureExample);
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getId());
if (BooleanUtils.isNotTrue(request.getIsUi())) {
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdEqualTo(request.getId());
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
deleteScenarioReportResource(request.getId());
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdEqualTo(request.getId());
definitionExecResultMapper.deleteByExample(execResultExample);
}else{
// UI 类型报告需要删除报告产生的截图
List<String> ids = new ArrayList<>();
ids.add(request.getId());
this.cleanUpUiReportImg(ids);
}
deleteApiDefinitionResult(report.getId());
// 补充逻辑如果是集成报告则把零时报告全部删除
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(request.getId());
if (report != null && StringUtils.isNotEmpty(report.getScenarioId())) {
List<String> list = getReportIds(report.getScenarioId());
if (CollectionUtils.isNotEmpty(list)) {
@ -651,7 +632,20 @@ public class ApiScenarioReportService {
this.deleteAPIReportBatch(reportRequest);
}
}
apiScenarioReportMapper.deleteByPrimaryKey(request.getId());
}
public void deleteScenarioReportResource(String id) {
apiScenarioReportMapper.deleteByPrimaryKey(id);
apiScenarioReportDetailMapper.deleteByPrimaryKey(id);
ApiScenarioReportResultExample example = new ApiScenarioReportResultExample();
example.createCriteria().andReportIdEqualTo(id);
apiScenarioReportResultMapper.deleteByExample(example);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdEqualTo(id);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
}
public void delete(String id) {
@ -677,32 +671,61 @@ public class ApiScenarioReportService {
public void deleteByIds(List<String> ids) {
if (CollectionUtils.isNotEmpty(ids)) {
ApiScenarioReportExample example = new ApiScenarioReportExample();
example.createCriteria().andIdIn(ids);
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
apiScenarioReportMapper.deleteByExample(example);
ApiScenarioReportResultExample reportResultExample = new ApiScenarioReportResultExample();
reportResultExample.createCriteria().andReportIdIn(ids);
apiScenarioReportResultMapper.deleteByExample(reportResultExample);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdIn(ids);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdIn(ids);
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdIn(ids);
definitionExecResultMapper.deleteByExample(execResultExample);
deleteScenarioReportByIds(ids);
deleteApiDefinitionResultByIds(ids);
}
}
public void deleteAPIReportBatch(APIReportBatchRequest reportRequest) {
List<String> ids = getIdsByDeleteBatchRequest(reportRequest);
ids = batchDeleteReportResource(reportRequest, ids, true);
//处理最后剩余的数据
if (!ids.isEmpty()) {
deleteScenarioReportByIds(ids);
deleteApiDefinitionResultByIds(ids);
}
}
public void deleteScenarioReportByIds(List<String> ids) {
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
ApiScenarioReportExample apiTestReportExample = new ApiScenarioReportExample();
apiTestReportExample.createCriteria().andIdIn(ids);
apiScenarioReportMapper.deleteByExample(apiTestReportExample);
ApiScenarioReportResultExample reportResultExample = new ApiScenarioReportResultExample();
reportResultExample.createCriteria().andReportIdIn(ids);
apiScenarioReportResultMapper.deleteByExample(reportResultExample);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdIn(ids);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
}
private void deleteApiDefinitionResultByIds(List<String> ids) {
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdIn(ids);
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdIn(ids);
definitionExecResultMapper.deleteByExample(execResultExample);
}
private void deleteApiDefinitionResult(String id) {
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdEqualTo(id);
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdEqualTo(id);
definitionExecResultMapper.deleteByExample(execResultExample);
}
public List<String> getIdsByDeleteBatchRequest(APIReportBatchRequest reportRequest) {
List<String> ids = reportRequest.getIds();
if (reportRequest.isSelectAllDate()) {
ids = this.idList(reportRequest);
@ -710,16 +733,18 @@ public class ApiScenarioReportService {
ids.removeAll(reportRequest.getUnSelectIds());
}
}
return ids;
}
public List<String> batchDeleteReportResource(APIReportBatchRequest reportRequest, List<String> ids, boolean deleteApiResult) {
List<String> myList = reportRequest.getIds().stream().distinct().collect(Collectors.toList());
reportRequest.setIds(myList);
//为预防数量太多调用删除方法时引起SQL过长的Bug此处采取分批执行的方式
//每次处理的数据数量
int handleCount = 7000;
//每次处理的集合
List<String> handleIdList = new ArrayList<>(handleCount);
while (ids.size() > handleCount) {
handleIdList = new ArrayList<>(handleCount);
List<String> handleIdList = new ArrayList<>(handleCount);
List<String> otherIdList = new ArrayList<>();
for (int index = 0; index < ids.size(); index++) {
if (index < handleCount) {
@ -729,67 +754,16 @@ public class ApiScenarioReportService {
}
}
//处理本次的数据
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(handleIdList);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
ApiScenarioReportExample apiTestReportExample = new ApiScenarioReportExample();
apiTestReportExample.createCriteria().andIdIn(handleIdList);
apiScenarioReportMapper.deleteByExample(apiTestReportExample);
deleteScenarioReportByIds(handleIdList);
ApiScenarioReportResultExample reportResultExample = new ApiScenarioReportResultExample();
reportResultExample.createCriteria().andReportIdIn(handleIdList);
apiScenarioReportResultMapper.deleteByExample(reportResultExample);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdIn(handleIdList);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
if (BooleanUtils.isNotTrue(reportRequest.getIsUi())) {
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdIn(handleIdList);
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdIn(handleIdList);
definitionExecResultMapper.deleteByExample(execResultExample);
if (deleteApiResult) {
deleteApiDefinitionResultByIds(handleIdList);
}
//转存剩余的数据
ids = otherIdList;
}
if(!BooleanUtils.isNotTrue(reportRequest.getIsUi())){
// UI 类型报告需要删除报告产生的截图
this.cleanUpUiReportImg(ids);
}
//处理最后剩余的数据
if (!ids.isEmpty()) {
ApiScenarioReportDetailExample detailExample = new ApiScenarioReportDetailExample();
detailExample.createCriteria().andReportIdIn(ids);
apiScenarioReportDetailMapper.deleteByExample(detailExample);
ApiScenarioReportExample apiTestReportExample = new ApiScenarioReportExample();
apiTestReportExample.createCriteria().andIdIn(ids);
apiScenarioReportMapper.deleteByExample(apiTestReportExample);
ApiScenarioReportResultExample reportResultExample = new ApiScenarioReportResultExample();
reportResultExample.createCriteria().andReportIdIn(ids);
apiScenarioReportResultMapper.deleteByExample(reportResultExample);
ApiScenarioReportStructureExample structureExample = new ApiScenarioReportStructureExample();
structureExample.createCriteria().andReportIdIn(ids);
apiScenarioReportStructureMapper.deleteByExample(structureExample);
if (BooleanUtils.isNotTrue(reportRequest.getIsUi())) {
ApiDefinitionExecResultExample definitionExecResultExample = new ApiDefinitionExecResultExample();
definitionExecResultExample.createCriteria().andIdIn(ids);
definitionExecResultMapper.deleteByExample(definitionExecResultExample);
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdIn(ids);
definitionExecResultMapper.deleteByExample(execResultExample);
}
}
return ids;
}
public long countByProjectIdAndCreateAndByScheduleInThisWeek(String projectId) {