fix(接口测试): 场景复制时处理csv文件的复制

This commit is contained in:
AgAngle 2024-05-21 10:53:30 +08:00 committed by Craftsman
parent 25f2f6a82b
commit eb0bdc72c9
3 changed files with 71 additions and 2 deletions

View File

@ -58,6 +58,10 @@ public class ApiScenarioAddRequest {
@Size(max = 50, message = "{api_scenario.environment_id.length_range}")
private String environmentId;
@Schema(description = "复制的原场景ID")
@Size(max = 50)
private String copyFromScenarioId;
@Schema(description = "场景的通用配置")
@Valid
private ScenarioConfig scenarioConfig = new ScenarioConfig();

View File

@ -126,7 +126,7 @@ public class ApiFileResourceService {
String fileName = getTempFileNameByFileId(fileId);
if (StringUtils.isBlank(fileName)) {
// 如果 fileName 查不到说明该文件已经从临时目录移到正式目录已经关联过了无需关联
break;
continue;
}
ApiFileResource apiFileResource = new ApiFileResource();
apiFileResource.setFileId(fileId);
@ -343,7 +343,7 @@ public class ApiFileResourceService {
apiFileResources.add(apiFileResource);
} catch (Exception e) {
LogUtils.error(e);
throw new MSException(Translator.get("file_copy_fail"));
throw new MSException(e);
}
});
@ -351,6 +351,20 @@ public class ApiFileResourceService {
}
}
public void copyFile(String sourceFolder, String targetFolder, String fileName) {
try {
FileCopyRequest fileCopyRequest = new FileCopyRequest();
fileCopyRequest.setCopyFolder(sourceFolder);
fileCopyRequest.setCopyfileName(fileName);
fileCopyRequest.setFileName(fileName);
fileCopyRequest.setFolder(targetFolder);
FileCenter.getDefaultRepository().copyFile(fileCopyRequest);
} catch (Exception e) {
LogUtils.error(e);
throw new MSException(e);
}
}
public List<ApiFileResource> selectByApiScenarioId(List<String> scenarioIds) {
ApiFileResourceExample example = new ApiFileResourceExample();
example.createCriteria().andResourceIdIn(scenarioIds);

View File

@ -133,6 +133,8 @@ public class ApiScenarioService extends MoveNodeService {
@Resource
private ApiFileResourceService apiFileResourceService;
@Resource
private ApiFileResourceMapper apiFileResourceMapper;
@Resource
private ApiScenarioStepMapper apiScenarioStepMapper;
@Resource
private ExtApiScenarioStepMapper extApiScenarioStepMapper;
@ -435,6 +437,9 @@ public class ApiScenarioService extends MoveNodeService {
apiScenarioBlob.setConfig(JSON.toJSONString(request.getScenarioConfig()).getBytes());
apiScenarioBlobMapper.insert(apiScenarioBlob);
// 处理复制场景时的文件复制
handleCopyFromScenarioFile(request, scenario, creator);
// 处理csv文件
handCsvFilesAdd(request, creator, scenario);
@ -443,9 +448,55 @@ public class ApiScenarioService extends MoveNodeService {
// 处理步骤文件
handleStepFilesAdd(request, creator, scenario);
return scenario;
}
/**
* 处理复制场景时的文件复制
* @param request
* @param scenario
*/
private void handleCopyFromScenarioFile(ApiScenarioAddRequest request, ApiScenario scenario, String userId) {
String copyFromScenarioId = request.getCopyFromScenarioId();
if (StringUtils.isNotBlank(copyFromScenarioId)) {
// 查询原场景的 csv 文件
ApiScenarioCsvExample example = new ApiScenarioCsvExample();
example.createCriteria().andScenarioIdEqualTo(copyFromScenarioId);
Map<String, ApiScenarioCsv> csvMap = apiScenarioCsvMapper.selectByExample(example)
.stream()
.collect(Collectors.toMap(ApiScenarioCsv::getFileId, Function.identity()));
if (!csvMap.isEmpty()) {
ScenarioConfig scenarioConfig = request.getScenarioConfig();
List<CsvVariable> csvVariables = scenarioConfig.getVariable().getCsvVariables();
for (CsvVariable csvVariable : csvVariables) {
String originFileId = csvVariable.getFile().getFileId();
ApiScenarioCsv copyFromCsv = csvMap.get(originFileId);
if (copyFromCsv != null && BooleanUtils.isTrue(csvVariable.getFile().getLocal())) {
// 重新生成 fileId从原场景复制文件
csvVariable.getFile().setFileId(IDGenerator.nextStr());
String sourceDir = DefaultRepositoryDir.getApiScenarioDir(request.getProjectId(), copyFromScenarioId);
String targetDir = DefaultRepositoryDir.getApiScenarioDir(request.getProjectId(), scenario.getId());
apiFileResourceService.copyFile(sourceDir + "/" + copyFromCsv.getFileId(),
targetDir + "/" + csvVariable.getFile().getFileId(),
csvVariable.getFile().getFileName());
// 在临时文件没有找到文件则不会创建关联关系这里手动添加
ApiFileResource apiFileResource = new ApiFileResource();
apiFileResource.setFileId(csvVariable.getFile().getFileId());
apiFileResource.setResourceId(scenario.getId());
apiFileResource.setResourceType(ApiResourceType.API_SCENARIO.name());
apiFileResource.setProjectId(scenario.getProjectId());
apiFileResource.setCreateTime(System.currentTimeMillis());
apiFileResource.setFileName(csvVariable.getFile().getFileName());
apiFileResourceMapper.insert(apiFileResource);
}
}
}
}
}
private void handleStepAdd(ApiScenarioAddRequest request, ApiScenario scenario) {
// 插入步骤
if (CollectionUtils.isNotEmpty(request.getSteps())) {