From 20127bc205c28a42ea156bf2c02dfea7f779587b Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Fri, 10 Sep 2021 15:40:02 +0800 Subject: [PATCH] =?UTF-8?q?feat=20(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E5=AE=8C=E6=88=90=E4=BC=98=E5=8C=96=E9=9C=80?= =?UTF-8?q?=E6=B1=822=20--story=3D1002974=20--user=3D=E8=B5=B5=E5=8B=87=20?= =?UTF-8?q?2.=E7=8E=AF=E5=A2=83=E5=8C=B9=E9=85=8D=EF=BC=8C=E6=A8=A1?= =?UTF-8?q?=E5=9D=97=E6=94=AF=E6=8C=81...=20https://www.tapd.cn/55049933/s?= =?UTF-8?q?/1045479?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/ApiAutomationController.java | 7 +- .../dto/definition/request/ElementUtil.java | 34 +++++++++- .../api/service/ApiAutomationService.java | 25 +++++++ .../automation/scenario/EditApiScenario.vue | 66 ++++++++++++++----- .../scenario/common/CustomizeReqInfo.vue | 35 +++++++--- 5 files changed, 138 insertions(+), 29 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 b0fe4d56ea..c537af5acb 100644 --- a/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java +++ b/backend/src/main/java/io/metersphere/api/controller/ApiAutomationController.java @@ -328,7 +328,12 @@ public class ApiAutomationController { @PostMapping(value = "/stop/batch") public String stopBatch(@RequestBody List reportIds) { - return taskService.stop(reportIds); + return taskService.stop(reportIds); + } + + @PostMapping("/setDomain") + public String setDomain(@RequestBody ApiScenarioEnvRequest request) { + return apiAutomationService.setDomain(request.getDefinition()); } } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java index dad0a8618e..540e67e1b4 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ElementUtil.java @@ -1,9 +1,10 @@ package io.metersphere.api.dto.definition.request; +import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import io.metersphere.api.dto.definition.request.controller.MsLoopController; -import io.metersphere.api.dto.definition.request.controller.MsTransactionController; +import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.mockconfig.MockConfigStaticData; import io.metersphere.api.dto.scenario.KeyValue; @@ -16,6 +17,8 @@ import io.metersphere.commons.constants.MsTestElementConstants; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.FileUtils; +import io.metersphere.commons.utils.LogUtil; +import io.metersphere.plugin.core.MsParameter; import io.metersphere.plugin.core.MsTestElement; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -23,6 +26,7 @@ import org.apache.jmeter.config.Arguments; import org.apache.jmeter.config.CSVDataSet; import org.apache.jmeter.config.RandomVariableConfig; import org.apache.jmeter.modifiers.CounterConfig; +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; @@ -286,4 +290,32 @@ public class ElementUtil { dataFormatting(elementJSONArray); } } + + public static void dataSetDomain(JSONArray hashTree, MsParameter msParameter) { + try { + for (int i = 0; i < hashTree.size(); i++) { + JSONObject element = hashTree.getJSONObject(i); + if (element != null && element.get("type").toString().equals("HTTPSamplerProxy")) { + MsHTTPSamplerProxy httpSamplerProxy = JSON.toJavaObject(element, MsHTTPSamplerProxy.class); + if (httpSamplerProxy != null + && (!httpSamplerProxy.isCustomizeReq() || (httpSamplerProxy.isCustomizeReq() && httpSamplerProxy.getIsRefEnvironment()))) { + HashTree tmpHashTree = new HashTree(); + httpSamplerProxy.toHashTree(tmpHashTree, null, msParameter); + if (tmpHashTree != null && tmpHashTree.getArray().length > 0) { + HTTPSamplerProxy object = (HTTPSamplerProxy) tmpHashTree.getArray()[0]; + if (object != null && StringUtils.isNotEmpty(object.getDomain())) { + element.fluentPut("domain", StringUtils.isNotEmpty(object.getProtocol()) ? object.getProtocol() + "://" + object.getDomain() : object.getDomain()); + } + } + } + } + if (element.containsKey("hashTree")) { + JSONArray elementJSONArray = element.getJSONArray("hashTree"); + dataSetDomain(elementJSONArray, msParameter); + } + } + } catch (Exception e) { + LogUtil.error(e.getMessage()); + } + } } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java index 6a3740c54f..15c29d00d0 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -600,6 +600,30 @@ public class ApiAutomationService { return apiScenarioMapper.selectByPrimaryKey(id); } + public String setDomain(String scenarioDefinition) { + JSONObject element = JSON.parseObject(scenarioDefinition); + ParameterConfig config = new ParameterConfig(); + Map envConfig = new HashMap<>(16); + Map environmentMap = (Map) element.get("environmentMap"); + 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); + env.setApiEnvironmentid(environment.getId()); + envConfig.put(projectId, env); + } + }); + config.setConfig(envConfig); + } + if (config.getConfig() != null && !config.getConfig().isEmpty()) { + ElementUtil.dataSetDomain(element.getJSONArray("hashTree"), config); + } + return JSON.toJSONString(element); + } + + public LinkedList getScenarioHashTree(String definition) { ObjectMapper objectMapper = new ObjectMapper(); objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); @@ -2518,6 +2542,7 @@ public class ApiAutomationService { /** * 用例自定义排序 + * * @param request */ public void updateOrder(ResetOrderRequest request) { diff --git a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue index 9be63e288d..f0fc3f9fb7 100644 --- a/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue +++ b/frontend/src/business/components/api/automation/scenario/EditApiScenario.vue @@ -450,6 +450,7 @@ export default { messageWebSocket: {}, buttonData: [], stepFilter: new STEP, + plugins: [], } }, created() { @@ -464,6 +465,7 @@ export default { this.getWsProjects(); this.getMaintainerOptions(); this.getApiScenario(); + this.getPlugins(); this.initPlugins(); this.buttonData = buttons(this); }, @@ -482,27 +484,53 @@ export default { }, }, methods: { + setDomain() { + if (this.projectEnvMap && this.projectEnvMap.size > 0) { + let scenario = { + id: this.currentScenario.id, + enableCookieShare: this.enableCookieShare, + name: this.currentScenario.name, + type: "scenario", + clazzName: TYPE_TO_C.get("scenario"), + variables: this.currentScenario.variables, + headers: this.currentScenario.headers, + referenced: 'Created', + environmentMap: strMapToObj(this.projectEnvMap), + hashTree: this.scenarioDefinition, + onSampleError: this.onSampleError, + projectId: this.currentScenario.projectId ? this.currentScenario.projectId : this.projectId, + }; + this.$post("/api/automation/setDomain", {definition: JSON.stringify(scenario)}, res => { + if (res.data) { + let data = JSON.parse(res.data); + this.scenarioDefinition = data.hashTree; + } + }) + } + }, initPlugins() { + if (this.plugins) { + this.plugins.forEach(item => { + let plugin = { + title: item.name, + show: this.showButton(item.jmeterClazz), + titleColor: "#555855", + titleBgColor: "#F4F4FF", + icon: "colorize", + click: () => { + this.addComponent(item.name, item) + } + } + if (this.operatingElements && this.operatingElements.includes(item.jmeterClazz)) { + this.buttonData.push(plugin); + } + }); + } + }, + getPlugins() { let url = "/plugin/list"; this.$get(url, response => { - let data = response.data; - if (data) { - data.forEach(item => { - let plugin = { - title: item.name, - show: this.showButton(item.jmeterClazz), - titleColor: "#555855", - titleBgColor: "#F4F4FF", - icon: "colorize", - click: () => { - this.addComponent(item.name, item) - } - } - if (this.operatingElements && this.operatingElements.includes(item.jmeterClazz)) { - this.buttonData.push(plugin); - } - }); - } + this.plugins = response.data; }); }, stop() { @@ -1279,6 +1307,7 @@ export default { } } this.loading = false; + this.setDomain(); this.sort(); // 初始化resourceId if (this.scenarioDefinition) { @@ -1352,6 +1381,7 @@ export default { }, setProjectEnvMap(projectEnvMap) { this.projectEnvMap = projectEnvMap; + this.setDomain(); }, getWsProjects() { this.$get("/project/listAll", res => { diff --git a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue index 4cd3c9974c..222f68386d 100644 --- a/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue +++ b/frontend/src/business/components/api/automation/scenario/common/CustomizeReqInfo.vue @@ -1,19 +1,26 @@