refactor(接口测试): 优化CASE重试相关代码

This commit is contained in:
fit2-zhao 2022-07-18 15:26:25 +08:00 committed by f2c-ci-robot[bot]
parent c43babbf2a
commit 54784f6dfe
3 changed files with 13 additions and 31 deletions

View File

@ -38,7 +38,6 @@ import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.ResultDTO;
import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.xpack.api.dto.MsRetryLoopController;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
import org.springframework.data.redis.core.RedisTemplate;
@ -134,13 +133,6 @@ public class ApiCaseSerialService {
}
if (caseWithBLOBs != null) {
String data = caseWithBLOBs.getRequest();
// 失败重试
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), true);
data = StringUtils.isNotEmpty(retryData) ? retryData : data;
}
HashTree jmeterHashTree = new HashTree();
MsTestPlan testPlan = new MsTestPlan();
testPlan.setHashTree(new LinkedList<>());
@ -151,13 +143,16 @@ public class ApiCaseSerialService {
group.setProjectId(caseWithBLOBs.getProjectId());
MsTestElement testElement = null;
if (runRequest.isRetryEnable() && runRequest.getRetryNum() > 0) {
MsRetryLoopController controller = JSON.parseObject(data, MsRetryLoopController.class);
GenerateHashTreeUtil.parse(data, controller);
MsTestElement element = parse(JSON.toJSONString(controller.getHashTree().get(0)), testId, envId, caseWithBLOBs.getProjectId());
controller.setHashTree(new LinkedList<>() {{
// 失败重试
ApiRetryOnFailureService apiRetryOnFailureService = CommonBeanFactory.getBean(ApiRetryOnFailureService.class);
String retryData = apiRetryOnFailureService.retry(data, runRequest.getRetryNum(), true);
data = StringUtils.isNotEmpty(retryData) ? retryData : data;
// 格式化数据
testElement = apiRetryOnFailureService.retryParse(data);
MsTestElement element = parse(JSON.toJSONString(testElement.getHashTree().get(0)), testId, envId, caseWithBLOBs.getProjectId());
testElement.setHashTree(new LinkedList<>() {{
this.add(element);
}});
testElement = controller;
} else {
testElement = parse(data, testId, envId, caseWithBLOBs.getProjectId());
}

View File

@ -1,5 +1,10 @@
package io.metersphere.api.exec.api;
import io.metersphere.plugin.core.MsTestElement;
public interface ApiRetryOnFailureService {
public String retry(String data, long retryNum, boolean isCase);
public MsTestElement retryParse(String retryCase);
}

View File

@ -27,7 +27,6 @@ import io.metersphere.plugin.core.MsTestElement;
import io.metersphere.service.EnvironmentGroupProjectService;
import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool;
import io.metersphere.xpack.api.dto.MsRetryLoopController;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
@ -72,23 +71,6 @@ public class GenerateHashTreeUtil {
}
}
public static void parse(String retryCase, MsRetryLoopController retryObj) {
try {
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
JSONObject element = JSON.parseObject(retryCase, Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(element);
if (element != null && StringUtils.isNotEmpty(element.getString("hashTree"))) {
LinkedList<MsTestElement> elements = mapper.readValue(element.getString("hashTree"),
new TypeReference<LinkedList<MsTestElement>>() {
});
retryObj.setHashTree(elements);
}
} catch (Exception e) {
LogUtil.error(e);
}
}
public static LinkedList<MsTestElement> getScenarioHashTree(String definition) {
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);