refactor(工作台): 接口同步功能优化

--user=郭雨琦
This commit is contained in:
guoyuqi 2022-07-25 14:02:56 +08:00 committed by xiaomeinvG
parent 4f1f2f1383
commit 9f9f678760
4 changed files with 100 additions and 85 deletions

View File

@ -37,6 +37,7 @@ import io.metersphere.base.domain.ApiDefinitionWithBLOBs;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
import io.metersphere.base.domain.ApiTestEnvironmentExample;
import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
@ -110,7 +111,7 @@ public class JmeterDefinitionParser extends ApiImportAbstractParser<ApiDefinitio
ApiTestCaseWithBLOBs apiTestCase = new ApiTestCaseWithBLOBs();
BeanUtils.copyBean(apiTestCase, apiDefinitionWithBLOBs);
apiTestCase.setApiDefinitionId(apiDefinitionWithBLOBs.getId());
apiTestCase.setStatus("Prepare");
apiTestCase.setCaseStatus(APITestStatus.Prepare.name());
apiTestCase.setPriority("P0");
definitionCases.add(apiTestCase);

View File

@ -273,6 +273,9 @@ public class ApiDefinitionExecResultService {
// 更新用例最后执行结果
caseWithBLOBs.setLastResultId(reportId);
caseWithBLOBs.setStatus(status);
if (status.equals("success")) {
caseWithBLOBs.setToBeUpdated(false);
}
caseWithBLOBs.setUpdateTime(System.currentTimeMillis());
apiTestCaseMapper.updateByPrimaryKey(caseWithBLOBs);

View File

@ -968,9 +968,7 @@ public class ApiDefinitionService {
apiTestCaseWithBLOBs.setCreateTime(System.currentTimeMillis());
}
apiTestCaseWithBLOBs.setUpdateTime(System.currentTimeMillis());
if (StringUtils.isBlank(apiTestCaseWithBLOBs.getStatus())) {
apiTestCaseWithBLOBs.setStatus(APITestStatus.Prepare.name());
}
if (StringUtils.isBlank(apiTestCaseWithBLOBs.getCaseStatus())) {
apiTestCaseWithBLOBs.setCaseStatus(APITestStatus.Prepare.name());
}
@ -1136,7 +1134,7 @@ public class ApiDefinitionService {
} else if (apiTestImportRequest.getCoverModule() != null && apiTestImportRequest.getCoverModule()) {
apiDefinition.setUpdateTime(System.currentTimeMillis());
}
if (CollectionUtils.isEmpty(caseList)) {
if (StringUtils.isBlank(apiDefinition.getCaseTotal()) || Integer.parseInt(apiDefinition.getCaseTotal()) == 0) {
apiDefinition.setToBeUpdated(false);
}
} else {
@ -1220,82 +1218,6 @@ public class ApiDefinitionService {
}
if (!StringUtils.equals(apiDefinition.getCreateUser(), existApi.getCreateUser())) {
return true;
}
if (!StringUtils.equals(apiDefinition.getStatus(), existApi.getStatus())) {
return true;
}
if (!StringUtils.equals(apiDefinition.getTags(), existApi.getTags())) {
if (apiDefinition.getTags() != null && Objects.equals(apiDefinition.getTags(), "") && existApi.getTags() != null && Objects.equals(existApi.getTags(), "")) {
return true;
}
}
if (!StringUtils.equals(existApi.getRemark(), apiDefinition.getRemark())) {
return true;
}
if (!StringUtils.equals(existApi.getDescription(), apiDefinition.getDescription())) {
return true;
}
JsonNode exApiResponse = null;
JsonNode apiResponse = null;
if (StringUtils.isBlank(apiDefinition.getResponse()) || StringUtils.isBlank(existApi.getResponse())) {
return !StringUtils.isBlank(apiDefinition.getResponse()) || !StringUtils.isBlank(existApi.getResponse());
}
try {
exApiResponse = objectMapper.readTree(existApi.getResponse());
apiResponse = objectMapper.readTree(apiDefinition.getResponse());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
if (exApiResponse == null || apiResponse == null) {
return exApiResponse != null || apiResponse != null;
}
if (exApiResponse.get("headers") != null && apiResponse.get("headers") != null) {
if (!StringUtils.equals(exApiResponse.get("headers").toString(), apiResponse.get("headers").toString())) {
return true;
}
}
if (exApiResponse.get("type") != null && apiResponse.get("type") != null) {
if (!StringUtils.equals(exApiResponse.get("type").toString(), apiResponse.get("type").toString())) {
return true;
}
}
if (exApiResponse.get("name") != null && apiResponse.get("name") != null) {
if (!StringUtils.equals(exApiResponse.get("name").toString(), apiResponse.get("name").toString())) {
return true;
}
}
if (exApiResponse.get("body") != null && apiResponse.get("body") != null) {
if (!StringUtils.equals(exApiResponse.get("body").toString(), apiResponse.get("body").toString())) {
return true;
}
}
if (exApiResponse.get("statusCode") != null && apiResponse.get("statusCode") != null) {
if (!StringUtils.equals(exApiResponse.get("statusCode").toString(), apiResponse.get("statusCode").toString())) {
return true;
}
}
if (exApiResponse.get("enable") != null && apiResponse.get("enable") != null) {
if (!StringUtils.equals(exApiResponse.get("enable").toString(), apiResponse.get("enable").toString())) {
return true;
}
}
JsonNode exApiRequest = null;
JsonNode apiRequest = null;
try {
@ -1349,6 +1271,79 @@ public class ApiDefinitionService {
}
}
if (!StringUtils.equals(apiDefinition.getCreateUser(), existApi.getCreateUser())) {
return true;
}
if (!StringUtils.equals(apiDefinition.getStatus(), existApi.getStatus()) && StringUtils.isNotBlank(existApi.getStatus()) && StringUtils.isNotBlank(apiDefinition.getStatus())) {
return true;
}
if (!StringUtils.equals(apiDefinition.getTags(), existApi.getTags())) {
if (apiDefinition.getTags() != null && Objects.equals(apiDefinition.getTags(), "") && existApi.getTags() != null && Objects.equals(existApi.getTags(), "")) {
return true;
}
}
if (!StringUtils.equals(existApi.getRemark(), apiDefinition.getRemark()) && StringUtils.isNotBlank(existApi.getRemark()) && StringUtils.isNotBlank(apiDefinition.getRemark())) {
return true;
}
if (!StringUtils.equals(existApi.getDescription(), apiDefinition.getDescription()) && StringUtils.isNotBlank(existApi.getDescription()) && StringUtils.isNotBlank(apiDefinition.getDescription())) {
return true;
}
JsonNode exApiResponse = null;
JsonNode apiResponse = null;
if (StringUtils.isBlank(apiDefinition.getResponse()) || StringUtils.isBlank(existApi.getResponse())) {
return !StringUtils.isBlank(apiDefinition.getResponse()) || !StringUtils.isBlank(existApi.getResponse());
}
try {
exApiResponse = objectMapper.readTree(existApi.getResponse());
apiResponse = objectMapper.readTree(apiDefinition.getResponse());
} catch (JsonProcessingException e) {
e.printStackTrace();
}
if (exApiResponse == null || apiResponse == null) {
return exApiResponse != null || apiResponse != null;
}
if (exApiResponse.get("headers") != null && apiResponse.get("headers") != null) {
if (!StringUtils.equals(exApiResponse.get("headers").toString(), apiResponse.get("headers").toString())) {
return true;
}
}
if (exApiResponse.get("type") != null && apiResponse.get("type") != null) {
if (!StringUtils.equals(exApiResponse.get("type").toString(), apiResponse.get("type").toString())) {
return true;
}
}
if (exApiResponse.get("name") != null && apiResponse.get("name") != null) {
if (!StringUtils.equals(exApiResponse.get("name").toString(), apiResponse.get("name").toString())) {
return true;
}
}
if (exApiResponse.get("body") != null && apiResponse.get("body") != null) {
if (!StringUtils.equals(exApiResponse.get("body").toString(), apiResponse.get("body").toString())) {
return true;
}
}
if (exApiResponse.get("statusCode") != null && apiResponse.get("statusCode") != null) {
if (!StringUtils.equals(exApiResponse.get("statusCode").toString(), apiResponse.get("statusCode").toString())) {
return true;
}
}
if (exApiResponse.get("enable") != null && apiResponse.get("enable") != null) {
return !StringUtils.equals(exApiResponse.get("enable").toString(), apiResponse.get("enable").toString());
}
return false;
}

View File

@ -643,6 +643,7 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
Map<String, List<ApiTestCaseWithBLOBs>> definitionIdCaseMAp = optionDataCases.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId));
List<ApiDefinitionWithBLOBs> optionData = new ArrayList<>();
List<ApiDefinitionWithBLOBs> repeatApiDefinitionWithBLOBs;
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>();
if (protocol.equals("HTTP")) {
//去重 如果url可重复 则模块+名称+请求方式+路径 唯一否则 请求方式+路径唯一
//覆盖模式留重复的最后一个不覆盖留第一个
@ -653,7 +654,6 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
//系统内重复的数据
repeatApiDefinitionWithBLOBs = extApiDefinitionMapper.selectRepeatByBLOBs(optionData, projectId);
//重复接口的case
Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap = new HashMap<>();
if (!repeatApiDefinitionWithBLOBs.isEmpty()) {
oldCaseMap = getOldCaseMap(repeatApiDefinitionWithBLOBs);
}
@ -749,7 +749,7 @@ 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);
}
@ -795,9 +795,25 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
if (optionData.isEmpty()) {
moduleMap = new HashMap<>();
}
//将原来的case和更改的case组合在一起为了同步的设置
List<String> caseIds = optionDataCases.stream().map(ApiTestCase::getId).filter(StringUtils::isNotBlank).collect(Collectors.toList());
buildCases(optionDataCases, oldCaseMap, caseIds);
return getUpdateApiModuleDTO(moduleMap, toUpdateList, optionData, optionDataCases);
}
private void buildCases(List<ApiTestCaseWithBLOBs> optionDataCases, Map<String, List<ApiTestCaseWithBLOBs>> oldCaseMap, List<String> caseIds) {
if (MapUtils.isNotEmpty(oldCaseMap)) {
List<ApiTestCaseWithBLOBs> oldCaseList = new ArrayList<>();
Collection<List<ApiTestCaseWithBLOBs>> values = oldCaseMap.values();
for (List<ApiTestCaseWithBLOBs> value : values) {
oldCaseList.addAll(value);
}
List<ApiTestCaseWithBLOBs> collect = oldCaseList.stream().filter(t -> !caseIds.contains(t.getId())).collect(Collectors.toList());
optionDataCases.addAll(collect);
}
}
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) {
@ -819,8 +835,8 @@ public class ApiModuleService extends NodeTreeService<ApiModuleDTO> {
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));
/*ArrayList<ApiTestCaseWithBLOBs> testCases = getDistinctNameCases(caseWithBLOBs);*/
oldCaseMap = caseWithBLOBs.stream().collect(Collectors.groupingBy(ApiTestCase::getApiDefinitionId));
return oldCaseMap;
}