refactor(接口测试): 场景导出后端代码优化

This commit is contained in:
Jianguo-Genius 2024-10-18 15:57:10 +08:00 committed by Craftsman
parent d05ee1ef71
commit 9b53716bac
6 changed files with 44 additions and 9 deletions

View File

@ -10,6 +10,8 @@ import java.util.List;
@Data @Data
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class ApiScenarioDetail extends ApiScenario { public class ApiScenarioDetail extends ApiScenario {
@Schema(description = "模块路径")
private String modulePath;
@Schema(description = "场景的通用配置") @Schema(description = "场景的通用配置")
private ScenarioConfig scenarioConfig = new ScenarioConfig(); private ScenarioConfig scenarioConfig = new ScenarioConfig();
@Schema(description = "步骤") @Schema(description = "步骤")

View File

@ -127,4 +127,6 @@ public interface ExtApiTestCaseMapper {
void clearApiChange(@Param("ids") List<String> ids); void clearApiChange(@Param("ids") List<String> ids);
List<ApiTestCaseWithBlob> selectAllDetailByApiIds(@Param("apiIds") List<String> apiIds); List<ApiTestCaseWithBlob> selectAllDetailByApiIds(@Param("apiIds") List<String> apiIds);
List<ApiTestCaseWithBlob> selectAllDetailByIds(@Param("ids") List<String> apiIds);
} }

View File

@ -980,6 +980,17 @@
#{id} #{id}
</foreach> </foreach>
</select> </select>
<select id="selectAllDetailByIds" resultMap="ApiTestCaseDetailMap">
SELECT apiTestCase.*,apiTestCaseBlob.request
FROM api_test_case apiTestCase
INNER JOIN api_test_case_blob apiTestCaseBlob ON apiTestCase.id = apiTestCaseBlob.id
WHERE apiTestCase.id IN
<foreach collection="ids" item="id" separator="," open="(" close=")">
#{id}
</foreach>
</select>
<select id="getNameInfo" resultType="io.metersphere.api.domain.ApiTestCase"> <select id="getNameInfo" resultType="io.metersphere.api.domain.ApiTestCase">
SELECT id,name SELECT id,name
FROM api_test_case FROM api_test_case

View File

@ -626,7 +626,7 @@ public class ApiScenarioDataTransferService {
private ApiScenarioExportResponse genMetersphereExportResponse(ApiScenarioBatchExportRequest request, Map<String, String> moduleMap, String exportType, String userId) { private ApiScenarioExportResponse genMetersphereExportResponse(ApiScenarioBatchExportRequest request, Map<String, String> moduleMap, String exportType, String userId) {
Project project = projectMapper.selectByPrimaryKey(request.getProjectId()); Project project = projectMapper.selectByPrimaryKey(request.getProjectId());
MetersphereApiScenarioExportResponse response = apiScenarioService.selectAndSortScenarioDetailWithIds(request.getSelectIds()); MetersphereApiScenarioExportResponse response = apiScenarioService.selectAndSortScenarioDetailWithIds(request.getSelectIds(), moduleMap);
response.setProjectId(project.getId()); response.setProjectId(project.getId());
response.setOrganizationId(project.getOrganizationId()); response.setOrganizationId(project.getOrganizationId());
@ -649,7 +649,7 @@ public class ApiScenarioDataTransferService {
}); });
List<ApiTestCaseWithBlob> apiTestCaseWithBlobs = null; List<ApiTestCaseWithBlob> apiTestCaseWithBlobs = null;
if (CollectionUtils.isNotEmpty(apiCaseIdList)) { if (CollectionUtils.isNotEmpty(apiCaseIdList)) {
apiTestCaseWithBlobs = extApiTestCaseMapper.selectAllDetailByApiIds(apiCaseIdList); apiTestCaseWithBlobs = extApiTestCaseMapper.selectAllDetailByIds(apiCaseIdList);
if (CollectionUtils.isNotEmpty(apiCaseIdList)) { if (CollectionUtils.isNotEmpty(apiCaseIdList)) {
apiTestCaseWithBlobs.forEach(item -> { apiTestCaseWithBlobs.forEach(item -> {
if (!apiDefinitionIdList.contains(item.getApiDefinitionId())) { if (!apiDefinitionIdList.contains(item.getApiDefinitionId())) {

View File

@ -1446,6 +1446,13 @@ public class ApiScenarioService extends MoveNodeService {
.collect(Collectors.toMap(ApiScenarioStepBlob::getId, blob -> new String(blob.getContent()))); .collect(Collectors.toMap(ApiScenarioStepBlob::getId, blob -> new String(blob.getContent())));
} }
public Map<String, String> getStepDetailMap(List<? extends ApiScenarioStepCommonDTO> steps) {
List<String> stepIdList = steps.stream()
.map(ApiScenarioStepCommonDTO::getId).distinct()
.toList();
return getStepBlobByIds(stepIdList).stream()
.collect(Collectors.toMap(ApiScenarioStepBlob::getId, blob -> new String(blob.getContent())));
}
public List<ApiScenarioStepBlob> getStepBlobByIds(List<String> stepIds) { public List<ApiScenarioStepBlob> getStepBlobByIds(List<String> stepIds) {
if (CollectionUtils.isEmpty(stepIds)) { if (CollectionUtils.isEmpty(stepIds)) {
return Collections.emptyList(); return Collections.emptyList();
@ -2467,7 +2474,7 @@ public class ApiScenarioService extends MoveNodeService {
return apiStepResourceInfo; return apiStepResourceInfo;
} }
public MetersphereApiScenarioExportResponse selectAndSortScenarioDetailWithIds(@Valid List<@NotBlank(message = "{id must not be blank}") String> scenarioIds) { public MetersphereApiScenarioExportResponse selectAndSortScenarioDetailWithIds(@Valid List<@NotBlank(message = "{id must not be blank}") String> scenarioIds, Map<String, String> moduleMap) {
MetersphereApiScenarioExportResponse response = new MetersphereApiScenarioExportResponse(); MetersphereApiScenarioExportResponse response = new MetersphereApiScenarioExportResponse();
// 数据准备 // 数据准备
@ -2481,13 +2488,18 @@ public class ApiScenarioService extends MoveNodeService {
.stream() .stream()
.distinct() // 这里可能存在多次引用相同场景步骤可能会重复去重 .distinct() // 这里可能存在多次引用相同场景步骤可能会重复去重
.collect(Collectors.toList()); .collect(Collectors.toList());
response.setScenarioStepList(allSteps);
//查询引用的场景ID //查询引用的场景ID
List<String> stepScenarioIds = allSteps.stream().filter(step -> isScenarioStep(step.getStepType())).map(ApiScenarioStepDTO::getResourceId).toList(); List<String> stepScenarioIds = allSteps.stream().filter(step -> isScenarioStep(step.getStepType())).map(ApiScenarioStepDTO::getResourceId).toList();
// 查询所有场景的blob // 查询所有场景的blob
List<String> allScenarioIds = new ArrayList<>(scenarioIds); List<String> allScenarioIds = new ArrayList<>(scenarioIds);
allScenarioIds.addAll(stepScenarioIds); stepScenarioIds.forEach(stepScenarioId -> {
if (!allScenarioIds.contains(stepScenarioId)) {
allScenarioIds.add(stepScenarioId);
}
});
ApiScenarioBlobExample scenarioBlobExample = new ApiScenarioBlobExample(); ApiScenarioBlobExample scenarioBlobExample = new ApiScenarioBlobExample();
scenarioBlobExample.createCriteria().andIdIn(allScenarioIds); scenarioBlobExample.createCriteria().andIdIn(allScenarioIds);
Map<String, ApiScenarioBlob> scenarioBlobMap = apiScenarioBlobMapper.selectByExampleWithBLOBs(scenarioBlobExample).stream().collect(Collectors.toMap(ApiScenarioBlob::getId, Function.identity())); Map<String, ApiScenarioBlob> scenarioBlobMap = apiScenarioBlobMapper.selectByExampleWithBLOBs(scenarioBlobExample).stream().collect(Collectors.toMap(ApiScenarioBlob::getId, Function.identity()));
@ -2497,17 +2509,17 @@ public class ApiScenarioService extends MoveNodeService {
response.setApiScenarioCsvList(apiScenarioCsvMapper.selectByExample(apiScenarioCsvExample)); response.setApiScenarioCsvList(apiScenarioCsvMapper.selectByExample(apiScenarioCsvExample));
// 导出的场景 // 导出的场景
response.setExportScenarioList(ApiScenarioUtils.parseApiScenarioDetail(exportApiScenarios, scenarioBlobMap)); response.setExportScenarioList(ApiScenarioUtils.parseApiScenarioDetail(exportApiScenarios, scenarioBlobMap, moduleMap));
//步骤引用的场景 //步骤引用的场景
if (CollectionUtils.isNotEmpty(stepScenarioIds)) { if (CollectionUtils.isNotEmpty(stepScenarioIds)) {
scenarioExample.clear(); scenarioExample.clear();
scenarioExample.createCriteria().andIdIn(stepScenarioIds); scenarioExample.createCriteria().andIdIn(stepScenarioIds);
List<ApiScenario> otherScenarios = apiScenarioMapper.selectByExample(scenarioExample); List<ApiScenario> otherScenarios = apiScenarioMapper.selectByExample(scenarioExample);
response.setExportScenarioList(ApiScenarioUtils.parseApiScenarioDetail(otherScenarios, scenarioBlobMap)); response.setRelatedScenarioList(ApiScenarioUtils.parseApiScenarioDetail(otherScenarios, scenarioBlobMap, moduleMap));
} }
response.setScenarioStepBlobMap(getPartialRefStepDetailMap(allSteps)); response.setScenarioStepBlobMap(getStepDetailMap(allSteps));
} }
return response; return response;

View File

@ -4,19 +4,27 @@ import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioBlob; import io.metersphere.api.domain.ApiScenarioBlob;
import io.metersphere.api.dto.scenario.ApiScenarioDetail; import io.metersphere.api.dto.scenario.ApiScenarioDetail;
import io.metersphere.api.dto.scenario.ScenarioConfig; import io.metersphere.api.dto.scenario.ScenarioConfig;
import io.metersphere.sdk.constants.ModuleConstants;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
public class ApiScenarioUtils { public class ApiScenarioUtils {
public static List<ApiScenarioDetail> parseApiScenarioDetail(List<ApiScenario> apiScenarios, Map<String, ApiScenarioBlob> scenarioBlobMap) { public static List<ApiScenarioDetail> parseApiScenarioDetail(List<ApiScenario> apiScenarios, Map<String, ApiScenarioBlob> scenarioBlobMap, Map<String, String> moduleMap) {
List<ApiScenarioDetail> returnList = new ArrayList<>(); List<ApiScenarioDetail> returnList = new ArrayList<>();
for (ApiScenario apiScenario : apiScenarios) { for (ApiScenario apiScenario : apiScenarios) {
ApiScenarioDetail apiScenarioDetail = BeanUtils.copyBean(new ApiScenarioDetail(), apiScenario); ApiScenarioDetail apiScenarioDetail = BeanUtils.copyBean(new ApiScenarioDetail(), apiScenario);
apiScenarioDetail.setSteps(List.of()); apiScenarioDetail.setSteps(List.of());
if (moduleMap.containsKey(apiScenarioDetail.getModuleId())) {
apiScenarioDetail.setModulePath(moduleMap.get(apiScenarioDetail.getModuleId()));
} else {
apiScenarioDetail.setModuleId(ModuleConstants.DEFAULT_NODE_ID);
apiScenarioDetail.setModulePath(Translator.get("api_unplanned_scenario"));
}
ApiScenarioBlob apiScenarioBlob = scenarioBlobMap.get(apiScenario.getId()); ApiScenarioBlob apiScenarioBlob = scenarioBlobMap.get(apiScenario.getId());
if (apiScenarioBlob != null) { if (apiScenarioBlob != null) {
apiScenarioDetail.setScenarioConfig(JSON.parseObject(new String(apiScenarioBlob.getConfig()), ScenarioConfig.class)); apiScenarioDetail.setScenarioConfig(JSON.parseObject(new String(apiScenarioBlob.getConfig()), ScenarioConfig.class));