fix(接口测试): 修复特殊JSON数据失败重跑解析失败问题

This commit is contained in:
fit2-zhao 2023-02-17 11:19:44 +08:00
parent dee8f1fac0
commit ca287cb175
1 changed files with 23 additions and 65 deletions

View File

@ -5,10 +5,6 @@ import io.metersphere.api.dto.definition.request.ElementUtil;
import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.dto.definition.request.MsTestPlan;
import io.metersphere.api.dto.definition.request.MsThreadGroup; import io.metersphere.api.dto.definition.request.MsThreadGroup;
import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.sampler.MsDubboSampler;
import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.definition.request.sampler.MsJDBCSampler;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.exec.queue.DBTestQueue; import io.metersphere.api.exec.queue.DBTestQueue;
import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.JMeterService;
import io.metersphere.api.jmeter.NewDriverManager; import io.metersphere.api.jmeter.NewDriverManager;
@ -22,7 +18,6 @@ import io.metersphere.base.mapper.ApiTestCaseMapper;
import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper; import io.metersphere.base.mapper.plan.TestPlanApiCaseMapper;
import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.CommonConstants; import io.metersphere.commons.constants.CommonConstants;
import io.metersphere.commons.constants.ElementConstants;
import io.metersphere.commons.constants.PropertyConstant; import io.metersphere.commons.constants.PropertyConstant;
import io.metersphere.commons.enums.ApiReportStatus; import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.utils.*; import io.metersphere.commons.utils.*;
@ -49,6 +44,7 @@ import java.util.*;
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public class ApiCaseSerialService { public class ApiCaseSerialService {
private final static String PROJECT_ID = "projectId"; private final static String PROJECT_ID = "projectId";
public static final String NAME = "name";
@Resource @Resource
private JMeterService jMeterService; private JMeterService jMeterService;
@Resource @Resource
@ -147,28 +143,28 @@ public class ApiCaseSerialService {
group.setName(runRequest.getTestPlanReportId()); group.setName(runRequest.getTestPlanReportId());
} }
group.setProjectId(caseWithBLOBs.getProjectId()); group.setProjectId(caseWithBLOBs.getProjectId());
MsTestElement testElement; group.setHashTree(new LinkedList<>());
// 数据兼容处理 // 数据兼容处理
JSONObject element = JSONUtil.parseObject(caseWithBLOBs.getRequest()); JSONObject element = JSONUtil.parseObject(caseWithBLOBs.getRequest());
ElementUtil.dataFormatting(element); ElementUtil.dataFormatting(element);
String data = element.toString(); parse(element, testId, envId, caseWithBLOBs.getProjectId());
String runData = element.toString();
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) { if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
try {
// 失败重试 // 失败重试
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class); ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), true); if (apiRetryOnFailureService != null) {
data = StringUtils.isNotEmpty(retryData) ? retryData : data; String retryData = apiRetryOnFailureService.retry(runData, 3, true);
// 格式化数据 if (StringUtils.isNotBlank(retryData)) {
testElement = apiRetryOnFailureService.retryParse(data); runData = retryData;
MsTestElement msTestElement = parse(JSON.toJSONString(testElement.getHashTree().get(0)), testId, envId, caseWithBLOBs.getProjectId());
testElement.setHashTree(new LinkedList<>() {{
this.add(msTestElement);
}});
} else {
testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId());
} }
}
} catch (Exception e) {
LoggerUtil.error("失败重试脚本生成失败 ", runRequest.getReportId(), e);
}
}
group.getHashTree().add(JSONUtil.parseObject(runData, MsTestElement.class));
group.setHashTree(new LinkedList<>());
group.getHashTree().add(testElement);
testPlan.getHashTree().add(group); testPlan.getHashTree().add(group);
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig());
LoggerUtil.info("用例资源:" + caseWithBLOBs.getName() + ", 生成执行脚本JMX成功", runRequest.getReportId()); LoggerUtil.info("用例资源:" + caseWithBLOBs.getName() + ", 生成执行脚本JMX成功", runRequest.getReportId());
@ -185,55 +181,17 @@ public class ApiCaseSerialService {
return null; return null;
} }
private MsTestElement parse(String api, String planId, String envId, String projectId) { private void parse(JSONObject element, String testId, String envId, String projectId) {
try { try {
JSONObject element = JSONUtil.parseObject(api); element.putOpt(NAME, testId);
LinkedList<MsTestElement> list = new LinkedList<>();
if (element != null && StringUtils.isNotEmpty(element.optString(ElementConstants.HASH_TREE))) {
list.addAll(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
}
if (element.optString(PropertyConstant.TYPE).equals(ElementConstants.HTTP_SAMPLER)) {
MsHTTPSamplerProxy httpSamplerProxy = JSONUtil.parseObject(element.toString(), MsHTTPSamplerProxy.class);
httpSamplerProxy.setHashTree(list);
httpSamplerProxy.setName(planId);
if (StringUtils.isNotEmpty(envId)) { if (StringUtils.isNotEmpty(envId)) {
httpSamplerProxy.setUseEnvironment(envId); element.putOpt(PropertyConstant.ENVIRONMENT, envId);
} }
return httpSamplerProxy; if (StringUtils.isBlank(element.optString(PROJECT_ID))) {
} element.putOpt(PROJECT_ID, projectId);
if (element.optString(PropertyConstant.TYPE).equals(ElementConstants.TCP_SAMPLER)) {
MsTCPSampler msTCPSampler = JSON.parseObject(api, MsTCPSampler.class);
if (StringUtils.isEmpty(msTCPSampler.getProjectId())) {
msTCPSampler.setProjectId(projectId);
}
if (StringUtils.isNotEmpty(envId)) {
msTCPSampler.setUseEnvironment(envId);
}
msTCPSampler.setHashTree(list);
msTCPSampler.setName(planId);
return msTCPSampler;
}
if (element.optString(PropertyConstant.TYPE).equals(ElementConstants.DUBBO_SAMPLER)) {
MsDubboSampler dubboSampler = JSON.parseObject(api, MsDubboSampler.class);
if (StringUtils.isNotEmpty(envId)) {
dubboSampler.setUseEnvironment(envId);
}
dubboSampler.setHashTree(list);
dubboSampler.setName(planId);
return dubboSampler;
}
if (element.optString(PropertyConstant.TYPE).equals(ElementConstants.JDBC_SAMPLER)) {
MsJDBCSampler jDBCSampler = JSON.parseObject(api, MsJDBCSampler.class);
if (StringUtils.isNotEmpty(envId)) {
jDBCSampler.setUseEnvironment(envId);
}
jDBCSampler.setHashTree(list);
jDBCSampler.setName(planId);
return jDBCSampler;
} }
} catch (Exception e) { } catch (Exception e) {
LogUtil.error(e); LogUtil.error(e);
} }
return null;
} }
} }