refactor(接口测试): 跨项目环境选择代码优化

This commit is contained in:
fit2-zhao 2022-04-21 17:49:41 +08:00 committed by 刘瑞斌
parent fe5489b1da
commit 2a47d69804
1 changed files with 77 additions and 148 deletions

View File

@ -3,9 +3,6 @@ package io.metersphere.api.exec.scenario;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.metersphere.api.dto.EnvironmentType;
import io.metersphere.api.dto.ScenarioEnv;
import io.metersphere.api.dto.automation.ApiScenarioDTO;
@ -17,13 +14,9 @@ import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy;
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import io.metersphere.api.exec.utils.GenerateHashTreeUtil;
import io.metersphere.api.service.ApiDefinitionService;
import io.metersphere.api.service.ApiTestCaseService;
import io.metersphere.api.service.ApiTestEnvironmentService;
import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.ApiScenarioMapper;
import io.metersphere.base.mapper.ApiTestEnvironmentMapper;
import io.metersphere.base.mapper.ProjectMapper;
import io.metersphere.base.mapper.TestPlanApiScenarioMapper;
import io.metersphere.base.mapper.*;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.MsTestElementConstants;
import io.metersphere.commons.exception.MSException;
@ -36,7 +29,10 @@ import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
@Service
@ -44,8 +40,6 @@ public class ApiScenarioEnvService {
@Resource
private ApiDefinitionService apiDefinitionService;
@Resource
private ApiTestCaseService apiTestCaseService;
@Resource
private ApiScenarioMapper apiScenarioMapper;
@Resource
private EnvironmentGroupProjectService environmentGroupProjectService;
@ -56,164 +50,99 @@ public class ApiScenarioEnvService {
@Resource
private TestPlanApiScenarioMapper testPlanApiScenarioMapper;
@Resource
private ObjectMapper mapper;
private ApiTestCaseMapper apiTestCaseMapper;
public ScenarioEnv getApiScenarioEnv(String definition) {
ScenarioEnv env = new ScenarioEnv();
if (StringUtils.isEmpty(definition)) {
return env;
}
List<MsTestElement> hashTree = GenerateHashTreeUtil.getScenarioHashTree(definition);
for (int i = 0; i < hashTree.size(); i++) {
MsTestElement tr = hashTree.get(i);
if (!tr.isEnable()) {
continue;
}
String referenced = tr.getReferenced();
if (StringUtils.equals(MsTestElementConstants.REF.name(), referenced)) {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
MsHTTPSamplerProxy http = (MsHTTPSamplerProxy) tr;
String refType = tr.getRefType();
if (StringUtils.equals(refType, "CASE")) {
http.setUrl(null);
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
http.setUrl(apiDefinition.getPath());
}
}
if (http.isEnable()) {
if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) {
env.getProjectIds().add(http.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
if (StringUtils.equals(tr.getRefType(), "CASE")) {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(tr.getId());
if (apiTestCaseWithBLOBs != null) {
env.getProjectIds().add(apiTestCaseWithBLOBs.getProjectId());
env.setFullUrl(false);
}
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
if (apiDefinition != null) {
env.getProjectIds().add(apiDefinition.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "scenario")) {
if (tr.isEnable()) {
ApiScenarioWithBLOBs apiScenario = apiScenarioMapper.selectByPrimaryKey(tr.getId());
if (apiScenario != null) {
env.getProjectIds().add(apiScenario.getProjectId());
String scenarioDefinition = apiScenario.getScenarioDefinition();
tr.setHashTree(GenerateHashTreeUtil.getScenarioHashTree(scenarioDefinition));
}
}
}
} else {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
// 校验是否是全路径
MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr;
if (httpSamplerProxy.isEnable()) {
if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || (httpSamplerProxy.getIsRefEnvironment() != null && httpSamplerProxy.getIsRefEnvironment())) {
env.getProjectIds().add(httpSamplerProxy.getProjectId());
env.setFullUrl(false);
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
env.getProjectIds().add(tr.getProjectId());
env.setFullUrl(false);
}
}
if (StringUtils.equals(tr.getType(), "scenario")) {
MsScenario scenario = (MsScenario) tr;
if (scenario.isEnvironmentEnable()) {
continue;
}
env.getProjectIds().add(tr.getProjectId());
}
if (CollectionUtils.isNotEmpty(tr.getHashTree())) {
getHashTree(tr.getHashTree(), env);
if (CollectionUtils.isNotEmpty(hashTree)) {
// 过滤掉禁用的步骤
hashTree = hashTree.stream().filter(item -> item.isEnable()).collect(Collectors.toList());
}
for (MsTestElement testElement : hashTree) {
this.formatElement(testElement, env);
if (CollectionUtils.isNotEmpty(testElement.getHashTree())) {
getHashTree(testElement.getHashTree(), env);
}
}
return env;
}
private void getHashTree(List<MsTestElement> tree, ScenarioEnv env) {
try {
for (int i = 0; i < tree.size(); i++) {
MsTestElement tr = tree.get(i);
if (!tr.isEnable()) {
continue;
// 过滤掉禁用的步骤
tree = tree.stream().filter(item -> item.isEnable()).collect(Collectors.toList());
for (MsTestElement element : tree) {
this.formatElement(element, env);
if (CollectionUtils.isNotEmpty(element.getHashTree())) {
getHashTree(element.getHashTree(), env);
}
String referenced = tr.getReferenced();
if (StringUtils.equals(MsTestElementConstants.REF.name(), referenced)) {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
MsHTTPSamplerProxy http = (MsHTTPSamplerProxy) tr;
String refType = tr.getRefType();
if (StringUtils.equals(refType, "CASE")) {
http.setUrl(null);
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
http.setUrl(apiDefinition.getPath());
}
if (http.isEnable()) {
if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) {
env.setFullUrl(false);
env.getProjectIds().add(http.getProjectId());
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
if (StringUtils.equals(tr.getRefType(), "CASE")) {
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseService.get(tr.getId());
env.getProjectIds().add(apiTestCaseWithBLOBs.getProjectId());
}
} catch (Exception e) {
LogUtil.error(e);
}
}
private void formatElement(MsTestElement testElement, ScenarioEnv env) {
if (StringUtils.equals(MsTestElementConstants.REF.name(), testElement.getReferenced())) {
if (StringUtils.equals(testElement.getType(), "HTTPSamplerProxy")) {
MsHTTPSamplerProxy http = (MsHTTPSamplerProxy) testElement;
// 引用用例URL清空
http.setUrl(StringUtils.equals(testElement.getRefType(), "CASE") ? null : http.getUrl());
// 非全路径校验
if (StringUtils.isBlank(http.getUrl()) || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) {
env.getProjectIds().add(http.getProjectId());
env.setFullUrl(false);
}
} else if (StringUtils.equals(testElement.getType(), "JDBCSampler") || StringUtils.equals(testElement.getType(), "TCPSampler")) {
if (StringUtils.isEmpty(testElement.getProjectId())) {
if (StringUtils.equals(testElement.getRefType(), "CASE")) {
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(testElement.getId());
if (testCase != null) {
env.getProjectIds().add(testCase.getProjectId());
env.setFullUrl(false);
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(tr.getId());
}
} else {
ApiDefinition apiDefinition = apiDefinitionService.get(testElement.getId());
if (apiDefinition != null) {
env.getProjectIds().add(apiDefinition.getProjectId());
env.setFullUrl(false);
}
} else if (StringUtils.equals(tr.getType(), "scenario")) {
if (tr.isEnable()) {
ApiScenarioWithBLOBs apiScenario = apiScenarioMapper.selectByPrimaryKey(tr.getId());
if (apiScenario != null) {
env.getProjectIds().add(apiScenario.getProjectId());
String scenarioDefinition = apiScenario.getScenarioDefinition();
JSONObject element1 = JSON.parseObject(scenarioDefinition, Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(element1);
LinkedList<MsTestElement> hashTree1 = mapper.readValue(element1.getString("hashTree"), new TypeReference<LinkedList<MsTestElement>>() {
});
tr.setHashTree(hashTree1);
}
}
}
} else {
if (StringUtils.equals(tr.getType(), "HTTPSamplerProxy")) {
// 校验是否是全路径
MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) tr;
if (httpSamplerProxy.isEnable()) {
if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || !ElementUtil.isURL(httpSamplerProxy.getUrl())) {
env.setFullUrl(false);
env.getProjectIds().add(httpSamplerProxy.getProjectId());
}
}
} else if (StringUtils.equals(tr.getType(), "JDBCSampler") || StringUtils.equals(tr.getType(), "TCPSampler")) {
env.getProjectIds().add(tr.getProjectId());
env.setFullUrl(false);
}
env.getProjectIds().add(testElement.getProjectId());
env.setFullUrl(false);
}
if (StringUtils.equals(tr.getType(), "scenario")) {
MsScenario scenario = (MsScenario) tr;
if (scenario.isEnvironmentEnable()) {
continue;
}
env.getProjectIds().add(tr.getProjectId());
}
if (CollectionUtils.isNotEmpty(tr.getHashTree())) {
getHashTree(tr.getHashTree(), env);
} else if (StringUtils.equals(testElement.getType(), "scenario") && StringUtils.isEmpty(testElement.getProjectId())) {
ApiScenarioWithBLOBs apiScenario = apiScenarioMapper.selectByPrimaryKey(testElement.getId());
if (apiScenario != null) {
env.getProjectIds().add(apiScenario.getProjectId());
String scenarioDefinition = apiScenario.getScenarioDefinition();
JSONObject element = JSON.parseObject(scenarioDefinition, Feature.DisableSpecialKeyDetect);
ElementUtil.dataFormatting(element);
testElement.setHashTree(GenerateHashTreeUtil.getScenarioHashTree(scenarioDefinition));
}
}
} catch (JsonProcessingException e) {
LogUtil.error(e);
} else {
if (StringUtils.equals(testElement.getType(), "HTTPSamplerProxy")) {
// 校验是否是全路径
MsHTTPSamplerProxy httpSamplerProxy = (MsHTTPSamplerProxy) testElement;
if (StringUtils.isBlank(httpSamplerProxy.getUrl()) || (httpSamplerProxy.getIsRefEnvironment() != null && httpSamplerProxy.getIsRefEnvironment())) {
env.getProjectIds().add(httpSamplerProxy.getProjectId());
env.setFullUrl(false);
}
} else if (StringUtils.equals(testElement.getType(), "JDBCSampler") || StringUtils.equals(testElement.getType(), "TCPSampler")) {
env.getProjectIds().add(testElement.getProjectId());
env.setFullUrl(false);
}
}
if (StringUtils.equals(testElement.getType(), "scenario") && !((MsScenario) testElement).isEnvironmentEnable()) {
env.getProjectIds().add(testElement.getProjectId());
}
}