fix(接口测试): 接口用例导入重构

--user=郭雨琦
--bug=1014439
https://www.tapd.cn/55049933/bugtrace/bugs/view/1155049933001014439
This commit is contained in:
guoyuqi 2022-07-11 15:59:09 +08:00 committed by xiaomeinvG
parent 2f06d5b95a
commit 94e03ffe05
3 changed files with 193 additions and 99 deletions

View File

@ -2,6 +2,7 @@ package io.metersphere.api.dto.definition;
import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiModule;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import lombok.Getter;
import lombok.Setter;
@ -13,5 +14,6 @@ public class UpdateApiModuleDTO {
private List<ApiModule> moduleList;
private List<ApiDefinitionWithBLOBs> needUpdateList;
private List<ApiDefinitionWithBLOBs> definitionWithBLOBs;
private List<ApiTestCaseWithBLOBs> caseWithBLOBs;
}

View File

@ -784,10 +784,10 @@ public class ApiDefinitionService {
}
}
private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper, ApiTestCaseMapper apiTestCaseMapper,
private ApiDefinition importCreate(ApiDefinitionWithBLOBs apiDefinition, ApiDefinitionMapper batchMapper,
ExtApiDefinitionMapper extApiDefinitionMapper,
ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks,
List<ApiDefinitionWithBLOBs> updateList) {
ApiTestImportRequest apiTestImportRequest, List<MockConfigImportDTO> mocks,
List<ApiDefinitionWithBLOBs> updateList, ApiTestCaseMapper apiTestCaseMapper, List<ApiTestCaseWithBLOBs> caseList) {
SaveApiDefinitionRequest saveReq = new SaveApiDefinitionRequest();
BeanUtils.copyBean(saveReq, apiDefinition);
apiDefinition.setCreateTime(System.currentTimeMillis());
@ -804,7 +804,7 @@ public class ApiDefinitionService {
List<ApiDefinitionWithBLOBs> collect = updateList.stream().filter(t -> t.getId().equals(apiDefinition.getId())).collect(Collectors.toList());
if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
_importCreate(collect, batchMapper, apiTestCaseMapper, apiDefinition, extApiDefinitionMapper, apiTestImportRequest, cases, mocks);
_importCreate(collect, batchMapper, apiDefinition, extApiDefinitionMapper, apiTestImportRequest, mocks, apiTestCaseMapper, caseList);
} else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) {
if (CollectionUtils.isEmpty(collect)) {
String originId = apiDefinition.getId();
@ -824,25 +824,35 @@ public class ApiDefinitionService {
}
batchMapper.insert(apiDefinition);
String requestStr = setImportHashTree(apiDefinition);
// case 设置版本
cases.forEach(c -> {
c.setVersionId(apiDefinition.getVersionId());
});
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
apiDefinition.setRequest(requestStr);
importApiCase(apiDefinition, apiTestImportRequest, apiTestCaseMapper);
importCase(apiDefinition, apiTestCaseMapper, caseList);
} else {
//不覆盖的接口如果没有sameRequest则不导入此时清空mock信息
mocks.clear();
}
} else {
_importCreate(collect, batchMapper, apiTestCaseMapper, apiDefinition, extApiDefinitionMapper, apiTestImportRequest, cases, mocks);
_importCreate(collect, batchMapper, apiDefinition, extApiDefinitionMapper, apiTestImportRequest, mocks, apiTestCaseMapper, caseList);
}
return apiDefinition;
}
private void importCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, List<ApiTestCaseWithBLOBs> caseList) {
for (ApiTestCaseWithBLOBs apiTestCaseWithBLOBs : caseList) {
apiTestCaseWithBLOBs.setApiDefinitionId(apiDefinition.getId());
apiTestCaseWithBLOBs.setVersionId(apiDefinition.getVersionId());
if (StringUtils.isNotBlank(apiTestCaseWithBLOBs.getId())) {
apiTestCaseMapper.updateByPrimaryKeyWithBLOBs(apiTestCaseWithBLOBs);
} else {
apiTestCaseWithBLOBs.setId(UUID.randomUUID().toString());
apiTestCaseMapper.insert(apiTestCaseWithBLOBs);
}
}
}
public Long getImportNextOrder(String projectId) {
Long order = currentApiOrder.get();
if (order == null) {
@ -863,9 +873,9 @@ public class ApiDefinitionService {
return order;
}
private void _importCreate(List<ApiDefinitionWithBLOBs> sameRequest, ApiDefinitionMapper batchMapper, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionWithBLOBs apiDefinition,
private void _importCreate(List<ApiDefinitionWithBLOBs> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition,
ExtApiDefinitionMapper extApiDefinitionMapper,
ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases, List<MockConfigImportDTO> mocks) {
ApiTestImportRequest apiTestImportRequest, List<MockConfigImportDTO> mocks, ApiTestCaseMapper apiTestCaseMapper, List<ApiTestCaseWithBLOBs> caseList) {
String originId = apiDefinition.getId();
if (CollectionUtils.isEmpty(sameRequest)) { // 没有这个接口 新增
@ -877,25 +887,19 @@ public class ApiDefinitionService {
apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion());
}
apiDefinition.setLatest(true); // 新增接口 latest = true
// case 设置版本
cases.forEach(c -> {
c.setVersionId(apiDefinition.getVersionId());
});
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
batchMapper.insert(apiDefinition);
String request = setImportHashTree(apiDefinition);
apiDefinition.setRequest(request);
importApiCase(apiDefinition, apiTestImportRequest, apiTestCaseMapper);
} else {
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
setImportTCPHashTree(apiDefinition);
}
batchMapper.insert(apiDefinition);
}
importCase(apiDefinition, apiTestCaseMapper, caseList);
} else { //如果存在则修改
if (StringUtils.isEmpty(apiTestImportRequest.getUpdateVersionId())) {
apiTestImportRequest.setUpdateVersionId(apiTestImportRequest.getDefaultVersion());
@ -918,6 +922,7 @@ public class ApiDefinitionService {
apiDefinition.setUserId(sameRequest.get(0).getUserId());
}
batchMapper.insert(apiDefinition);
importCase(apiDefinition, apiTestCaseMapper, caseList);
} else {
ApiDefinitionWithBLOBs existApi = apiOp.get();
apiDefinition.setStatus(existApi.getStatus());
@ -940,13 +945,7 @@ public class ApiDefinitionService {
apiDefinition.setUpdateTime(existApi.getUpdateTime());
}
}
// case 设置版本
cases.forEach(c -> {
c.setVersionId(apiDefinition.getVersionId());
if (apiDefinition.getToBeUpdated() != null && apiDefinition.getToBeUpdated()) {
c.setToBeUpdated(true);
}
});
if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) {
apiDefinition.setTags(existApi.getTags()); // 其他格式 tag 不变MS 格式替换
}
@ -957,19 +956,17 @@ public class ApiDefinitionService {
apiDefinition.setOrder(existApi.getOrder());
batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
apiDefinition.setRequest(request);
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
importApiCase(apiDefinition, apiTestImportRequest, apiTestCaseMapper);
} else {
apiDefinition.setId(existApi.getId());
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
setImportTCPHashTree(apiDefinition);
}
apiDefinition.setOrder(existApi.getOrder());
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
}
importCase(apiDefinition, apiTestCaseMapper, caseList);
}
extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId());
extApiDefinitionMapper.addLatestVersion(apiDefinition.getRefId());
@ -1467,6 +1464,8 @@ public class ApiDefinitionService {
List<ApiDefinitionWithBLOBs> updateList = updateApiModuleDTO.getNeedUpdateList();
List<ApiDefinitionWithBLOBs> data = updateApiModuleDTO.getDefinitionWithBLOBs();
List<ApiModule> moduleList = updateApiModuleDTO.getModuleList();
List<ApiTestCaseWithBLOBs> caseWithBLOBs = updateApiModuleDTO.getCaseWithBLOBs();
Map<String, List<ApiTestCaseWithBLOBs>> apiIdCaseMap = caseWithBLOBs.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId));
ApiDefinitionMapper batchMapper = sqlSession.getMapper(ApiDefinitionMapper.class);
ApiTestCaseMapper apiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class);
@ -1489,6 +1488,7 @@ public class ApiDefinitionService {
}
for (int i = 0; i < data.size(); i++) {
ApiDefinitionWithBLOBs item = data.get(i);
List<ApiTestCaseWithBLOBs> caseList = apiIdCaseMap.get(item.getId());
this.setModule(item);
if (item.getName().length() > 255) {
item.setName(item.getName().substring(0, 255));
@ -1502,14 +1502,14 @@ public class ApiDefinitionService {
String apiId = item.getId();
EsbApiParamsWithBLOBs model = apiImport.getEsbApiParamsMap().get(apiId);
request.setModeId("fullCoverage");//标准版ESB数据导入不区分是否覆盖默认都为覆盖
importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), updateList);
importCreate(item, batchMapper, extApiDefinitionMapper, request, apiImport.getMocks(), updateList, apiTestCaseMapper, caseList);
if (model != null) {
apiImport.getEsbApiParamsMap().remove(apiId);
model.setResourceId(item.getId());
apiImport.getEsbApiParamsMap().put(item.getId(), model);
}
} else {
importCreate(item, batchMapper, apiTestCaseMapper, extApiDefinitionMapper, request, apiImport.getCases(), apiImport.getMocks(), updateList);
importCreate(item, batchMapper, extApiDefinitionMapper, request, apiImport.getMocks(), updateList, apiTestCaseMapper, caseList);
}
if (i % 300 == 0) {
sqlSession.flushStatements();
@ -1537,9 +1537,6 @@ public class ApiDefinitionService {
mockConfigService.importMock(apiImport, sqlSession, request);
}
if (!CollectionUtils.isEmpty(apiImport.getCases())) {
importMsCase(apiImport, sqlSession, request, apiTestCaseMapper);
}
if (sqlSession != null && sqlSessionFactory != null) {
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
}

View File

@ -8,6 +8,7 @@ import io.metersphere.api.dto.definition.parse.ApiDefinitionImport;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiDefinitionMapper;
import io.metersphere.base.mapper.ApiModuleMapper;
import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionMapper;
import io.metersphere.base.mapper.ext.ExtApiModuleMapper;
import io.metersphere.commons.constants.TestCaseConstants;
@ -37,7 +38,6 @@ import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;
@Service
@ -60,6 +60,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
private ApiTestCaseService apiTestCaseService;
@Resource
private ApiDefinitionService apiDefinitionService;
@Resource
private ApiTestCaseMapper apiTestCaseMapper;
@Resource
SqlSessionFactory sqlSessionFactory;
@ -451,23 +453,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
}
public ApiModule getModuleByNameAndLevel(String projectId, String protocol, String name, Integer level) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol).andNameEqualTo(name).andLevelEqualTo(level);
List<ApiModule> modules = apiModuleMapper.selectByExample(example);
if (CollectionUtils.isNotEmpty(modules)) {
return modules.get(0);
} else {
return null;
}
}
public List<ApiModule> getMListByProAndProtocol(String projectId, String protocol) {
ApiModuleExample example = new ApiModuleExample();
example.createCriteria().andProjectIdEqualTo(projectId).andProtocolEqualTo(protocol);
return apiModuleMapper.selectByExample(example);
}
public String getLogDetails(List<String> ids) {
ApiModuleExample example = new ApiModuleExample();
ApiModuleExample.Criteria criteria = example.createCriteria();
@ -649,7 +634,13 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (chooseModuleId != null) {
chooseModule = idModuleMap.get(chooseModuleId);
}
//导入的case,导入的接口是有ID的所以导入的case已经标记过一遍接口ID了,这里是处理覆盖时接口ID的变动
List<ApiTestCaseWithBLOBs> importCases = apiImport.getCases();
List<ApiTestCaseWithBLOBs> optionDataCases = new ArrayList<>();
//将ID全部置于null,覆盖的时候会增加ID用以区分更新还是新增
removeRepeatCase(fullCoverage, importCases, optionDataCases);
Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp = optionDataCases.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId));
List<ApiDefinitionWithBLOBs> optionData = new ArrayList<>();
List<ApiDefinitionWithBLOBs> repeatApiDefinitionWithBLOBs;
if (protocol.equals("HTTP")) {
@ -661,6 +652,12 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
setModule(moduleMap, pidChildrenMap, idPathMap, idModuleMap, optionData, chooseModule);
//系统内重复的数据
repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByBLOBs(optionData, projectId);
//重复接口的case
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>();
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
oldCaseMap = getOldCaseMap(repeatApiDefinitionWithBLOBs);
}
//处理数据
if (urlRepeat) {
Map<String, ApiDefinitionWithBLOBs> methodPathMap;
@ -679,33 +676,34 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//覆盖接口
if (fullCoverage) {
//允许覆盖模块用导入的重复数据的最后一条覆盖查询的所有重复数据
//允许覆盖模块用导入的重复数据的最后一条覆盖查询的所有重复数据; case 在覆盖的时候是拼接到原来的casename唯一不覆盖就用原来的
if (fullCoverageApi) {
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
startCoverModule(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId);
startCoverModule(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
}
} else {
//覆盖但不覆盖模块
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
moduleMap = judgeModuleMap(moduleMap, methodPathMap, repeatDataMap);
startCover(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId);
startCover(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
}
}
} else {
//不覆盖,同一接口不做更新
//不覆盖,同一接口不做更新;可能创建新版本case也直接创建
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
removeSameData(repeatDataMap, methodPathMap, optionData, moduleMap, versionId);
removeSameData(repeatDataMap, methodPathMap, optionData, moduleMap, versionId, definitionIdCaseMAp, optionDataCases);
}
} //最后在整个体统内检查一遍
}
//最后在整个体统内检查一遍
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
Map<String, List<ApiDefinitionWithBLOBs>> repeatMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath()));
Map<String, ApiDefinitionWithBLOBs> optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getMethod() + t.getPath() + t.getModulePath(), api -> api));
if (fullCoverage) {
startCover(toUpdateList, optionData, optionMap, repeatMap, updateVersionId);
startCover(toUpdateList, optionData, optionMap, repeatMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
} else {
//不覆盖,同一接口不做更新
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
removeSameData(repeatMap, optionMap, optionData, moduleMap, versionId);
removeSameData(repeatMap, optionMap, optionData, moduleMap, versionId, definitionIdCaseMAp, optionDataCases);
}
}
}
@ -719,7 +717,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (fullCoverage) {
if (fullCoverageApi) {
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
startCoverModule(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId);
startCoverModule(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId, oldCaseMap, definitionIdCaseMAp);
}
} else {
//不覆盖模块
@ -728,13 +726,13 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//导入文件没有新增接口无需创建接口模块
moduleMap = new HashMap<>();
}
startCover(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId);
startCover(toUpdateList, optionData, methodPathMap, repeatDataMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
}
}
} else {
//不覆盖,同一接口不做更新
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
removeSameData(repeatDataMap, methodPathMap, optionData, moduleMap, versionId);
removeSameData(repeatDataMap, methodPathMap, optionData, moduleMap, versionId, definitionIdCaseMAp, optionDataCases);
}
}
}
@ -750,7 +748,11 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//获取系统内重复数据
repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByProtocol(nameList, protocol, projectId);
//重复接口的case
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>();
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
oldCaseMap = getOldCaseMap(repeatApiDefinitionWithBLOBs);
}
Map<String, ApiDefinitionWithBLOBs> repeatDataMap = null;
Map<String, ApiDefinitionWithBLOBs> nameModuleMap = null;
@ -769,23 +771,23 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//处理数据
if (fullCoverage) {
if (fullCoverageApi) {
coverModule(toUpdateList, nameModuleMap, repeatDataMap, updateVersionId);
coverModule(toUpdateList, nameModuleMap, repeatDataMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
} else {
moduleMap = cover(moduleMap, toUpdateList, nameModuleMap, repeatDataMap, updateVersionId);
moduleMap = cover(moduleMap, toUpdateList, nameModuleMap, repeatDataMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
}
} else {
//不覆盖
removeRepeat(optionData, nameModuleMap, repeatDataMap, moduleMap, versionId);
removeRepeat(optionData, nameModuleMap, repeatDataMap, moduleMap, versionId, definitionIdCaseMAp, optionDataCases);
}
//系统内检查重复
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
Map<String, ApiDefinitionWithBLOBs> repeatMap = repeatApiDefinitionWithBLOBs.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api));
Map<String, ApiDefinitionWithBLOBs> optionMap = optionData.stream().collect(Collectors.toMap(t -> t.getName() + t.getModulePath(), api -> api));
if (fullCoverage) {
cover(moduleMap, toUpdateList, optionMap, repeatMap, updateVersionId);
cover(moduleMap, toUpdateList, optionMap, repeatMap, updateVersionId, definitionIdCaseMAp, oldCaseMap);
} else {
//不覆盖,同一接口不做更新
removeRepeat(optionData, optionMap, repeatMap, moduleMap, versionId);
removeRepeat(optionData, optionMap, repeatMap, moduleMap, versionId, definitionIdCaseMAp, optionDataCases);
}
}
}
@ -793,27 +795,55 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (optionData.isEmpty()) {
moduleMap = new HashMap<>();
}
return getUpdateApiModuleDTO(moduleMap, toUpdateList, optionData);
return getUpdateApiModuleDTO(moduleMap, toUpdateList, optionData, optionDataCases);
}
private void removeRepeatCase(Boolean fullCoverage, List<ApiTestCaseWithBLOBs> importCases, List<ApiTestCaseWithBLOBs> optionDataCases) {
LinkedHashMap<String, List<ApiTestCaseWithBLOBs>> apiIdNameMap = importCases.stream().collect(Collectors.groupingBy(t -> t.getName() + t.getApiDefinitionId(), LinkedHashMap::new, Collectors.toList()));
if (fullCoverage) {
apiIdNameMap.forEach((k, v) -> {
v.get(v.size() - 1).setId(null);
optionDataCases.add(v.get(v.size() - 1));
});
} else {
apiIdNameMap.forEach((k, v) -> {
v.get(0).setId(null);
optionDataCases.add(v.get(0));
});
}
}
private Map<String, List<ApiTestCaseWithBLOBs>> getOldCaseMap(List<ApiDefinitionWithBLOBs> repeatApiDefinitionWithBLOBs) {
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap;
List<String> definitionIds = repeatApiDefinitionWithBLOBs.stream().map(ApiDefinition::getId).collect(Collectors.toList());
ApiTestCaseExample testCaseExample = new ApiTestCaseExample();
testCaseExample.createCriteria().andApiDefinitionIdIn(definitionIds);
List<ApiTestCaseWithBLOBs> caseWithBLOBs = apiTestCaseMapper.selectByExampleWithBLOBs(testCaseExample);
ArrayList<ApiTestCaseWithBLOBs> testCases = getDistinctNameCases(caseWithBLOBs);
oldCaseMap = testCases.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId));
return oldCaseMap;
}
private UpdateApiModuleDTO getUpdateApiModuleDTO(Map<String, ApiModule> moduleMap, List<ApiDefinitionWithBLOBs> toUpdateList, List<ApiDefinitionWithBLOBs> optionData) {
private UpdateApiModuleDTO getUpdateApiModuleDTO(Map<String, ApiModule> moduleMap, List<ApiDefinitionWithBLOBs> toUpdateList, List<ApiDefinitionWithBLOBs> optionData, List<ApiTestCaseWithBLOBs> optionDataCases) {
UpdateApiModuleDTO updateApiModuleDTO = new UpdateApiModuleDTO();
updateApiModuleDTO.setModuleList(new ArrayList<>(moduleMap.values()));
updateApiModuleDTO.setNeedUpdateList(toUpdateList);
updateApiModuleDTO.setDefinitionWithBLOBs(optionData);
updateApiModuleDTO.setCaseWithBLOBs(optionDataCases);
return updateApiModuleDTO;
}
private void removeRepeat(List<ApiDefinitionWithBLOBs> optionData, Map<String, ApiDefinitionWithBLOBs> nameModuleMap,
Map<String, ApiDefinitionWithBLOBs> repeatDataMap, Map<String, ApiModule> moduleMap,
String versionId) {
String versionId, Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp,
List<ApiTestCaseWithBLOBs> optionDataCases) {
if (nameModuleMap != null) {
Map<String, List<ApiDefinitionWithBLOBs>> moduleOptionData = optionData.stream().collect(Collectors.groupingBy(ApiDefinition::getModulePath));
Map<String, ApiDefinitionWithBLOBs> finalNameModuleMap = nameModuleMap;
repeatDataMap.forEach((k, v) -> {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = finalNameModuleMap.get(k);
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = nameModuleMap.get(k);
if (apiDefinitionWithBLOBs != null) {
List<ApiTestCaseWithBLOBs> distinctNameCases = definitionIdCaseMAp.get(apiDefinitionWithBLOBs.getId());
String modulePath = apiDefinitionWithBLOBs.getModulePath();
List<ApiDefinitionWithBLOBs> moduleDatas = moduleOptionData.get(modulePath);
if (moduleDatas != null && moduleDatas.size() <= 1) {
@ -824,30 +854,38 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//不覆盖选择版本如果被选版本有同接口不导入否则创建新版本接口
if (v.getVersionId().equals(versionId)) {
optionData.remove(apiDefinitionWithBLOBs);
distinctNameCases.forEach(optionDataCases::remove);
} else {
//这里是为了标识当前数据是需要创建版本的不是全新增的数据
apiDefinitionWithBLOBs.setVersionId("new");
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
apiDefinitionWithBLOBs.setOrder(v.getOrder());
apiDefinitionWithBLOBs.setRefId(v.getRefId());
apiDefinitionWithBLOBs.setLatest(v.getLatest());
addNewVersionApi(apiDefinitionWithBLOBs, v);
}
}
});
}
}
private void addNewVersionApi(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs, ApiDefinitionWithBLOBs v) {
apiDefinitionWithBLOBs.setVersionId("new");
apiDefinitionWithBLOBs.setNum(v.getNum());
apiDefinitionWithBLOBs.setStatus(v.getStatus());
apiDefinitionWithBLOBs.setOrder(v.getOrder());
apiDefinitionWithBLOBs.setRefId(v.getRefId());
apiDefinitionWithBLOBs.setLatest(v.getLatest());
}
private Map<String, ApiModule> cover(Map<String, ApiModule> moduleMap, List<ApiDefinitionWithBLOBs> toUpdateList,
Map<String, ApiDefinitionWithBLOBs> nameModuleMap, Map<String, ApiDefinitionWithBLOBs> repeatDataMap, String updateVersionId) {
Map<String, ApiDefinitionWithBLOBs> nameModuleMap, Map<String, ApiDefinitionWithBLOBs> repeatDataMap,
String updateVersionId, Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp,
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap) {
//覆盖但不覆盖模块
if (nameModuleMap != null) {
//导入文件没有新增接口无需创建接口模块
moduleMap = judgeModule(moduleMap, nameModuleMap, repeatDataMap);
Map<String, ApiDefinitionWithBLOBs> finalNameModuleMap = nameModuleMap;
repeatDataMap.forEach((k, v) -> {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = finalNameModuleMap.get(k);
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = nameModuleMap.get(k);
if (apiDefinitionWithBLOBs != null) {
//该接口的case
Map<String, ApiTestCaseWithBLOBs> caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs);
apiDefinitionWithBLOBs.setId(v.getId());
apiDefinitionWithBLOBs.setVersionId(updateVersionId);
apiDefinitionWithBLOBs.setModuleId(v.getModuleId());
@ -858,6 +896,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
apiDefinitionWithBLOBs.setRefId(v.getRefId());
apiDefinitionWithBLOBs.setLatest(v.getLatest());
apiDefinitionWithBLOBs.setCreateTime(v.getCreateTime());
//组合case
buildCaseList(oldCaseMap, caseNameMap, v);
toUpdateList.add(apiDefinitionWithBLOBs);
}
});
@ -882,14 +922,19 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
return moduleMap;
}
private void coverModule(List<ApiDefinitionWithBLOBs> toUpdateList, Map<String, ApiDefinitionWithBLOBs> nameModuleMap, Map<String, ApiDefinitionWithBLOBs> repeatDataMap, String updateVersionId) {
private void coverModule(List<ApiDefinitionWithBLOBs> toUpdateList, Map<String, ApiDefinitionWithBLOBs> nameModuleMap,
Map<String, ApiDefinitionWithBLOBs> repeatDataMap, String updateVersionId, Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp,
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap) {
if (nameModuleMap != null) {
Map<String, ApiDefinitionWithBLOBs> finalNameModuleMap = nameModuleMap;
repeatDataMap.forEach((k, v) -> {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = finalNameModuleMap.get(k);
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = nameModuleMap.get(k);
if (apiDefinitionWithBLOBs != null) {
//该接口的case
Map<String, ApiTestCaseWithBLOBs> caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs);
apiDefinitionWithBLOBs.setId(v.getId());
setApiParam(apiDefinitionWithBLOBs, updateVersionId, v);
//组合case
buildCaseList(oldCaseMap, caseNameMap, v);
toUpdateList.add(apiDefinitionWithBLOBs);
}
});
@ -956,12 +1001,14 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
}
private void removeSameData(Map<String, List<ApiDefinitionWithBLOBs>> repeatDataMap, Map<String, ApiDefinitionWithBLOBs> methodPathMap,
List<ApiDefinitionWithBLOBs> optionData, Map<String, ApiModule> moduleMap, String versionId) {
List<ApiDefinitionWithBLOBs> optionData, Map<String, ApiModule> moduleMap, String versionId,
Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp, List<ApiTestCaseWithBLOBs> optionDataCases) {
Map<String, List<ApiDefinitionWithBLOBs>> moduleOptionData = optionData.stream().collect(Collectors.groupingBy(ApiDefinition::getModulePath));
BiConsumer<String, List<ApiDefinitionWithBLOBs>> stringListBiConsumer = (k, v) -> {
repeatDataMap.forEach((k, v) -> {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = methodPathMap.get(k);
if (apiDefinitionWithBLOBs != null) {
List<ApiTestCaseWithBLOBs> distinctNameCases = definitionIdCaseMAp.get(apiDefinitionWithBLOBs.getId());
String modulePath = apiDefinitionWithBLOBs.getModulePath();
List<ApiDefinitionWithBLOBs> moduleDatas = moduleOptionData.get(modulePath);
if (moduleDatas != null && moduleDatas.size() <= 1) {
@ -973,19 +1020,22 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
List<ApiDefinitionWithBLOBs> sameVersionList = v.stream().filter(t -> t.getVersionId().equals(versionId)).collect(Collectors.toList());
if (!sameVersionList.isEmpty()) {
optionData.remove(apiDefinitionWithBLOBs);
distinctNameCases.forEach(optionDataCases::remove);
} else {
for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) {
apiDefinitionWithBLOBs.setVersionId("new");
apiDefinitionWithBLOBs.setNum(definitionWithBLOBs.getNum());
apiDefinitionWithBLOBs.setStatus(definitionWithBLOBs.getStatus());
apiDefinitionWithBLOBs.setOrder(definitionWithBLOBs.getOrder());
apiDefinitionWithBLOBs.setRefId(definitionWithBLOBs.getRefId());
apiDefinitionWithBLOBs.setLatest(definitionWithBLOBs.getLatest());
addNewVersionApi(apiDefinitionWithBLOBs, definitionWithBLOBs);
}
}
}
};
repeatDataMap.forEach(stringListBiConsumer);
});
}
private ArrayList<ApiTestCaseWithBLOBs> getDistinctNameCases(List<ApiTestCaseWithBLOBs> importCases) {
ArrayList<ApiTestCaseWithBLOBs> distinctNameCase = importCases.stream().collect(
Collectors.collectingAndThen(
Collectors.toCollection(() -> new TreeSet<>(Comparator.comparing(ApiTestCase::getName))), ArrayList::new)
);
return distinctNameCase;
}
private void setApiParam(ApiDefinitionWithBLOBs apiDefinitionWithBLOBs, String versionId, ApiDefinitionWithBLOBs definitionWithBLOBs) {
@ -1014,12 +1064,20 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
private void startCoverModule(List<ApiDefinitionWithBLOBs> toUpdateList, List<ApiDefinitionWithBLOBs> optionData,
Map<String, ApiDefinitionWithBLOBs> methodPathMap, Map<String, List<ApiDefinitionWithBLOBs>> repeatDataMap,
String updateVersionId) {
String updateVersionId, Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp,
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap) {
List<ApiDefinitionWithBLOBs> coverApiList = new ArrayList<>();
repeatDataMap.forEach((k, v) -> {
//导入的与系统是相同接口的数据
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = methodPathMap.get(k);
if (apiDefinitionWithBLOBs != null) {
//该接口的case
Map<String, ApiTestCaseWithBLOBs> caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs);
//循环系统内重复接口
for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) {
//组合case
buildCaseList(oldCaseMap, caseNameMap, definitionWithBLOBs);
ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs();
BeanUtils.copyBean(api, apiDefinitionWithBLOBs);
api.setId(definitionWithBLOBs.getId());
@ -1038,14 +1096,51 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
buildOtherParam(toUpdateList, optionData, coverApiList);
}
private void buildCaseList(Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap,
Map<String, ApiTestCaseWithBLOBs> caseNameMap,
ApiDefinitionWithBLOBs definitionWithBLOBs) {
//找出每个接口的case
List<ApiTestCaseWithBLOBs> apiTestCases = oldCaseMap.get(definitionWithBLOBs.getId());
Map<String, ApiTestCaseWithBLOBs> oldCaseNameMap;
if (!apiTestCases.isEmpty()) {
oldCaseNameMap = apiTestCases.stream().collect(Collectors.toMap(ApiTestCase::getName, testCase -> testCase));
caseNameMap.forEach((name, caseWithBLOBs1) -> {
//如果导入的有重名覆盖接口ID替换成系统内的
caseWithBLOBs1.setApiDefinitionId(definitionWithBLOBs.getId());
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs1 = oldCaseNameMap.get(name);
if (apiTestCaseWithBLOBs1 != null) {
caseWithBLOBs1.setId(apiTestCaseWithBLOBs1.getId());
caseWithBLOBs1.setVersion(apiTestCaseWithBLOBs1.getVersion() == null ? 0 : caseWithBLOBs1.getVersion() + 1);
oldCaseNameMap.remove(name);
} else {
caseWithBLOBs1.setVersion(0);
}
});
}
}
private Map<String, ApiTestCaseWithBLOBs> getDistinctCaseNameMap(Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp, ApiDefinitionWithBLOBs apiDefinitionWithBLOBs) {
List<ApiTestCaseWithBLOBs> caseWithBLOBs = definitionIdCaseMAp.get(apiDefinitionWithBLOBs.getId());
return caseWithBLOBs.stream().collect(Collectors.toMap(ApiTestCase::getName, testCase -> testCase));
}
private void startCover(List<ApiDefinitionWithBLOBs> toUpdateList, List<ApiDefinitionWithBLOBs> optionData,
Map<String, ApiDefinitionWithBLOBs> methodPathMap, Map<String, List<ApiDefinitionWithBLOBs>> repeatDataMap,
String updateVersionId) {
String updateVersionId, Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp,
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap) {
List<ApiDefinitionWithBLOBs> coverApiList = new ArrayList<>();
repeatDataMap.forEach((k, v) -> {
ApiDefinitionWithBLOBs apiDefinitionWithBLOBs = methodPathMap.get(k);
if (apiDefinitionWithBLOBs != null) {
//该接口的case
Map<String, ApiTestCaseWithBLOBs> caseNameMap = getDistinctCaseNameMap(definitionIdCaseMAp, apiDefinitionWithBLOBs);
for (ApiDefinitionWithBLOBs definitionWithBLOBs : v) {
//组合case
buildCaseList(oldCaseMap, caseNameMap, definitionWithBLOBs);
ApiDefinitionWithBLOBs api = new ApiDefinitionWithBLOBs();
BeanUtils.copyBean(api, apiDefinitionWithBLOBs);
api.setId(definitionWithBLOBs.getId());