diff --git a/backend/src/main/java/io/metersphere/api/dto/definition/request/extract/MsExtract.java b/backend/src/main/java/io/metersphere/api/dto/definition/request/extract/MsExtract.java index 9d7a0dc552..295890e786 100644 --- a/backend/src/main/java/io/metersphere/api/dto/definition/request/extract/MsExtract.java +++ b/backend/src/main/java/io/metersphere/api/dto/definition/request/extract/MsExtract.java @@ -6,6 +6,7 @@ import io.metersphere.api.dto.definition.request.ParameterConfig; import lombok.Data; import lombok.EqualsAndHashCode; import org.apache.commons.collections.CollectionUtils; +import org.apache.commons.lang3.StringUtils; import org.apache.jmeter.extractor.JSR223PostProcessor; import org.apache.jmeter.extractor.RegexExtractor; import org.apache.jmeter.extractor.XPath2Extractor; @@ -57,6 +58,8 @@ public class MsExtract extends MsTestElement { JSR223PostProcessor shell = new JSR223PostProcessor(); shell.setEnabled(true); shell.setName(this.getName()); + shell.setProperty(TestElement.TEST_CLASS, JSR223PostProcessor.class.getName()); + shell.setProperty(TestElement.GUI_CLASS, SaveService.aliasToClass("TestBeanGUI")); shell.setProperty("script", "io.metersphere.api.jmeter.JMeterVars.addVars(prev.hashCode(),vars," + "\"" + extract.toString() + "\"" + ");"); samplerHashTree.add(shell); } diff --git a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java index 3689eca69b..0b20990141 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiDefinitionExecResultService.java @@ -5,8 +5,10 @@ import io.metersphere.api.dto.datacount.ExecutedCaseInfoResult; import io.metersphere.api.jmeter.TestResult; import io.metersphere.base.domain.ApiDefinitionExecResult; import io.metersphere.base.domain.ApiDefinitionExecResultExample; +import io.metersphere.base.domain.ApiTestCaseWithBLOBs; import io.metersphere.base.domain.TestPlanApiCase; import io.metersphere.base.mapper.ApiDefinitionExecResultMapper; +import io.metersphere.base.mapper.ApiTestCaseMapper; import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper; import io.metersphere.commons.constants.ApiRunMode; import io.metersphere.commons.utils.DateUtils; @@ -15,15 +17,15 @@ import io.metersphere.track.dto.TestPlanDTO; import io.metersphere.track.request.testcase.QueryTestPlanRequest; import io.metersphere.track.service.TestPlanApiCaseService; import io.metersphere.track.service.TestPlanService; +import org.apache.commons.collections4.CollectionUtils; 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.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; -import org.springframework.util.CollectionUtils; import javax.annotation.Resource; -import java.util.List; -import java.util.Objects; -import java.util.UUID; import java.util.*; @Service @@ -37,32 +39,48 @@ public class ApiDefinitionExecResultService { private TestPlanApiCaseService testPlanApiCaseService; @Resource private TestPlanService testPlanService; + @Resource + private ApiTestCaseMapper apiTestCaseMapper; + @Resource + SqlSessionFactory sqlSessionFactory; public void saveApiResult(TestResult result, String type) { - result.getScenarios().get(0).getRequestResults().forEach(item -> { - ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); - saveResult.setId(UUID.randomUUID().toString()); - saveResult.setCreateTime(System.currentTimeMillis()); - saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); - saveResult.setName(item.getName()); - saveResult.setResourceId(item.getName()); - saveResult.setContent(JSON.toJSONString(item)); - saveResult.setStartTime(item.getStartTime()); - String status = item.isSuccess() ? "success" : "error"; - saveResult.setEndTime(item.getResponseResult().getResponseTime()); - saveResult.setType(type); - saveResult.setStatus(status); - if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) { - testPlanApiCaseService.setExecResult(item.getName(), status); - } - apiDefinitionExecResultMapper.insert(saveResult); - }); + if (CollectionUtils.isNotEmpty(result.getScenarios())) { + SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH); + ApiDefinitionExecResultMapper definitionExecResultMapper = sqlSession.getMapper(ApiDefinitionExecResultMapper.class); + result.getScenarios().get(0).getRequestResults().forEach(item -> { + ApiDefinitionExecResult saveResult = new ApiDefinitionExecResult(); + saveResult.setId(UUID.randomUUID().toString()); + saveResult.setCreateTime(System.currentTimeMillis()); + saveResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); + saveResult.setName(item.getName()); + saveResult.setResourceId(item.getName()); + saveResult.setContent(JSON.toJSONString(item)); + saveResult.setStartTime(item.getStartTime()); + String status = item.isSuccess() ? "success" : "error"; + saveResult.setEndTime(item.getResponseResult().getResponseTime()); + saveResult.setType(type); + saveResult.setStatus(status); + if (StringUtils.equals(type, ApiRunMode.API_PLAN.name())) { + testPlanApiCaseService.setExecResult(item.getName(), status); + } + // 更新用例最后执行结果 + ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = new ApiTestCaseWithBLOBs(); + apiTestCaseWithBLOBs.setId(saveResult.getResourceId()); + apiTestCaseWithBLOBs.setLastResultId(saveResult.getId()); + + apiTestCaseMapper.updateByPrimaryKeySelective(apiTestCaseWithBLOBs); + definitionExecResultMapper.insert(saveResult); + }); + sqlSession.flushStatements(); + } } /** * 定时任务触发的保存逻辑 - * 定时任务时,userID要改为定时任务中的用户 + * 定时任务时,userID要改为定时任务中的用户 + * * @param result * @param type */ @@ -87,7 +105,7 @@ public class ApiDefinitionExecResultService { userID = scheduleCreateUser; apiCase.setStatus(status); testPlanApiCaseService.updateByPrimaryKeySelective(apiCase); - }else { + } else { userID = Objects.requireNonNull(SessionUtils.getUser()).getId(); testPlanApiCaseService.setExecResult(item.getName(), status); } @@ -144,26 +162,26 @@ public class ApiDefinitionExecResultService { if (startTime == null) { return new ArrayList<>(0); } else { - Listlist = extApiDefinitionExecResultMapper.findFaliureCaseInfoByProjectIDAndExecuteTimeAndLimitNumber(projectId, startTime.getTime()); + List list = extApiDefinitionExecResultMapper.findFaliureCaseInfoByProjectIDAndExecuteTimeAndLimitNumber(projectId, startTime.getTime()); List returnList = new ArrayList<>(limitNumber); - for(int i = 0;i 提供前台做超链接 ExecutedCaseInfoResult item = list.get(i); QueryTestPlanRequest planRequest = new QueryTestPlanRequest(); planRequest.setProjectId(projectId); - if("scenario".equals(item.getCaseType())){ + if ("scenario".equals(item.getCaseType())) { planRequest.setScenarioId(item.getTestCaseID()); - }else if("apiCase".equals(item.getCaseType())){ + } else if ("apiCase".equals(item.getCaseType())) { planRequest.setApiId(item.getTestCaseID()); } List dtoList = testPlanService.selectTestPlanByRelevancy(planRequest); item.setTestPlanDTOList(dtoList); returnList.add(item); - }else { + } else { break; } } 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 471d8513d0..c151f7c1f7 100644 --- a/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java +++ b/backend/src/main/java/io/metersphere/api/service/ApiTestCaseService.java @@ -222,7 +222,6 @@ public class ApiTestCaseService { test.setUpdateUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); test.setProjectId(request.getProjectId()); test.setRequest(JSONObject.toJSONString(request.getRequest())); - test.setResponse(JSONObject.toJSONString(request.getResponse())); test.setPriority(request.getPriority()); test.setUpdateTime(System.currentTimeMillis()); test.setDescription(request.getDescription()); @@ -242,7 +241,6 @@ public class ApiTestCaseService { test.setUpdateUserId(Objects.requireNonNull(SessionUtils.getUser()).getId()); test.setProjectId(request.getProjectId()); test.setRequest(JSONObject.toJSONString(request.getRequest())); - test.setResponse(JSONObject.toJSONString(request.getResponse())); test.setCreateTime(System.currentTimeMillis()); test.setPriority(request.getPriority()); test.setUpdateTime(System.currentTimeMillis()); @@ -438,7 +436,7 @@ public class ApiTestCaseService { // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (testCaseWithBLOBs != null && StringUtils.isNotEmpty(testCaseWithBLOBs.getRequest())) { try { - HashTree jmeterHashTree = this.generateHashTree(request,testCaseWithBLOBs); + HashTree jmeterHashTree = this.generateHashTree(request, testCaseWithBLOBs); String runMode = ApiRunMode.DEFINITION.name(); // 调用执行方法 jMeterService.runDefinition(request.getReportId(), jmeterHashTree, request.getReportId(), runMode); @@ -449,7 +447,8 @@ public class ApiTestCaseService { } return request.getReportId(); } - public String run(ApiTestCaseWithBLOBs apiCaseBolbs,String id,String debugReportId,String testPlanID,String runMode) { + + public String run(ApiTestCaseWithBLOBs apiCaseBolbs, String id, String debugReportId, String testPlanID, String runMode) { // 多态JSON普通转换会丢失内容,需要通过 ObjectMapper 获取 if (apiCaseBolbs != null && StringUtils.isNotEmpty(apiCaseBolbs.getRequest())) { try { @@ -457,7 +456,7 @@ public class ApiTestCaseService { RunCaseRequest request = new RunCaseRequest(); request.setCaseId(apiTestCase.getId()); request.setTestPlanId(testPlanID); - HashTree jmeterHashTree = this.generateHashTree(request,apiCaseBolbs); + HashTree jmeterHashTree = this.generateHashTree(request, apiCaseBolbs); // 调用执行方法 jMeterService.runDefinition(id, jmeterHashTree, debugReportId, runMode); @@ -467,18 +466,19 @@ public class ApiTestCaseService { } return id; } - public HashTree generateHashTree(RunCaseRequest request,ApiTestCaseWithBLOBs testCaseWithBLOBs) throws Exception { + + public HashTree generateHashTree(RunCaseRequest request, ApiTestCaseWithBLOBs testCaseWithBLOBs) throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false); MsTestElement element = mapper.readValue(testCaseWithBLOBs.getRequest(), new TypeReference() { }); - if(StringUtils.isBlank(request.getEnvironmentId())){ + if (StringUtils.isBlank(request.getEnvironmentId())) { TestPlanApiCaseExample example = new TestPlanApiCaseExample(); example.createCriteria().andTestPlanIdEqualTo(request.getTestPlanId()).andApiCaseIdEqualTo(request.getCaseId()); - List list=testPlanApiCaseMapper.selectByExample(example); + List list = testPlanApiCaseMapper.selectByExample(example); request.setEnvironmentId(list.get(0).getEnvironmentId()); element.setName(list.get(0).getId()); - }else{ + } else { element.setName(request.getCaseId()); } @@ -498,19 +498,19 @@ public class ApiTestCaseService { testPlan.getHashTree().add(group); ApiTestEnvironmentService environmentService = CommonBeanFactory.getBean(ApiTestEnvironmentService.class); ApiTestEnvironmentWithBLOBs environment = environmentService.get(request.getEnvironmentId()); - ParameterConfig parameterConfig=new ParameterConfig(); + ParameterConfig parameterConfig = new ParameterConfig(); if (environment != null && environment.getConfig() != null) { - parameterConfig.setConfig( JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class)); + parameterConfig.setConfig(JSONObject.parseObject(environment.getConfig(), EnvironmentConfig.class)); } testPlan.toHashTree(jmeterHashTree, testPlan.getHashTree(), parameterConfig); return jmeterHashTree; } - public String getExecResult(String id){ + public String getExecResult(String id) { ApiDefinitionExecResultExample apidefinitionexecresultexample = new ApiDefinitionExecResultExample(); ApiDefinitionExecResultExample.Criteria criteria = apidefinitionexecresultexample.createCriteria(); criteria.andResourceIdEqualTo(id); - String status=apiDefinitionExecResultMapper.selectByExample(apidefinitionexecresultexample).get(0).getStatus(); + String status = apiDefinitionExecResultMapper.selectByExample(apidefinitionexecresultexample).get(0).getStatus(); return status; } } diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiTestCase.java b/backend/src/main/java/io/metersphere/base/domain/ApiTestCase.java index 3b52eb1481..26257a63b7 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiTestCase.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiTestCase.java @@ -28,5 +28,7 @@ public class ApiTestCase implements Serializable { private String tags; + private String lastResultId; + private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseExample.java b/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseExample.java index 75d8f72d6d..845ec0d060 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseExample.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseExample.java @@ -843,6 +843,76 @@ public class ApiTestCaseExample { addCriterion("tags not between", value1, value2, "tags"); return (Criteria) this; } + + public Criteria andLastResultIdIsNull() { + addCriterion("last_result_id is null"); + return (Criteria) this; + } + + public Criteria andLastResultIdIsNotNull() { + addCriterion("last_result_id is not null"); + return (Criteria) this; + } + + public Criteria andLastResultIdEqualTo(String value) { + addCriterion("last_result_id =", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdNotEqualTo(String value) { + addCriterion("last_result_id <>", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdGreaterThan(String value) { + addCriterion("last_result_id >", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdGreaterThanOrEqualTo(String value) { + addCriterion("last_result_id >=", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdLessThan(String value) { + addCriterion("last_result_id <", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdLessThanOrEqualTo(String value) { + addCriterion("last_result_id <=", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdLike(String value) { + addCriterion("last_result_id like", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdNotLike(String value) { + addCriterion("last_result_id not like", value, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdIn(List values) { + addCriterion("last_result_id in", values, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdNotIn(List values) { + addCriterion("last_result_id not in", values, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdBetween(String value1, String value2) { + addCriterion("last_result_id between", value1, value2, "lastResultId"); + return (Criteria) this; + } + + public Criteria andLastResultIdNotBetween(String value1, String value2) { + addCriterion("last_result_id not between", value1, value2, "lastResultId"); + return (Criteria) this; + } } public static class Criteria extends GeneratedCriteria { diff --git a/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseWithBLOBs.java b/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseWithBLOBs.java index 04611f6d57..f045c1949e 100644 --- a/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseWithBLOBs.java +++ b/backend/src/main/java/io/metersphere/base/domain/ApiTestCaseWithBLOBs.java @@ -13,7 +13,5 @@ public class ApiTestCaseWithBLOBs extends ApiTestCase implements Serializable { private String request; - private String response; - private static final long serialVersionUID = 1L; } \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ApiTestCaseMapper.xml index df98be023b..5e294ca291 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ApiTestCaseMapper.xml @@ -13,11 +13,11 @@ + - @@ -79,10 +79,10 @@ id, project_id, `name`, priority, api_definition_id, create_user_id, update_user_id, - create_time, update_time, num, tags + create_time, update_time, num, tags, last_result_id - description, request, response + description, request @@ -277,15 +277,15 @@ tags = #{record.tags,jdbcType=VARCHAR}, + + last_result_id = #{record.lastResultId,jdbcType=VARCHAR}, + description = #{record.description,jdbcType=LONGVARCHAR}, request = #{record.request,jdbcType=LONGVARCHAR}, - - response = #{record.response,jdbcType=LONGVARCHAR}, - @@ -304,9 +304,9 @@ update_time = #{record.updateTime,jdbcType=BIGINT}, num = #{record.num,jdbcType=INTEGER}, tags = #{record.tags,jdbcType=VARCHAR}, + last_result_id = #{record.lastResultId,jdbcType=VARCHAR}, description = #{record.description,jdbcType=LONGVARCHAR}, - request = #{record.request,jdbcType=LONGVARCHAR}, - response = #{record.response,jdbcType=LONGVARCHAR} + request = #{record.request,jdbcType=LONGVARCHAR} @@ -323,7 +323,8 @@ create_time = #{record.createTime,jdbcType=BIGINT}, update_time = #{record.updateTime,jdbcType=BIGINT}, num = #{record.num,jdbcType=INTEGER}, - tags = #{record.tags,jdbcType=VARCHAR} + tags = #{record.tags,jdbcType=VARCHAR}, + last_result_id = #{record.lastResultId,jdbcType=VARCHAR} @@ -361,15 +362,15 @@ tags = #{tags,jdbcType=VARCHAR}, + + last_result_id = #{lastResultId,jdbcType=VARCHAR}, + description = #{description,jdbcType=LONGVARCHAR}, request = #{request,jdbcType=LONGVARCHAR}, - - response = #{response,jdbcType=LONGVARCHAR}, - where id = #{id,jdbcType=VARCHAR} @@ -385,9 +386,9 @@ update_time = #{updateTime,jdbcType=BIGINT}, num = #{num,jdbcType=INTEGER}, tags = #{tags,jdbcType=VARCHAR}, + last_result_id = #{lastResultId,jdbcType=VARCHAR}, description = #{description,jdbcType=LONGVARCHAR}, - request = #{request,jdbcType=LONGVARCHAR}, - response = #{response,jdbcType=LONGVARCHAR} + request = #{request,jdbcType=LONGVARCHAR} where id = #{id,jdbcType=VARCHAR} @@ -401,7 +402,8 @@ create_time = #{createTime,jdbcType=BIGINT}, update_time = #{updateTime,jdbcType=BIGINT}, num = #{num,jdbcType=INTEGER}, - tags = #{tags,jdbcType=VARCHAR} + tags = #{tags,jdbcType=VARCHAR}, + last_result_id = #{lastResultId,jdbcType=VARCHAR} where id = #{id,jdbcType=VARCHAR} \ No newline at end of file diff --git a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml index 7471e449ca..1ee90b76b7 100644 --- a/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml +++ b/backend/src/main/java/io/metersphere/base/mapper/ext/ExtApiTestCaseMapper.xml @@ -15,7 +15,6 @@ - @@ -190,39 +189,18 @@