diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index b0ae06627b..4f321bdca7 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -189,7 +189,7 @@ public class MsScenario extends MsTestElement { } }); } - if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null + if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) { config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=") diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java index bac961dde6..261b52098b 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsTestElement.java @@ -47,8 +47,10 @@ import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; import java.io.ByteArrayOutputStream; +import java.util.HashMap; import java.util.LinkedList; import java.util.List; +import java.util.Map; import java.util.stream.Collectors; @JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type") @@ -166,7 +168,7 @@ public abstract class MsTestElement { } public Arguments addArguments(ParameterConfig config) { - if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null + if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) { Arguments arguments = new Arguments(); arguments.setEnabled(true); @@ -181,11 +183,14 @@ public abstract class MsTestElement { return null; } - protected EnvironmentConfig getEnvironmentConfig(String environmentId) { + protected Map getEnvironmentConfig(String environmentId) { ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId); if (environment != null && environment.getConfig() != null) { - return JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + // 单独接口执行 + Map map = new HashMap<>(); + map.put(this.getProjectId(), JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class)); + return map; } return null; } @@ -269,7 +274,7 @@ public abstract class MsTestElement { getFullPath(element.getParent(), path); } - protected String getParentName(MsTestElement parent, ParameterConfig config) { + protected String getParentName(MsTestElement parent) { if (parent != null) { if (MsTestElementConstants.LoopController.name().equals(parent.getType())) { MsLoopController loopController = (MsLoopController) parent; diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java index 6de3bd6d23..e5117db258 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java @@ -30,4 +30,11 @@ public class ParameterConfig { * 项目ID,支持单接口执行 */ private String projectId; -} + + public boolean isEffective(String projectId) { + if (this.config != null && this.config.get(projectId) != null) { + return true; + } + return false; + } +} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java index fcded90f88..cae25e201f 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/auth/MsAuthManager.java @@ -1,19 +1,15 @@ package io.metersphere.api.dto.definition.request.auth; -import com.alibaba.fastjson.JSONObject; import com.alibaba.fastjson.annotation.JSONField; import com.alibaba.fastjson.annotation.JSONType; import io.metersphere.api.dto.definition.request.MsTestElement; import io.metersphere.api.dto.definition.request.ParameterConfig; -import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; -import io.metersphere.api.service.ApiTestEnvironmentService; -import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; -import io.metersphere.commons.utils.CommonBeanFactory; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.protocol.http.control.AuthManager; import org.apache.jmeter.protocol.http.control.Authorization; +import org.apache.jmeter.protocol.http.sampler.HTTPSamplerProxy; import org.apache.jmeter.save.SaveService; import org.apache.jmeter.testelement.TestElement; import org.apache.jorphan.collections.HashTree; @@ -66,18 +62,37 @@ public class MsAuthManager extends MsTestElement { if (this.url != null) { auth.setURL(this.url); } else { - if (environment != null) { - ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); - ApiTestEnvironmentWithBLOBs environmentWithBLOBs = environmentService.get(environment); - EnvironmentConfig envConfig = JSONObject.parseObject(environmentWithBLOBs.getConfig(), EnvironmentConfig.class); - this.url = envConfig.getHttpConfig().getProtocol() + "://" + envConfig.getHttpConfig().getSocket(); + if (config != null && config.isEffective(this.getProjectId())) { + if (config.isEffective(this.getProjectId())) { + String url = config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol() + "://" + config.getConfig().get(this.getProjectId()).getHttpConfig().getSocket(); + auth.setURL(url); + } } } - auth.setDomain(this.domain); auth.setUser(this.username); auth.setPass(this.password); auth.setMechanism(AuthManager.Mechanism.DIGEST); authManager.addAuth(auth); tree.add(authManager); } + + public void setAuth(HashTree tree, MsAuthManager msAuthManager, HTTPSamplerProxy samplerProxy) { + try { + AuthManager authManager = new AuthManager(); + authManager.setEnabled(true); + authManager.setName(StringUtils.isNotEmpty(this.getName()) ? this.getName() : "AuthManager"); + authManager.setProperty(TestElement.TEST_CLASS, AuthManager.class.getName()); + authManager.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("AuthPanel")); + Authorization auth = new Authorization(); + auth.setURL(samplerProxy.getUrl().toString()); + auth.setDomain(samplerProxy.getDomain()); + auth.setUser(msAuthManager.getUsername()); + auth.setPass(msAuthManager.getPassword()); + auth.setMechanism(AuthManager.Mechanism.DIGEST); + authManager.addAuth(auth); + tree.add(authManager); + } catch (Exception e) { + e.printStackTrace(); + } + } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java index 440c8572bd..718df9b625 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/processors/MsJSR223Processor.java @@ -36,7 +36,7 @@ public class MsJSR223Processor extends MsTestElement { } else { processor.setName("JSR223Processor"); } - String name = this.getParentName(this.getParent(), config); + String name = this.getParentName(this.getParent()); if (StringUtils.isNotEmpty(name) && !config.isOperating()) { processor.setName(this.getName() + "<->" + name); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java index 177698749e..75800b50ad 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsDubboSampler.java @@ -76,7 +76,7 @@ public class MsDubboSampler extends MsTestElement { DubboSample sampler = new DubboSample(); sampler.setEnabled(this.isEnable()); sampler.setName(this.getName()); - String name = this.getParentName(this.getParent(), config); + String name = this.getParentName(this.getParent()); if (StringUtils.isNotEmpty(name) && !config.isOperating()) { sampler.setName(this.getName() + "<->" + name); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 726df485fe..cf61040421 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -8,7 +8,6 @@ import io.metersphere.api.dto.definition.request.auth.MsAuthManager; import io.metersphere.api.dto.definition.request.dns.MsDNSCacheManager; import io.metersphere.api.dto.scenario.Body; import io.metersphere.api.dto.scenario.KeyValue; -import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.utils.LogUtil; import io.metersphere.commons.utils.ScriptEngineUtils; @@ -101,7 +100,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { HTTPSamplerProxy sampler = new HTTPSamplerProxy(); sampler.setEnabled(this.isEnable()); sampler.setName(this.getName()); - String name = this.getParentName(this.getParent(), config); + String name = this.getParentName(this.getParent()); if (StringUtils.isNotEmpty(name) && !config.isOperating()) { sampler.setName(this.getName() + "<->" + name); } @@ -114,14 +113,10 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setFollowRedirects(this.isFollowRedirects()); sampler.setUseKeepAlive(true); sampler.setDoMultipart(this.isDoMultipartPost()); - if (config != null && config.getConfig() != null) { - config.setConfig(config.getConfig()); - } else { + if (config.getConfig() == null) { // 单独接口执行 this.setProjectId(config.getProjectId()); - Map map = new HashMap<>(); - map.put(this.getProjectId(), getEnvironmentConfig(useEnvironment)); - config.setConfig(map); + config.setConfig(getEnvironmentConfig(useEnvironment)); } // 添加环境中的公共变量 @@ -130,7 +125,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { tree.add(arguments); } try { - if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null) { + if (config.isEffective(this.getProjectId())) { String url = config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol() + "://" + config.getConfig().get(this.getProjectId()).getHttpConfig().getSocket(); // 补充如果是完整URL 则用自身URL boolean isUrl = false; @@ -219,13 +214,13 @@ public class MsHTTPSamplerProxy extends MsTestElement { } // 通用请求Headers - if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getHttpConfig() != null + if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getHttpConfig() != null && CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders())) { setHeader(httpSamplerTree, config.getConfig().get(this.getProjectId()).getHttpConfig().getHeaders()); } //判断是否要开启DNS - if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()) != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null + if (config.isEffective(this.getProjectId()) && config.getConfig().get(this.getProjectId()).getCommonConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig().isEnableHost()) { MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig().get(this.getProjectId())); @@ -236,7 +231,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { } } if (this.authManager != null) { - this.authManager.toHashTree(tree, hashTree, config); + this.authManager.setAuth(tree, this.authManager, sampler); } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java index b3fa3b2f9d..baf4ea93a4 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsJDBCSampler.java @@ -113,7 +113,7 @@ public class MsJDBCSampler extends MsTestElement { JDBCSampler sampler = new JDBCSampler(); sampler.setEnabled(this.isEnable()); sampler.setName(this.getName()); - String name = this.getParentName(this.getParent(), config); + String name = this.getParentName(this.getParent()); if (StringUtils.isNotEmpty(name) && !config.isOperating()) { sampler.setName(this.getName() + "<->" + name); } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java index 20583aca7b..bc9d4be3bd 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsTCPSampler.java @@ -23,9 +23,7 @@ import org.apache.jmeter.testelement.property.StringProperty; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; -import java.util.ArrayList; -import java.util.List; -import java.util.Random; +import java.util.*; @Data @EqualsAndHashCode(callSuper = true) @@ -59,7 +57,7 @@ public class MsTCPSampler extends MsTestElement { private String password = ""; @JSONField(ordinal = 33) private String request; -// @JSONField(ordinal = 34) + // @JSONField(ordinal = 34) // private Object requestResult; @JSONField(ordinal = 35) private List parameters; @@ -77,7 +75,11 @@ public class MsTCPSampler extends MsTestElement { if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) { this.getRefElement(this); } -// config.setConfig(getEnvironmentConfig(useEnvironment)); + if (config.getConfig() == null) { + // 单独接口执行 + this.setProjectId(config.getProjectId()); + config.setConfig(getEnvironmentConfig(useEnvironment)); + } parseEnvironment(config.getConfig().get(this.projectId)); // 添加环境中的公共变量 @@ -111,7 +113,7 @@ public class MsTCPSampler extends MsTestElement { TCPSampler tcpSampler = new TCPSampler(); tcpSampler.setEnabled(this.isEnable()); tcpSampler.setName(this.getName()); - String name = this.getParentName(this.getParent(), config); + String name = this.getParentName(this.getParent()); if (StringUtils.isNotEmpty(name) && !config.isOperating()) { tcpSampler.setName(this.getName() + "<->" + name); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index cf39bcd701..cc58b14e73 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -13,6 +13,7 @@ import io.metersphere.api.dto.definition.parse.ApiDefinitionImportParserFactory; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest; @@ -85,6 +86,8 @@ public class ApiDefinitionService { private ScheduleMapper scheduleMapper; @Resource private ApiTestCaseMapper apiTestCaseMapper; + @Resource + private ApiTestEnvironmentService environmentService; private static Cache cache = Cache.newHardMemoryCache(0, 3600 * 24); @@ -427,8 +430,19 @@ public class ApiDefinitionService { public String run(RunDefinitionRequest request, List bodyFiles) { List bodyUploadIds = new ArrayList<>(request.getBodyUploadIds()); FileUtils.createBodyFiles(bodyUploadIds, bodyFiles); + ParameterConfig config = new ParameterConfig(); config.setProjectId(request.getProjectId()); + + Map envConfig = new HashMap<>(); + Map map = request.getEnvironmentMap(); + if (map != null && map.size() > 0) { + ApiTestEnvironmentWithBLOBs environment = environmentService.get(map.get(request.getProjectId())); + EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class); + envConfig.put(request.getProjectId(), env); + config.setConfig(envConfig); + } + HashTree hashTree = request.getTestElement().generateHashTree(config); String runMode = ApiRunMode.DEFINITION.name(); if (StringUtils.isNotBlank(request.getType()) && StringUtils.equals(request.getType(), ApiRunMode.API_PLAN.name())) { diff --git a/frontend/src/business/components/api/automation/scenario/ApiScenarioEnv.vue b/frontend/src/business/components/api/automation/scenario/ApiScenarioEnv.vue index 5ec2dccd2d..5547777048 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiScenarioEnv.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiScenarioEnv.vue @@ -6,8 +6,8 @@ :destroy-on-close="true" :before-close="handleClose"> -
-
+
+
{{ getProjectName(pe.id) }} @@ -29,51 +29,55 @@
- 取 消 - 确 定 - + 取 消 + 确 定 + + + + diff --git a/frontend/src/business/components/api/automation/scenario/ApiScenarioModule.vue b/frontend/src/business/components/api/automation/scenario/ApiScenarioModule.vue index 3e53c9b4a5..de524efdf7 100644 --- a/frontend/src/business/components/api/automation/scenario/ApiScenarioModule.vue +++ b/frontend/src/business/components/api/automation/scenario/ApiScenarioModule.vue @@ -150,14 +150,14 @@ break; } }, - list() { + list(projectId) { let url = undefined; if (this.isPlanModel) { url = '/api/automation/module/list/plan/' + this.planId; } else if (this.isRelevanceModel) { url = "/api/automation/module/list/" + this.relevanceProjectId; } else { - url = "/api/automation/module/list/" + this.projectId; + url = "/api/automation/module/list/" + (projectId ? projectId : this.projectId); if (!this.projectId) { return; } diff --git a/frontend/src/business/components/api/automation/scenario/DebugRun.vue b/frontend/src/business/components/api/automation/scenario/DebugRun.vue index ec0d33c99e..c9519440db 100644 --- a/frontend/src/business/components/api/automation/scenario/DebugRun.vue +++ b/frontend/src/business/components/api/automation/scenario/DebugRun.vue @@ -2,7 +2,7 @@
diff --git a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue index 25caf7618a..4f4ef858e5 100644 --- a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue +++ b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue @@ -113,23 +113,23 @@ 环境配置 - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + @@ -149,7 +149,7 @@ @@ -186,7 +186,8 @@ - + @@ -208,47 +209,47 @@