feat(接口测试): 接口导入,如果导入的接口没有用例,且这个接口为新建的,那需要为这个接口新建一个用例

--user
This commit is contained in:
guoyuqi 2022-08-29 16:48:56 +08:00 committed by xiaomeinvG
parent 6ea3e73282
commit 993b566856
1 changed files with 59 additions and 52 deletions

View File

@ -833,9 +833,8 @@ public class ApiDefinitionService {
} else {
apiDefinition.setVersionId(apiTestImportRequest.getDefaultVersion());
}
String requestStr = setImportHashTree(apiDefinition);
caseList = setRequestAndAddNewCase(apiDefinition, caseList, true);
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
apiDefinition.setRequest(requestStr);
batchMapper.insert(apiDefinition);
importCase(apiDefinition, apiTestCaseMapper, caseList);
extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId());
@ -851,8 +850,24 @@ public class ApiDefinitionService {
return apiDefinition;
}
private List<ApiTestCaseWithBLOBs> setRequestAndAddNewCase(ApiDefinitionWithBLOBs apiDefinition, List<ApiTestCaseWithBLOBs> caseList, boolean newCreate) {
boolean createCase = false;
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
createCase = setImportHashTree(apiDefinition);
} else if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
createCase = setImportTCPHashTree(apiDefinition);
}
if (newCreate && createCase && CollectionUtils.isEmpty(caseList)) {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = addNewCase(apiDefinition);
caseList = new ArrayList<>();
caseList.add(apiTestCaseWithBLOBs);
}
return caseList;
}
private void importCase(ApiDefinitionWithBLOBs apiDefinition, ApiTestCaseMapper apiTestCaseMapper, List<ApiTestCaseWithBLOBs> caseList) {
if (caseList == null || caseList.isEmpty()) {
if (CollectionUtils.isEmpty(caseList)) {
return;
}
for (int i = 0; i < caseList.size(); i++) {
@ -939,16 +954,8 @@ public class ApiDefinitionService {
}
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
String request = setImportHashTree(apiDefinition);
apiDefinition.setRequest(request);
caseList = setRequestAndAddNewCase(apiDefinition, caseList, true);
batchMapper.insert(apiDefinition);
} else {
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
setImportTCPHashTree(apiDefinition);
}
batchMapper.insert(apiDefinition);
}
importCase(apiDefinition, apiTestCaseMapper, caseList);
extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId());
extApiDefinitionMapper.addLatestVersion(apiDefinition.getRefId());
@ -997,7 +1004,7 @@ public class ApiDefinitionService {
apiDefinition.setUserId(existApi.getUserId());
}
//Check whether the content has changed, if not, do not change the creation time
if (apiDefinition.getProtocol().equals("HTTP")) {
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
Boolean toChangeTime = checkIsSynchronize(existApi, apiDefinition);
if (toChangeTime) {
apiDefinition.setUpdateTime(System.currentTimeMillis());
@ -1009,23 +1016,11 @@ public class ApiDefinitionService {
if (!StringUtils.equalsIgnoreCase(apiTestImportRequest.getPlatform(), ApiImportPlatform.Metersphere.name())) {
apiDefinition.setTags(existApi.getTags()); // 其他格式 tag 不变MS 格式替换
}
if (StringUtils.equalsIgnoreCase(apiDefinition.getProtocol(), RequestType.HTTP)) {
//如果存在则修改
apiDefinition.setId(existApi.getId());
String request = setImportHashTree(apiDefinition);
apiDefinition.setOrder(existApi.getOrder());
apiDefinition.setRequest(request);
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
} else {
apiDefinition.setId(existApi.getId());
if (StringUtils.equalsAnyIgnoreCase(apiDefinition.getProtocol(), RequestType.TCP)) {
setImportTCPHashTree(apiDefinition);
}
setRequestAndAddNewCase(apiDefinition, caseList, false);
apiDefinition.setOrder(existApi.getOrder());
reSetImportMocksApiId(mocks, originId, apiDefinition.getId(), apiDefinition.getNum());
batchMapper.updateByPrimaryKeyWithBLOBs(apiDefinition);
}
importCase(apiDefinition, apiTestCaseMapper, caseList);
}
extApiDefinitionMapper.clearLatestVersion(apiDefinition.getRefId());
@ -1033,6 +1028,15 @@ public class ApiDefinitionService {
}
}
private ApiTestCaseWithBLOBs addNewCase(ApiDefinitionWithBLOBs apiDefinition) {
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
apiTestCase.setApiDefinitionId(apiDefinition.getId());
apiTestCase.setProjectId(apiDefinition.getProjectId());
apiTestCase.setName(apiDefinition.getName());
apiTestCase.setRequest(apiDefinition.getRequest());
return apiTestCase;
}
public Boolean checkIsSynchronize(ApiDefinitionWithBLOBs existApi, ApiDefinitionWithBLOBs apiDefinition) {
ApiDefinition exApi;
@ -1176,23 +1180,6 @@ public class ApiDefinitionService {
return false;
}
/**
* 如果是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 void reSetImportMocksApiId(List<MockConfigImportDTO> mocks, String originId, String newId, int apiNum) {
if (CollectionUtils.isNotEmpty(mocks)) {
int index = 1;
@ -1206,22 +1193,42 @@ public class ApiDefinitionService {
}
}
private String setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) {
private boolean setImportHashTree(ApiDefinitionWithBLOBs apiDefinition) {
String request = apiDefinition.getRequest();
MsHTTPSamplerProxy msHTTPSamplerProxy = JSONObject.parseObject(request, MsHTTPSamplerProxy.class, Feature.DisableSpecialKeyDetect);
boolean createCase = CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getHeaders());
if (CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getArguments()) && !createCase) {
createCase = true;
}
if (msHTTPSamplerProxy.getBody() != null && !createCase) {
createCase = true;
}
if (CollectionUtils.isNotEmpty(msHTTPSamplerProxy.getRest()) && !createCase) {
createCase = true;
}
msHTTPSamplerProxy.setId(apiDefinition.getId());
msHTTPSamplerProxy.setHashTree(new LinkedList<>());
apiDefinition.setRequest(JSONObject.toJSONString(msHTTPSamplerProxy));
return request;
return createCase;
}
private String setImportTCPHashTree(ApiDefinitionWithBLOBs apiDefinition) {
private boolean setImportTCPHashTree(ApiDefinitionWithBLOBs apiDefinition) {
String request = apiDefinition.getRequest();
MsTCPSampler tcpSampler = JSONObject.parseObject(request, MsTCPSampler.class, Feature.DisableSpecialKeyDetect);
boolean createCase = CollectionUtils.isNotEmpty(tcpSampler.getParameters());
if (StringUtils.isNotBlank(tcpSampler.getJsonDataStruct()) && !createCase) {
createCase = true;
}
if (StringUtils.isNotBlank(tcpSampler.getRawDataStruct()) && !createCase) {
createCase = true;
}
if (CollectionUtils.isNotEmpty(tcpSampler.getXmlDataStruct()) && !createCase) {
createCase = true;
}
tcpSampler.setId(apiDefinition.getId());
tcpSampler.setHashTree(new LinkedList<>());
apiDefinition.setRequest(JSONObject.toJSONString(tcpSampler));
return request;
return createCase;
}
private void deleteFileByTestId(String apiId) {