From 7d33f66a26e976169f65450756378b4b2357e9be Mon Sep 17 00:00:00 2001 From: wenyann <64353056+wenyann@users.noreply.github.com> Date: Wed, 13 Jan 2021 10:40:46 +0800 Subject: [PATCH 1/9] =?UTF-8?q?fix:=20=E6=B7=BB=E5=8A=A0=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E7=8E=AF=E5=A2=83=EF=BC=8C=E6=9F=A5=E8=AF=A2=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=AE=A1=E5=88=92=E4=B8=8B=E6=80=A7=E8=83=BD=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../api/dto/definition/RunCaseRequest.java | 6 +++ .../request/sampler/MsHTTPSamplerProxy.java | 7 +++- .../api/jmeter/APIBackendListenerClient.java | 2 +- .../api/service/ApiAutomationService.java | 3 ++ .../api/service/ApiTestCaseService.java | 38 +++++++++++++------ .../base/mapper/ext/ExtTestCaseMapper.xml | 29 ++++++++++---- .../mapper/ext/ExtTestPlanTestCaseMapper.xml | 9 +++++ 7 files changed, 72 insertions(+), 22 deletions(-) diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/RunCaseRequest.java b/backend/src/main/java/io/metersphere/api/dto/definition/RunCaseRequest.java index 993509e950..6ab3648046 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/RunCaseRequest.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/RunCaseRequest.java @@ -10,4 +10,10 @@ public class RunCaseRequest { private String caseId; private String reportId; + + private String runMode; + + private String environmentId; + + private String testPlanId; } 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 bde8cb14cb..0ae7c5a4b6 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 @@ -109,8 +109,11 @@ public class MsHTTPSamplerProxy extends MsTestElement { sampler.setFollowRedirects(this.isFollowRedirects()); sampler.setUseKeepAlive(true); sampler.setDoMultipart(this.isDoMultipartPost()); - - config.setConfig(getEnvironmentConfig(useEnvironment)); + if(config != null && config.getConfig() != null){ + config.setConfig(config.getConfig()); + }else{ + config.setConfig(getEnvironmentConfig(useEnvironment)); + } // 添加环境中的公共变量 Arguments arguments = this.addArguments(config); diff --git a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java index feb3e61052..e5e92f9e37 100644 --- a/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java +++ b/backend/src/main/java/io/metersphere/api/jmeter/APIBackendListenerClient.java @@ -165,7 +165,7 @@ public class APIBackendListenerClient extends AbstractBackendListenerClient impl apiReportService.complete(testResult, report); } else if (StringUtils.equals(this.runMode, ApiRunMode.DELIMIT.name())) { // 调试操作,不需要存储结果 - if (StringUtils.isBlank(debugReportId)) { + if (!StringUtils.isBlank(debugReportId)) { apiDefinitionService.addResult(testResult); } else { apiDefinitionService.addResult(testResult); 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 10a92ada82..d3a5a627fb 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -395,6 +395,9 @@ public class ApiAutomationService { if (StringUtils.isNotBlank(request.getRunMode()) && StringUtils.equals(request.getRunMode(), ApiRunMode.SCENARIO_PLAN.name())) { runMode = ApiRunMode.SCENARIO_PLAN.name(); } + if (StringUtils.isNotBlank(request.getRunMode()) && StringUtils.equals(request.getRunMode(), ApiRunMode.DELIMIT.name())) { + runMode = ApiRunMode.DELIMIT.name(); + } // 调用执行方法 jMeterService.runDefinition(request.getId(), jmeterHashTree, request.getReportId(), runMode); return request.getId(); 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 bf62372aba..f6de4b0a81 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -13,13 +13,11 @@ import io.metersphere.api.dto.definition.request.MsTestPlan; import io.metersphere.api.dto.definition.request.MsThreadGroup; import io.metersphere.api.dto.definition.request.ParameterConfig; import io.metersphere.api.dto.definition.request.sampler.MsHTTPSamplerProxy; +import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; import io.metersphere.api.dto.scenario.request.RequestType; import io.metersphere.api.jmeter.JMeterService; import io.metersphere.base.domain.*; -import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; -import io.metersphere.base.mapper.ApiDefinitionMapper; -import io.metersphere.base.mapper.ApiTestCaseMapper; -import io.metersphere.base.mapper.ApiTestFileMapper; +import io.metersphere.base.mapper.*; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanApiCaseMapper; @@ -73,7 +71,9 @@ public class ApiTestCaseService { @Resource private JMeterService jMeterService; @Resource - private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; + private ApiDefinitionExecResultMapper apiDefinitionExecResultMapper; + @Resource + TestPlanApiCaseMapper testPlanApiCaseMapper; private static final String BODY_FILE_DIR = "/opt/metersphere/data/body"; @@ -84,10 +84,10 @@ public class ApiTestCaseService { public List listSimple(ApiTestCaseRequest request) { request.setOrders(ServiceUtils.getDefaultOrder(request.getOrders())); - if(request.isSelectThisWeedData()){ + if (request.isSelectThisWeedData()) { Map weekFirstTimeAndLastTime = DateUtils.getWeedFirstTimeAndLastTime(new Date()); Date weekFirstTime = weekFirstTimeAndLastTime.get("firstTime"); - if(weekFirstTime!=null){ + if (weekFirstTime != null) { request.setCreateTime(weekFirstTime.getTime()); } } @@ -434,6 +434,7 @@ public class ApiTestCaseService { public String run(RunCaseRequest request) { ApiTestCaseWithBLOBs testCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(request.getCaseId()); + // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) { try { @@ -441,7 +442,16 @@ public class ApiTestCaseService { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); MsTestElement element = mapper.readValue(testCaseWithBLOBs.getRequest(), new TypeReference() { }); - element.setName(request.getCaseId()); + if(StringUtils.isBlank(request.getEnvironmentId())){ + TestPlanApiCaseExample example = new TestPlanApiCaseExample(); + example.createCriteria().andTestPlanIdEqualTo(request.getTestPlanId()).andApiCaseIdEqualTo(request.getCaseId()); + List list=testPlanApiCaseMapper.selectByExample(example); + request.setEnvironmentId(list.get(0).getEnvironmentId()); + element.setName(list.get(0).getId()); + }else{ + element.setName(request.getCaseId()); + } + // 测试计划 MsTestPlan testPlan = new MsTestPlan(); testPlan.setHashTree(new LinkedList<>()); @@ -456,10 +466,14 @@ public class ApiTestCaseService { hashTrees.add(element); group.setHashTree(hashTrees); testPlan.getHashTree().add(group); - - testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), new ParameterConfig()); - - String runMode = ApiRunMode.DELIMIT.name(); + ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); + ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId()); + ParameterConfig parameterConfig=new ParameterConfig(); + if (environment != null && environment.getConfig() != null) { + parameterConfig.setConfig( JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class)); + } + testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), parameterConfig); + String runMode = request.getRunMode(); // 调用执行方法 jMeterService.runDefinition(request.getReportId(), jmeterHashTree, request.getReportId(), runMode); diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml index e37e83af06..21a2253880 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestCaseMapper.xml @@ -105,7 +105,8 @@ @@ -378,7 +389,11 @@