diff --git a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/child/MsCsvChildPreConverter.java b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/child/MsCsvChildPreConverter.java index 9928a02124..3f4cd1058c 100644 --- a/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/child/MsCsvChildPreConverter.java +++ b/backend/services/api-test/src/main/java/io/metersphere/api/parser/jmeter/child/MsCsvChildPreConverter.java @@ -67,9 +67,11 @@ public class MsCsvChildPreConverter extends AbstractJmeterElementConverter step.setScenarioId(scenario.getId())); - saveStepCsv(scenario.getId(), steps, csvSteps); + + csvSteps = filterNotExistCsv(request.getScenarioConfig(), csvSteps); + saveStepCsv(scenario.getId(), csvSteps); } } + private List filterNotExistCsv(ScenarioConfig scenarioConfig, List csvSteps) { + Set csvIdSet = + getCsvVariables(scenarioConfig) + .stream() + .map(CsvVariable::getId) + .collect(Collectors.toSet()); + + csvSteps = csvSteps.stream() + .filter(step -> csvIdSet.contains(step.getFileId())) + .collect(Collectors.toList()); + return csvSteps; + } + private void handCsvFilesAdd(ApiScenarioAddRequest request, String creator, ApiScenario scenario) { - if (request.getScenarioConfig() == null || request.getScenarioConfig().getVariable() == null || request.getScenarioConfig().getVariable().getCsvVariables() == null) { + List csvVariables = getCsvVariables(request.getScenarioConfig()); + + if (CollectionUtils.isEmpty(csvVariables)) { return; } - List csvVariables = request.getScenarioConfig().getVariable().getCsvVariables(); // 处理 csv 相关数据表 handleCsvDataUpdate(csvVariables, scenario, List.of()); @@ -544,6 +560,13 @@ public class ApiScenarioService extends MoveNodeService { handleCsvFileUpdate(csvVariables, List.of(), scenario, creator); } + private List getCsvVariables(ScenarioConfig scenarioConfig) { + if (scenarioConfig == null ||scenarioConfig.getVariable() == null || scenarioConfig.getVariable().getCsvVariables() == null) { + return List.of(); + } + return scenarioConfig.getVariable().getCsvVariables(); + } + private void handleStepFilesAdd(ApiScenarioAddRequest request, String creator, ApiScenario scenario) { Map stepFileParam = request.getStepFileParam(); if (MapUtils.isNotEmpty(stepFileParam)) { @@ -576,7 +599,7 @@ public class ApiScenarioService extends MoveNodeService { return resourceUpdateRequest; } - private void saveStepCsv(String scenarioId, List steps, List csvSteps) { + private void saveStepCsv(String scenarioId, List csvSteps) { // 先删除 ApiScenarioCsvStepExample csvStepExample = new ApiScenarioCsvStepExample(); csvStepExample.createCriteria().andScenarioIdEqualTo(scenarioId); @@ -589,10 +612,11 @@ public class ApiScenarioService extends MoveNodeService { } private void handleCsvUpdate(ScenarioConfig scenarioConfig, ApiScenario scenario, String userId) { - if (scenarioConfig == null || scenarioConfig.getVariable() == null || scenarioConfig.getVariable().getCsvVariables() == null) { + if (scenarioConfig == null) { return; } - List csvVariables = scenarioConfig.getVariable().getCsvVariables(); + + List csvVariables = getCsvVariables(scenarioConfig); List dbCsv = getApiScenarioCsv(scenario.getId()); // 更新 csv 相关数据表 @@ -782,7 +806,8 @@ public class ApiScenarioService extends MoveNodeService { apiScenarioSteps.forEach(step -> step.setScenarioId(scenario.getId())); scenarioCsvSteps.forEach(step -> step.setScenarioId(scenario.getId())); - saveStepCsv(scenario.getId(), apiScenarioSteps, scenarioCsvSteps); + scenarioCsvSteps = filterNotExistCsv(request.getScenarioConfig(), scenarioCsvSteps); + saveStepCsv(scenario.getId(), scenarioCsvSteps); // 获取待更新的步骤详情 addSpecialStepDetails(steps, request.getStepDetails()); List updateStepBlobs = getUpdateStepBlobs(apiScenarioSteps, request.getStepDetails()); @@ -960,11 +985,11 @@ public class ApiScenarioService extends MoveNodeService { if (CollectionUtils.isNotEmpty(step.getCsvIds())) { //如果是csv文件 需要保存到apiScenarioCsvStep表中 - step.getCsvIds().forEach(fileId -> { + step.getCsvIds().forEach(csvId -> { ApiScenarioCsvStep csvStep = new ApiScenarioCsvStep(); csvStep.setId(IDGenerator.nextStr()); csvStep.setStepId(apiScenarioStep.getId()); - csvStep.setFileId(fileId); + csvStep.setFileId(csvId); csvSteps.add(csvStep); }); } diff --git a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java index 2a14e8559c..07f3440275 100644 --- a/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java +++ b/backend/services/api-test/src/test/java/io/metersphere/api/controller/ApiScenarioControllerTests.java @@ -175,6 +175,8 @@ public class ApiScenarioControllerTests extends BaseTest { private UserMapper userMapper; @Resource private ApiScenarioReportService scenarioReportService; + @Resource + private ApiScenarioCsvStepMapper apiScenarioCsvStepMapper; private static String fileMetadataId; private static String fileMetadataStepId; @@ -1235,6 +1237,7 @@ public class ApiScenarioControllerTests extends BaseTest { Assertions.assertEquals(apiScenarioDetail.getCreateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getCreateUser()).getName()); Assertions.assertEquals(apiScenarioDetail.getUpdateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getUpdateUser()).getName()); Assertions.assertFalse(apiScenarioDetail.getFollow()); + // 验证数据 assertGetApiScenarioSteps(this.addApiScenarioSteps, apiScenarioDetail.getSteps()); @@ -1327,6 +1330,13 @@ public class ApiScenarioControllerTests extends BaseTest { for (int i = 0; i < addApiScenarioSteps.size(); i++) { ApiScenarioStepRequest stepRequest = (ApiScenarioStepRequest) addApiScenarioSteps.get(i); ApiScenarioStepDTO stepDTO = (ApiScenarioStepDTO) steps.get(i); + ApiScenarioCsvStepExample example = new ApiScenarioCsvStepExample(); + example.createCriteria().andStepIdEqualTo(stepDTO.getId()); + List csvIds = apiScenarioCsvStepMapper.selectByExample(example).stream().map(ApiScenarioCsvStep::getFileId).collect(Collectors.toList()); + stepRequest.setCsvIds(csvIds); + if (stepDTO.getCsvIds() == null) { + stepDTO.setCsvIds(List.of()); + } Assertions.assertEquals(BeanUtils.copyBean(new ApiScenarioStepCommonDTO(), stepRequest), BeanUtils.copyBean(new ApiScenarioStepCommonDTO(), stepDTO)); assertGetApiScenarioSteps(stepRequest.getChildren(), stepDTO.getChildren()); }