fix(接口测试): fix api scenario import with related any api scenarios

https://www.tapd.cn/55049933/s/1598844
This commit is contained in:
Jianguo-Genius 2024-10-28 11:49:44 +08:00 committed by 刘瑞斌
parent 84fe354d67
commit 5f3d4682b6
6 changed files with 54 additions and 19 deletions

View File

@ -18,7 +18,8 @@ import java.util.Map;
*/ */
@Data @Data
public class MetersphereApiScenarioExportResponse extends ApiScenarioExportResponse { public class MetersphereApiScenarioExportResponse extends ApiScenarioExportResponse {
@Schema(description = "是否包含关联资源")
private boolean hasRelatedResource;
@Schema(description = "导出的场景") @Schema(description = "导出的场景")
private List<ApiScenarioDetail> exportScenarioList = new ArrayList<>(); private List<ApiScenarioDetail> exportScenarioList = new ArrayList<>();

View File

@ -1,6 +1,7 @@
package io.metersphere.api.dto.scenario; package io.metersphere.api.dto.scenario;
import io.metersphere.api.domain.ApiScenario; import io.metersphere.api.domain.ApiScenario;
import io.metersphere.system.uid.IDGenerator;
import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import lombok.EqualsAndHashCode; import lombok.EqualsAndHashCode;
@ -16,4 +17,14 @@ public class ApiScenarioDetail extends ApiScenario {
private ScenarioConfig scenarioConfig = new ScenarioConfig(); private ScenarioConfig scenarioConfig = new ScenarioConfig();
@Schema(description = "步骤") @Schema(description = "步骤")
private List<ApiScenarioStepDTO> steps; private List<ApiScenarioStepDTO> steps;
public void resetConfigCsvId() {
if (scenarioConfig == null || scenarioConfig.getVariable() == null || scenarioConfig.getVariable().getCsvVariables() == null) {
return;
}
scenarioConfig.getVariable().getCsvVariables().forEach(csvVariable -> {
csvVariable.setId(IDGenerator.nextStr());
});
}
} }

View File

@ -1,6 +1,7 @@
package io.metersphere.api.parser.api.dataimport; package io.metersphere.api.parser.api.dataimport;
import io.metersphere.api.constants.ApiScenarioStepType;
import io.metersphere.api.domain.ApiScenarioCsv; import io.metersphere.api.domain.ApiScenarioCsv;
import io.metersphere.api.dto.converter.ApiScenarioImportParseResult; import io.metersphere.api.dto.converter.ApiScenarioImportParseResult;
import io.metersphere.api.dto.converter.ApiScenarioStepParseResult; import io.metersphere.api.dto.converter.ApiScenarioStepParseResult;
@ -55,26 +56,31 @@ public class MetersphereParserApiScenario implements ApiScenarioImportParser {
for (ApiScenarioDetail apiScenarioDetail : exportScenarioList) { for (ApiScenarioDetail apiScenarioDetail : exportScenarioList) {
apiScenarioDetail.resetConfigCsvId();
returnResult.getImportScenarioList().add( returnResult.getImportScenarioList().add(
this.parseApiScenario(projectId, apiScenarioDetail, scenarioStepMap, scenarioStepBlobMap, apiScenarioCsvMap)); this.parseApiScenario(projectId, metersphereApiScenarioExportResponse.isHasRelatedResource(), apiScenarioDetail, scenarioStepMap, scenarioStepBlobMap, apiScenarioCsvMap));
} }
for (ApiScenarioDetail apiScenarioDetail : relatedScenarioList) { if (metersphereApiScenarioExportResponse.isHasRelatedResource()) {
returnResult.getRelatedScenarioList().add( for (ApiScenarioDetail apiScenarioDetail : relatedScenarioList) {
this.parseApiScenario(projectId, apiScenarioDetail, scenarioStepMap, scenarioStepBlobMap, apiScenarioCsvMap)); apiScenarioDetail.resetConfigCsvId();
returnResult.getRelatedScenarioList().add(
this.parseApiScenario(projectId, true, apiScenarioDetail, scenarioStepMap, scenarioStepBlobMap, apiScenarioCsvMap));
}
} }
return returnResult; return returnResult;
} }
private ApiScenarioImportDetail parseApiScenario(String projectId, private ApiScenarioImportDetail parseApiScenario(String projectId,
boolean hasRelatedData,
ApiScenarioDetail apiScenarioDetail, ApiScenarioDetail apiScenarioDetail,
Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap, Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap,
Map<String, String> scenarioStepBlobMap, Map<String, String> scenarioStepBlobMap,
Map<String, List<ApiScenarioCsv>> apiScenarioCsvMap) { Map<String, List<ApiScenarioCsv>> apiScenarioCsvMap) {
ApiScenarioImportDetail apiScenarioImportDetail = new ApiScenarioImportDetail(); ApiScenarioImportDetail apiScenarioImportDetail = new ApiScenarioImportDetail();
BeanUtils.copyBean(apiScenarioImportDetail, apiScenarioDetail); BeanUtils.copyBean(apiScenarioImportDetail, apiScenarioDetail);
ApiScenarioStepParseResult parseResult = this.parseStepDetails(apiScenarioStepMap, apiScenarioDetail.getId(), scenarioStepBlobMap); ApiScenarioStepParseResult parseResult = this.parseStepDetails(apiScenarioStepMap, apiScenarioDetail.getId(), hasRelatedData, scenarioStepBlobMap);
apiScenarioImportDetail.setSteps(parseResult.getStepList()); apiScenarioImportDetail.setSteps(parseResult.getStepList());
apiScenarioImportDetail.setStepDetails(parseResult.getStepDetails()); apiScenarioImportDetail.setStepDetails(parseResult.getStepDetails());
apiScenarioImportDetail.setProjectId(projectId); apiScenarioImportDetail.setProjectId(projectId);
@ -82,7 +88,7 @@ public class MetersphereParserApiScenario implements ApiScenarioImportParser {
return apiScenarioImportDetail; return apiScenarioImportDetail;
} }
private ApiScenarioStepParseResult parseStepDetails(Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap, String scenarioId, Map<String, String> scenarioStepBlobMap) { private ApiScenarioStepParseResult parseStepDetails(Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap, String scenarioId, boolean hasRelatedData, Map<String, String> scenarioStepBlobMap) {
ApiScenarioStepParseResult apiScenarioStepParseResult = new ApiScenarioStepParseResult(); ApiScenarioStepParseResult apiScenarioStepParseResult = new ApiScenarioStepParseResult();
List<ApiScenarioStepDTO> stepList = apiScenarioStepMap.getOrDefault(scenarioId, new ArrayList<>()); List<ApiScenarioStepDTO> stepList = apiScenarioStepMap.getOrDefault(scenarioId, new ArrayList<>());
@ -109,8 +115,8 @@ public class MetersphereParserApiScenario implements ApiScenarioImportParser {
if (scenarioStepBlobMap.containsKey(oldStepId)) { if (scenarioStepBlobMap.containsKey(oldStepId)) {
apiScenarioStepParseResult.getStepDetails().put(stepRequest.getId(), scenarioStepBlobMap.get(oldStepId).getBytes()); apiScenarioStepParseResult.getStepDetails().put(stepRequest.getId(), scenarioStepBlobMap.get(oldStepId).getBytes());
} }
stepRequest.setChildren(this.buildTreeStep(this.getChildScenarioStep(oldStepId, apiScenarioStepMap), stepRequest.setChildren(this.buildTreeStep(this.getChildScenarioStep(oldStepId, stepDTO.getStepType(), stepDTO.getScenarioId(), hasRelatedData, apiScenarioStepMap),
apiScenarioStepMap, scenarioStepBlobMap, apiScenarioStepParseResult)); hasRelatedData, apiScenarioStepMap, scenarioStepBlobMap, apiScenarioStepParseResult));
apiScenarioStepParseResult.getStepList().add(stepRequest); apiScenarioStepParseResult.getStepList().add(stepRequest);
} }
@ -119,6 +125,7 @@ public class MetersphereParserApiScenario implements ApiScenarioImportParser {
private List<ApiScenarioStepRequest> buildTreeStep( private List<ApiScenarioStepRequest> buildTreeStep(
List<ApiScenarioStepDTO> childScenarioStep, List<ApiScenarioStepDTO> childScenarioStep,
boolean hasRelatedData,
Map<String, List<ApiScenarioStepDTO>> allScenarioStepMap, Map<String, List<ApiScenarioStepDTO>> allScenarioStepMap,
Map<String, String> scenarioStepBlobMap, Map<String, String> scenarioStepBlobMap,
ApiScenarioStepParseResult apiScenarioStepParseResult) { ApiScenarioStepParseResult apiScenarioStepParseResult) {
@ -133,22 +140,36 @@ public class MetersphereParserApiScenario implements ApiScenarioImportParser {
if (scenarioStepBlobMap.containsKey(oldChildId)) { if (scenarioStepBlobMap.containsKey(oldChildId)) {
apiScenarioStepParseResult.getStepDetails().put(childRequest.getId(), scenarioStepBlobMap.get(oldChildId).getBytes()); apiScenarioStepParseResult.getStepDetails().put(childRequest.getId(), scenarioStepBlobMap.get(oldChildId).getBytes());
} }
childRequest.setChildren(this.buildTreeStep(this.getChildScenarioStep(oldChildId, allScenarioStepMap), childRequest.setChildren(this.buildTreeStep(this.getChildScenarioStep(oldChildId, childDTO.getStepType(), childDTO.getScenarioId(), hasRelatedData, allScenarioStepMap),
allScenarioStepMap, scenarioStepBlobMap, apiScenarioStepParseResult)); hasRelatedData, allScenarioStepMap, scenarioStepBlobMap, apiScenarioStepParseResult));
returnList.add(childRequest); returnList.add(childRequest);
} }
return returnList; return returnList;
} }
private List<ApiScenarioStepDTO> getChildScenarioStep(String parentId, Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap) { private List<ApiScenarioStepDTO> getChildScenarioStep(String parentId, String parentStepType, String scenarioId, boolean hasRelatedData, Map<String, List<ApiScenarioStepDTO>> apiScenarioStepMap) {
List<ApiScenarioStepDTO> childStepList = new ArrayList<>(); List<ApiScenarioStepDTO> childStepList = new ArrayList<>();
apiScenarioStepMap.values().forEach(stepList -> { List<ApiScenarioStepDTO> allScenarioStepList = apiScenarioStepMap.get(scenarioId);
for (ApiScenarioStepDTO stepDTO : stepList) { if (CollectionUtils.isNotEmpty(allScenarioStepList)) {
allScenarioStepList.forEach(stepDTO -> {
if (StringUtils.equals(stepDTO.getParentId(), parentId)) { if (StringUtils.equals(stepDTO.getParentId(), parentId)) {
childStepList.add(stepDTO); childStepList.add(stepDTO);
} }
} });
}); }
if (CollectionUtils.isEmpty(childStepList) && StringUtils.equalsIgnoreCase(parentStepType, ApiScenarioStepType.API_SCENARIO.name()) && !hasRelatedData) {
//如果没有找到场景步骤并且文件是不导出关联关系的配置则可能这是一个引用的场景我们要去所有场景步骤中找
apiScenarioStepMap.forEach((otherScenarioId, v) -> {
if (StringUtils.equals(otherScenarioId, scenarioId)) {
return;
}
v.forEach(stepDTO -> {
if (StringUtils.equals(stepDTO.getParentId(), parentId)) {
childStepList.add(stepDTO);
}
});
});
}
return childStepList; return childStepList;
} }
} }

View File

@ -1145,7 +1145,7 @@ public class ApiScenarioDataTransferService {
MetersphereApiScenarioExportResponse response = apiScenarioService.selectAndSortScenarioDetailWithIds(request.getSelectIds(), moduleMap); MetersphereApiScenarioExportResponse response = apiScenarioService.selectAndSortScenarioDetailWithIds(request.getSelectIds(), moduleMap);
response.setProjectId(project.getId()); response.setProjectId(project.getId());
response.setOrganizationId(project.getOrganizationId()); response.setOrganizationId(project.getOrganizationId());
response.setHasRelatedResource(request.isExportAllRelatedData());
if (request.isExportAllRelatedData()) { if (request.isExportAllRelatedData()) {
// 全量导出导出引用的apiapiCase // 全量导出导出引用的apiapiCase
List<String> apiDefinitionIdList = new ArrayList<>(); List<String> apiDefinitionIdList = new ArrayList<>();

View File

@ -1824,7 +1824,9 @@ public class ApiScenarioService extends MoveNodeService {
ApiScenarioStepDTO returnDTO = new ApiScenarioStepDTO(); ApiScenarioStepDTO returnDTO = new ApiScenarioStepDTO();
BeanUtils.copyBean(returnDTO, dto); BeanUtils.copyBean(returnDTO, dto);
if (StringUtils.isNotBlank(parentId) && !StringUtils.equalsIgnoreCase(parentId, dto.getId())) { if (StringUtils.isNotBlank(parentId) && !StringUtils.equalsIgnoreCase(parentId, dto.getId())) {
returnDTO.setParentId(parentId); if (StringUtils.isBlank(returnDTO.getParentId())) {
returnDTO.setParentId(parentId);
}
} }
if (returnDTO.getConfig() != null && StringUtils.isNotBlank(returnDTO.getConfig().toString())) { if (returnDTO.getConfig() != null && StringUtils.isNotBlank(returnDTO.getConfig().toString())) {
if (returnDTO.getConfig() instanceof String configVal) { if (returnDTO.getConfig() instanceof String configVal) {

View File

@ -165,7 +165,7 @@ public class ApiScenarioControllerImportAndExportTests extends BaseTest {
MetersphereApiScenarioExportResponse exportResponse = ApiDataUtils.parseObject(fileContent, MetersphereApiScenarioExportResponse.class); MetersphereApiScenarioExportResponse exportResponse = ApiDataUtils.parseObject(fileContent, MetersphereApiScenarioExportResponse.class);
Assertions.assertEquals(exportResponse.getExportScenarioList().size(), 8); Assertions.assertEquals(exportResponse.getExportScenarioList().size(), 6);
MsFileUtils.deleteDir("/tmp/api-scenario-export/"); MsFileUtils.deleteDir("/tmp/api-scenario-export/");
} }