refactor: 接口导入的时候同时根据id判断是否是同一个用例

This commit is contained in:
chenjianxing 2021-09-16 11:38:03 +08:00 committed by jianxing
parent 549971b006
commit e76f83a379
2 changed files with 35 additions and 15 deletions

View File

@ -105,8 +105,6 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
private void parseApiDefinition(ApiDefinitionWithBLOBs apiDefinition, ApiTestImportRequest importRequest,
Map<String, List<ApiTestCaseWithBLOBs>> caseMap, Map<String, NodeTree> nodeMap) {
String originId = apiDefinition.getId();
String id = UUID.randomUUID().toString();
if (nodeMap != null && nodeMap.get(apiDefinition.getModuleId()) != null) {
NodeTree nodeTree = nodeMap.get(apiDefinition.getModuleId());
apiDefinition.setModuleId(nodeTree.getNewId());
@ -119,11 +117,10 @@ public class MsDefinitionParser extends MsAbstractParser<ApiDefinitionImport> {
parseModule(apiDefinition.getModulePath(), importRequest, apiDefinition);
}
apiDefinition.setId(id);
apiDefinition.setProjectId(this.projectId);
String request = apiDefinition.getRequest();
JSONObject requestObj = JSONObject.parseObject(request);
requestObj.put("id", id);
// requestObj.put("id", id);
apiDefinition.setRequest(JSONObject.toJSONString(requestObj));
apiDefinition.setCreateUser(SessionUtils.getUserId());
apiDefinition.setUserId(SessionUtils.getUserId());

View File

@ -336,6 +336,14 @@ public class ApiDefinitionService {
}
}
private List<ApiDefinition> getSameRequestById(String id, String projectId) {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria().andStatusNotEqualTo("Trash")
.andProjectIdEqualTo(projectId)
.andIdEqualTo(id);
return apiDefinitionMapper.selectByExample(example);
}
private List<ApiDefinition> getSameRequestWithName(SaveApiDefinitionRequest request) {
ApiDefinitionExample example = new ApiDefinitionExample();
example.createCriteria()
@ -473,6 +481,9 @@ public class ApiDefinitionService {
// 如果勾选了允许重复则判断更新要加上name字段
sameRequest = getSameRequestWithName(saveReq);
}
if (CollectionUtils.isEmpty(sameRequest)) {
sameRequest = getSameRequestById(apiDefinition.getId(), apiTestImportRequest.getProjectId());
}
if (StringUtils.equals("fullCoverage", apiTestImportRequest.getModeId())) {
_importCreate(sameRequest, batchMapper, apiDefinition, apiTestCaseMapper, apiTestImportRequest, cases);
} else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) {
@ -513,23 +524,25 @@ public class ApiDefinitionService {
private void _importCreate(List<ApiDefinition> sameRequest, ApiDefinitionMapper batchMapper, ApiDefinitionWithBLOBs apiDefinition,
ApiTestCaseMapper apiTestCaseMapper, ApiTestImportRequest apiTestImportRequest, List<ApiTestCaseWithBLOBs> cases) {
String originId = apiDefinition.getId();
if (CollectionUtils.isEmpty(sameRequest)) {
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
String request = setImportHashTree(apiDefinition);
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
apiDefinition.setId(UUID.randomUUID().toString());
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
batchMapper.insert(apiDefinition);
apiDefinition.setRequest(request);
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, true);
} else {
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
String request = setImportTCPHashTree(apiDefinition);
setImportTCPHashTree(apiDefinition);
}
apiDefinition.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId()));
batchMapper.insert(apiDefinition);
}
} else {
String originId = apiDefinition.getId();
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
//如果存在则修改
apiDefinition.setId(sameRequest.get(0).getId());
@ -540,27 +553,37 @@ public class ApiDefinitionService {
apiDefinition.setOrder(sameRequest.get(0).getOrder());
apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
apiDefinition.setRequest(request);
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
importApiCase(apiDefinition, apiTestCaseMapper, apiTestImportRequest, false);
// 如果是带用例导出重新设置接口id
if (CollectionUtils.isNotEmpty(cases)) {
cases.forEach(item -> {
if (StringUtils.equals(item.getApiDefinitionId(), originId)) {
item.setApiDefinitionId(apiDefinition.getId());
}
});
}
} else {
apiDefinition.setId(sameRequest.get(0).getId());
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
String request = setImportTCPHashTree(apiDefinition);
setImportTCPHashTree(apiDefinition);
}
apiDefinition.setOrder(sameRequest.get(0).getOrder());
reSetImportCasesApiId(cases, originId, apiDefinition.getId());
apiDefinitionMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
}
}
}
/**
* 如果是MS格式带用例导出最后创建用例重新设置接口id
* @param cases
* @param originId
* @param newId
*/
private void reSetImportCasesApiId(List<ApiTestCaseWithBLOBs> cases, String originId, String newId) {
if (CollectionUtils.isNotEmpty(cases)) {
cases.forEach(item -> {
if (StringUtils.equals(item.getApiDefinitionId(), originId)) {
item.setApiDefinitionId(newId);
}
});
}
}
private String setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) {
String request = apiDefinition.getRequest();
MsHTTPSamplerProxy msHTTPSamplerProxy = JSONObject.parseObject(request, MsHTTPSamplerProxy.class);