From b34d373e78b93d8fc2cc549be39a822b5f9b0397 Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Wed, 30 Nov 2022 10:45:27 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E6=8E=A5=E5=8F=A3=E6=B5=8B=E8=AF=95):?= =?UTF-8?q?=20=E5=9C=BA=E6=99=AF=E6=AD=A5=E9=AA=A4=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E4=BC=98=E5=8C=96=E7=BB=9F=E4=B8=80=E8=A7=A3=E6=9E=90=E6=96=B9?= =?UTF-8?q?=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../dto/definition/request/ElementUtil.java | 149 ++---------------- .../dto/definition/request/MsScenario.java | 18 +-- .../processors/post/MsJDBCPostProcessor.java | 46 ------ .../processors/pre/MsJDBCPreProcessor.java | 33 ---- .../request/sampler/MsJDBCSampler.java | 25 +-- .../api/exec/api/ApiCaseSerialService.java | 12 +- .../exec/scenario/ApiScenarioEnvService.java | 14 +- .../commons/constants/ElementConstants.java | 2 +- .../commons/utils/GenerateHashTreeUtil.java | 27 +--- .../metersphere/commons/utils/JSONUtil.java | 23 +++ .../sechedule/SwaggerUrlImportJob.java | 7 +- .../definition/ApiTestCaseService.java | 29 ++-- 12 files changed, 75 insertions(+), 310 deletions(-) diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index 5729b8b03e..081cb50844 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -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 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 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 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 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 envConfig = new HashMap<>(); if (environmentMap != null && !environmentMap.isEmpty()) { - Map 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 elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference>() { - }); - 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 copyAssertion(List envAssertions) { List assertions = new LinkedList<>(); if (CollectionUtils.isNotEmpty(envAssertions)) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index fc05e87950..2937ff1260 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -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 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 sourceHashTree = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference>() { - }); + LinkedList sourceHashTree = JSONUtil.readValue(element.optString(ElementConstants.HASH_TREE)); // 场景变量 if (StringUtils.isNotEmpty(element.optString("variables")) && (this.variableEnable == null || this.variableEnable)) { - LinkedList variables = mapper.readValue(element.optString("variables"), - new TypeReference>() { - }); - this.setVariables(variables); + this.setVariables(JSONUtil.parseArray(element.optString("variables"), ScenarioVariable.class)); } // 场景请求头 if (StringUtils.isNotEmpty(element.optString("headers")) && (this.variableEnable == null || this.variableEnable)) { - LinkedList headers = mapper.readValue(element.optString("headers"), - new TypeReference>() { - }); - this.setHeaders(headers); + this.setHeaders(JSONUtil.parseArray(element.optString("headers"), KeyValue.class)); } this.setHashTree(sourceHashTree); hashTree.clear(); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java index 2dfae81f69..c127c1b510 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/post/MsJDBCPostProcessor.java @@ -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 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 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; } }); } diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java index 0960d5d281..78c379e862 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/pre/MsJDBCPreProcessor.java @@ -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 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); diff --git a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index e156b4c11e..34922c610c 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/api-test/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -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 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 { diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java index 04ef901732..2e60fc0f9b 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/api/ApiCaseSerialService.java @@ -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 redisTemplate; @Resource private TestPlanApiCaseMapper testPlanApiCaseMapper; @@ -194,14 +190,10 @@ public class ApiCaseSerialService { JSONObject element = JSONUtil.parseObject(api); LinkedList list = new LinkedList<>(); if (element != null && StringUtils.isNotEmpty(element.optString(ElementConstants.HASH_TREE))) { - LinkedList elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE), - new TypeReference>() { - }); - 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 httpSamplerProxy = JSONUtil.parseObject(element.toString(), MsHTTPSamplerProxy.class); httpSamplerProxy.setHashTree(list); httpSamplerProxy.setName(planId); if (StringUtils.isNotEmpty(envId)) { diff --git a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java index f6c6ecc742..8b99d93d36 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java +++ b/api-test/backend/src/main/java/io/metersphere/api/exec/scenario/ApiScenarioEnvService.java @@ -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()); } } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java index 38116e44f6..8d3de1490d 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/constants/ElementConstants.java @@ -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 REQUESTS = new ArrayList() {{ this.add(ElementConstants.HTTP_SAMPLER); this.add(ElementConstants.DUBBO_SAMPLER); diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java index b523af0be8..d318641ec8 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/GenerateHashTreeUtil.java @@ -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 elements = mapper.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString(), new TypeReference>() { - }); - scenario.setHashTree(elements); + scenario.setHashTree(JSONUtil.readValue(element.optJSONArray(ElementConstants.HASH_TREE).toString())); } if (element != null && StringUtils.isNotEmpty(element.optString("variables"))) { - LinkedList variables = mapper.readValue(element.optString("variables"), new TypeReference>() { - }); - 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 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>() { - }); - } - } 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<>(); } diff --git a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java index 5265a8146c..b153e7d5f4 100644 --- a/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java +++ b/api-test/backend/src/main/java/io/metersphere/commons/utils/JSONUtil.java @@ -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 List parseArray(String content, Class valueType) { + CollectionType javaType = typeFactory.constructCollectionType(LinkedList.class, valueType); + try { + return objectMapper.readValue(content, javaType); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + public static T convertValue(Object content, Class valueType) { try { return objectMapper.convertValue(content, valueType); @@ -236,6 +250,15 @@ public class JSONUtil { } } + public static LinkedList readValue(String content) { + try { + return objectMapper.readValue(content, new TypeReference>() { + }); + } catch (Exception e) { + return new LinkedList<>(); + } + } + public static ObjectNode createObj() { return objectMapper.createObjectNode(); } diff --git a/api-test/backend/src/main/java/io/metersphere/sechedule/SwaggerUrlImportJob.java b/api-test/backend/src/main/java/io/metersphere/sechedule/SwaggerUrlImportJob.java index 743d1a95fc..abaae59881 100644 --- a/api-test/backend/src/main/java/io/metersphere/sechedule/SwaggerUrlImportJob.java +++ b/api-test/backend/src/main/java/io/metersphere/sechedule/SwaggerUrlImportJob.java @@ -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 headers = JSON.parseArray(configObj.optString("headers"), KeyValue.class); + List headers = JSONUtil.parseArray(configObj.optString("headers"), KeyValue.class); if (CollectionUtils.isNotEmpty(headers)) { request.setHeaders(headers); } - List arguments = JSON.parseArray(configObj.optString("arguments"), KeyValue.class); + List 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); } diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java index 71e978f79e..d346495c0b 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiTestCaseService.java @@ -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 elements = mapper.readValue(element.optString(ElementConstants.HASH_TREE), new TypeReference>() { - }); - 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) {