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

View File

@ -1,5 +1,10 @@
package io.metersphere.api.exec.api; package io.metersphere.api.exec.api;
import io.metersphere.plugin.core.MsTestElement;
public interface ApiRetryOnFailureService { public interface ApiRetryOnFailureService {
public String retry(String data, long retryNum, boolean isCase); 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.service.EnvironmentGroupProjectService;
import io.metersphere.utils.LoggerUtil; import io.metersphere.utils.LoggerUtil;
import io.metersphere.vo.BooleanPool; import io.metersphere.vo.BooleanPool;
import io.metersphere.xpack.api.dto.MsRetryLoopController;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree; 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) { public static LinkedList<MsTestElement> getScenarioHashTree(String definition) {
ObjectMapper objectMapper = new ObjectMapper(); ObjectMapper objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);