From c8fa84042ef69c7ef869874fb8477c1c422e35f4 Mon Sep 17 00:00:00 2001 From: song-tianyang Date: Tue, 12 Apr 2022 14:02:08 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):=20?= =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95=E9=A6=96?= =?UTF-8?q?=E9=A1=B5=E5=9C=BA=E6=99=AF=E9=9D=A2=E6=9D=BF=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3=E8=A6=86=E7=9B=96=E7=8E=87=E7=BB=9F=E8=AE=A1=E4=B8=8D?= =?UTF-8?q?=E5=AF=B9=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 修复接口测试首页场景面板的接口覆盖率统计不对的问题 --- .../api/controller/APITestController.java | 5 +- .../api/service/ApiAutomationService.java | 110 ++---------------- .../ApiScenarioReferenceIdService.java | 11 ++ .../service/HistoricalDataUpgradeService.java | 4 - .../metersphere/base/domain/ApiScenario.java | 3 +- .../base/domain/ApiScenarioWithBLOBs.java | 2 - .../base/mapper/ApiScenarioMapper.xml | 25 +--- .../base/mapper/ext/ExtApiScenarioMapper.java | 12 +- .../base/mapper/ext/ExtApiScenarioMapper.xml | 12 +- .../ext/ExtApiScenarioReferenceIdMapper.java | 9 ++ .../ext/ExtApiScenarioReferenceIdMapper.xml | 11 ++ .../listener/AppStartListener.java | 1 - .../db/migration/V115__1.20__release.sql | 5 +- 13 files changed, 59 insertions(+), 151 deletions(-) create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReferenceIdMapper.java create mode 100644 backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiScenarioReferenceIdMapper.xml diff --git a/backend/src/main/java/io/metersphere/api/controller/APITestController.java b/backend/src/main/java/io/metersphere/api/controller/APITestController.java index 822ad24f00..2270bb28a7 100644 --- a/backend/src/main/java/io/metersphere/api/controller/APITestController.java +++ b/backend/src/main/java/io/metersphere/api/controller/APITestController.java @@ -294,11 +294,10 @@ public class APITestController { * 接口覆盖率 * 复制的接口定义/复制或引用的单接口用例/ 添加的自定义请求 url 路径与现有的接口定义一致的请求 */ - List allScenarioInfoList = apiAutomationService.selectIdAndUseUrlByProjectId(projectId); + List allScenarioIdList = apiAutomationService.selectIdsByProjectId(projectId); List allEffectiveApiIdList = apiDefinitionService.selectEffectiveIdByProjectId(projectId); -// List allEffectiveApiCaseList = apiTestCaseService.selectEffectiveTestCaseByProjectId(projectId); try { - float intetfaceCoverageRageNumber = apiAutomationService.countInterfaceCoverage(allScenarioInfoList, allEffectiveApiIdList); + float intetfaceCoverageRageNumber = apiAutomationService.countInterfaceCoverage(allScenarioIdList, allEffectiveApiIdList); DecimalFormat df = new DecimalFormat("0.0"); returnStr = df.format(intetfaceCoverageRageNumber) + "%"; }catch (Exception e){ diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 6f311a2397..63dbf2083f 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -247,8 +247,6 @@ public class ApiAutomationService { scenario.setCreateTime(System.currentTimeMillis()); scenario.setNum(nextNum); - List useUrl = this.parseUrl(scenario); - scenario.setUseUrl(JSONArray.toJSONString(useUrl)); scenario.setOrder(ServiceUtils.getNextOrder(scenario.getProjectId(), extApiScenarioMapper::getLastOrder)); scenario.setRefId(request.getId()); scenario.setLatest(true); @@ -359,8 +357,6 @@ public class ApiAutomationService { } deleteUpdateBodyFile(scenario, beforeScenario); - List useUrl = this.parseUrl(scenario); - scenario.setUseUrl(JSONArray.toJSONString(useUrl)); scenario.setCreateUser(null); // 更新时不更新创建人 ApiScenarioExample example = new ApiScenarioExample(); example.createCriteria().andIdEqualTo(scenario.getId()).andVersionIdEqualTo(request.getVersionId()); @@ -922,8 +918,8 @@ public class ApiAutomationService { return extApiScenarioMapper.countByProjectID(projectId); } - public List selectIdAndUseUrlByProjectId(String projectId) { - return extApiScenarioMapper.selectIdAndUseUrlByProjectId(projectId); + public List selectIdsByProjectId(String projectId) { + return extApiScenarioMapper.selectIdsByProjectId(projectId); } public long countScenarioByProjectIDAndCreatInThisWeek(String projectId) { @@ -1082,10 +1078,6 @@ public class ApiAutomationService { ApiScenarioWithBLOBs apiScenarioWithBLOBs = new ApiScenarioWithBLOBs(); BeanUtils.copyBean(apiScenarioWithBLOBs, request); apiScenarioWithBLOBs.setUpdateTime(System.currentTimeMillis()); - if (apiScenarioWithBLOBs.getScenarioDefinition() != null) { - List useUrl = this.parseUrl(apiScenarioWithBLOBs); - apiScenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); - } apiScenarioMapper.updateByExampleSelective( apiScenarioWithBLOBs, apiScenarioExample); @@ -1127,8 +1119,6 @@ public class ApiAutomationService { ApiScenarioWithBLOBs scenarioWithBLOBs, ApiTestImportRequest apiTestImportRequest, ApiTestCaseMapper apiTestCaseMapper, ApiDefinitionMapper apiDefinitionMapper) { if (CollectionUtils.isEmpty(sameRequest)) { scenarioWithBLOBs.setId(UUID.randomUUID().toString()); - List useUrl = this.parseUrl(scenarioWithBLOBs); - scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); scenarioWithBLOBs.setOrder(getImportNextOrder(apiTestImportRequest.getProjectId())); // 导入时设置版本 scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); @@ -1165,8 +1155,6 @@ public class ApiAutomationService { scenarioWithBLOBs.setVersionId(apiTestImportRequest.getUpdateVersionId()); scenarioWithBLOBs.setOrder(existScenario.getOrder()); scenarioWithBLOBs.setNum(existScenario.getNum()); - List useUrl = this.parseUrl(scenarioWithBLOBs); - scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); batchMapper.updateByPrimaryKeyWithBLOBs(scenarioWithBLOBs); } checkReferenceCase(scenarioWithBLOBs, apiTestCaseMapper, apiDefinitionMapper); @@ -1229,8 +1217,6 @@ public class ApiAutomationService { _importCreate(sameRequest, batchMapper, extApiScenarioMapper, scenarioWithBLOBs, apiTestImportRequest, apiTestCaseMapper, apiDefinitionMapper); } else if (StringUtils.equals("incrementalMerge", apiTestImportRequest.getModeId())) { if (CollectionUtils.isEmpty(sameRequest)) { - List useUrl = this.parseUrl(scenarioWithBLOBs); - scenarioWithBLOBs.setUseUrl(JSONArray.toJSONString(useUrl)); scenarioWithBLOBs.setOrder(getImportNextOrder(request.getProjectId())); scenarioWithBLOBs.setId(UUID.randomUUID().toString()); scenarioWithBLOBs.setRefId(scenarioWithBLOBs.getId()); @@ -1555,86 +1541,27 @@ public class ApiAutomationService { *

* 匹配场景中用到的路径 * - * @param allScenarioInfoList 场景集合(id / scenario大字段 必须有数据) + * @param scenarioIdList 场景集合(id / scenario大字段 必须有数据) * @param allEffectiveApiList 接口集合(id / path 必须有数据) * @return */ - public float countInterfaceCoverage(List allScenarioInfoList, List allEffectiveApiList) { - if (allEffectiveApiList == null || allEffectiveApiList.isEmpty()) { + public float countInterfaceCoverage(List scenarioIdList, List allEffectiveApiList) { + if (CollectionUtils.isEmpty(scenarioIdList) || CollectionUtils.isEmpty(allEffectiveApiList)) { return 0; } + List refIdList = apiScenarioReferenceIdService.findByScenarioIds(scenarioIdList); - Map> urlMap = new HashMap<>(); + int containsCount = 0; for (ApiDefinition model : allEffectiveApiList) { - String url = model.getPath(); - String method = model.getMethod(); - String id = model.getId(); - - ApiMethodUrlDTO dto = new ApiMethodUrlDTO(url, method); - - if (urlMap.containsKey(dto)) { - urlMap.get(dto).add(id); - } else { - List list = new ArrayList<>(); - list.add(id); - urlMap.put(dto, list); + if(refIdList.contains(model.getId())){ + containsCount ++; } } - if (urlMap.isEmpty()) { - return 100; - } - - List urlList = new ArrayList<>(); - for (ApiScenarioWithBLOBs model : allScenarioInfoList) { - List useUrl = this.getScenarioUseUrl(model); - if (CollectionUtils.isNotEmpty(useUrl)) { - for (ApiMethodUrlDTO dto : useUrl) { - if (!urlList.contains(dto)) { - urlList.add(dto); - } - } - } - } - - List containsApiIdList = new ArrayList<>(); - for (ApiMethodUrlDTO urlDTO : urlList) { - List apiIdList = urlMap.get(urlDTO); - if (apiIdList != null) { - for (String api : apiIdList) { - if (!containsApiIdList.contains(api)) { - containsApiIdList.add(api); - } - } - } - } - - int allApiIdCount = 0; - for (List allApiIdList : urlMap.values()) { - if (CollectionUtils.isNotEmpty(allApiIdList)) { - allApiIdCount += allApiIdList.size(); - } - } - - float coverageRageNumber = (float) containsApiIdList.size() * 100 / allApiIdCount; + float coverageRageNumber = (float) containsCount * 100 / allEffectiveApiList.size(); return coverageRageNumber; } - private List getScenarioUseUrl(ApiScenarioWithBLOBs model) { - List useUrlList = new ArrayList<>(); - try { - useUrlList = JSONArray.parseArray(model.getUseUrl(), ApiMethodUrlDTO.class); - } catch (Exception e) { - } - return useUrlList; - } - - public List parseUrl(ApiScenarioWithBLOBs scenario) { - List urlList = new ArrayList<>(); - // 去除未生效且影响性能的方法 - return urlList; - } - public ScenarioEnv getApiScenarioProjectId(String id) { ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id); ScenarioEnv scenarioEnv = new ScenarioEnv(); @@ -1734,23 +1661,6 @@ public class ApiAutomationService { return null; } - public void checkApiScenarioUseUrl() { - List noUrlScenarioIdList = extApiScenarioMapper.selectIdsByUseUrlIsNull(); - for (String id : noUrlScenarioIdList) { - ApiScenarioWithBLOBs scenario = apiScenarioMapper.selectByPrimaryKey(id); - if (scenario.getUseUrl() == null) { - List useUrl = this.parseUrl(scenario); - if (useUrl != null) { - ApiScenarioWithBLOBs updateModel = new ApiScenarioWithBLOBs(); - updateModel.setId(scenario.getId()); - updateModel.setUseUrl(JSONArray.toJSONString(useUrl)); - apiScenarioMapper.updateByPrimaryKeySelective(updateModel); - apiScenarioReferenceIdService.saveApiAndScenarioRelation(updateModel); - } - } - } - } - public void checkApiScenarioReferenceId() { List scenarioNoRefs = extApiScenarioMapper.selectByNoReferenceId(); for (ApiScenarioWithBLOBs model : scenarioNoRefs) { diff --git a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReferenceIdService.java b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReferenceIdService.java index b0e5015e73..f23af7607c 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiScenarioReferenceIdService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiScenarioReferenceIdService.java @@ -7,6 +7,7 @@ import io.metersphere.base.domain.ApiScenarioReferenceId; import io.metersphere.base.domain.ApiScenarioReferenceIdExample; import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.mapper.ApiScenarioReferenceIdMapper; +import io.metersphere.base.mapper.ext.ExtApiScenarioReferenceIdMapper; import io.metersphere.commons.utils.SessionUtils; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.MapUtils; @@ -31,6 +32,8 @@ public class ApiScenarioReferenceIdService { @Resource private ApiScenarioReferenceIdMapper apiScenarioReferenceIdMapper; @Resource + private ExtApiScenarioReferenceIdMapper extApiScenarioReferenceIdMapper; + @Resource private SqlSessionFactory sqlSessionFactory; public List findByReferenceIds(List deleteIds) { @@ -134,4 +137,12 @@ public class ApiScenarioReferenceIdService { return apiScenarioReferenceIdMapper.selectByExample(example); } } + + public List findByScenarioIds(List scenarioIdList) { + if(CollectionUtils.isEmpty(scenarioIdList)){ + return new ArrayList<>(); + }else { + return extApiScenarioReferenceIdMapper.selectRefIdsFromScenarioIds(scenarioIdList); + } + } } diff --git a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java index a51f72e135..b2824b0c2f 100644 --- a/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java +++ b/backend/src/main/java/io/metersphere/api/service/HistoricalDataUpgradeService.java @@ -368,8 +368,6 @@ public class HistoricalDataUpgradeService { scenario.setUpdateTime(System.currentTimeMillis()); scenario.setStatus(ScenarioStatus.Underway.name()); scenario.setUserId(SessionUtils.getUserId()); - List useUrl = apiAutomationService.parseUrl(scenario); - scenario.setUseUrl(JSONArray.toJSONString(useUrl)); mapper.updateByPrimaryKeySelective(scenario); apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario); } else { @@ -389,8 +387,6 @@ public class HistoricalDataUpgradeService { scenario.setStatus(ScenarioStatus.Underway.name()); scenario.setUserId(SessionUtils.getUserId()); scenario.setNum(num); - List useUrl = apiAutomationService.parseUrl(scenario); - scenario.setUseUrl(JSONArray.toJSONString(useUrl)); mapper.insert(scenario); apiScenarioReferenceIdService.saveApiAndScenarioRelation(scenario); } diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiScenario.java b/backend/src/main/java/io/metersphere/base/domain/ApiScenario.java index c2af6b66d1..a5fbec5ad4 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiScenario.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiScenario.java @@ -1,8 +1,7 @@ package io.metersphere.base.domain; -import lombok.Data; - import java.io.Serializable; +import lombok.Data; @Data public class ApiScenario implements Serializable { diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiScenarioWithBLOBs.java b/backend/src/main/java/io/metersphere/base/domain/ApiScenarioWithBLOBs.java index 8e2ff82c0e..cb3645680d 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiScenarioWithBLOBs.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiScenarioWithBLOBs.java @@ -13,8 +13,6 @@ public class ApiScenarioWithBLOBs extends ApiScenario implements Serializable { private String description; - private String useUrl; - private String environmentJson; private static final long serialVersionUID = 1L; diff --git a/backend/src/main/java/io/metersphere/base/mapper/ApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ApiScenarioMapper.xml index 12fcc980cd..9869d26eb9 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ApiScenarioMapper.xml @@ -37,7 +37,6 @@ - @@ -106,7 +105,7 @@ latest - scenario_definition, description, use_url, environment_json + scenario_definition, description, environment_json SELECT id,scenario_definition FROM api_scenario WHERE project_id = #{0} AND status != 'Trash' - + + - + select reference_id from api_scenario_reference_id + where api_scenario_id IN + + #{v} + + + \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/listener/AppStartListener.java b/backend/src/main/java/io/metersphere/listener/AppStartListener.java index e6289d31f3..81662db0c2 100644 --- a/backend/src/main/java/io/metersphere/listener/AppStartListener.java +++ b/backend/src/main/java/io/metersphere/listener/AppStartListener.java @@ -137,7 +137,6 @@ public class AppStartListener implements ApplicationListener