fix(接口测试): csv部分配置不生效

This commit is contained in:
AgAngle 2024-05-22 18:10:35 +08:00 committed by Craftsman
parent 8647152be3
commit 58f9137cec
4 changed files with 49 additions and 10 deletions

View File

@ -67,9 +67,11 @@ public class MsCsvChildPreConverter extends AbstractJmeterElementConverter<Abstr
csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI)); csvDataSet.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass(JmeterAlias.TEST_BEAN_GUI));
csvDataSet.setName(StringUtils.isEmpty(csvVariable.getName()) ? CSVDataSet.class.getSimpleName() : csvVariable.getName()); csvDataSet.setName(StringUtils.isEmpty(csvVariable.getName()) ? CSVDataSet.class.getSimpleName() : csvVariable.getName());
csvDataSet.setProperty(JmeterProperty.FILE_ENCODING, StringUtils.isEmpty(csvVariable.getEncoding()) ? StandardCharsets.UTF_8.name() : csvVariable.getEncoding()); csvDataSet.setProperty(JmeterProperty.FILE_ENCODING, StringUtils.isEmpty(csvVariable.getEncoding()) ? StandardCharsets.UTF_8.name() : csvVariable.getEncoding());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.IGNORE_FIRST_LINE, csvVariable.getIgnoreFirstLine());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.STOP_THREAD, csvVariable.getStopThreadOnEof());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.FILE_NAME, path); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.FILE_NAME, path);
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.SHARE_MODE, shareMode); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.SHARE_MODE, shareMode);
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.RECYCLE, true); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.RECYCLE, csvVariable.getRecycleOnEof());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.VARIABLE_NAMES, csvVariable.getVariableNames()); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.VARIABLE_NAMES, csvVariable.getVariableNames());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.DELIMITER, csvVariable.getDelimiter()); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.DELIMITER, csvVariable.getDelimiter());
csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.QUOTED_DATA, csvVariable.getAllowQuotedData()); csvDataSet.setProperty(JmeterProperty.CSVDataSetProperty.QUOTED_DATA, csvVariable.getAllowQuotedData());

View File

@ -14,6 +14,8 @@ public class JmeterProperty {
public final static String FILE_ENCODING = "fileEncoding"; public final static String FILE_ENCODING = "fileEncoding";
public class CSVDataSetProperty { public class CSVDataSetProperty {
public static final String IGNORE_FIRST_LINE = "ignoreFirstLine";
public static final String STOP_THREAD = "stopThread";
public static final String FILE_NAME = "filename"; public static final String FILE_NAME = "filename";
public static final String SHARE_MODE = "shareMode"; public static final String SHARE_MODE = "shareMode";
public static final String RECYCLE = "recycle"; public static final String RECYCLE = "recycle";

View File

@ -527,15 +527,31 @@ public class ApiScenarioService extends MoveNodeService {
} }
csvSteps.forEach(step -> step.setScenarioId(scenario.getId())); csvSteps.forEach(step -> step.setScenarioId(scenario.getId()));
saveStepCsv(scenario.getId(), steps, csvSteps);
csvSteps = filterNotExistCsv(request.getScenarioConfig(), csvSteps);
saveStepCsv(scenario.getId(), csvSteps);
} }
} }
private List<ApiScenarioCsvStep> filterNotExistCsv(ScenarioConfig scenarioConfig, List<ApiScenarioCsvStep> csvSteps) {
Set<String> 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) { private void handCsvFilesAdd(ApiScenarioAddRequest request, String creator, ApiScenario scenario) {
if (request.getScenarioConfig() == null || request.getScenarioConfig().getVariable() == null || request.getScenarioConfig().getVariable().getCsvVariables() == null) { List<CsvVariable> csvVariables = getCsvVariables(request.getScenarioConfig());
if (CollectionUtils.isEmpty(csvVariables)) {
return; return;
} }
List<CsvVariable> csvVariables = request.getScenarioConfig().getVariable().getCsvVariables();
// 处理 csv 相关数据表 // 处理 csv 相关数据表
handleCsvDataUpdate(csvVariables, scenario, List.of()); handleCsvDataUpdate(csvVariables, scenario, List.of());
@ -544,6 +560,13 @@ public class ApiScenarioService extends MoveNodeService {
handleCsvFileUpdate(csvVariables, List.of(), scenario, creator); handleCsvFileUpdate(csvVariables, List.of(), scenario, creator);
} }
private List<CsvVariable> 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) { private void handleStepFilesAdd(ApiScenarioAddRequest request, String creator, ApiScenario scenario) {
Map<String, ResourceAddFileParam> stepFileParam = request.getStepFileParam(); Map<String, ResourceAddFileParam> stepFileParam = request.getStepFileParam();
if (MapUtils.isNotEmpty(stepFileParam)) { if (MapUtils.isNotEmpty(stepFileParam)) {
@ -576,7 +599,7 @@ public class ApiScenarioService extends MoveNodeService {
return resourceUpdateRequest; return resourceUpdateRequest;
} }
private void saveStepCsv(String scenarioId, List<ApiScenarioStep> steps, List<ApiScenarioCsvStep> csvSteps) { private void saveStepCsv(String scenarioId, List<ApiScenarioCsvStep> csvSteps) {
// 先删除 // 先删除
ApiScenarioCsvStepExample csvStepExample = new ApiScenarioCsvStepExample(); ApiScenarioCsvStepExample csvStepExample = new ApiScenarioCsvStepExample();
csvStepExample.createCriteria().andScenarioIdEqualTo(scenarioId); csvStepExample.createCriteria().andScenarioIdEqualTo(scenarioId);
@ -589,10 +612,11 @@ public class ApiScenarioService extends MoveNodeService {
} }
private void handleCsvUpdate(ScenarioConfig scenarioConfig, ApiScenario scenario, String userId) { private void handleCsvUpdate(ScenarioConfig scenarioConfig, ApiScenario scenario, String userId) {
if (scenarioConfig == null || scenarioConfig.getVariable() == null || scenarioConfig.getVariable().getCsvVariables() == null) { if (scenarioConfig == null) {
return; return;
} }
List<CsvVariable> csvVariables = scenarioConfig.getVariable().getCsvVariables();
List<CsvVariable> csvVariables = getCsvVariables(scenarioConfig);
List<ApiScenarioCsv> dbCsv = getApiScenarioCsv(scenario.getId()); List<ApiScenarioCsv> dbCsv = getApiScenarioCsv(scenario.getId());
// 更新 csv 相关数据表 // 更新 csv 相关数据表
@ -782,7 +806,8 @@ public class ApiScenarioService extends MoveNodeService {
apiScenarioSteps.forEach(step -> step.setScenarioId(scenario.getId())); apiScenarioSteps.forEach(step -> step.setScenarioId(scenario.getId()));
scenarioCsvSteps.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()); addSpecialStepDetails(steps, request.getStepDetails());
List<ApiScenarioStepBlob> updateStepBlobs = getUpdateStepBlobs(apiScenarioSteps, request.getStepDetails()); List<ApiScenarioStepBlob> updateStepBlobs = getUpdateStepBlobs(apiScenarioSteps, request.getStepDetails());
@ -960,11 +985,11 @@ public class ApiScenarioService extends MoveNodeService {
if (CollectionUtils.isNotEmpty(step.getCsvIds())) { if (CollectionUtils.isNotEmpty(step.getCsvIds())) {
//如果是csv文件 需要保存到apiScenarioCsvStep表中 //如果是csv文件 需要保存到apiScenarioCsvStep表中
step.getCsvIds().forEach(fileId -> { step.getCsvIds().forEach(csvId -> {
ApiScenarioCsvStep csvStep = new ApiScenarioCsvStep(); ApiScenarioCsvStep csvStep = new ApiScenarioCsvStep();
csvStep.setId(IDGenerator.nextStr()); csvStep.setId(IDGenerator.nextStr());
csvStep.setStepId(apiScenarioStep.getId()); csvStep.setStepId(apiScenarioStep.getId());
csvStep.setFileId(fileId); csvStep.setFileId(csvId);
csvSteps.add(csvStep); csvSteps.add(csvStep);
}); });
} }

View File

@ -175,6 +175,8 @@ public class ApiScenarioControllerTests extends BaseTest {
private UserMapper userMapper; private UserMapper userMapper;
@Resource @Resource
private ApiScenarioReportService scenarioReportService; private ApiScenarioReportService scenarioReportService;
@Resource
private ApiScenarioCsvStepMapper apiScenarioCsvStepMapper;
private static String fileMetadataId; private static String fileMetadataId;
private static String fileMetadataStepId; 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.getCreateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getCreateUser()).getName());
Assertions.assertEquals(apiScenarioDetail.getUpdateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getUpdateUser()).getName()); Assertions.assertEquals(apiScenarioDetail.getUpdateUserName(), userMapper.selectByPrimaryKey(apiScenarioDetail.getUpdateUser()).getName());
Assertions.assertFalse(apiScenarioDetail.getFollow()); Assertions.assertFalse(apiScenarioDetail.getFollow());
// 验证数据 // 验证数据
assertGetApiScenarioSteps(this.addApiScenarioSteps, apiScenarioDetail.getSteps()); assertGetApiScenarioSteps(this.addApiScenarioSteps, apiScenarioDetail.getSteps());
@ -1327,6 +1330,13 @@ public class ApiScenarioControllerTests extends BaseTest {
for (int i = 0; i < addApiScenarioSteps.size(); i++) { for (int i = 0; i < addApiScenarioSteps.size(); i++) {
ApiScenarioStepRequest stepRequest = (ApiScenarioStepRequest) addApiScenarioSteps.get(i); ApiScenarioStepRequest stepRequest = (ApiScenarioStepRequest) addApiScenarioSteps.get(i);
ApiScenarioStepDTO stepDTO = (ApiScenarioStepDTO) steps.get(i); ApiScenarioStepDTO stepDTO = (ApiScenarioStepDTO) steps.get(i);
ApiScenarioCsvStepExample example = new ApiScenarioCsvStepExample();
example.createCriteria().andStepIdEqualTo(stepDTO.getId());
List<String> 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)); Assertions.assertEquals(BeanUtils.copyBean(new ApiScenarioStepCommonDTO(), stepRequest), BeanUtils.copyBean(new ApiScenarioStepCommonDTO(), stepDTO));
assertGetApiScenarioSteps(stepRequest.getChildren(), stepDTO.getChildren()); assertGetApiScenarioSteps(stepRequest.getChildren(), stepDTO.getChildren());
} }