From 8e49e466a5d29b9a1c66a47562bca289e8c228ea Mon Sep 17 00:00:00 2001 From: "song.tianyang" Date: Mon, 22 Mar 2021 18:31:37 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E5=9C=BA=E6=99=AF=E5=AF=BC=E5=85=A5esb?= =?UTF-8?q?=E6=A1=88=E4=BE=8B=20=E6=8A=A5=E6=96=87=E6=A0=BC=E5=BC=8F?= =?UTF-8?q?=E9=97=AE=E9=A2=98=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 场景导入esb案例 报文格式问题处理 --- .../controller/ApiAutomationController.java | 1 - .../controller/ApiDefinitionController.java | 13 ++ .../api/service/ApiTestCaseService.java | 7 +- .../api/service/EsbApiParamService.java | 173 +++++++++++++++--- .../automation/scenario/api/ApiRelevance.vue | 24 +++ .../scenario/component/ApiComponent.vue | 26 ++- .../scenario/component/ComponentConfig.vue | 2 +- .../complete/EditCompleteTCPApi.vue | 2 - 8 files changed, 210 insertions(+), 38 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java index bc38ce887f..f42742fece 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -64,7 +64,6 @@ public class ApiAutomationController { public void update(@RequestPart("request") SaveApiScenarioRequest request, @RequestPart(value = "files") List bodyFiles) { apiAutomationService.update(request, bodyFiles); } - @GetMapping("/delete/{id}") public void delete(@PathVariable String id) { apiAutomationService.delete(id); diff --git a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java index 4ee9dd1caa..6fca4e8cb6 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiDefinitionController.java @@ -12,6 +12,7 @@ import io.metersphere.api.dto.definition.request.ScheduleInfoSwaggerUrlRequest; import io.metersphere.api.dto.swaggerurl.SwaggerTaskResult; import io.metersphere.api.dto.swaggerurl.SwaggerUrlRequest; import io.metersphere.api.service.ApiDefinitionService; +import io.metersphere.api.service.EsbApiParamService; import io.metersphere.base.domain.ApiDefinition; import io.metersphere.base.domain.Schedule; import io.metersphere.commons.constants.RoleConstants; @@ -24,6 +25,7 @@ import io.metersphere.controller.request.ScheduleRequest; import io.metersphere.service.CheckPermissionService; import io.metersphere.service.ScheduleService; import io.metersphere.track.request.testcase.ApiCaseRelevanceRequest; +import org.apache.commons.lang3.StringUtils; import org.apache.shiro.authz.annotation.Logical; import org.apache.shiro.authz.annotation.RequiresRoles; import org.springframework.web.bind.annotation.*; @@ -45,6 +47,8 @@ public class ApiDefinitionController { private ApiDefinitionService apiDefinitionService; @Resource private CheckPermissionService checkPermissionService; + @Resource + private EsbApiParamService esbApiParamService; @PostMapping("/list/{goPage}/{pageSize}") public Pager> list(@PathVariable int goPage, @PathVariable int pageSize, @RequestBody ApiDefinitionRequest request) { @@ -97,6 +101,15 @@ public class ApiDefinitionController { apiDefinitionService.deleteBatch(ids); } + @PostMapping(value = "/updateEsbRequest") + public SaveApiDefinitionRequest updateEsbRequest(@RequestBody SaveApiDefinitionRequest request) { + if(StringUtils.equals(request.getMethod(),"ESB")){ + //ESB的接口类型数据,采用TCP方式去发送。并将方法类型改为TCP。 并修改发送数据 + request = esbApiParamService.updateEsbRequest(request); + } + return request; + } + @PostMapping("/deleteBatchByParams") public void deleteBatchByParams(@RequestBody ApiBatchRequest request) { apiDefinitionService.deleteByParams(request); diff --git a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java index 7bd9858db4..5ffa9ad8d5 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -145,7 +145,9 @@ public class ApiTestCaseService { } public ApiTestCaseWithBLOBs get(String id) { - return apiTestCaseMapper.selectByPrimaryKey(id); + ApiTestCaseWithBLOBs returnBlobs = apiTestCaseMapper.selectByPrimaryKey(id); + esbApiParamService.handleApiEsbParams(returnBlobs); + return returnBlobs; } public ApiTestCase create(SaveApiTestCaseRequest request, List bodyFiles) { @@ -435,6 +437,9 @@ public class ApiTestCaseService { public Map getRequest(ApiTestCaseRequest request) { List list = extApiTestCaseMapper.getRequest(request); + for (ApiTestCaseWithBLOBs model : list) { + esbApiParamService.handleApiEsbParams(model); + } return list.stream().collect(Collectors.toMap(ApiTestCaseWithBLOBs::getId, ApiTestCaseWithBLOBs::getRequest)); } diff --git a/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java b/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java index 211f2cecbf..a8d38ced9e 100644 --- a/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java +++ b/backend/src/main/java/io/metersphere/api/service/EsbApiParamService.java @@ -10,6 +10,7 @@ import io.metersphere.api.dto.definition.SaveApiDefinitionRequest; import io.metersphere.api.dto.definition.SaveApiTestCaseRequest; import io.metersphere.api.dto.definition.request.sampler.MsTCPSampler; import io.metersphere.api.dto.scenario.KeyValue; +import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.EsbApiParamsExample; import io.metersphere.base.domain.EsbApiParamsWithBLOBs; import io.metersphere.base.mapper.EsbApiParamsMapper; @@ -110,25 +111,76 @@ public class EsbApiParamService { if (esbParamBlobs == null) { return; } - try { - if (StringUtils.isNotEmpty(res.getRequest())) { - JSONObject jsonObj = JSONObject.parseObject(res.getRequest()); - - JSONArray esbDataArray = JSONArray.parseArray(esbParamBlobs.getDataStruct()); - jsonObj.put("esbDataStruct", esbDataArray); - - JSONArray responseDataArray = JSONArray.parseArray(esbParamBlobs.getResponseDataStruct()); - jsonObj.put("backEsbDataStruct", responseDataArray); - - JSONObject backedScriptObj = JSONObject.parseObject(esbParamBlobs.getBackedScript()); - jsonObj.put("backScript", backedScriptObj); - - jsonObj.put("esbFrontedScript", esbParamBlobs.getFrontedScript()); - + if (StringUtils.isNotEmpty(res.getRequest())) { + JSONObject jsonObj = this.addEsbInfoToJsonString(esbParamBlobs, res.getRequest()); + if (jsonObj != null) { res.setRequest(jsonObj.toJSONString()); } - } catch (Exception e) { + } + +// try { +// if (StringUtils.isNotEmpty(res.getRequest())) { +// JSONObject jsonObj = JSONObject.parseObject(res.getRequest()); +// +// JSONArray esbDataArray = JSONArray.parseArray(esbParamBlobs.getDataStruct()); +// jsonObj.put("esbDataStruct", esbDataArray); +// +// JSONArray responseDataArray = JSONArray.parseArray(esbParamBlobs.getResponseDataStruct()); +// jsonObj.put("backEsbDataStruct", responseDataArray); +// +// JSONObject backedScriptObj = JSONObject.parseObject(esbParamBlobs.getBackedScript()); +// jsonObj.put("backScript", backedScriptObj); +// +// jsonObj.put("esbFrontedScript", esbParamBlobs.getFrontedScript()); +// +// res.setRequest(jsonObj.toJSONString()); +// } +// } catch (Exception e) { +// } + } + + public void handleApiEsbParams(ApiTestCaseWithBLOBs res) { + EsbApiParamsWithBLOBs esbParamBlobs = this.getEsbParamBLOBsByResourceID(res.getId()); + if (esbParamBlobs == null) { + return; + } + if (StringUtils.isNotEmpty(res.getRequest())) { + JSONObject jsonObj = this.addEsbInfoToJsonString(esbParamBlobs, res.getRequest()); + if (jsonObj != null) { + res.setRequest(jsonObj.toJSONString()); + } + + } + } + + private JSONObject addEsbInfoToJsonString(EsbApiParamsWithBLOBs esbParamBlobs, String requestString) { + JSONObject returnObj = null; + try { + returnObj = JSONObject.parseObject(requestString); + + JSONArray esbDataArray = JSONArray.parseArray(esbParamBlobs.getDataStruct()); + if (esbDataArray == null) { + returnObj.put("esbDataStruct", ""); + } else { + returnObj.put("esbDataStruct", esbDataArray); + } + + JSONArray responseDataArray = JSONArray.parseArray(esbParamBlobs.getResponseDataStruct()); + if (responseDataArray == null) { + returnObj.put("backEsbDataStruct", ""); + } else { + returnObj.put("backEsbDataStruct", responseDataArray); + } + + returnObj.put("esbFrontedScript", esbParamBlobs.getFrontedScript()); + + JSONObject backedScriptObj = JSONObject.parseObject(esbParamBlobs.getBackedScript()); + returnObj.put("backScript", backedScriptObj); + } catch (Exception e) { + + } + return returnObj; } public void handleApiEsbParams(ApiTestCaseResult res) { @@ -136,23 +188,30 @@ public class EsbApiParamService { if (esbParamBlobs == null) { return; } - try { - if (StringUtils.isNotEmpty(res.getRequest())) { - JSONObject jsonObj = JSONObject.parseObject(res.getRequest()); - JSONArray esbDataArray = JSONArray.parseArray(esbParamBlobs.getDataStruct()); - jsonObj.put("esbDataStruct", esbDataArray); - jsonObj.put("esbFrontedScript", esbParamBlobs.getFrontedScript()); - - JSONArray responseDataArray = JSONArray.parseArray(esbParamBlobs.getResponseDataStruct()); - jsonObj.put("backEsbDataStruct", responseDataArray); - + if (StringUtils.isNotEmpty(res.getRequest())) { + JSONObject jsonObj = this.addEsbInfoToJsonString(esbParamBlobs, res.getRequest()); + if (jsonObj != null) { res.setRequest(jsonObj.toJSONString()); } - } catch (Exception e) { + } +// try { +// if (StringUtils.isNotEmpty(res.getRequest())) { +// JSONObject jsonObj = JSONObject.parseObject(res.getRequest()); +// JSONArray esbDataArray = JSONArray.parseArray(esbParamBlobs.getDataStruct()); +// jsonObj.put("esbDataStruct", esbDataArray); +// jsonObj.put("esbFrontedScript", esbParamBlobs.getFrontedScript()); +// +// JSONArray responseDataArray = JSONArray.parseArray(esbParamBlobs.getResponseDataStruct()); +// jsonObj.put("backEsbDataStruct", responseDataArray); +// +// res.setRequest(jsonObj.toJSONString()); +// } +// } catch (Exception e) { +// } } - public SaveApiDefinitionRequest handleEsbRequest(SaveApiDefinitionRequest request) { + public SaveApiDefinitionRequest updateEsbRequest(SaveApiDefinitionRequest request) { try { //修改reqeust.parameters //用户交互感受:ESB的发送数据以报文模板为主框架,同时前端不再有key-value的表格数据填充。 @@ -162,8 +221,27 @@ public class EsbApiParamService { MsTCPSampler tcpSampler = (MsTCPSampler) request.getRequest(); List keyValueList = this.genKeyValueListByDataStruct(tcpSampler, request.getEsbDataStruct()); tcpSampler.setParameters(keyValueList); + request.setRequest(tcpSampler); + } + //更新EsbApiParams类 +// EsbApiParamsWithBLOBs esbApiParams = this.createEsbApiParam(request.getId(), request.getEsbDataStruct(), request.getBackEsbDataStruct(), request.getBackScript()); + } catch (Exception e) { + e.printStackTrace(); + } + return request; + } + public SaveApiDefinitionRequest handleEsbRequest(SaveApiDefinitionRequest request) { + try { + //修改reqeust.parameters + //用户交互感受:ESB的发送数据以报文模板为主框架,同时前端不再有key-value的表格数据填充。 + //业务逻辑: 发送ESB接口数据时,使用报文模板中的数据,同时报文模板中的${取值}目的是为了拼接数据结构(比如xml的子节点) + //代码实现: 此处打算解析前端传来的EsbDataStruct数据结构,将数据结构按照报文模板中的${取值}为最高优先级组装keyValue对象。这样Jmeter会自动拼装为合适的xml + if (StringUtils.isNotEmpty(request.getEsbDataStruct())) { + MsTCPSampler tcpSampler = (MsTCPSampler) request.getRequest(); + tcpSampler.setProtocol("ESB"); + List keyValueList = this.genKeyValueListByDataStruct(tcpSampler, request.getEsbDataStruct()); + tcpSampler.setParameters(keyValueList); } - //更新EsbApiParams类 EsbApiParamsWithBLOBs esbApiParams = this.createEsbApiParam(request.getId(), request.getEsbDataStruct(), request.getBackEsbDataStruct(), request.getBackScript()); } catch (Exception e) { @@ -172,6 +250,43 @@ public class EsbApiParamService { return request; } +// public RunDefinitionRequest handleEsbRequest(RunDefinitionRequest request) { +// try { +// //修改reqeust.parameters +// //用户交互感受:ESB的发送数据以报文模板为主框架,同时前端不再有key-value的表格数据填充。 +// //业务逻辑: 发送ESB接口数据时,使用报文模板中的数据,同时报文模板中的${取值}目的是为了拼接数据结构(比如xml的子节点) +// //代码实现: 此处打算解析前端传来的EsbDataStruct数据结构,将数据结构按照报文模板中的${取值}为最高优先级组装keyValue对象。这样Jmeter会自动拼装为合适的xml +// if (StringUtils.isNotEmpty(request.getEsbDataStruct())) { +// if(request.getTestElement() instanceof MsTestPlan){ +// MsTestPlan testPlan = (MsTestPlan)request.getTestElement(); +// for (MsTestElement testElement: testPlan.getHashTree()) { +// if(testElement instanceof MsThreadGroup){ +// MsThreadGroup group = (MsThreadGroup)testElement; +// for (MsTestElement groupElement: testPlan.getHashTree()) { +// if(groupElement instanceof MsScenario){ +// MsScenario scenario = (MsScenario)groupElement; +// for (MsTestElement scenarioElement: scenario.getHashTree()) { +// if(scenarioElement instanceof MsTCPSampler){ +// MsTCPSampler tcpSampler = (MsTCPSampler) scenarioElement; +// List keyValueList = this.genKeyValueListByDataStruct(tcpSampler, request.getEsbDataStruct()); +// tcpSampler.setParameters(keyValueList); +// } +// } +// } +// } +// } +// } +// } +// } +// +// //更新EsbApiParams类 +// EsbApiParamsWithBLOBs esbApiParams = this.createEsbApiParam(request.getId(), request.getEsbDataStruct(), request.getBackEsbDataStruct(), request.getBackScript()); +// } catch (Exception e) { +// e.printStackTrace(); +// } +// return request; +// } + //通过esb数据结构生成keyValue集合,以及发送参数 private List genKeyValueListByDataStruct(MsTCPSampler tcpSampler, String esbDataStruct) { List keyValueList = new ArrayList<>(); diff --git a/frontend/src/business/components/api/automation/scenario/api/ApiRelevance.vue b/frontend/src/business/components/api/automation/scenario/api/ApiRelevance.vue index d6dbdccc9d..c27bce8408 100644 --- a/frontend/src/business/components/api/automation/scenario/api/ApiRelevance.vue +++ b/frontend/src/business/components/api/automation/scenario/api/ApiRelevance.vue @@ -50,6 +50,7 @@ import MsMainContainer from "../../../../common/components/MsMainContainer"; import ScenarioRelevanceApiList from "./RelevanceApiList"; import RelevanceDialog from "../../../../track/plan/view/comonents/base/RelevanceDialog"; import TestCaseRelevanceBase from "@/business/components/track/plan/view/comonents/base/TestCaseRelevanceBase"; +import {getUUID} from "@/common/js/utils"; export default { name: "ApiRelevance", @@ -97,6 +98,29 @@ export default { apiCases.forEach((item) => { item.request = response.data[item.id]; item.projectId = this.projectId; + + 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 = ""; + } + + 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); this.$refs.baseRelevance.close(); diff --git a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue index 5ea30dcb85..b5cf6df0fb 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ApiComponent.vue @@ -32,7 +32,9 @@

{{ $t('api_test.definition.request.req_param') }}

- + + + - +
+ +
+
+ +
0 ? requireComponent("./apiDefinition/EsbDefinition.vue") : {}; + const esbDefinitionResponse = (requireComponent!=null&&requireComponent.keys().length) > 0 ? requireComponent("./apiDefinition/EsbDefinitionResponse.vue") : {}; export default { name: "MsApiComponent", @@ -101,7 +112,9 @@ components: { CustomizeReqInfo, ApiBaseComponent, ApiResponseComponent, - MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm, MsRequestResultTail, MsRun + MsSqlBasisParameters, MsTcpBasisParameters, MsDubboBasisParameters, MsApiRequestForm, MsRequestResultTail, MsRun, + "esbDefinition": esbDefinition.default, + "esbDefinitionResponse": esbDefinitionResponse.default }, data() { return { @@ -109,6 +122,7 @@ reportId: "", runData: [], isShowInput: false, + showXpackCompnent:false, } }, created() { @@ -121,6 +135,7 @@ } // 加载引用对象数据 this.getApiInfo(); + console.log(JSON.stringify(this.request)); if (this.request.protocol === 'HTTP') { this.setUrl(this.request.url); this.setUrl(this.request.path); @@ -134,6 +149,9 @@ } } } + if (requireComponent != null && JSON.stringify(esbDefinition) != '{}'&& JSON.stringify(esbDefinitionResponse) != '{}') { + this.showXpackCompnent = true; + } }, computed: { displayColor() { @@ -284,7 +302,7 @@ let debugData = { id: this.currentScenario.id, name: this.currentScenario.name, type: "scenario", variables: this.currentScenario.variables, referenced: 'Created', headers: this.currentScenario.headers, - enableCookieShare: this.enableCookieShare, environmentId: this.currentEnvironmentId, hashTree: [this.request] + enableCookieShare: this.enableCookieShare, environmentId: this.currentEnvironmentId, hashTree: [this.request], }; this.runData.push(debugData); /*触发执行操作*/ diff --git a/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue b/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue index a87ce11b4b..877f8c1414 100644 --- a/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue +++ b/frontend/src/business/components/api/automation/scenario/component/ComponentConfig.vue @@ -101,7 +101,7 @@ getComponent(type) { if (type === ELEMENT_TYPE.JSR223PreProcessor) { this.title = this.$t('api_test.definition.request.pre_script'); - this.titleColor = "#B8741A"; + this.titleColor = "#b8741a"; this.backgroundColor = "#F9F1EA"; return "MsJsr233Processor"; } else if (type === ELEMENT_TYPE.JSR223PostProcessor) { diff --git a/frontend/src/business/components/api/definition/components/complete/EditCompleteTCPApi.vue b/frontend/src/business/components/api/definition/components/complete/EditCompleteTCPApi.vue index c2590a3911..8acbaed4cb 100644 --- a/frontend/src/business/components/api/definition/components/complete/EditCompleteTCPApi.vue +++ b/frontend/src/business/components/api/definition/components/complete/EditCompleteTCPApi.vue @@ -131,8 +131,6 @@ export default { if(this.request.backScript != null){ this.basisData.backScript = JSON.stringify(this.request.backScript); } - - } this.$emit('saveApi', this.basisData); }