feat(接口自动化): 导出jmx文件

This commit is contained in:
fit2-zhao 2021-02-20 15:55:10 +08:00
parent e831eb5f3d
commit 44f04d6a2c
3 changed files with 83 additions and 4 deletions

View File

@ -160,5 +160,11 @@ public class ApiAutomationController {
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);
}
}

View File

@ -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;
}
}

View File

@ -435,6 +435,47 @@ public class ApiAutomationService {
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);
}
/**
* 场景测试执行
*
@ -805,16 +846,32 @@ public class ApiAutomationService {
return apiImport;
}
public ApiScenrioExportResult export(ApiScenarioBatchRequest request) {
private List<ApiScenarioWithBLOBs> getExportResult(ApiScenarioBatchRequest request) {
ServiceUtils.getSelectAllIds(request, request.getCondition(),
(query) -> extApiScenarioMapper.selectIdsByQuery((ApiScenarioRequest) query));
ApiScenarioExample example = new ApiScenarioExample();
example.createCriteria().andIdIn(request.getIds());
List<ApiScenarioWithBLOBs> apiScenarioWithBLOBs = apiScenarioMapper.selectByExampleWithBLOBs(example);
return apiScenarioWithBLOBs;
}
public ApiScenrioExportResult export(ApiScenarioBatchRequest request) {
ApiScenrioExportResult result = new ApiScenrioExportResult();
result.setData(apiScenarioWithBLOBs);
result.setData(getExportResult(request));
result.setProjectId(request.getProjectId());
result.setVersion(System.getenv("MS_VERSION"));
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;
}
}