fix: 场景关联ESB接口时复制的ESB案例无法构建树结构数据

修复场景关联ESB接口时复制的ESB案例无法构建树结构数据
This commit is contained in:
song.tianyang 2021-03-29 16:50:33 +08:00
parent cb9a4d4a83
commit 809142af53
5 changed files with 90 additions and 18 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

@ -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);