refactor: 测试计划执行接口案例-将方法迁移到测试计划Service中
测试计划执行接口案例-将方法迁移到测试计划Service中
This commit is contained in:
parent
0f983aae63
commit
097ab76a70
|
@ -299,7 +299,7 @@ public class ApiAutomationService {
|
|||
return null;
|
||||
}
|
||||
|
||||
private void createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID) {
|
||||
public void createScenarioReport(String id, String scenarioId, String scenarioName, String triggerMode, String execType, String projectId, String userID) {
|
||||
APIScenarioReportResult report = new APIScenarioReportResult();
|
||||
report.setId(id);
|
||||
report.setTestId(id);
|
||||
|
@ -417,88 +417,6 @@ public class ApiAutomationService {
|
|||
return request.getId();
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试计划的定时任务--执行场景案例
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public String run(SchedulePlanScenarioExecuteRequest request) {
|
||||
MsTestPlan testPlan = new MsTestPlan();
|
||||
testPlan.setHashTree(new LinkedList<>());
|
||||
HashTree jmeterHashTree = new ListedHashTree();
|
||||
Map<String, Map<String, String>> testPlanScenarioIdMap = request.getTestPlanScenarioIDMap();
|
||||
for (Map.Entry<String, Map<String, String>> entry : testPlanScenarioIdMap.entrySet()) {
|
||||
String testPlanID = entry.getKey();
|
||||
Map<String, String> planScenarioIdMap = entry.getValue();
|
||||
List<ApiScenarioWithBLOBs> apiScenarios = extApiScenarioMapper.selectIds(new ArrayList<>(planScenarioIdMap.keySet()));
|
||||
try {
|
||||
boolean isFirst = true;
|
||||
for (ApiScenarioWithBLOBs item : apiScenarios) {
|
||||
String apiScenarioID = item.getId();
|
||||
String planScenarioID = planScenarioIdMap.get(apiScenarioID);
|
||||
if (StringUtils.isEmpty(planScenarioID)) {
|
||||
continue;
|
||||
}
|
||||
if (item.getStepTotal() == 0) {
|
||||
// 只有一个场景且没有测试步骤,则提示
|
||||
if (apiScenarios.size() == 1) {
|
||||
MSException.throwException((item.getName() + "," + Translator.get("automation_exec_info")));
|
||||
}
|
||||
LogUtil.warn(item.getName() + "," + Translator.get("automation_exec_info"));
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
// 创建场景报告
|
||||
//不同的运行模式,第二个参数入参不同
|
||||
createScenarioReport(group.getName(),
|
||||
planScenarioID + ":" + request.getTestPlanReportId(),
|
||||
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());
|
||||
String runMode = ApiRunMode.SCHEDULE_SCENARIO_PLAN.name();
|
||||
// 调用执行方法
|
||||
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
|
||||
return request.getId();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 获取前台查询条件查询的所有(未经分页筛选)数据ID
|
||||
*
|
||||
|
|
|
@ -40,22 +40,24 @@ public class TestPlanTestJob extends MsScheduleJob {
|
|||
private Map<String,String> apiTestCaseIdMap;
|
||||
private Map<String,String> performanceIdMap;
|
||||
|
||||
private ApiAutomationService apiAutomationService;
|
||||
private PerformanceTestService performanceTestService;
|
||||
private TestPlanScenarioCaseService testPlanScenarioCaseService;
|
||||
private TestPlanApiCaseService testPlanApiCaseService;
|
||||
private ApiTestCaseService apiTestCaseService;
|
||||
private TestPlanReportService testPlanReportService;
|
||||
private TestPlanLoadCaseService testPlanLoadCaseService;
|
||||
private TestPlanService testPlanService;
|
||||
|
||||
public TestPlanTestJob() {
|
||||
this.apiAutomationService = CommonBeanFactory.getBean(ApiAutomationService.class);
|
||||
this.performanceTestService = CommonBeanFactory.getBean(PerformanceTestService.class);
|
||||
this.testPlanScenarioCaseService = CommonBeanFactory.getBean(TestPlanScenarioCaseService.class);
|
||||
this.testPlanApiCaseService = CommonBeanFactory.getBean(TestPlanApiCaseService.class);
|
||||
this.apiTestCaseService = CommonBeanFactory.getBean(ApiTestCaseService.class);
|
||||
this.testPlanReportService = CommonBeanFactory.getBean(TestPlanReportService.class);
|
||||
this.testPlanLoadCaseService = CommonBeanFactory.getBean(TestPlanLoadCaseService.class);
|
||||
this.testPlanService = CommonBeanFactory.getBean(TestPlanService.class);
|
||||
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -128,7 +130,7 @@ public class TestPlanTestJob extends MsScheduleJob {
|
|||
scenarioRequest.setTestPlanID(this.resourceId);
|
||||
scenarioRequest.setRunMode(ApiRunMode.SCHEDULE_SCENARIO_PLAN.name());
|
||||
scenarioRequest.setTestPlanReportId(testPlanReport.getId());
|
||||
apiAutomationService.run(scenarioRequest);
|
||||
testPlanService.runApiCase(scenarioRequest);
|
||||
LogUtil.info("-------------- testplan schedule ---------- scenario case over -----------------");
|
||||
|
||||
//执行性能测试任务
|
||||
|
|
|
@ -1,21 +1,29 @@
|
|||
package io.metersphere.track.service;
|
||||
|
||||
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.alibaba.fastjson.JSONArray;
|
||||
import com.alibaba.fastjson.JSONObject;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.automation.ApiScenarioDTO;
|
||||
import io.metersphere.api.dto.automation.ScenarioStatus;
|
||||
import io.metersphere.api.dto.automation.SchedulePlanScenarioExecuteRequest;
|
||||
import io.metersphere.api.dto.automation.TestPlanScenarioRequest;
|
||||
import io.metersphere.api.dto.definition.ApiTestCaseRequest;
|
||||
import io.metersphere.api.dto.definition.TestPlanApiCaseDTO;
|
||||
import io.metersphere.api.dto.definition.request.*;
|
||||
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
|
||||
import io.metersphere.api.jmeter.JMeterService;
|
||||
import io.metersphere.api.service.ApiAutomationService;
|
||||
import io.metersphere.base.domain.*;
|
||||
import io.metersphere.base.mapper.*;
|
||||
import io.metersphere.base.mapper.ext.ExtApiScenarioMapper;
|
||||
import io.metersphere.base.mapper.ext.ExtTestCaseMapper;
|
||||
import io.metersphere.base.mapper.ext.ExtTestPlanMapper;
|
||||
import io.metersphere.base.mapper.ext.ExtTestPlanTestCaseMapper;
|
||||
import io.metersphere.commons.constants.NoticeConstants;
|
||||
import io.metersphere.commons.constants.TestPlanStatus;
|
||||
import io.metersphere.commons.constants.TestPlanTestCaseStatus;
|
||||
import io.metersphere.commons.constants.*;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.user.SessionUser;
|
||||
import io.metersphere.commons.utils.*;
|
||||
|
@ -37,6 +45,8 @@ import org.apache.ibatis.annotations.Param;
|
|||
import org.apache.ibatis.session.ExecutorType;
|
||||
import org.apache.ibatis.session.SqlSession;
|
||||
import org.apache.ibatis.session.SqlSessionFactory;
|
||||
import org.apache.jorphan.collections.HashTree;
|
||||
import org.apache.jorphan.collections.ListedHashTree;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
@ -61,6 +71,8 @@ public class TestPlanService {
|
|||
@Resource
|
||||
TestPlanTestCaseMapper testPlanTestCaseMapper;
|
||||
@Resource
|
||||
ExtApiScenarioMapper extApiScenarioMapper;
|
||||
@Resource
|
||||
SqlSessionFactory sqlSessionFactory;
|
||||
@Lazy
|
||||
@Resource
|
||||
|
@ -87,6 +99,10 @@ public class TestPlanService {
|
|||
private TestPlanScenarioCaseService testPlanScenarioCaseService;
|
||||
@Resource
|
||||
private TestPlanLoadCaseService testPlanLoadCaseService;
|
||||
@Resource
|
||||
private JMeterService jMeterService;
|
||||
@Resource
|
||||
private ApiAutomationService apiAutomationService;
|
||||
|
||||
public synchronized void addTestPlan(AddTestPlanRequest testPlan) {
|
||||
if (getTestPlanByName(testPlan.getName()).size() > 0) {
|
||||
|
@ -728,4 +744,85 @@ public class TestPlanService {
|
|||
public String findScheduleCreateUserById(String testPlanId) {
|
||||
return extTestPlanMapper.findScheduleCreateUserById(testPlanId);
|
||||
}
|
||||
|
||||
/**
|
||||
* 测试计划的定时任务--执行场景案例
|
||||
*
|
||||
* @param request
|
||||
* @return
|
||||
*/
|
||||
public String runApiCase(SchedulePlanScenarioExecuteRequest request) {
|
||||
MsTestPlan testPlan = new MsTestPlan();
|
||||
testPlan.setHashTree(new LinkedList<>());
|
||||
HashTree jmeterHashTree = new ListedHashTree();
|
||||
Map<String, Map<String, String>> testPlanScenarioIdMap = request.getTestPlanScenarioIDMap();
|
||||
for (Map.Entry<String, Map<String, String>> entry : testPlanScenarioIdMap.entrySet()) {
|
||||
String testPlanID = entry.getKey();
|
||||
Map<String, String> planScenarioIdMap = entry.getValue();
|
||||
List<ApiScenarioWithBLOBs> apiScenarios = extApiScenarioMapper.selectIds(new ArrayList<>(planScenarioIdMap.keySet()));
|
||||
try {
|
||||
boolean isFirst = true;
|
||||
for (ApiScenarioWithBLOBs item : apiScenarios) {
|
||||
String apiScenarioID = item.getId();
|
||||
String planScenarioID = planScenarioIdMap.get(apiScenarioID);
|
||||
if (StringUtils.isEmpty(planScenarioID)) {
|
||||
continue;
|
||||
}
|
||||
if (item.getStepTotal() == 0) {
|
||||
// 只有一个场景且没有测试步骤,则提示
|
||||
if (apiScenarios.size() == 1) {
|
||||
MSException.throwException((item.getName() + "," + Translator.get("automation_exec_info")));
|
||||
}
|
||||
LogUtil.warn(item.getName() + "," + Translator.get("automation_exec_info"));
|
||||
continue;
|
||||
}
|
||||
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);
|
||||
// 创建场景报告
|
||||
//不同的运行模式,第二个参数入参不同
|
||||
apiAutomationService.createScenarioReport(group.getName(),
|
||||
planScenarioID + ":" + request.getTestPlanReportId(),
|
||||
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());
|
||||
String runMode = ApiRunMode.SCHEDULE_SCENARIO_PLAN.name();
|
||||
// 调用执行方法
|
||||
jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode);
|
||||
return request.getId();
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue