This commit is contained in:
fit2-zhao 2021-03-29 18:23:24 +08:00
commit 2bd6172e4a
9 changed files with 126 additions and 37 deletions

View File

@ -29,6 +29,7 @@ public class EsbDataStruct {
private boolean required;
private String description;
private List<EsbDataStruct> children;
private String status = "";
public void init(){
this.uuid = UUID.randomUUID().toString();
@ -127,6 +128,9 @@ public class EsbDataStruct {
if (element != null) {
if (this.children == null || this.children.isEmpty()) {
if(this.value == null ){
this.value = "";
}
element.addText(this.value);
} else {
for (EsbDataStruct child : children) {

View File

@ -114,23 +114,26 @@ public class MsScenario extends MsTestElement {
// 设置共享cookie
config.setEnableCookieShare(enableCookieShare);
Map<String, EnvironmentConfig> envConfig = new HashMap<>(16);
// 兼容历史数据
if (environmentMap == null || environmentMap.isEmpty()) {
environmentMap = new HashMap<>(16);
if (StringUtils.isNotBlank(environmentId)) {
environmentMap.put(SessionUtils.getCurrentProjectId(), environmentId);
}
}
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);
if (config.getConfig() == null) {
// 兼容历史数据
if (this.environmentMap == null || this.environmentMap.isEmpty()) {
this.environmentMap = new HashMap<>(16);
if (StringUtils.isNotBlank(environmentId)) {
// 兼容1.8之前 没有environmentMap但有environmentId的数据
this.environmentMap.put("historyProjectID", environmentId);
}
});
config.setConfig(envConfig);
}
if (this.environmentMap != null && !this.environmentMap.isEmpty()) {
this.environmentMap.keySet().forEach(projectId -> {
ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class);
ApiTestEnvironmentWithBLOBs environment = environmentService.get(this.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);

View File

@ -129,6 +129,11 @@ public class MsHTTPSamplerProxy extends MsTestElement {
config.setConfig(getEnvironmentConfig(useEnvironment));
}
// 1.8 之前历史数据
if(StringUtils.isEmpty(this.getProjectId()) && config.getConfig()!= null && !config.getConfig().isEmpty()){
this.setProjectId("historyProjectID");
}
// 添加环境中的公共变量
Arguments arguments = this.addArguments(config);
if (arguments != null) {

View File

@ -2,6 +2,7 @@ package io.metersphere.api.dto.definition.request.sampler;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.annotation.JSONType;
import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.ParameterConfig;
import io.metersphere.api.dto.definition.request.processors.pre.MsJSR223PreProcessor;
@ -70,6 +71,12 @@ public class MsTCPSampler extends MsTestElement {
@JSONField(ordinal = 39)
private String projectId;
/**
* 新加两个参数场景保存/修改时需要的参数不会传递JMeter只是用于最后的保留
*/
private List<EsbDataStruct> esbDataStruct;
private List<EsbDataStruct> backEsbDataStruct;
@Override
public void toHashTree(HashTree tree, List<MsTestElement> hashTree, ParameterConfig config) {
// 非导出操作且不是启用状态则跳过执行

View File

@ -90,6 +90,8 @@ public class ApiAutomationService {
@Resource
@Lazy
private TestPlanScenarioCaseService testPlanScenarioCaseService;
@Resource
private EsbApiParamService esbApiParamService;
public List<ApiScenarioDTO> list(ApiScenarioRequest request) {
request = this.initRequest(request, true, true);
@ -184,6 +186,9 @@ public class ApiAutomationService {
scenario.setCreateTime(System.currentTimeMillis());
scenario.setNum(getNextNum(request.getProjectId()));
//检查场景的请求步骤如果含有ESB请求步骤的话要做参数计算处理
esbApiParamService.checkScenarioRequests(request);
apiScenarioMapper.insert(scenario);
List<String> bodyUploadIds = request.getBodyUploadIds();
@ -205,6 +210,9 @@ public class ApiAutomationService {
List<String> bodyUploadIds = request.getBodyUploadIds();
FileUtils.createBodyFiles(bodyUploadIds, bodyFiles);
//检查场景的请求步骤如果含有ESB请求步骤的话要做参数计算处理
esbApiParamService.checkScenarioRequests(request);
final ApiScenarioWithBLOBs scenario = buildSaveScenario(request);
apiScenarioMapper.updateByPrimaryKeySelective(scenario);
extScheduleMapper.updateNameByResourceID(request.getId(), request.getName());// 修改场景name同步到修改首页定时任务

View File

@ -3,11 +3,13 @@ package io.metersphere.api.service;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import io.metersphere.api.dto.automation.EsbDataStruct;
import io.metersphere.api.dto.automation.SaveApiScenarioRequest;
import io.metersphere.api.dto.automation.parse.EsbDataParser;
import io.metersphere.api.dto.definition.ApiDefinitionResult;
import io.metersphere.api.dto.definition.ApiTestCaseResult;
import io.metersphere.api.dto.definition.SaveApiDefinitionRequest;
import io.metersphere.api.dto.definition.SaveApiTestCaseRequest;
import io.metersphere.api.dto.definition.request.MsTestElement;
import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler;
import io.metersphere.api.dto.scenario.KeyValue;
import io.metersphere.base.domain.ApiTestCaseWithBLOBs;
@ -313,6 +315,35 @@ public class EsbApiParamService {
return keyValueList;
}
private List<KeyValue> genKeyValueListByDataStruct(MsTCPSampler tcpSampler, List<EsbDataStruct> dataStructRequestList) {
List<KeyValue> keyValueList = new ArrayList<>();
String sendRequest = tcpSampler.getRequest();
String paramRegexStr = "\\$\\{([^}]*)\\}";
try {
if (StringUtils.isNotEmpty(sendRequest)) {
List<String> paramList = new ArrayList<>();
Pattern regex = Pattern.compile(paramRegexStr);
Matcher matcher = regex.matcher(sendRequest);
while (matcher.find()) {
paramList.add(matcher.group(1));
}
for (String param : paramList) {
String value = this.genValueFromEsbDataStructByParam(dataStructRequestList, param);
if (StringUtils.isNotEmpty(value)) {
KeyValue kv = new KeyValue();
kv.setName(param);
kv.setValue(value);
kv.setRequired(true);
keyValueList.add(kv);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return keyValueList;
}
//通过报文模版中的变量参数解析报文数据结构生成对应的xml数据
private String genValueFromEsbDataStructByParam(List<EsbDataStruct> dataStructRequestList, String param) {
String returnValue = "";
@ -341,10 +372,32 @@ public class EsbApiParamService {
return request;
}
public void handleEsbRequest(MsTCPSampler tcpSampler) {
try {
//修改reqeust.parameters, 将树结构类型数据转化为表格类型数据供执行时参数的提取
if (tcpSampler.getEsbDataStruct() != null ) {
List<KeyValue> keyValueList = this.genKeyValueListByDataStruct(tcpSampler, tcpSampler.getEsbDataStruct());
tcpSampler.setParameters(keyValueList);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public void deleteByResourceIdIn(List<String> apiIds) {
EsbApiParamsExample example = new EsbApiParamsExample();
example.createCriteria().andResourceIdIn(apiIds);
esbApiParamsMapper.deleteByExample(example);
}
public void checkScenarioRequests(SaveApiScenarioRequest request) {
if(request.getScenarioDefinition() != null ){
List<MsTestElement> hashTreeList = request.getScenarioDefinition().getHashTree();
for (MsTestElement testElement :hashTreeList) {
if(testElement instanceof MsTCPSampler){
this.handleEsbRequest((MsTCPSampler)testElement);
}
}
}
}
}

View File

@ -101,25 +101,25 @@ export default {
let requestObj = JSON.parse(item.request);
if(requestObj.esbDataStruct != null ){
//ESB
let param = {};
param.request = requestObj;
param.method = "ESB";
param.esbDataStruct = JSON.stringify(requestObj.esbDataStruct);
if(requestObj.backEsbDataStruct != null){
param.backEsbDataStruct = JSON.stringify(requestObj.backEsbDataStruct);
}else{
param.backEsbDataStruct = "";
}
// //ESB
// let param = {};
// param.request = requestObj;
// param.method = "ESB";
// param.esbDataStruct = JSON.stringify(requestObj.esbDataStruct);
// if(requestObj.backEsbDataStruct != null){
// param.backEsbDataStruct = JSON.stringify(requestObj.backEsbDataStruct);
// }else{
// param.backEsbDataStruct = "";
// }
this.$post("/api/definition/updateEsbRequest", param, response => {
if(response.data!=null){
if(response.data.request!=null){
item.request = JSON.stringify(response.data.request);
param.method = "TCP";
}
}
})
// this.$post("/api/definition/updateEsbRequest", param, response => {
// if(response.data!=null){
// if(response.data.request!=null){
// item.request = JSON.stringify(response.data.request);
// param.method = "TCP";
// }
// }
// })
}
});
this.$emit('save', apiCases, 'CASE', reference);

View File

@ -68,8 +68,8 @@
let projectId = "";
// envMap
if (!this.envMap) {
projectId = getCurrentProjectID();
if (!this.envMap || this.envMap.size === 0) {
projectId = this.$store.state.projectId;
} else {
//
projectId = this.runData.projectId;

View File

@ -178,6 +178,10 @@
<div v-else-if="apiInfo.requestBodyParamType == 'JSON-SCHEMA'" style="margin-left: 10px">
<ms-json-code-edit :body="apiInfo.jsonSchemaBody" ref="jsonCodeEdit"/>
</div>
<!-- <div v-else-if="apiInfo.requestBodyParamType == 'XML'" style="margin-left: 10px">-->
<!-- <ms-json-code-edit :body="apiInfo.jsonSchemaBody" ref="jsonCodeEdit"/>-->
<!-- <editor v-model="formatData" :lang="mode" @init="editorInit" :theme="theme" :height="height"/>-->
<!-- </div>-->
<div v-else class="showDataDiv">
<br/>
<p style="margin: 0px 20px;"
@ -423,7 +427,12 @@ export default {
formatRowData(dataType, data) {
var returnData = data;
if (data) {
returnData = data.replace(/\n/g, '<br>');
if(dataType === 'XML'){
returnData = "<xmp>"+returnData+"</xmp>";
}else{
returnData = data.replace(/\n/g, '<br>');
}
}
return returnData;
},