refactor(测试计划): 优化用例运行环境展示

This commit is contained in:
WangXu10 2024-06-11 10:22:30 +08:00 committed by Craftsman
parent 8822e05455
commit da5c545e14
11 changed files with 106 additions and 76 deletions

View File

@ -0,0 +1,26 @@
package io.metersphere.plan.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import java.io.Serial;
import java.io.Serializable;
/**
* @author wx
*/
@Data
public class TestPlanCollectionEnvDTO implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
@Schema(description = "ID")
private String Id;
@Schema(description = "环境ID")
private String environmentId;
@Schema(description = "环境名称")
private String environmentName;
}

View File

@ -26,5 +26,5 @@ public class BasePlanCaseBatchRequest extends TableBatchProcessDTO implements Se
private List<String> moduleIds;
@Schema(description = "计划集id")
private List<String> collectionIds;
private String collectionId;
}

View File

@ -18,6 +18,6 @@ public class TestPlanApiCaseRequest extends ApiTestCasePageRequest {
private String testPlanId;
@Schema(description = "计划集id")
private List<String> collectionIds;
private String collectionId;
}

View File

@ -35,5 +35,5 @@ public class TestPlanCaseRequest extends BasePageRequest implements Serializable
private List<String> moduleIds;
@Schema(description = "计划集id")
private List<String> collectionIds;
private String collectionId;
}

View File

@ -66,9 +66,6 @@ public class TestPlanApiCasePageResponse implements Serializable {
@Schema(description = "测试集id")
private String testPlanCollectionId;
@Schema(description = "计划集环境")
private String collectEnvironmentId;
@Schema(description = "报告id")
private String lastExecReportId;
}

View File

@ -419,13 +419,11 @@
t.last_exec_result,
t.execute_user,
t.last_exec_time,
t.last_exec_report_id,
tpc.environment_id as collectEnvironmentId
t.last_exec_report_id
FROM
api_test_case atc
INNER JOIN api_definition a ON atc.api_definition_id = a.id
inner join test_plan_api_case t on atc.id = t.api_case_id
left join test_plan_collection tpc on tpc.id = t.test_plan_collection_id
WHERE atc.deleted =#{deleted}
and t.test_plan_id = #{request.testPlanId}
<include refid="queryApiCaseWhereCondition"/>
@ -463,11 +461,8 @@
#{nodeId}
</foreach>
</if>
<if test="request.collectionIds != null and request.collectionIds.size() > 0">
and t.test_plan_collection_id in
<foreach collection="request.collectionIds" item="collectionId" separator="," open="(" close=")">
#{collectionId}
</foreach>
<if test="request.collectionId != null and request.collectionId != ''">
and t.test_plan_collection_id = #{request.collectionId}
</if>
<include refid="apiCaseFilters">
<property name="filter" value="request.filter"/>
@ -625,11 +620,8 @@
#{nodeId}
</foreach>
</if>
<if test="request.collectionIds != null and request.collectionIds.size() > 0">
and t.test_plan_collection_id in
<foreach collection="request.collectionIds" item="collectionId" separator="," open="(" close=")">
#{collectionId}
</foreach>
<if test="request.collectionId != null and request.collectionId != ''">
and t.test_plan_collection_id = #{request.collectionId}
</if>
<include refid="filters">
<property name="filter" value="request.condition.filter"/>

View File

@ -1,6 +1,7 @@
package io.metersphere.plan.mapper;
import io.metersphere.plan.dto.TestPlanCollectionConfigDTO;
import io.metersphere.plan.dto.TestPlanCollectionEnvDTO;
import org.apache.ibatis.annotations.Param;
import java.util.List;
@ -9,4 +10,6 @@ public interface ExtTestPlanCollectionMapper {
List<TestPlanCollectionConfigDTO> getList(@Param("planId") String planId);
List<TestPlanCollectionEnvDTO> selectSecondCollectionEnv(@Param("type") String type, @Param("parentId") String parentId);
}

View File

@ -20,4 +20,16 @@
WHERE tpc.test_plan_id = #{planId}
</select>
<select id="selectSecondCollectionEnv" resultType="io.metersphere.plan.dto.TestPlanCollectionEnvDTO">
SELECT
tpc.id,
tpc.environment_id,
e.`name` as environmentName
FROM
test_plan_collection tpc
LEFT JOIN environment e ON tpc.environment_id = e.id
WHERE
tpc.type = #{type}
and tpc.parent_id <![CDATA[<>]]> #{parentId}
</select>
</mapper>

View File

@ -142,11 +142,8 @@
or functional_case.num like concat('%', #{request.keyword},'%')
)
</if>
<if test="request.collectionIds != null and request.collectionIds.size() > 0">
and test_plan_functional_case.test_plan_collection_id in
<foreach collection="request.collectionIds" item="collectionId" separator="," open="(" close=")">
#{collectionId}
</foreach>
<if test="request.collectionId != null and request.collectionId != ''">
and test_plan_functional_case.test_plan_collection_id = #{request.collectionId}
</if>
<include refid="filters">
<property name="filter" value="request.filter"/>
@ -180,11 +177,8 @@
or functional_case.tags like concat('%', #{request.condition.keyword},'%')
)
</if>
<if test="request.collectionIds != null and request.collectionIds.size() > 0">
and test_plan_functional_case.test_plan_collection_id in
<foreach collection="request.collectionIds" item="collectionId" separator="," open="(" close=")">
#{collectionId}
</foreach>
<if test="request.collectionId != null and request.collectionId != ''">
and test_plan_functional_case.test_plan_collection_id = #{request.collectionId}
</if>
<include refid="filters">
<property name="filter" value="request.condition.filter"/>

View File

@ -21,10 +21,7 @@ import io.metersphere.plan.dto.request.*;
import io.metersphere.plan.dto.response.TestPlanApiCasePageResponse;
import io.metersphere.plan.dto.response.TestPlanAssociationResponse;
import io.metersphere.plan.dto.response.TestPlanOperationResponse;
import io.metersphere.plan.mapper.ExtTestPlanApiCaseMapper;
import io.metersphere.plan.mapper.TestPlanApiCaseMapper;
import io.metersphere.plan.mapper.TestPlanCollectionMapper;
import io.metersphere.plan.mapper.TestPlanMapper;
import io.metersphere.plan.mapper.*;
import io.metersphere.project.domain.Project;
import io.metersphere.project.domain.ProjectExample;
import io.metersphere.project.dto.ModuleCountDTO;
@ -96,6 +93,8 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G
private TestPlanResourceLogService testPlanResourceLogService;
@Resource
private TestPlanCollectionMapper testPlanCollectionMapper;
@Resource
private ExtTestPlanCollectionMapper extTestPlanCollectionMapper;
public TestPlanApiCaseService() {
GetRunScriptServiceRegister.register(ApiExecuteResourceType.TEST_PLAN_API_CASE, this);
@ -216,13 +215,51 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G
private void buildApiCaseResponse(List<TestPlanApiCasePageResponse> apiCaseList) {
if (CollectionUtils.isNotEmpty(apiCaseList)) {
Map<String, String> projectMap = getProject(apiCaseList);
Map<String, String> envMap = getEnvironmentMap(apiCaseList);
Map<String, String> userMap = getUserMap(apiCaseList);
apiCaseList.forEach(apiCase -> {
apiCase.setProjectName(projectMap.get(apiCase.getProjectId()));
apiCase.setEnvironmentName(envMap.get(apiCase.getId()));
apiCase.setCreateUserName(userMap.get(apiCase.getCreateUser()));
});
handleCaseAndEnv(apiCaseList, projectMap, userMap);
}
}
private void handleCaseAndEnv(List<TestPlanApiCasePageResponse> apiCaseList, Map<String, String> projectMap, Map<String, String> userMap) {
//获取二级节点环境
List<TestPlanCollectionEnvDTO> secondEnv = extTestPlanCollectionMapper.selectSecondCollectionEnv(CaseType.API_CASE.getKey(), ModuleConstants.ROOT_NODE_PARENT_ID);
Map<String, TestPlanCollectionEnvDTO> secondEnvMap = secondEnv.stream().collect(Collectors.toMap(TestPlanCollectionEnvDTO::getId, item -> item));
//当前用例环境
List<String> caseEnvIds = apiCaseList.stream().map(TestPlanApiCasePageResponse::getEnvironmentId).toList();
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andIdIn(caseEnvIds);
List<Environment> caseEnv = environmentMapper.selectByExample(environmentExample);
Map<String, String> caseEnvMap = caseEnv.stream().collect(Collectors.toMap(Environment::getId, Environment::getName));
apiCaseList.forEach(item -> {
item.setProjectName(projectMap.get(item.getProjectId()));
item.setCreateUserName(userMap.get(item.getCreateUser()));
TestPlanCollectionEnvDTO collectEnv = secondEnvMap.get(item.getTestPlanCollectionId());
if (StringUtils.equalsIgnoreCase(collectEnv.getEnvironmentId(), ModuleConstants.ROOT_NODE_PARENT_ID)) {
//计划集 == 默认环境 处理默认环境
doHandleDefaultEnv(item, caseEnvMap);
} else {
//计划集 != 默认环境
doHandleEnv(item, collectEnv);
}
});
}
private void doHandleEnv(TestPlanApiCasePageResponse item, TestPlanCollectionEnvDTO collectEnv) {
if (StringUtils.isNotBlank(collectEnv.getEnvironmentName())) {
item.setEnvironmentId(collectEnv.getEnvironmentId());
item.setEnvironmentName(collectEnv.getEnvironmentName());
} else {
item.setEnvironmentId(null);
item.setEnvironmentName(null);
}
}
private void doHandleDefaultEnv(TestPlanApiCasePageResponse item, Map<String, String> caseEnvMap) {
if (caseEnvMap.containsKey(item.getEnvironmentId())) {
item.setEnvironmentName(caseEnvMap.get(item.getEnvironmentId()));
} else {
item.setEnvironmentId(null);
item.setEnvironmentName(null);
}
}
@ -233,39 +270,6 @@ public class TestPlanApiCaseService extends TestPlanResourceService implements G
return userLoginService.getUserNameMap(userIds.stream().filter(StringUtils::isNotBlank).distinct().toList());
}
private Map<String, String> getEnvironmentMap(List<TestPlanApiCasePageResponse> apiCaseList) {
Map<String, String> envMap = new HashMap<>();
//默认环境
List<TestPlanApiCasePageResponse> defaultEnv = apiCaseList.stream().filter(item -> StringUtils.equalsIgnoreCase(ModuleConstants.ROOT_NODE_PARENT_ID, item.getCollectEnvironmentId())).toList();
if (CollectionUtils.isNotEmpty(defaultEnv)) {
List<String> defaultEnvIds = defaultEnv.stream().map(TestPlanApiCasePageResponse::getEnvironmentId).distinct().toList();
Map<String, TestPlanApiCasePageResponse> defaultEnvMap = defaultEnv.stream().collect(Collectors.toMap(TestPlanApiCasePageResponse::getEnvironmentId, testPlanApiCasePageResponse -> testPlanApiCasePageResponse));
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andIdIn(defaultEnvIds);
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
environments.forEach(item -> {
TestPlanApiCasePageResponse testPlanApiCasePageResponse = defaultEnvMap.get(item.getId());
envMap.put(testPlanApiCasePageResponse.getId(), item.getName());
});
}
//非默认环境
List<TestPlanApiCasePageResponse> collectEnv = apiCaseList.stream().filter(item -> !StringUtils.equalsIgnoreCase(ModuleConstants.ROOT_NODE_PARENT_ID, item.getCollectEnvironmentId())).toList();
if (CollectionUtils.isNotEmpty(collectEnv)) {
List<String> collectEnvIds = collectEnv.stream().map(TestPlanApiCasePageResponse::getCollectEnvironmentId).distinct().toList();
Map<String, List<TestPlanApiCasePageResponse>> collectEnvMap = collectEnv.stream().collect(Collectors.groupingBy(TestPlanApiCasePageResponse::getCollectEnvironmentId));
EnvironmentExample environmentExample = new EnvironmentExample();
environmentExample.createCriteria().andIdIn(collectEnvIds);
List<Environment> environments = environmentMapper.selectByExample(environmentExample);
environments.forEach(item -> {
List<TestPlanApiCasePageResponse> list = collectEnvMap.get(item.getId());
list.forEach(response -> {
envMap.put(response.getId(), item.getName());
});
});
}
return envMap;
}
private Map<String, String> getProject(List<TestPlanApiCasePageResponse> apiCaseList) {
List<String> projectIds = apiCaseList.stream().map(TestPlanApiCasePageResponse::getProjectId).toList();
ProjectExample projectExample = new ProjectExample();

View File

@ -30,20 +30,22 @@ VALUES
INSERT INTO `test_plan_api_case`(`id`, `test_plan_id`, `api_case_id`, `environment_id`, `last_exec_result`, `last_exec_report_id`, `execute_user`, `create_time`, `create_user`, `pos`, `test_plan_collection_id`, `last_exec_time`)
VALUES
('wxxx_1', 'wxxx_1', 'wxxx_api_case_1', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_1', 1716370415311),
('wxxx_2', 'wxxx_1', 'wxxx_api_case_2', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311),
('wxxx_1', 'wxxx_1', 'wxxx_api_case_1', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311),
('wxxx_2', 'wxxx_1', 'wxxx_api_case_2', '123', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_3', 1716370415311),
('wxxx_3', 'wxxx_2', 'wxxx_api_case_3', '1', NULL, NULL, 'admin', 1716370415311, 'admin', 2, 'wxxx_2', 1716370415311);
INSERT INTO `test_plan_collection`(`id`, `test_plan_id`, `name`, `type`, `environment_id`, `test_resource_pool_id`, `pos`, `create_user`, `create_time`, `parent_id`)
VALUES
('wxxx_1', 'wxxx_1', 'wxxx_1', 'API', 'NONE', 'NONE', 1, 'admin', 1716370415311, 'NONE'),
('wxxx_2', 'wxxx_1', 'wxxx_2', 'API', '123', 'NONE', 2, 'admin', 1716370415311, 'NONE'),
('wxxx_2', 'wxxx_1', 'wxxx_2', 'API', '123', 'NONE', 2, 'admin', 1716370415311, 'wxxx_1'),
('wxxx_3', 'wxxx_2', 'wxxx_3', 'API', 'NONE', 'NONE', 3, 'admin', 1716370415311, 'wxxx_1');
INSERT INTO `environment`(`id`, `name`, `project_id`, `create_user`, `update_user`, `create_time`, `update_time`, `mock`, `description`, `pos`)
VALUES ('123', 'Mock环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 64);
VALUES
('123', 'Mock环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 64),
('223', '测试环境', 'wxx_1234', 'admin', 'admin', 1716175907000, 1716175907000, b'1', NULL, 128);
INSERT INTO `api_definition_module`(`id`, `name`, `parent_id`, `project_id`, `pos`, `create_time`, `update_time`, `update_user`, `create_user`)
VALUES ('123', 'Halo', 'NONE', 'wxx_1234', 384, 1716280762025, 1716280762025, '805048669970432', '805048669970432');