feat(接口自动化): 导出jmx文件
This commit is contained in:
parent
e831eb5f3d
commit
44f04d6a2c
|
@ -160,5 +160,11 @@ public class ApiAutomationController {
|
||||||
return apiAutomationService.export(request);
|
return apiAutomationService.export(request);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@PostMapping(value = "/export/jmx")
|
||||||
|
@RequiresRoles(value = {RoleConstants.TEST_USER, RoleConstants.TEST_MANAGER}, logical = Logical.OR)
|
||||||
|
public List<ApiScenrioExportJmx> exportJmx(@RequestBody ApiScenarioBatchRequest request) {
|
||||||
|
return apiAutomationService.exportJmx(request);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
package io.metersphere.api.dto.automation;
|
||||||
|
|
||||||
|
import lombok.Getter;
|
||||||
|
import lombok.Setter;
|
||||||
|
|
||||||
|
@Getter
|
||||||
|
@Setter
|
||||||
|
public class ApiScenrioExportJmx {
|
||||||
|
private String name;
|
||||||
|
private String jmx;
|
||||||
|
|
||||||
|
public ApiScenrioExportJmx(String name, String jmx) {
|
||||||
|
this.name = name;
|
||||||
|
this.jmx = jmx;
|
||||||
|
}
|
||||||
|
}
|
|
@ -435,6 +435,47 @@ public class ApiAutomationService {
|
||||||
return jmeterHashTree;
|
return jmeterHashTree;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String generateJmx(ApiScenarioWithBLOBs apiScenario) {
|
||||||
|
HashTree jmeterHashTree = new ListedHashTree();
|
||||||
|
MsTestPlan testPlan = new MsTestPlan();
|
||||||
|
testPlan.setHashTree(new LinkedList<>());
|
||||||
|
try {
|
||||||
|
MsThreadGroup group = new MsThreadGroup();
|
||||||
|
group.setLabel(apiScenario.getName());
|
||||||
|
group.setName(apiScenario.getName());
|
||||||
|
|
||||||
|
ObjectMapper mapper = new ObjectMapper();
|
||||||
|
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||||
|
JSONObject element = JSON.parseObject(apiScenario.getScenarioDefinition());
|
||||||
|
MsScenario scenario = JSONObject.parseObject(apiScenario.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());
|
||||||
|
group.setHashTree(new LinkedList<MsTestElement>() {{
|
||||||
|
this.add(scenario);
|
||||||
|
}});
|
||||||
|
|
||||||
|
testPlan.getHashTree().add(group);
|
||||||
|
} catch (Exception ex) {
|
||||||
|
MSException.throwException(ex.getMessage());
|
||||||
|
}
|
||||||
|
|
||||||
|
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
|
||||||
|
return testPlan.getJmx(jmeterHashTree);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 场景测试执行
|
* 场景测试执行
|
||||||
*
|
*
|
||||||
|
@ -668,7 +709,7 @@ public class ApiAutomationService {
|
||||||
HashTree jmeterHashTree = generateHashTree(apiScenarios, request, null);
|
HashTree jmeterHashTree = generateHashTree(apiScenarios, request, null);
|
||||||
String jmx = testPlan.getJmx(jmeterHashTree);
|
String jmx = testPlan.getJmx(jmeterHashTree);
|
||||||
|
|
||||||
jmx = apiTestService.updateJmxString(jmx,testName,false);
|
jmx = apiTestService.updateJmxString(jmx, testName, false);
|
||||||
|
|
||||||
//将ThreadGroup的testname改为接口名称
|
//将ThreadGroup的testname改为接口名称
|
||||||
// Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板
|
// Document doc = DocumentHelper.parseText(jmx);// 获取可续保保单列表报文模板
|
||||||
|
@ -805,16 +846,32 @@ public class ApiAutomationService {
|
||||||
return apiImport;
|
return apiImport;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ApiScenrioExportResult export(ApiScenarioBatchRequest request) {
|
private List<ApiScenarioWithBLOBs> getExportResult(ApiScenarioBatchRequest request) {
|
||||||
ServiceUtils.getSelectAllIds(request, request.getCondition(),
|
ServiceUtils.getSelectAllIds(request, request.getCondition(),
|
||||||
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));
|
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));
|
||||||
ApiScenarioExample example = new ApiScenarioExample();
|
ApiScenarioExample example = new ApiScenarioExample();
|
||||||
example.createCriteria().andIdIn(request.getIds());
|
example.createCriteria().andIdIn(request.getIds());
|
||||||
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = apiScenarioMapper.selectByExampleWithBLOBs(example);
|
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = apiScenarioMapper.selectByExampleWithBLOBs(example);
|
||||||
ApiScenrioExportResult result = new ApiScenrioExportResult();
|
return apiScenarioWithBLOBs;
|
||||||
result.setData(apiScenarioWithBLOBs);
|
}
|
||||||
|
|
||||||
|
public ApiScenrioExportResult export(ApiScenarioBatchRequest request) {
|
||||||
|
ApiScenrioExportResult result = new ApiScenrioExportResult();
|
||||||
|
result.setData(getExportResult(request));
|
||||||
result.setProjectId(request.getProjectId());
|
result.setProjectId(request.getProjectId());
|
||||||
result.setVersion(System.getenv("MS_VERSION"));
|
result.setVersion(System.getenv("MS_VERSION"));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public List<ApiScenrioExportJmx> exportJmx(ApiScenarioBatchRequest request) {
|
||||||
|
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = getExportResult(request);
|
||||||
|
// 生成jmx
|
||||||
|
List<ApiScenrioExportJmx> resList = new ArrayList<>();
|
||||||
|
apiScenarioWithBLOBs.forEach(item -> {
|
||||||
|
ApiScenrioExportJmx scenrioExportJmx = new ApiScenrioExportJmx(item.getName(), generateJmx(item));
|
||||||
|
resList.add(scenrioExportJmx);
|
||||||
|
});
|
||||||
|
return resList;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue