From 711c787f8d23a4537f4feef1fca74a5dfa6bbffc Mon Sep 17 00:00:00 2001 From: fit2-zhao Date: Tue, 29 Dec 2020 09:56:26 +0800 Subject: [PATCH] =?UTF-8?q?fix(=E6=8E=A5=E5=8F=A3=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E5=8C=96):=20=E6=B7=BB=E5=8A=A0=E6=89=A7=E8=A1=8C=E6=97=B6=20m?= =?UTF-8?q?ock=20=E5=A4=84=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../request/sampler/MsHTTPSamplerProxy.java | 9 +++-- .../api/service/ApiAutomationService.java | 33 +++---------------- .../api/service/ApiDefinitionService.java | 14 +------- 3 files changed, 11 insertions(+), 45 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java index 0a633e7251..cc4947e9ff 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/sampler/MsHTTPSamplerProxy.java @@ -13,6 +13,7 @@ import io.metersphere.api.service.ApiTestEnvironmentService; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.commons.utils.CommonBeanFactory; import io.metersphere.commons.utils.LogUtil; +import io.metersphere.commons.utils.ScriptEngineUtils; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; @@ -222,7 +223,8 @@ public class MsHTTPSamplerProxy extends MsTestElement { stringBuffer.append("/"); Map keyValueMap = new HashMap<>(); this.getRest().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue -> - keyValueMap.put(keyValue.getName(), keyValue.getValue()) + keyValueMap.put(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ? + ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()) ); try { Pattern p = Pattern.compile("(\\{)([\\w]+)(\\})"); @@ -244,7 +246,8 @@ public class MsHTTPSamplerProxy extends MsTestElement { stringBuffer.append(path); stringBuffer.append("?"); this.getArguments().stream().filter(KeyValue::isEnable).filter(KeyValue::isValid).forEach(keyValue -> - stringBuffer.append(keyValue.getName()).append("=").append(keyValue.getValue()).append("&") + stringBuffer.append(keyValue.getName()).append("=").append(keyValue.getValue() != null && keyValue.getValue().startsWith("@") ? + ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()).append("&") ); return stringBuffer.substring(0, stringBuffer.length() - 1); } @@ -252,7 +255,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { private Arguments httpArguments(List list) { Arguments arguments = new Arguments(); list.stream().filter(KeyValue::isValid).filter(KeyValue::isEnable).forEach(keyValue -> { - HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), keyValue.getValue()); + HTTPArgument httpArgument = new HTTPArgument(keyValue.getName(), keyValue.getValue() != null && keyValue.getValue().startsWith("@") ? ScriptEngineUtils.calculate(keyValue.getValue()) : keyValue.getValue()); httpArgument.setAlwaysEncoded(keyValue.isEncode()); if (StringUtils.isNotBlank(keyValue.getContentType())) { httpArgument.setContentType(keyValue.getContentType()); 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 0f4c0ab9d8..19c1b0376d 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -13,7 +13,6 @@ import io.metersphere.api.dto.definition.request.*; import io.metersphere.api.dto.scenario.KeyValue; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.jmeter.JMeterService; -import io.metersphere.api.parse.old.JmeterDocumentParser; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiScenarioMapper; import io.metersphere.base.mapper.ApiScenarioReportMapper; @@ -39,7 +38,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.jmeter.save.SaveService; import org.apache.jorphan.collections.HashTree; import org.apache.jorphan.collections.ListedHashTree; import org.springframework.stereotype.Service; @@ -47,8 +45,6 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.web.multipart.MultipartFile; import javax.annotation.Resource; -import java.io.ByteArrayInputStream; -import java.io.InputStream; import java.util.*; import java.util.stream.Collectors; @@ -353,25 +349,14 @@ public class ApiAutomationService { } catch (Exception ex) { LogUtil.error(ex.getMessage()); } - testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); + testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); String runMode = ApiRunMode.SCENARIO.name(); if (StringUtils.isNotBlank(request.getRunMode()) && StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) { runMode = ApiRunMode.SCENARIO_PLAN.name(); } // 调用执行方法 - String jmx = testPlan.getJmx(jmeterHashTree); - byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes()); - InputStream is = new ByteArrayInputStream(bytes); - try { - Object scriptWrapper = SaveService.loadElement(is); - HashTree hashTree = JMeterService.getHashTree(scriptWrapper); - // 调用执行方法 - jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode); - } catch (Exception e) { - e.printStackTrace(); - LogUtil.error(e.getMessage()); - } + jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode); return request.getId(); } @@ -395,20 +380,10 @@ public class ApiAutomationService { config.setConfig(envConfig); HashTree hashTree = request.getTestElement().generateHashTree(config); // 调用执行方法 - String jmx = request.getTestElement().getJmx(hashTree); createScenarioReport(request.getId(), request.getScenarioId(), request.getScenarioName(), ReportTriggerMode.MANUAL.name(), request.getExecuteType(), request.getProjectId(), SessionUtils.getUserId()); - byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes()); - InputStream is = new ByteArrayInputStream(bytes); - try { - Object scriptWrapper = SaveService.loadElement(is); - HashTree jmeterHashTree = JMeterService.getHashTree(scriptWrapper); - // 调用执行方法 - jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), ApiRunMode.SCENARIO.name()); - } catch (Exception e) { - e.printStackTrace(); - LogUtil.error(e.getMessage()); - } + // 调用执行方法 + jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), ApiRunMode.SCENARIO.name()); return request.getId(); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java index 4dc8b340d0..da1c4eed4a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionService.java @@ -14,7 +14,6 @@ import io.metersphere.api.jmeter.JMeterService; import io.metersphere.api.jmeter.TestResult; import io.metersphere.api.parse.ApiImportParser; import io.metersphere.api.parse.ApiImportParserFactory; -import io.metersphere.api.parse.old.JmeterDocumentParser; import io.metersphere.base.domain.*; import io.metersphere.base.mapper.ApiDefinitionMapper; import io.metersphere.base.mapper.ApiTestFileMapper; @@ -35,7 +34,6 @@ import org.apache.commons.lang3.StringUtils; import org.apache.ibatis.session.ExecutorType; import org.apache.ibatis.session.SqlSession; import org.apache.ibatis.session.SqlSessionFactory; -import org.apache.jmeter.save.SaveService; import org.apache.jorphan.collections.HashTree; import org.aspectj.util.FileUtil; import org.springframework.stereotype.Service; @@ -309,17 +307,7 @@ public class ApiDefinitionService { runMode = ApiRunMode.API_PLAN.name(); } // 调用执行方法 - String jmx = request.getTestElement().getJmx(hashTree); - byte[] bytes = JmeterDocumentParser.parse(jmx.getBytes()); - InputStream is = new ByteArrayInputStream(bytes); - try { - Object scriptWrapper = SaveService.loadElement(is); - HashTree testPlan = JMeterService.getHashTree(scriptWrapper); - // 调用执行方法 - jMeterService.runDefinition(request.getId(), testPlan, request.getReportId(), runMode); - } catch (Exception e) { - e.printStackTrace(); - } + jMeterService.runDefinition(request.getId(), hashTree, request.getReportId(), runMode); return request.getId(); }