feat(接口自动化): 跨项目添加场景

This commit is contained in:
shiziyuan9527 2021-02-24 16:29:22 +08:00
parent 0a997065dd
commit ef08532187
12 changed files with 84 additions and 49 deletions

View File

@ -26,8 +26,10 @@ import org.apache.jmeter.save.SaveService;
import org.apache.jmeter.testelement.TestElement;
import org.apache.jorphan.collections.HashTree;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
@Data
@ -52,6 +54,9 @@ public class MsScenario extends MsTestElement {
@JSONField(ordinal = 26)
private List<KeyValue> headers;
@JSONField(ordinal = 27)
private Map<String, String> environmentMap;
private static final String BODY_FILE_DIR = "/opt/metersphere/data/body";
public MsScenario() {
@ -88,12 +93,17 @@ public class MsScenario extends MsTestElement {
config.setStep(this.getName());
config.setStepType("SCENARIO");
config.setEnableCookieShare(enableCookieShare);
if (StringUtils.isNotEmpty(environmentId)) {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId);
if (environment != null && environment.getConfig() != null) {
config.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
}
Map<String,EnvironmentConfig> envConfig = new HashMap<>();
if (environmentMap != null && !environmentMap.isEmpty()) {
environmentMap.keySet().forEach(projectId -> {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentMap.get(projectId));
if (environment != null && environment.getConfig() != null) {
EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
envConfig.put(projectId, env);
}
});
config.setConfig(envConfig);
}
if (CollectionUtils.isNotEmpty(this.getVariables())) {
config.setVariables(this.variables);
@ -155,9 +165,9 @@ public class MsScenario extends MsTestElement {
}
});
}
if (config != null && config.getConfig() != null && config.getConfig().getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().getCommonConfig().getVariables())) {
config.getConfig().getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
if (config != null && config.getConfig() != null && 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(), "=")
);
}

View File

@ -105,8 +105,6 @@ public abstract class MsTestElement {
private boolean customizeReq;
@JSONField(ordinal = 12)
private String projectId;
@JSONField(ordinal = 13)
private Map<String, String> environmentMap;
private MsTestElement parent;
@ -169,14 +167,14 @@ public abstract class MsTestElement {
}
public Arguments addArguments(ParameterConfig config) {
if (config != null && config.getConfig() != null && config.getConfig().getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().getCommonConfig().getVariables())) {
if (config != null && config.getConfig() != null && config.getConfig().get(this.getProjectId()).getCommonConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables())) {
Arguments arguments = new Arguments();
arguments.setEnabled(true);
arguments.setName(name + "Variables");
arguments.setProperty(TestElement.TEST_CLASS, Arguments.class.getName());
arguments.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("ArgumentsPanel"));
config.getConfig().getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
config.getConfig().get(this.getProjectId()).getCommonConfig().getVariables().stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue ->
arguments.addArgument(keyValue.getName(), keyValue.getValue(), "=")
);
return arguments;
@ -184,10 +182,9 @@ public abstract class MsTestElement {
return null;
}
protected EnvironmentConfig getEnvironmentConfig(String projectId) {
String env = environmentMap.get(projectId);
protected EnvironmentConfig getEnvironmentConfig(String environmentId) {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(env);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(environmentId);
if (environment != null && environment.getConfig() != null) {
return JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
}

View File

@ -5,11 +5,12 @@ import io.metersphere.api.dto.scenario.environment.EnvironmentConfig;
import lombok.Data;
import java.util.List;
import java.util.Map;
@Data
public class ParameterConfig {
// 环境配置
private EnvironmentConfig config;
private Map<String,EnvironmentConfig> config;
// 公共场景参数
private List<ScenarioVariable> variables;
// 公共Cookie

View File

@ -118,7 +118,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
if (config != null && config.getConfig() != null) {
config.setConfig(config.getConfig());
} else {
config.setConfig(getEnvironmentConfig(useEnvironment));
// config.setConfig(getEnvironmentConfig(useEnvironment));
}
// 添加环境中的公共变量
@ -128,7 +128,7 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
try {
if (config != null && config.getConfig() != null) {
String url = config.getConfig().getHttpConfig().getProtocol() + "://" + config.getConfig().getHttpConfig().getSocket();
String url = config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol() + "://" + config.getConfig().get(this.getProjectId()).getHttpConfig().getSocket();
// 补充如果是完整URL 则用自身URL
boolean isUrl = false;
if (StringUtils.isNotEmpty(this.getUrl()) && isURL(this.getUrl())) {
@ -141,9 +141,9 @@ public class MsHTTPSamplerProxy extends MsTestElement {
sampler.setPort(urlObject.getPort());
sampler.setProtocol(urlObject.getProtocol());
} else {
sampler.setDomain(config.getConfig().getHttpConfig().getDomain());
sampler.setPort(config.getConfig().getHttpConfig().getPort());
sampler.setProtocol(config.getConfig().getHttpConfig().getProtocol());
sampler.setDomain(config.getConfig().get(this.getProjectId()).getHttpConfig().getDomain());
sampler.setPort(config.getConfig().get(this.getProjectId()).getHttpConfig().getPort());
sampler.setProtocol(config.getConfig().get(this.getProjectId()).getHttpConfig().getProtocol());
}
String envPath = StringUtils.equals(urlObject.getPath(), "/") ? "" : urlObject.getPath();
if (StringUtils.isNotBlank(this.getPath()) && !isUrl) {
@ -215,16 +215,16 @@ public class MsHTTPSamplerProxy extends MsTestElement {
}
// 通用请求Headers
if (config != null && config.getConfig() != null && config.getConfig().getHttpConfig() != null
&& CollectionUtils.isNotEmpty(config.getConfig().getHttpConfig().getHeaders())) {
setHeader(httpSamplerTree, config.getConfig().getHttpConfig().getHeaders());
if (config != null && config.getConfig() != null && 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().getCommonConfig() != null
&& config.getConfig().getCommonConfig().isEnableHost()) {
MsDNSCacheManager.addEnvironmentVariables(httpSamplerTree, this.getName(), config.getConfig());
MsDNSCacheManager.addEnvironmentDNS(httpSamplerTree, this.getName(), config.getConfig());
if (config != null && config.getConfig() != null && 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()));
}
if (CollectionUtils.isNotEmpty(hashTree)) {
for (MsTestElement el : hashTree) {

View File

@ -69,6 +69,8 @@ public class MsTCPSampler extends MsTestElement {
private MsJSR223PreProcessor tcpPreProcessor;
@JSONField(ordinal = 38)
private String protocol = "TCP";
@JSONField(ordinal = 39)
private String projectId;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
@ -78,8 +80,8 @@ public class MsTCPSampler extends MsTestElement {
if (this.getReferenced() != null && MsTestElementConstants.REF.name().equals(this.getReferenced())) {
this.getRefElement(this);
}
config.setConfig(getEnvironmentConfig(useEnvironment));
parseEnvironment(config.getConfig());
// config.setConfig(getEnvironmentConfig(useEnvironment));
parseEnvironment(config.getConfig().get(this.projectId));
// 添加环境中的公共变量
Arguments arguments = this.addArguments(config);

View File

@ -548,10 +548,14 @@ public class ApiAutomationService {
public String debugRun(RunDefinitionRequest request, List<MultipartFile> bodyFiles) {
List<String> bodyUploadIds = new ArrayList<>(request.getBodyUploadIds());
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
EnvironmentConfig envConfig = null;
if (request.getEnvironmentId() != null) {
ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId());
envConfig = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
Map<String,EnvironmentConfig> envConfig = new HashMap<>();
Map<String, String> map = request.getEnvironmentMap();
if (map != null) {
map.keySet().forEach(id -> {
ApiTestEnvironmentWithBLOBs environment = environmentService.get(map.get(id));
EnvironmentConfig env = JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class);
envConfig.put(id, env);
});
}
ParameterConfig config = new ParameterConfig();
config.setConfig(envConfig);

View File

@ -513,9 +513,9 @@ public class ApiTestCaseService {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId());
ParameterConfig parameterConfig = new ParameterConfig();
if (environment != null && environment.getConfig() != null) {
parameterConfig.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
}
// if (environment != null && environment.getConfig() != null) {
// parameterConfig.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class));
// }
testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), parameterConfig);
return jmeterHashTree;
}

View File

@ -39,7 +39,8 @@ import {parseEnvironment} from "@/business/components/api/test/model/Environment
export default {
name: "ApiScenarioEnv",
props: {
projectIds: Set
projectIds: Set,
envMap: Map
},
data() {
return {
@ -72,7 +73,7 @@ export default {
let item = {};
item.id = id;
item.envs = envs;
item.selectEnv = '';
item.selectEnv = this.envMap.get(id);
this.data.push(item)
})
})
@ -80,7 +81,9 @@ export default {
open() {
this.data = [];
this.dialogVisible = true;
this.init();
if (this.projectIds.size > 0) {
this.init();
}
},
getWsProjects() {
this.$get("/project/listAll", res => {

View File

@ -373,7 +373,7 @@
}
// todo
if (projectId != null) {
if (projectId != null && typeof projectId === 'string') {
this.condition.projectId = projectId;
} else if (this.projectId != null) {
this.condition.projectId = this.projectId;

View File

@ -113,7 +113,7 @@
threadGroup.name = this.reportId;
threadGroup.enableCookieShare = this.runData.enableCookieShare;
let map = this.environment;
this.runData.environmentMap = this.strMapToObj(map);
this.runData.projectId = getCurrentProjectID();
threadGroup.hashTree.push(this.runData);
testPlan.hashTree.push(threadGroup);
let reqObj = {id: this.reportId, reportId: this.reportId, scenarioName: this.runData.name,

View File

@ -186,7 +186,7 @@
<!--场景导入 -->
<scenario-relevance @save="addScenario" ref="scenarioRelevance"/>
<api-scenario-env :project-ids="projectIds" ref="apiScenarioEnv" @setProjectEnvMap="setProjectEnvMap"/>
<api-scenario-env :project-ids="projectIds" :env-map="projectEnvMap" ref="apiScenarioEnv" @setProjectEnvMap="setProjectEnvMap"/>
<!-- 环境 -->
<api-environment-config ref="environmentConfig" @close="environmentConfigClose"/>
@ -316,7 +316,7 @@ export default {
mounted() {
getProject.$on('addProjectEnv', (projectId, projectEnv) => {
this.projectIds.add(projectId);
this.projectEnvMap.set(projectId, projectEnv);
// this.projectEnvMap.set(projectId, projectEnv);
})
},
directives: {OutsideClick},
@ -660,6 +660,9 @@ export default {
const parent = node.parent
const hashTree = parent.data.hashTree || parent.data;
const index = hashTree.findIndex(d => d.resourceId != undefined && row.resourceId != undefined && d.resourceId === row.resourceId)
if (hashTree[index] && hashTree[index].projectId) {
this.projectIds.delete(hashTree[index].projectId);
}
hashTree.splice(index, 1);
this.sort();
this.reload();
@ -893,6 +896,14 @@ export default {
}
})
},
objToStrMap(obj) {
let strMap = new Map();
for (let k of Object.keys(obj)) {
strMap.set(k, obj[k]);
}
return strMap;
},
getApiScenario() {
if (this.currentScenario.tags != undefined && !(this.currentScenario.tags instanceof Array)) {
this.currentScenario.tags = JSON.parse(this.currentScenario.tags);
@ -911,6 +922,7 @@ export default {
let obj = JSON.parse(response.data.scenarioDefinition);
if (obj) {
this.currentEnvironmentId = obj.environmentId;
this.projectEnvMap = this.objToStrMap(obj.environmentMap);
this.currentScenario.variables = [];
let index = 1;
if (obj.variables) {
@ -942,6 +954,13 @@ export default {
})
}
},
strMapToObj(strMap){
let obj= Object.create(null);
for (let[k,v] of strMap) {
obj[k] = v;
}
return obj;
},
setParameter() {
this.currentScenario.stepTotal = this.scenarioDefinition.length;
this.currentScenario.projectId = getCurrentProjectID();
@ -955,9 +974,9 @@ export default {
variables: this.currentScenario.variables,
headers: this.currentScenario.headers,
referenced: 'Created',
environmentMap: this.projectEnvMap,
environmentMap: this.strMapToObj(this.projectEnvMap),
hashTree: this.scenarioDefinition,
projectId: this.projectId
projectId: this.projectId,
};
this.currentScenario.scenarioDefinition = scenario;
if (this.currentScenario.tags instanceof Array) {

View File

@ -223,7 +223,6 @@ export class Scenario extends BaseConfig {
this.headers = [];
this.requests = [];
this.environmentId = undefined;
this.environmentMap = undefined;
this.dubboConfig = undefined;
this.environment = undefined;
this.enableCookieShare = false;