mirror of
https://gitee.com/fit2cloud-feizhiyun/MeterSphere.git
synced 2024-12-02 12:09:13 +08:00
refactor(接口测试): 场景步骤解析优化统一解析方式
This commit is contained in:
parent
f42792c0e9
commit
655e4bd239
@ -1,8 +1,5 @@
|
||||
package io.metersphere.api.dto.definition.request;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.EnvironmentType;
|
||||
import io.metersphere.api.dto.definition.request.assertions.MsAssertions;
|
||||
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
|
||||
@ -21,7 +18,6 @@ import io.metersphere.api.dto.definition.request.timer.MsConstantTimer;
|
||||
import io.metersphere.api.dto.definition.request.unknown.MsJmeterElement;
|
||||
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
|
||||
import io.metersphere.api.dto.scenario.DatabaseConfig;
|
||||
import io.metersphere.api.dto.scenario.KeyValue;
|
||||
import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
|
||||
import io.metersphere.api.dto.scenario.environment.item.EnvAssertions;
|
||||
import io.metersphere.base.domain.ApiScenarioWithBLOBs;
|
||||
@ -48,7 +44,6 @@ import org.apache.jmeter.config.Arguments;
|
||||
import org.apache.jmeter.config.CSVDataSet;
|
||||
import org.apache.jmeter.config.RandomVariableConfig;
|
||||
import org.apache.jmeter.modifiers.CounterConfig;
|
||||
import org.apache.jmeter.modifiers.JSR223PreProcessor;
|
||||
import org.apache.jmeter.modifiers.UserParameters;
|
||||
import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy;
|
||||
import org.apache.jmeter.save.SaveService;
|
||||
@ -73,20 +68,6 @@ public class ElementUtil {
|
||||
private static final String ASSERTIONS = ElementConstants.ASSERTIONS;
|
||||
private static final String BODY_FILE_DIR = FileUtils.BODY_FILE_DIR;
|
||||
|
||||
public static Arguments addArguments(ParameterConfig config, String projectId, String name) {
|
||||
if (config.isEffective(projectId) && config.getConfig().get(projectId).getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(projectId).getCommonConfig().getVariables())) {
|
||||
Arguments arguments = new Arguments();
|
||||
arguments.setEnabled(true);
|
||||
arguments.setName(StringUtils.isNoneBlank(name) ? name : "Arguments");
|
||||
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
|
||||
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
|
||||
config.getConfig().get(projectId).getCommonConfig().getVariables().stream().filter(ScenarioVariable::isConstantValid).filter(ScenarioVariable::isEnable).forEach(keyValue -> arguments.addArgument(keyValue.getName(), keyValue.getValue(), "="));
|
||||
if (arguments.getArguments().size() > 0) {
|
||||
return arguments;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Map<String, EnvironmentConfig> getEnvironmentConfig(String environmentId, String projectId) {
|
||||
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
|
||||
@ -178,7 +159,7 @@ public class ElementUtil {
|
||||
BodyFile file = item.getFiles().get(0);
|
||||
String fileId = item.getId();
|
||||
boolean isRef = false;
|
||||
String path = null;
|
||||
String path;
|
||||
if (StringUtils.equalsIgnoreCase(file.getStorage(), StorageConstants.FILE_REF.name())) {
|
||||
isRef = true;
|
||||
fileId = file.getFileId();
|
||||
@ -255,36 +236,6 @@ public class ElementUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static String getFullPath(MsTestElement element, String path) {
|
||||
if (element.getParent() == null) {
|
||||
return path;
|
||||
}
|
||||
if (MsTestElementConstants.LoopController.name().equals(element.getType())) {
|
||||
MsLoopController loopController = (MsLoopController) element;
|
||||
if (StringUtils.equals(loopController.getLoopType(), LoopConstants.WHILE.name()) && loopController.getWhileController() != null) {
|
||||
path = "While 循环" + DelimiterConstants.STEP_DELIMITER.toString() + "While 循环-" + "${MS_LOOP_CONTROLLER_CONFIG}";
|
||||
}
|
||||
if (StringUtils.equals(loopController.getLoopType(), LoopConstants.FOREACH.name()) && loopController.getForEachController() != null) {
|
||||
path = "ForEach 循环" + DelimiterConstants.STEP_DELIMITER.toString() + " ForEach 循环-" + "${MS_LOOP_CONTROLLER_CONFIG}";
|
||||
}
|
||||
if (StringUtils.equals(loopController.getLoopType(), LoopConstants.LOOP_COUNT.name()) && loopController.getCountController() != null) {
|
||||
path = "次数循环" + DelimiterConstants.STEP_DELIMITER.toString() + "次数循环-" + "${MS_LOOP_CONTROLLER_CONFIG}";
|
||||
}
|
||||
} else {
|
||||
path = StringUtils.isEmpty(element.getName()) ? element.getType() : element.getName() + DelimiterConstants.STEP_DELIMITER.toString() + path;
|
||||
}
|
||||
return getFullPath(element.getParent(), path);
|
||||
}
|
||||
|
||||
public static String getParentName(MsTestElement parent) {
|
||||
if (parent != null) {
|
||||
// 获取全路径以备后面使用
|
||||
String fullPath = getFullPath(parent, new String());
|
||||
return fullPath + DelimiterConstants.SEPARATOR.toString() + parent.getName();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
public static String getFullIndexPath(MsTestElement element, String path) {
|
||||
if (element == null || element.getParent() == null) {
|
||||
return path;
|
||||
@ -379,28 +330,6 @@ public class ElementUtil {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 只找出场景直接依赖
|
||||
*
|
||||
* @param hashTree
|
||||
* @param referenceRelationships
|
||||
*/
|
||||
public static void relationships(JSONArray hashTree, List<String> referenceRelationships) {
|
||||
for (int i = 0; i < hashTree.length(); i++) {
|
||||
JSONObject element = hashTree.optJSONObject(i);
|
||||
if (element != null && StringUtils.equals(element.get(PropertyConstant.TYPE).toString(), ElementConstants.SCENARIO) && StringUtils.equals(element.get("referenced").toString(), "REF")) {
|
||||
if (!referenceRelationships.contains(element.get("id").toString())) {
|
||||
referenceRelationships.add(element.get("id").toString());
|
||||
}
|
||||
} else {
|
||||
if (element.has(ElementConstants.HASH_TREE)) {
|
||||
JSONArray elementJSONArray = element.optJSONArray(ElementConstants.HASH_TREE);
|
||||
relationships(elementJSONArray, referenceRelationships);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static void dataFormatting(JSONArray hashTree) {
|
||||
for (int i = 0; i < hashTree.length(); i++) {
|
||||
JSONObject element = hashTree.optJSONObject(i);
|
||||
@ -431,8 +360,10 @@ public class ElementUtil {
|
||||
|
||||
public static void dataSetDomain(JSONArray hashTree, MsParameter msParameter) {
|
||||
try {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class);
|
||||
BaseEnvGroupProjectService environmentGroupProjectService = CommonBeanFactory.getBean(BaseEnvGroupProjectService.class);
|
||||
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
|
||||
|
||||
for (int i = 0; i < hashTree.length(); i++) {
|
||||
JSONObject element = hashTree.optJSONObject(i);
|
||||
boolean isScenarioEnv = false;
|
||||
@ -442,40 +373,33 @@ public class ElementUtil {
|
||||
if (scenario.isEnvironmentEnable()) {
|
||||
isScenarioEnv = true;
|
||||
Map<String, String> environmentMap = new HashMap<>();
|
||||
ApiScenarioMapper apiScenarioMapper = CommonBeanFactory.getBean(ApiScenarioMapper.class);
|
||||
BaseEnvGroupProjectService environmentGroupProjectService = CommonBeanFactory.getBean(BaseEnvGroupProjectService.class);
|
||||
ApiScenarioWithBLOBs apiScenarioWithBLOBs = apiScenarioMapper.selectByPrimaryKey(scenario.getId());
|
||||
String environmentType = apiScenarioWithBLOBs.getEnvironmentType();
|
||||
String environmentGroupId = apiScenarioWithBLOBs.getEnvironmentGroupId();
|
||||
String environmentJson = apiScenarioWithBLOBs.getEnvironmentJson();
|
||||
if (StringUtils.equals(environmentType, EnvironmentType.GROUP.name())) {
|
||||
environmentMap = environmentGroupProjectService.getEnvMap(environmentGroupId);
|
||||
} else if (StringUtils.equals(environmentType, EnvironmentType.JSON.name())) {
|
||||
environmentMap = JSON.parseObject(environmentJson, Map.class);
|
||||
if (apiScenarioWithBLOBs == null) {
|
||||
continue;
|
||||
}
|
||||
Map<String, EnvironmentConfig> envConfig = new HashMap<>(16);
|
||||
if (StringUtils.equals(apiScenarioWithBLOBs.getEnvironmentType(), EnvironmentType.GROUP.name())) {
|
||||
environmentMap = environmentGroupProjectService.getEnvMap(apiScenarioWithBLOBs.getEnvironmentGroupId());
|
||||
} else if (StringUtils.equals(apiScenarioWithBLOBs.getEnvironmentType(), EnvironmentType.JSON.name())) {
|
||||
environmentMap = JSON.parseObject(apiScenarioWithBLOBs.getEnvironmentJson(), Map.class);
|
||||
}
|
||||
Map<String, EnvironmentConfig> envConfig = new HashMap<>();
|
||||
if (environmentMap != null && !environmentMap.isEmpty()) {
|
||||
Map<String, String> finalEnvironmentMap = environmentMap;
|
||||
environmentMap.keySet().forEach(projectId -> {
|
||||
BaseEnvironmentService apiTestEnvironmentService = CommonBeanFactory.getBean(BaseEnvironmentService.class);
|
||||
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(finalEnvironmentMap.get(projectId));
|
||||
for (String projectId : environmentMap.keySet()) {
|
||||
ApiTestEnvironmentWithBLOBs environment = apiTestEnvironmentService.get(environmentMap.get(projectId));
|
||||
if (environment != null && environment.getConfig() != null) {
|
||||
EnvironmentConfig env = JSONUtil.parseObject(environment.getConfig(), EnvironmentConfig.class);
|
||||
env.setEnvironmentId(environment.getId());
|
||||
envConfig.put(projectId, env);
|
||||
}
|
||||
});
|
||||
}
|
||||
config.setConfig(envConfig);
|
||||
}
|
||||
}
|
||||
} else if (element != null && element.get(PropertyConstant.TYPE).toString().equals(ElementConstants.HTTP_SAMPLER)) {
|
||||
MsHTTPSamplerProxy httpSamplerProxy = JSON.parseObject(element.toString(), MsHTTPSamplerProxy.class);
|
||||
if (httpSamplerProxy != null && (!httpSamplerProxy.isCustomizeReq() || (httpSamplerProxy.isCustomizeReq() && BooleanUtils.isTrue(httpSamplerProxy.getIsRefEnvironment())))) {
|
||||
// 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取
|
||||
if (element != null && element.has(ElementConstants.HASH_TREE)) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
httpSamplerProxy.setHashTree(elements);
|
||||
httpSamplerProxy.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
|
||||
}
|
||||
HashTree tmpHashTree = new HashTree();
|
||||
httpSamplerProxy.toHashTree(tmpHashTree, null, msParameter);
|
||||
@ -645,23 +569,6 @@ public class ElementUtil {
|
||||
return resourceId + "_" + ElementUtil.getFullIndexPath(parent, indexPath);
|
||||
}
|
||||
|
||||
public static JSR223PreProcessor argumentsToProcessor(Arguments arguments) {
|
||||
JSR223PreProcessor processor = new JSR223PreProcessor();
|
||||
processor.setEnabled(true);
|
||||
processor.setName("User Defined Variables");
|
||||
processor.setProperty("scriptLanguage", "beanshell");
|
||||
processor.setProperty(TestElement.TEST_CLASS, JSR223PreProcessor.class.getName());
|
||||
processor.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI"));
|
||||
StringBuffer script = new StringBuffer();
|
||||
if (arguments != null) {
|
||||
for (int i = 0; i < arguments.getArguments().size(); ++i) {
|
||||
String argValue = arguments.getArgument(i).getValue();
|
||||
script.append("vars.put(\"" + arguments.getArgument(i).getName() + "\",\"" + argValue + "\");").append(StringUtils.LF);
|
||||
}
|
||||
processor.setProperty("script", script.toString());
|
||||
}
|
||||
return processor;
|
||||
}
|
||||
|
||||
public static UserParameters argumentsToUserParameters(Arguments arguments) {
|
||||
UserParameters processor = new UserParameters();
|
||||
@ -946,28 +853,6 @@ public class ElementUtil {
|
||||
return null;
|
||||
}
|
||||
|
||||
public static void replaceFileMetadataId(MsTestElement testElement, String newFileMetadataId, String oldFileMetadataId) {
|
||||
if (testElement != null && testElement instanceof MsHTTPSamplerProxy) {
|
||||
if (((MsHTTPSamplerProxy) testElement).getBody() != null && CollectionUtils.isNotEmpty(((MsHTTPSamplerProxy) testElement).getBody().getKvs())) {
|
||||
for (KeyValue keyValue : ((MsHTTPSamplerProxy) testElement).getBody().getKvs()) {
|
||||
if (CollectionUtils.isNotEmpty(keyValue.getFiles())) {
|
||||
for (BodyFile bodyFile : keyValue.getFiles()) {
|
||||
if (StringUtils.equals(bodyFile.getFileId(), oldFileMetadataId)) {
|
||||
bodyFile.setFileId(newFileMetadataId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (CollectionUtils.isNotEmpty(testElement.getHashTree())) {
|
||||
for (MsTestElement childElement : testElement.getHashTree()) {
|
||||
replaceFileMetadataId(childElement, newFileMetadataId, oldFileMetadataId);
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static List<MsAssertions> copyAssertion(List<EnvAssertions> envAssertions) {
|
||||
List<MsAssertions> assertions = new LinkedList<>();
|
||||
if (CollectionUtils.isNotEmpty(envAssertions)) {
|
||||
|
@ -1,8 +1,5 @@
|
||||
package io.metersphere.api.dto.definition.request;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.EnvironmentType;
|
||||
import io.metersphere.api.dto.definition.request.controller.MsCriticalSectionController;
|
||||
import io.metersphere.api.dto.definition.request.processors.MsJSR223Processor;
|
||||
@ -188,8 +185,6 @@ public class MsScenario extends MsTestElement {
|
||||
private boolean setRefScenario(List<MsTestElement> hashTree) {
|
||||
try {
|
||||
ApiScenarioMapper apiAutomationService = CommonBeanFactory.getBean(ApiScenarioMapper.class);
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
ApiScenarioWithBLOBs scenario = apiAutomationService.selectByPrimaryKey(this.getId());
|
||||
if (scenario != null && StringUtils.isNotEmpty(scenario.getScenarioDefinition())) {
|
||||
JSONObject elementOrg = JSONUtil.parseObject(scenario.getScenarioDefinition());
|
||||
@ -198,21 +193,14 @@ public class MsScenario extends MsTestElement {
|
||||
ElementUtil.dataFormatting(element.optJSONArray(ElementConstants.HASH_TREE));
|
||||
this.setName(scenario.getName());
|
||||
this.setProjectId(scenario.getProjectId());
|
||||
LinkedList<MsTestElement> sourceHashTree = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
LinkedList<MsTestElement> sourceHashTree = JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE));
|
||||
// 场景变量
|
||||
if (StringUtils.isNotEmpty(element.optString("variables")) && (this.variableEnable == null || this.variableEnable)) {
|
||||
LinkedList<ScenarioVariable> variables = mapper.readValue(element.optString("variables"),
|
||||
new TypeReference<LinkedList<ScenarioVariable>>() {
|
||||
});
|
||||
this.setVariables(variables);
|
||||
this.setVariables(JSONUtil.parseArray(element.optString("variables"), ScenarioVariable.class));
|
||||
}
|
||||
// 场景请求头
|
||||
if (StringUtils.isNotEmpty(element.optString("headers")) && (this.variableEnable == null || this.variableEnable)) {
|
||||
LinkedList<KeyValue> headers = mapper.readValue(element.optString("headers"),
|
||||
new TypeReference<LinkedList<KeyValue>>() {
|
||||
});
|
||||
this.setHeaders(headers);
|
||||
this.setHeaders(JSONUtil.parseArray(element.optString("headers"), KeyValue.class));
|
||||
}
|
||||
this.setHashTree(sourceHashTree);
|
||||
hashTree.clear();
|
||||
|
@ -19,7 +19,6 @@ import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.JSONUtil;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.constants.RunModeConstants;
|
||||
import io.metersphere.environment.service.BaseEnvironmentService;
|
||||
import io.metersphere.plugin.core.MsParameter;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
@ -37,7 +36,6 @@ import org.apache.jmeter.testelement.TestElement;
|
||||
import org.apache.jorphan.collections.HashTree;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -52,27 +50,15 @@ public class MsJDBCPostProcessor extends MsTestElement {
|
||||
// type 必须放最前面,以便能够转换正确的类
|
||||
private String type = ElementConstants.JDBC_POST;
|
||||
private String clazzName = MsJDBCPostProcessor.class.getCanonicalName();
|
||||
|
||||
|
||||
private DatabaseConfig dataSource;
|
||||
|
||||
private String query;
|
||||
|
||||
private long queryTimeout;
|
||||
|
||||
private String resultVariable;
|
||||
|
||||
private String variableNames;
|
||||
|
||||
private List<KeyValue> variables;
|
||||
|
||||
private String environmentId;
|
||||
|
||||
private String dataSourceId;
|
||||
|
||||
private String protocol = "SQL";
|
||||
|
||||
|
||||
private String useEnvironment;
|
||||
|
||||
@Override
|
||||
@ -93,26 +79,6 @@ public class MsJDBCPostProcessor extends MsTestElement {
|
||||
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
|
||||
}
|
||||
|
||||
// 数据兼容处理
|
||||
if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
|
||||
// 1.8 之后 当前正常数据
|
||||
} else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
|
||||
// 1.8 前后 混合数据
|
||||
this.setProjectId(getParentProjectId());
|
||||
} else {
|
||||
// 1.8 之前 数据
|
||||
if (config.getConfig() != null) {
|
||||
if (config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
|
||||
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
|
||||
} else {
|
||||
// 测试计划执行
|
||||
Iterator<String> it = config.getConfig().keySet().iterator();
|
||||
if (it.hasNext()) {
|
||||
this.setProjectId(it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//如果当前数据源为null,则获取已选环境的数据源
|
||||
if (this.dataSource == null) {
|
||||
// 自选了数据源
|
||||
@ -168,17 +134,6 @@ public class MsJDBCPostProcessor extends MsTestElement {
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getParentProjectId() {
|
||||
MsTestElement parent = this.getParent();
|
||||
while (parent != null) {
|
||||
if (StringUtils.isNotBlank(parent.getProjectId())) {
|
||||
return parent.getProjectId();
|
||||
}
|
||||
parent = parent.getParent();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private void setRefElement() {
|
||||
try {
|
||||
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
|
||||
@ -230,7 +185,6 @@ public class MsJDBCPostProcessor extends MsTestElement {
|
||||
envConfig.getDatabaseConfigs().forEach(item -> {
|
||||
if (item.getId().equals(this.dataSourceId)) {
|
||||
this.dataSource = item;
|
||||
return;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
@ -20,7 +20,6 @@ import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.JSONUtil;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.constants.RunModeConstants;
|
||||
import io.metersphere.environment.service.BaseEnvironmentService;
|
||||
import io.metersphere.plugin.core.MsParameter;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
@ -38,7 +37,6 @@ import org.apache.jmeter.testelement.TestElement;
|
||||
import org.apache.jorphan.collections.HashTree;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -81,26 +79,6 @@ public class MsJDBCPreProcessor extends MsTestElement {
|
||||
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
|
||||
}
|
||||
|
||||
// 数据兼容处理
|
||||
if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
|
||||
// 1.8 之后 当前正常数据
|
||||
} else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
|
||||
// 1.8 前后 混合数据
|
||||
this.setProjectId(getParentProjectId());
|
||||
} else {
|
||||
// 1.8 之前 数据
|
||||
if (config.getConfig() != null) {
|
||||
if (config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
|
||||
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
|
||||
} else {
|
||||
// 测试计划执行
|
||||
Iterator<String> it = config.getConfig().keySet().iterator();
|
||||
if (it.hasNext()) {
|
||||
this.setProjectId(it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
//如果当前数据源为null,则获取已选环境的数据源
|
||||
if (this.dataSource == null) {
|
||||
// 自选了数据源
|
||||
@ -156,17 +134,6 @@ public class MsJDBCPreProcessor extends MsTestElement {
|
||||
return false;
|
||||
}
|
||||
|
||||
private String getParentProjectId() {
|
||||
MsTestElement parent = this.getParent();
|
||||
while (parent != null) {
|
||||
if (StringUtils.isNotBlank(parent.getProjectId())) {
|
||||
return parent.getProjectId();
|
||||
}
|
||||
parent = parent.getParent();
|
||||
}
|
||||
return "";
|
||||
}
|
||||
|
||||
private void setRefElement() {
|
||||
try {
|
||||
ApiDefinitionService apiDefinitionService = CommonBeanFactory.getBean(ApiDefinitionService.class);
|
||||
|
@ -21,7 +21,6 @@ import io.metersphere.commons.utils.CommonBeanFactory;
|
||||
import io.metersphere.commons.utils.HashTreeUtil;
|
||||
import io.metersphere.commons.utils.JSONUtil;
|
||||
import io.metersphere.commons.utils.LogUtil;
|
||||
import io.metersphere.constants.RunModeConstants;
|
||||
import io.metersphere.environment.service.BaseEnvironmentService;
|
||||
import io.metersphere.plugin.core.MsParameter;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
@ -40,7 +39,6 @@ import org.apache.jmeter.testelement.TestElement;
|
||||
import org.apache.jorphan.collections.HashTree;
|
||||
import org.json.JSONObject;
|
||||
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
@ -87,29 +85,10 @@ public class MsJDBCSampler extends MsTestElement {
|
||||
this.setProjectId(config.getProjectId());
|
||||
config.setConfig(ElementUtil.getEnvironmentConfig(StringUtils.isNotEmpty(useEnvironment) ? useEnvironment : environmentId, this.getProjectId()));
|
||||
}
|
||||
// 数据兼容处理
|
||||
if (config.getConfig() != null && StringUtils.isNotEmpty(this.getProjectId()) && config.getConfig().containsKey(this.getProjectId())) {
|
||||
// 1.8 之后 当前正常数据
|
||||
} else if (config.getConfig() != null && config.getConfig().containsKey(getParentProjectId())) {
|
||||
// 1.8 前后 混合数据
|
||||
this.setProjectId(getParentProjectId());
|
||||
} else {
|
||||
// 1.8 之前 数据
|
||||
if (config.getConfig() != null) {
|
||||
if (config.getConfig().containsKey(RunModeConstants.HIS_PRO_ID.toString())) {
|
||||
this.setProjectId(RunModeConstants.HIS_PRO_ID.toString());
|
||||
} else {
|
||||
// 测试计划执行
|
||||
Iterator<String> it = config.getConfig().keySet().iterator();
|
||||
if (it.hasNext()) {
|
||||
this.setProjectId(it.next());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
EnvironmentConfig envConfig = null;
|
||||
// 自定义请求非引用环境取自身环境
|
||||
if (StringUtils.equalsIgnoreCase(this.getReferenced(), "Created") && (isRefEnvironment == null || !isRefEnvironment)) {
|
||||
if (StringUtils.equalsIgnoreCase(this.getReferenced(), ElementConstants.STEP_CREATED) && (isRefEnvironment == null || !isRefEnvironment)) {
|
||||
this.dataSource = null;
|
||||
envConfig = this.initDataSource();
|
||||
} else {
|
||||
|
@ -1,8 +1,6 @@
|
||||
package io.metersphere.api.exec.api;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.definition.request.ElementUtil;
|
||||
import io.metersphere.api.dto.definition.request.MsTestPlan;
|
||||
import io.metersphere.api.dto.definition.request.MsThreadGroup;
|
||||
@ -58,8 +56,6 @@ public class ApiCaseSerialService {
|
||||
@Resource
|
||||
private ApiTestCaseMapper apiTestCaseMapper;
|
||||
@Resource
|
||||
private ObjectMapper mapper;
|
||||
@Resource
|
||||
private RedisTemplate<String, Object> redisTemplate;
|
||||
@Resource
|
||||
private TestPlanApiCaseMapper testPlanApiCaseMapper;
|
||||
@ -194,14 +190,10 @@ public class ApiCaseSerialService {
|
||||
JSONObject element = JSONUtil.parseObject(api);
|
||||
LinkedList<MsTestElement> list = new LinkedList<>();
|
||||
if (element != null && StringUtils.isNotEmpty(element.optString(ElementConstants.HASH_TREE))) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE),
|
||||
new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
list.addAll(elements);
|
||||
list.addAll(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
|
||||
}
|
||||
if (element.optString(PropertyConstant.TYPE).equals(ElementConstants.HTTP_SAMPLER)) {
|
||||
MsHTTPSamplerProxy httpSamplerProxy = mapper.readValue(element.toString(), new TypeReference<MsHTTPSamplerProxy>() {
|
||||
});
|
||||
MsHTTPSamplerProxy httpSamplerProxy = JSONUtil.parseObject(element.toString(), MsHTTPSamplerProxy.class);
|
||||
httpSamplerProxy.setHashTree(list);
|
||||
httpSamplerProxy.setName(planId);
|
||||
if (StringUtils.isNotEmpty(envId)) {
|
||||
|
@ -101,11 +101,13 @@ public class ApiScenarioEnvService {
|
||||
http.setUrl(StringUtils.equals(testElement.getRefType(), CommonConstants.CASE) ? null : http.getUrl());
|
||||
|
||||
// 非全路径校验
|
||||
if (!StringUtils.equalsIgnoreCase(http.getReferenced(), "Created") || (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) {
|
||||
if (!StringUtils.equalsIgnoreCase(http.getReferenced(), ElementConstants.STEP_CREATED)
|
||||
|| (http.getIsRefEnvironment() != null && http.getIsRefEnvironment())) {
|
||||
env.getProjectIds().add(http.getProjectId());
|
||||
env.setFullUrl(false);
|
||||
}
|
||||
} else if (StringUtils.equals(testElement.getType(), ElementConstants.JDBC_SAMPLER) || StringUtils.equals(testElement.getType(), ElementConstants.TCP_SAMPLER)) {
|
||||
} else if (StringUtils.equals(testElement.getType(), ElementConstants.JDBC_SAMPLER)
|
||||
|| StringUtils.equals(testElement.getType(), ElementConstants.TCP_SAMPLER)) {
|
||||
if (StringUtils.isEmpty(testElement.getProjectId())) {
|
||||
if (StringUtils.equals(testElement.getRefType(), CommonConstants.CASE)) {
|
||||
ApiTestCase testCase = apiTestCaseMapper.selectByPrimaryKey(testElement.getId());
|
||||
@ -141,17 +143,19 @@ public class ApiScenarioEnvService {
|
||||
if (httpSamplerProxy.isCustomizeReq()) {
|
||||
env.getProjectIds().add(httpSamplerProxy.getProjectId());
|
||||
env.setFullUrl(httpSamplerProxy.getIsRefEnvironment() == null ? true : !httpSamplerProxy.getIsRefEnvironment());
|
||||
} else if (!StringUtils.equalsIgnoreCase(httpSamplerProxy.getReferenced(), "Created") || (httpSamplerProxy.getIsRefEnvironment() != null && httpSamplerProxy.getIsRefEnvironment())) {
|
||||
} else if (!StringUtils.equalsIgnoreCase(httpSamplerProxy.getReferenced(), ElementConstants.STEP_CREATED) || (httpSamplerProxy.getIsRefEnvironment() != null && httpSamplerProxy.getIsRefEnvironment())) {
|
||||
env.getProjectIds().add(httpSamplerProxy.getProjectId());
|
||||
env.setFullUrl(false);
|
||||
}
|
||||
|
||||
} else if (StringUtils.equals(testElement.getType(), ElementConstants.JDBC_SAMPLER) || StringUtils.equals(testElement.getType(), ElementConstants.TCP_SAMPLER)) {
|
||||
} else if (StringUtils.equals(testElement.getType(), ElementConstants.JDBC_SAMPLER)
|
||||
|| StringUtils.equals(testElement.getType(), ElementConstants.TCP_SAMPLER)) {
|
||||
env.getProjectIds().add(testElement.getProjectId());
|
||||
env.setFullUrl(false);
|
||||
}
|
||||
}
|
||||
if (StringUtils.equals(testElement.getType(), ElementConstants.SCENARIO) && !((MsScenario) testElement).isEnvironmentEnable()) {
|
||||
if (StringUtils.equals(testElement.getType(), ElementConstants.SCENARIO)
|
||||
&& !((MsScenario) testElement).isEnvironmentEnable()) {
|
||||
env.getProjectIds().add(testElement.getProjectId());
|
||||
}
|
||||
}
|
||||
|
@ -30,7 +30,7 @@ public class ElementConstants {
|
||||
public static final String CONSTANT_TIMER = "ConstantTimer";
|
||||
public static final String ASSERTIONS = "Assertions";
|
||||
public static final String EXTRACT = "Extract";
|
||||
|
||||
public static final String STEP_CREATED = "Created";
|
||||
public final static List<String> REQUESTS = new ArrayList<String>() {{
|
||||
this.add(ElementConstants.HTTP_SAMPLER);
|
||||
this.add(ElementConstants.DUBBO_SAMPLER);
|
||||
|
@ -1,10 +1,6 @@
|
||||
package io.metersphere.commons.utils;
|
||||
|
||||
|
||||
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import io.metersphere.api.dto.EnvironmentType;
|
||||
import io.metersphere.api.dto.definition.request.*;
|
||||
import io.metersphere.api.dto.definition.request.variable.ScenarioVariable;
|
||||
@ -52,21 +48,15 @@ public class GenerateHashTreeUtil {
|
||||
}
|
||||
|
||||
public static void parse(String scenarioDefinition, MsScenario scenario) {
|
||||
ObjectMapper mapper = new ObjectMapper();
|
||||
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
try {
|
||||
JSONObject element = JSONUtil.parseObject(scenarioDefinition);
|
||||
ElementUtil.dataFormatting(element);
|
||||
// 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取
|
||||
if (element != null && element.has(ElementConstants.HASH_TREE)) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
scenario.setHashTree(elements);
|
||||
scenario.setHashTree(JSONUtil.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString()));
|
||||
}
|
||||
if (element != null && StringUtils.isNotEmpty(element.optString("variables"))) {
|
||||
LinkedList<ScenarioVariable> variables = mapper.readValue(element.optString("variables"), new TypeReference<LinkedList<ScenarioVariable>>() {
|
||||
});
|
||||
scenario.setVariables(variables);
|
||||
scenario.setVariables(JSONUtil.parseArray(element.optString("variables"), ScenarioVariable.class));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
@ -74,17 +64,10 @@ public class GenerateHashTreeUtil {
|
||||
}
|
||||
|
||||
public static LinkedList<MsTestElement> getScenarioHashTree(String definition) {
|
||||
ObjectMapper objectMapper = new ObjectMapper();
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
JSONObject element = JSONUtil.parseObject(definition);
|
||||
try {
|
||||
if (element != null && element.has(ElementConstants.HASH_TREE)) {
|
||||
ElementUtil.dataFormatting(element);
|
||||
return objectMapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
}
|
||||
} catch (JsonProcessingException e) {
|
||||
LogUtil.error(e.getMessage(), e);
|
||||
if (element != null && element.has(ElementConstants.HASH_TREE)) {
|
||||
ElementUtil.dataFormatting(element);
|
||||
return JSONUtil.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString());
|
||||
}
|
||||
return new LinkedList<>();
|
||||
}
|
||||
|
@ -2,16 +2,20 @@ package io.metersphere.commons.utils;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonAutoDetect;
|
||||
import com.fasterxml.jackson.annotation.PropertyAccessor;
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.DeserializationFeature;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.fasterxml.jackson.databind.SerializationFeature;
|
||||
import com.fasterxml.jackson.databind.node.ArrayNode;
|
||||
import com.fasterxml.jackson.databind.node.ObjectNode;
|
||||
import com.fasterxml.jackson.databind.type.CollectionType;
|
||||
import com.fasterxml.jackson.databind.type.TypeFactory;
|
||||
import com.google.gson.Gson;
|
||||
import com.google.gson.GsonBuilder;
|
||||
import com.google.gson.JsonParser;
|
||||
import io.metersphere.commons.constants.PropertyConstant;
|
||||
import io.metersphere.commons.exception.MSException;
|
||||
import io.metersphere.plugin.core.MsTestElement;
|
||||
import org.apache.commons.lang3.ObjectUtils;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.json.JSONArray;
|
||||
@ -26,6 +30,7 @@ import java.util.*;
|
||||
|
||||
public class JSONUtil {
|
||||
private static final ObjectMapper objectMapper = new ObjectMapper();
|
||||
private static final TypeFactory typeFactory = objectMapper.getTypeFactory();
|
||||
|
||||
static {
|
||||
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
|
||||
@ -44,6 +49,15 @@ public class JSONUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> List<T> parseArray(String content, Class<T> valueType) {
|
||||
CollectionType javaType = typeFactory.constructCollectionType(LinkedList.class, valueType);
|
||||
try {
|
||||
return objectMapper.readValue(content, javaType);
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
public static <T> T convertValue(Object content, Class<T> valueType) {
|
||||
try {
|
||||
return objectMapper.convertValue(content, valueType);
|
||||
@ -236,6 +250,15 @@ public class JSONUtil {
|
||||
}
|
||||
}
|
||||
|
||||
public static LinkedList<MsTestElement> readValue(String content) {
|
||||
try {
|
||||
return objectMapper.readValue(content, new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
} catch (Exception e) {
|
||||
return new LinkedList<>();
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjectNode createObj() {
|
||||
return objectMapper.createObjectNode();
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ package io.metersphere.sechedule;
|
||||
import io.metersphere.api.dto.ApiTestImportRequest;
|
||||
import io.metersphere.api.dto.definition.request.auth.MsAuthManager;
|
||||
import io.metersphere.api.dto.scenario.KeyValue;
|
||||
import io.metersphere.commons.utils.JSONUtil;
|
||||
import io.metersphere.service.definition.ApiDefinitionService;
|
||||
import io.metersphere.base.domain.SwaggerUrlProject;
|
||||
import io.metersphere.commons.constants.ScheduleGroup;
|
||||
@ -59,15 +60,15 @@ public class SwaggerUrlImportJob extends MsScheduleJob {
|
||||
// 获取鉴权设置
|
||||
if (StringUtils.isNotBlank(config)) {
|
||||
JSONObject configObj = JSON.parseObject(config, JSONObject.class);
|
||||
List<KeyValue> headers = JSON.parseArray(configObj.optString("headers"), KeyValue.class);
|
||||
List<KeyValue> headers = JSONUtil.parseArray(configObj.optString("headers"), KeyValue.class);
|
||||
if (CollectionUtils.isNotEmpty(headers)) {
|
||||
request.setHeaders(headers);
|
||||
}
|
||||
List<KeyValue> arguments = JSON.parseArray(configObj.optString("arguments"), KeyValue.class);
|
||||
List<KeyValue> arguments = JSONUtil.parseArray(configObj.optString("arguments"), KeyValue.class);
|
||||
if (CollectionUtils.isNotEmpty(arguments)) {
|
||||
request.setArguments(arguments);
|
||||
}
|
||||
MsAuthManager msAuthManager = JSON.parseObject(configObj.optString("authManager"), MsAuthManager.class);
|
||||
MsAuthManager msAuthManager = JSONUtil.parseObject(configObj.optString("authManager"), MsAuthManager.class);
|
||||
if (msAuthManager != null) {
|
||||
request.setAuthManager(msAuthManager);
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package io.metersphere.service.definition;
|
||||
|
||||
import com.fasterxml.jackson.core.type.TypeReference;
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.github.pagehelper.PageHelper;
|
||||
import io.metersphere.api.dto.*;
|
||||
import io.metersphere.api.dto.automation.ApiScenarioDTO;
|
||||
@ -94,8 +92,6 @@ public class ApiTestCaseService {
|
||||
@Resource
|
||||
private TcpApiParamService tcpApiParamService;
|
||||
@Resource
|
||||
private ObjectMapper mapper;
|
||||
@Resource
|
||||
private ApiCaseExecutionInfoService apiCaseExecutionInfoService;
|
||||
@Resource
|
||||
private ExtApiDefinitionMapper extApiDefinitionMapper;
|
||||
@ -760,23 +756,16 @@ public class ApiTestCaseService {
|
||||
JSONObject element = JSONUtil.parseObject(apiTestCase.getRequest());
|
||||
ElementUtil.dataFormatting(element);
|
||||
MsHTTPSamplerProxy req = JSON.parseObject(element.toString(), MsHTTPSamplerProxy.class);
|
||||
try {
|
||||
if (element != null && StringUtils.isNotEmpty(element.optString(ElementConstants.HASH_TREE))) {
|
||||
LinkedList<MsTestElement> elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference<LinkedList<MsTestElement>>() {
|
||||
});
|
||||
req.setHashTree(elements);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(method)) {
|
||||
req.setMethod(method);
|
||||
}
|
||||
if (StringUtils.isNotEmpty(path)) {
|
||||
req.setPath(path);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LogUtil.error(e);
|
||||
if (element != null && StringUtils.isNotEmpty(element.optString(ElementConstants.HASH_TREE))) {
|
||||
req.setHashTree(JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)));
|
||||
}
|
||||
String requestStr = JSON.toJSONString(req);
|
||||
apiTestCase.setRequest(requestStr);
|
||||
if (StringUtils.isNotBlank(method)) {
|
||||
req.setMethod(method);
|
||||
}
|
||||
if (StringUtils.isNotBlank(path)) {
|
||||
req.setPath(path);
|
||||
}
|
||||
apiTestCase.setRequest(JSON.toJSONString(req));
|
||||
// sync case
|
||||
ApiCaseBatchSyncService apiCaseBatchSyncService = CommonBeanFactory.getBean(ApiCaseBatchSyncService.class);
|
||||
if (apiCaseBatchSyncService != null) {
|
||||
|
Loading…
Reference in New Issue
Block a user