refactor(接口自动化): 执行代码优化
This commit is contained in:
parent
dfd7f6de59
commit
96900cd1f4
|
@ -325,22 +325,17 @@ public class ApiAutomationService {
|
|||
}
|
||||
|
||||
/**
|
||||
* 场景测试执行
|
||||
* 生成HashTree
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
* @param apiScenarios 场景
|
||||
* @param request 请求参数
|
||||
* @param isSave 是否需要生成报告
|
||||
* @return hashTree
|
||||
*/
|
||||
public String run(RunScenarioRequest request) {
|
||||
List<ApiScenarioWithBLOBs> apiScenarios = null;
|
||||
List<String> ids = request.getScenarioIds();
|
||||
if (request.isSelectAllDate()) {
|
||||
ids = this.getAllScenarioIdsByFontedSelect(
|
||||
request.getModuleIds(), request.getName(), request.getProjectId(), request.getFilters(), request.getUnSelectIds());
|
||||
}
|
||||
apiScenarios = extApiScenarioMapper.selectIds(ids);
|
||||
private HashTree generateHashTree(List<ApiScenarioWithBLOBs> apiScenarios, RunScenarioRequest request, boolean isSave) {
|
||||
HashTree jmeterHashTree = new ListedHashTree();
|
||||
MsTestPlan testPlan = new MsTestPlan();
|
||||
testPlan.setHashTree(new LinkedList<>());
|
||||
HashTree jmeterHashTree = new ListedHashTree();
|
||||
try {
|
||||
boolean isFirst = true;
|
||||
for (ApiScenarioWithBLOBs item : apiScenarios) {
|
||||
|
@ -380,49 +375,44 @@ public class ApiAutomationService {
|
|||
LinkedList<MsTestElement> scenarios = new LinkedList<>();
|
||||
scenarios.add(scenario);
|
||||
// 创建场景报告
|
||||
createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
|
||||
request.getExecuteType(), item.getProjectId(), request.getReportUserID());
|
||||
if (isSave) {
|
||||
createScenarioReport(group.getName(), item.getId(), item.getName(), request.getTriggerMode() == null ? ReportTriggerMode.MANUAL.name() : request.getTriggerMode(),
|
||||
request.getExecuteType(), item.getProjectId(), request.getReportUserID());
|
||||
}
|
||||
group.setHashTree(scenarios);
|
||||
testPlan.getHashTree().add(group);
|
||||
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
MSException.throwException(ex.getMessage());
|
||||
}
|
||||
|
||||
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
|
||||
return jmeterHashTree;
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景测试执行
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public String run(RunScenarioRequest request) {
|
||||
List<ApiScenarioWithBLOBs> apiScenarios = null;
|
||||
List<String> ids = request.getScenarioIds();
|
||||
if (request.isSelectAllDate()) {
|
||||
ids = this.getAllScenarioIdsByFontedSelect(
|
||||
request.getModuleIds(), request.getName(), request.getProjectId(), request.getFilters(), request.getUnSelectIds());
|
||||
}
|
||||
apiScenarios = extApiScenarioMapper.selectIds(ids);
|
||||
|
||||
String runMode = ApiRunMode.SCENARIO.name();
|
||||
if (StringUtils.isNotBlank(request.getRunMode()) && StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) {
|
||||
runMode = ApiRunMode.SCENARIO_PLAN.name();
|
||||
}
|
||||
// 调用执行方法
|
||||
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
|
||||
jMeterService.runDefinition(request.getId(), generateHashTree(apiScenarios, request, true), request.getReportId(), runMode);
|
||||
return request.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前台查询条件查询的所有(未经分页筛选)数据ID
|
||||
*
|
||||
* @param moduleIds 模块ID_前台查询时所选择的
|
||||
* @param name 搜索条件_名称_前台查询时所输入的
|
||||
* @param projectId 所属项目_前台查询时所在项目
|
||||
* @param filters 过滤集合__前台查询时的过滤条件
|
||||
* @param unSelectIds 未勾选ID_前台没有勾选的ID
|
||||
* @return
|
||||
*/
|
||||
private List<String> getAllScenarioIdsByFontedSelect(List<String> moduleIds, String name, String projectId, List<String> filters, List<String> unSelectIds) {
|
||||
ApiScenarioRequest selectRequest = new ApiScenarioRequest();
|
||||
selectRequest.setModuleIds(moduleIds);
|
||||
selectRequest.setName(name);
|
||||
selectRequest.setProjectId(projectId);
|
||||
selectRequest.setFilters(filters);
|
||||
selectRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
||||
List<ApiScenarioDTO> list = extApiScenarioMapper.list(selectRequest);
|
||||
List<String> allIds = list.stream().map(ApiScenarioDTO::getId).collect(Collectors.toList());
|
||||
List<String> ids = allIds.stream().filter(id -> !unSelectIds.contains(id)).collect(Collectors.toList());
|
||||
return ids;
|
||||
}
|
||||
|
||||
/**
|
||||
* 场景测试执行
|
||||
*
|
||||
|
@ -460,6 +450,29 @@ public class ApiAutomationService {
|
|||
return dto;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取前台查询条件查询的所有(未经分页筛选)数据ID
|
||||
*
|
||||
* @param moduleIds 模块ID_前台查询时所选择的
|
||||
* @param name 搜索条件_名称_前台查询时所输入的
|
||||
* @param projectId 所属项目_前台查询时所在项目
|
||||
* @param filters 过滤集合__前台查询时的过滤条件
|
||||
* @param unSelectIds 未勾选ID_前台没有勾选的ID
|
||||
* @return
|
||||
*/
|
||||
private List<String> getAllScenarioIdsByFontedSelect(List<String> moduleIds, String name, String projectId, List<String> filters, List<String> unSelectIds) {
|
||||
ApiScenarioRequest selectRequest = new ApiScenarioRequest();
|
||||
selectRequest.setModuleIds(moduleIds);
|
||||
selectRequest.setName(name);
|
||||
selectRequest.setProjectId(projectId);
|
||||
selectRequest.setFilters(filters);
|
||||
selectRequest.setWorkspaceId(SessionUtils.getCurrentWorkspaceId());
|
||||
List<ApiScenarioDTO> list = extApiScenarioMapper.list(selectRequest);
|
||||
List<String> allIds = list.stream().map(ApiScenarioDTO::getId).collect(Collectors.toList());
|
||||
List<String> ids = allIds.stream().filter(id -> !unSelectIds.contains(id)).collect(Collectors.toList());
|
||||
return ids;
|
||||
}
|
||||
|
||||
public String addScenarioToPlan(SaveApiPlanRequest request) {
|
||||
if (CollectionUtils.isEmpty(request.getPlanIds())) {
|
||||
MSException.throwException(Translator.get("plan id is null "));
|
||||
|
@ -578,51 +591,8 @@ public class ApiAutomationService {
|
|||
apiScenarios = extApiScenarioMapper.selectIds(ids);
|
||||
MsTestPlan testPlan = new MsTestPlan();
|
||||
testPlan.setHashTree(new LinkedList<>());
|
||||
HashTree jmeterHashTree = new ListedHashTree();
|
||||
try {
|
||||
boolean isFirst = true;
|
||||
for (ApiScenarioWithBLOBs item : apiScenarios) {
|
||||
if (item.getStepTotal() == 0) {
|
||||
MSException.throwException(item.getName() + "," + Translator.get("automation_exec_info"));
|
||||
break;
|
||||
}
|
||||
MsThreadGroup group = new MsThreadGroup();
|
||||
group.setLabel(item.getName());
|
||||
group.setName(UUID.randomUUID().toString());
|
||||
// 批量执行的结果直接存储为报告
|
||||
if (isFirst) {
|
||||
group.setName(request.getId());
|
||||
isFirst = false;
|
||||
}
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
JSONObject element = JSON.parseObject(item.getScenarioDefinition());
|
||||
MsScenario scenario = JSONObject.parseObject(item.getScenarioDefinition(), MsScenario.class);
|
||||
|
||||
// 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取
|
||||
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"),
|
||||
new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
scenario.setHashTree(elements);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(element.getString("variables"))) {
|
||||
LinkedList<ScenarioVariable> variables = mapper.readValue(element.getString("variables"),
|
||||
new TypeReference<LinkedList<ScenarioVariable>>() {
|
||||
});
|
||||
scenario.setVariables(variables);
|
||||
}
|
||||
group.setEnableCookieShare(scenario.isEnableCookieShare());
|
||||
LinkedList<MsTestElement> scenarios = new LinkedList<>();
|
||||
scenarios.add(scenario);
|
||||
group.setHashTree(scenarios);
|
||||
testPlan.getHashTree().add(group);
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
MSException.throwException(ex.getMessage());
|
||||
}
|
||||
|
||||
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
|
||||
HashTree jmeterHashTree = generateHashTree(apiScenarios, request, false);
|
||||
String jmx = testPlan.getJmx(jmeterHashTree);
|
||||
String name = request.getName() + ".jmx";
|
||||
|
||||
|
|
Loading…
Reference in New Issue