fix(接口测试): 修复场景复制自定义请求时会造成接口覆盖率不准确的问题

This commit is contained in:
Jianguo-Genius 2024-11-29 15:12:53 +08:00 committed by 建国
parent 633e540b30
commit 28afcbf901
3 changed files with 25 additions and 16 deletions

View File

@ -140,8 +140,8 @@ public class ApiHomeController {
List<ApiDefinition> apiDefinitionHasCase = apiDefinitionService.selectBaseInfoByProjectIdAndHasCase(projectId, versionId); List<ApiDefinition> apiDefinitionHasCase = apiDefinitionService.selectBaseInfoByProjectIdAndHasCase(projectId, versionId);
//没有case的接口 //没有case的接口
List<ApiDefinition> apiNoCaseList = apiDefinitionService.getAPiNotInCollection(protocolAllDefinitionMap, apiDefinitionHasCase); List<ApiDefinition> apiNoCaseList = apiDefinitionService.getAPiNotInCollection(protocolAllDefinitionMap, apiDefinitionHasCase);
Map<String, Map<String, String>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(projectId, null); Map<String, Map<String, List<String>>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(projectId, null);
List<String> apiIdInScenario = apiAutomationService.getApiIdInScenario(projectId, scenarioUrlList, apiNoCaseList); List<String> apiIdInScenario = apiAutomationService.getApiIdInScenario(scenarioUrlList, apiNoCaseList);
Map<String, List<ApiDefinition>> unCoverageApiMap = apiDefinitionService.getUnCoverageApiMap(apiNoCaseList, apiIdInScenario); Map<String, List<ApiDefinition>> unCoverageApiMap = apiDefinitionService.getUnCoverageApiMap(apiNoCaseList, apiIdInScenario);
Map<String, List<ApiDefinition>> coverageApiMap = apiDefinitionService.filterMap(protocolAllDefinitionMap, unCoverageApiMap); Map<String, List<ApiDefinition>> coverageApiMap = apiDefinitionService.filterMap(protocolAllDefinitionMap, unCoverageApiMap);
@ -213,7 +213,7 @@ public class ApiHomeController {
ApiDataCountDTO apiCountResult = new ApiDataCountDTO(); ApiDataCountDTO apiCountResult = new ApiDataCountDTO();
//统计覆盖率 //统计覆盖率
CoveredDTO coveredDTO = new CoveredDTO(); CoveredDTO coveredDTO = new CoveredDTO();
Map<String, Map<String, String>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(projectId, versionId); Map<String, Map<String, List<String>>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(projectId, versionId);
List<ApiDefinition> allEffectiveApiIdList = apiDefinitionService.selectEffectiveIdByProjectId(projectId, versionId); List<ApiDefinition> allEffectiveApiIdList = apiDefinitionService.selectEffectiveIdByProjectId(projectId, versionId);
try { try {
coveredDTO = apiAutomationService.countInterfaceCoverage(projectId, scenarioUrlList, allEffectiveApiIdList); coveredDTO = apiAutomationService.countInterfaceCoverage(projectId, scenarioUrlList, allEffectiveApiIdList);

View File

@ -431,8 +431,8 @@ public class ApiDefinitionService {
} }
if (CollectionUtils.isNotEmpty(definitionList)) { if (CollectionUtils.isNotEmpty(definitionList)) {
//如果查询条件中有未覆盖/已覆盖 则需要解析出没有用例的接口中有多少是符合场景覆盖规律的然后将这些接口的id作为查询参数. 这里不根据版本筛选覆盖的url //如果查询条件中有未覆盖/已覆盖 则需要解析出没有用例的接口中有多少是符合场景覆盖规律的然后将这些接口的id作为查询参数. 这里不根据版本筛选覆盖的url
Map<String, Map<String, String>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(request.getProjectId(), null); Map<String, Map<String, List<String>>> scenarioUrlList = apiAutomationService.selectScenarioUseUrlByProjectId(request.getProjectId(), null);
List<String> apiIdInScenario = apiAutomationService.getApiIdInScenario(request.getProjectId(), scenarioUrlList, definitionList); List<String> apiIdInScenario = apiAutomationService.getApiIdInScenario(scenarioUrlList, definitionList);
if (CollectionUtils.isNotEmpty(apiIdInScenario)) { if (CollectionUtils.isNotEmpty(apiIdInScenario)) {
request.setCoverageIds(apiIdInScenario); request.setCoverageIds(apiIdInScenario);
} }

View File

@ -1877,12 +1877,12 @@ public class ApiScenarioService {
* @param apiList 接口集合id / path 必须有数据 * @param apiList 接口集合id / path 必须有数据
* @return * @return
*/ */
public CoveredDTO countInterfaceCoverage(String projectId, Map<String, Map<String, String>> scenarioUrlMap, List<ApiDefinition> apiList) { public CoveredDTO countInterfaceCoverage(String projectId, Map<String, Map<String, List<String>>> scenarioUrlMap, List<ApiDefinition> apiList) {
CoveredDTO coverage = new CoveredDTO(); CoveredDTO coverage = new CoveredDTO();
if (CollectionUtils.isEmpty(apiList)) { if (CollectionUtils.isEmpty(apiList)) {
return coverage; return coverage;
} }
int urlContainsCount = this.getApiIdInScenario(projectId, scenarioUrlMap, apiList).size(); int urlContainsCount = this.getApiIdInScenario(scenarioUrlMap, apiList).size();
coverage.setCovered(urlContainsCount); coverage.setCovered(urlContainsCount);
coverage.setNotCovered(apiList.size() - urlContainsCount); coverage.setNotCovered(apiList.size() - urlContainsCount);
float coverageRageNumber = (float) urlContainsCount * 100 / apiList.size(); float coverageRageNumber = (float) urlContainsCount * 100 / apiList.size();
@ -1891,21 +1891,22 @@ public class ApiScenarioService {
return coverage; return coverage;
} }
public List<String> getApiIdInScenario(String projectId, Map<String, Map<String, String>> scenarioUrlMap, List<ApiDefinition> apiList) { public List<String> getApiIdInScenario(Map<String, Map<String, List<String>>> scenarioUrlMap, List<ApiDefinition> apiList) {
List<String> apiIdList = new ArrayList<>(); List<String> apiIdList = new ArrayList<>();
if (MapUtils.isNotEmpty(scenarioUrlMap) && CollectionUtils.isNotEmpty(apiList)) { if (MapUtils.isNotEmpty(scenarioUrlMap) && CollectionUtils.isNotEmpty(apiList)) {
for (ApiDefinition model : apiList) { for (ApiDefinition model : apiList) {
if (StringUtils.equalsIgnoreCase(model.getProtocol(), "http")) { if (StringUtils.equalsIgnoreCase(model.getProtocol(), "http")) {
Map<String, String> stepIdAndUrlMap = scenarioUrlMap.get(model.getMethod()); Map<String, List<String>> stepIdAndUrlMap = scenarioUrlMap.get(model.getMethod());
if (stepIdAndUrlMap != null) { if (stepIdAndUrlMap != null) {
Collection<String> scenarioUrlList = scenarioUrlMap.get(model.getMethod()).values(); Collection<String> scenarioUrlList = scenarioUrlMap.get(model.getMethod()).values()
.stream().flatMap(Collection::stream).collect(Collectors.toList());
boolean matchedUrl = MockApiUtils.isUrlInList(model.getPath(), scenarioUrlList); boolean matchedUrl = MockApiUtils.isUrlInList(model.getPath(), scenarioUrlList);
if (matchedUrl) { if (matchedUrl) {
apiIdList.add(model.getId()); apiIdList.add(model.getId());
} }
} }
} else { } else {
Map<String, String> stepIdAndUrlMap = scenarioUrlMap.get("MS_NOT_HTTP"); Map<String, List<String>> stepIdAndUrlMap = scenarioUrlMap.get("MS_NOT_HTTP");
if (stepIdAndUrlMap != null && stepIdAndUrlMap.containsKey(model.getId())) { if (stepIdAndUrlMap != null && stepIdAndUrlMap.containsKey(model.getId())) {
apiIdList.add(model.getId()); apiIdList.add(model.getId());
} }
@ -2325,17 +2326,25 @@ public class ApiScenarioService {
* @param projectId * @param projectId
* @return <get/post, <step-id,url>> * @return <get/post, <step-id,url>>
*/ */
public Map<String, Map<String, String>> selectScenarioUseUrlByProjectId(String projectId, String versionId) { public Map<String, Map<String, List<String>>> selectScenarioUseUrlByProjectId(String projectId, String versionId) {
List<ApiScenarioReferenceId> list = apiScenarioReferenceIdService.selectUrlByProjectId(projectId, versionId); List<ApiScenarioReferenceId> list = apiScenarioReferenceIdService.selectUrlByProjectId(projectId, versionId);
Map<String, Map<String, String>> returnMap = new HashMap<>(); Map<String, Map<String, List<String>>> returnMap = new HashMap<>();
if (CollectionUtils.isNotEmpty(list)) { if (CollectionUtils.isNotEmpty(list)) {
list.forEach(item -> { list.forEach(item -> {
String method = item.getMethod() == null ? "MS_NOT_HTTP" : item.getMethod(); String method = item.getMethod() == null ? "MS_NOT_HTTP" : item.getMethod();
if (returnMap.containsKey(method)) { if (returnMap.containsKey(method)) {
returnMap.get(method).put(item.getReferenceId(), StringUtils.trim(item.getUrl())); if (returnMap.get(method).containsKey(item.getReferenceId())) {
returnMap.get(method).get(item.getReferenceId()).add(StringUtils.trim(item.getUrl()));
} else {
returnMap.get(method).put(item.getReferenceId(), new ArrayList<>() {{
this.add(StringUtils.trim(item.getUrl()));
}});
}
} else { } else {
Map<String, String> urlMap = new HashMap<>() {{ Map<String, List<String>> urlMap = new HashMap<>() {{
this.put(item.getReferenceId(), StringUtils.trim(item.getUrl())); this.put(item.getReferenceId(), new ArrayList<>() {{
this.add(StringUtils.trim(item.getUrl()));
}});
}}; }};
returnMap.put(method, urlMap); returnMap.put(method, urlMap);
} }