diff --git a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioDTO.java b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioDTO.java index 358ca88ea3..b1f9e6e044 100644 --- a/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioDTO.java +++ b/backend/src/main/java/io/metersphere/api/dto/automation/ApiScenarioDTO.java @@ -13,4 +13,9 @@ public class ApiScenarioDTO extends ApiScenarioWithBLOBs { private String projectName; private String userName; private List tagNames; + + /** + * 场景跨项目ID + */ + private List projectIds; } diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java index 4f321bdca7..77e2c687db 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/MsScenario.java @@ -15,6 +15,7 @@ import io.metersphere.api.service.ApiTestEnvironmentService; import io.metersphere.base.domain.ApiScenarioWithBLOBs; import io.metersphere.base.domain.ApiTestEnvironmentWithBLOBs; import io.metersphere.commons.utils.CommonBeanFactory; +import io.metersphere.commons.utils.ScriptEngineUtils; import io.metersphere.commons.utils.SessionUtils; import lombok.Data; import lombok.EqualsAndHashCode; @@ -132,7 +133,7 @@ public class MsScenario extends MsTestElement { // 场景变量和环境变量 Arguments arguments = arguments(config); if (arguments != null) { - tree.add(arguments); + tree.add(config.valueSupposeMock(arguments)); } this.addCsvDataSet(tree, variables); this.addCounter(tree, variables); diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java index e5117db258..acf0e3313d 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/ParameterConfig.java @@ -2,7 +2,9 @@ package io.metersphere.api.dto.definition.request; import io.metersphere.api.dto.definition.request.variable.ScenarioVariable; import io.metersphere.api.dto.scenario.environment.EnvironmentConfig; +import io.metersphere.commons.utils.ScriptEngineUtils; import lombok.Data; +import org.apache.jmeter.config.Arguments; import java.util.List; import java.util.Map; @@ -37,4 +39,13 @@ public class ParameterConfig { } return false; } + + static public Arguments valueSupposeMock(Arguments arguments) { + for(int i = 0; i < arguments.getArguments().size(); ++i) { + String argValue = arguments.getArgument(i).getValue(); + arguments.getArgument(i).setValue(ScriptEngineUtils.calculate(argValue)); + } + return arguments; + } + } \ No newline at end of file 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 cf61040421..128685a0e8 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 @@ -122,7 +122,7 @@ public class MsHTTPSamplerProxy extends MsTestElement { // 添加环境中的公共变量 Arguments arguments = this.addArguments(config); if (arguments != null) { - tree.add(arguments); + tree.add(config.valueSupposeMock(arguments)); } try { if (config.isEffective(this.getProjectId())) { 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 88fc2242a2..5c99b63cd1 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiAutomationService.java @@ -654,24 +654,29 @@ public class ApiAutomationService { } public void relevance(ApiCaseRelevanceRequest request) { - List ids = request.getSelectIds(); - if (CollectionUtils.isEmpty(ids)) { + Map> mapping = request.getMapping(); + Map envMap = request.getEnvMap(); + Set set = mapping.keySet(); + if (set.isEmpty()) { return; } - List apiScenarios = selectByIds(ids); - SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); - - ExtTestPlanScenarioCaseMapper batchMapper = sqlSession.getMapper(ExtTestPlanScenarioCaseMapper.class); - apiScenarios.forEach(scenario -> { + set.forEach(id -> { + Map newEnvMap = new HashMap<>(16); + if (envMap != null && !envMap.isEmpty()) { + List list = mapping.get(id); + list.forEach(l -> { + newEnvMap.put(l, envMap.get(l)); + }); + } TestPlanApiScenario testPlanApiScenario = new TestPlanApiScenario(); testPlanApiScenario.setId(UUID.randomUUID().toString()); - testPlanApiScenario.setApiScenarioId(scenario.getId()); + testPlanApiScenario.setApiScenarioId(id); testPlanApiScenario.setTestPlanId(request.getPlanId()); testPlanApiScenario.setCreateTime(System.currentTimeMillis()); testPlanApiScenario.setUpdateTime(System.currentTimeMillis()); - batchMapper.insertIfNotExists(testPlanApiScenario); + testPlanApiScenario.setEnvironment(JSON.toJSONString(newEnvMap)); + testPlanApiScenarioMapper.insert(testPlanApiScenario); }); - sqlSession.flushStatements(); } public List selectByIds(List ids) { 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 fbad14793a..ee452d424a 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -22,6 +22,7 @@ import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.base.mapper.ext.ExtApiTestCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanApiCaseMapper; import io.metersphere.base.mapper.ext.ExtTestPlanTestCaseMapper; +import io.metersphere.commons.constants.TestPlanStatus; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.utils.*; import io.metersphere.i18n.Translator; @@ -49,6 +50,8 @@ import java.util.stream.Collectors; @Service @Transactional(rollbackFor = Exception.class) public class ApiTestCaseService { + @Resource + TestPlanMapper testPlanMapper; @Resource private ApiTestCaseMapper apiTestCaseMapper; @Resource @@ -330,6 +333,14 @@ public class ApiTestCaseService { testPlanApiCase.setUpdateTime(System.currentTimeMillis()); batchMapper.insertIfNotExists(testPlanApiCase); }); + TestPlan testPlan = testPlanMapper.selectByPrimaryKey(request.getPlanId()); + if (StringUtils.equals(testPlan.getStatus(), TestPlanStatus.Prepare.name()) + || StringUtils.equals(testPlan.getStatus(), TestPlanStatus.Completed.name())) { + testPlan.setStatus(TestPlanStatus.Underway.name()); + testPlan.setActualStartTime(System.currentTimeMillis()); // 将状态更新为进行中时,开始时间也要更新 + testPlan.setActualEndTime(null); + testPlanMapper.updateByPrimaryKey(testPlan); + } sqlSession.flushStatements(); } diff --git a/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenario.java b/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenario.java index 7d347de5f9..652185c880 100644 --- a/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenario.java +++ b/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenario.java @@ -13,8 +13,6 @@ public class TestPlanApiScenario implements Serializable { private String status; - private String environmentId; - private Long createTime; private Long updateTime; @@ -25,5 +23,7 @@ public class TestPlanApiScenario implements Serializable { private String reportId; + private String environment; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenarioExample.java b/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenarioExample.java index a900880358..40d9e139f9 100644 --- a/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenarioExample.java +++ b/backend/src/main/java/io/metersphere/base/domain/TestPlanApiScenarioExample.java @@ -384,76 +384,6 @@ public class TestPlanApiScenarioExample { return (Criteria) this; } - public Criteria andEnvironmentIdIsNull() { - addCriterion("environment_id is null"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdIsNotNull() { - addCriterion("environment_id is not null"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdEqualTo(String value) { - addCriterion("environment_id =", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdNotEqualTo(String value) { - addCriterion("environment_id <>", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdGreaterThan(String value) { - addCriterion("environment_id >", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdGreaterThanOrEqualTo(String value) { - addCriterion("environment_id >=", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdLessThan(String value) { - addCriterion("environment_id <", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdLessThanOrEqualTo(String value) { - addCriterion("environment_id <=", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdLike(String value) { - addCriterion("environment_id like", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdNotLike(String value) { - addCriterion("environment_id not like", value, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdIn(List values) { - addCriterion("environment_id in", values, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdNotIn(List values) { - addCriterion("environment_id not in", values, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdBetween(String value1, String value2) { - addCriterion("environment_id between", value1, value2, "environmentId"); - return (Criteria) this; - } - - public Criteria andEnvironmentIdNotBetween(String value1, String value2) { - addCriterion("environment_id not between", value1, value2, "environmentId"); - return (Criteria) this; - } - public Criteria andCreateTimeIsNull() { addCriterion("create_time is null"); return (Criteria) this; diff --git a/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.java b/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.java index 63a39d7d6f..017d3a112e 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.java +++ b/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.java @@ -16,15 +16,21 @@ public interface TestPlanApiScenarioMapper { int insertSelective(TestPlanApiScenario record); + List selectByExampleWithBLOBs(TestPlanApiScenarioExample example); + List selectByExample(TestPlanApiScenarioExample example); TestPlanApiScenario selectByPrimaryKey(String id); int updateByExampleSelective(@Param("record") TestPlanApiScenario record, @Param("example") TestPlanApiScenarioExample example); + int updateByExampleWithBLOBs(@Param("record") TestPlanApiScenario record, @Param("example") TestPlanApiScenarioExample example); + int updateByExample(@Param("record") TestPlanApiScenario record, @Param("example") TestPlanApiScenarioExample example); int updateByPrimaryKeySelective(TestPlanApiScenario record); + int updateByPrimaryKeyWithBLOBs(TestPlanApiScenario record); + int updateByPrimaryKey(TestPlanApiScenario record); } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.xml index 08150fd137..9e6a517c29 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/TestPlanApiScenarioMapper.xml @@ -6,13 +6,15 @@ - + + + @@ -72,9 +74,28 @@ - id, test_plan_id, api_scenario_id, `status`, environment_id, create_time, update_time, - pass_rate, last_result, report_id + id, test_plan_id, api_scenario_id, `status`, create_time, update_time, pass_rate, + last_result, report_id + + environment + + - select + , + from test_plan_api_scenario where id = #{id,jdbcType=VARCHAR} @@ -107,13 +130,13 @@ insert into test_plan_api_scenario (id, test_plan_id, api_scenario_id, - `status`, environment_id, create_time, - update_time, pass_rate, last_result, - report_id) + `status`, create_time, update_time, + pass_rate, last_result, report_id, + environment) values (#{id,jdbcType=VARCHAR}, #{testPlanId,jdbcType=VARCHAR}, #{apiScenarioId,jdbcType=VARCHAR}, - #{status,jdbcType=VARCHAR}, #{environmentId,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, - #{updateTime,jdbcType=BIGINT}, #{passRate,jdbcType=VARCHAR}, #{lastResult,jdbcType=VARCHAR}, - #{reportId,jdbcType=VARCHAR}) + #{status,jdbcType=VARCHAR}, #{createTime,jdbcType=BIGINT}, #{updateTime,jdbcType=BIGINT}, + #{passRate,jdbcType=VARCHAR}, #{lastResult,jdbcType=VARCHAR}, #{reportId,jdbcType=VARCHAR}, + #{environment,jdbcType=LONGVARCHAR}) insert into test_plan_api_scenario @@ -130,9 +153,6 @@ `status`, - - environment_id, - create_time, @@ -148,6 +168,9 @@ report_id, + + environment, + @@ -162,9 +185,6 @@ #{status,jdbcType=VARCHAR}, - - #{environmentId,jdbcType=VARCHAR}, - #{createTime,jdbcType=BIGINT}, @@ -180,6 +200,9 @@ #{reportId,jdbcType=VARCHAR}, + + #{environment,jdbcType=LONGVARCHAR}, + select api_scenario.id, api_scenario.project_id, api_scenario.tags, api_scenario.user_id, api_scenario.num, + api_scenario.scenario_definition, api_scenario.api_scenario_module_id,api_scenario.module_path, api_scenario.name, api_scenario.level, api_scenario.status, api_scenario.principal, api_scenario.step_total, api_scenario.follow_people, api_scenario.last_result,api_scenario.pass_rate,api_scenario.report_id, diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml index 4800d42428..ac1de53c06 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtTestPlanScenarioCaseMapper.xml @@ -17,7 +17,7 @@