fix: 测试计划中功能用例关联的用例执行完自动更新功能用例的状态

This commit is contained in:
chenjianxing 2021-12-13 19:34:36 +08:00 committed by jianxing
parent ef6fe018e2
commit 7708a39416
9 changed files with 81 additions and 88 deletions

View File

@ -15,14 +15,17 @@ import io.metersphere.base.mapper.TestCaseReviewApiCaseMapper;
import io.metersphere.base.mapper.ext.ExtApiDefinitionExecResultMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.track.dto.TestPlanDTO;
import io.metersphere.track.request.testcase.QueryTestPlanRequest;
import io.metersphere.track.request.testcase.TrackCount;
import io.metersphere.track.service.TestCaseReviewApiCaseService;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanService;
import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -61,6 +64,12 @@ public class ApiDefinitionExecResultService {
@Resource
private NoticeSendService noticeSendService;
@Resource
private TestPlanTestCaseService testPlanTestCaseService;
@Resource
private ApiTestCaseService apiTestCaseService;
public ApiDefinitionExecResult getInfo(String id) {
return apiDefinitionExecResultMapper.selectByPrimaryKey(id);
}
@ -131,6 +140,9 @@ public class ApiDefinitionExecResultService {
} else {
apiDefinitionExecResultMapper.updateByPrimaryKeyWithBLOBs(saveResult);
}
updateTestCaseStates(saveResult.getResourceId());
apiDefinitionService.removeCache(result.getTestId());
if (StringUtils.isNotEmpty(result.getTestId())) {
MessageCache.caseExecResourceLock.remove(result.getTestId());
@ -144,6 +156,7 @@ public class ApiDefinitionExecResultService {
}
}
private void sendNotice(ApiDefinitionExecResult result) {
String resourceId = result.getResourceId();
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(resourceId);
@ -348,6 +361,8 @@ public class ApiDefinitionExecResultService {
testCaseReviewApiCaseService.setExecResult(caseId, status, item.getStartTime());
}
updateTestCaseStates(caseId);
if (StringUtils.isNotEmpty(caseId)) {
apiIdResultMap.put(caseId, item.isSuccess() ? TestPlanApiExecuteStatus.SUCCESS.name() : TestPlanApiExecuteStatus.FAILD.name());
}
@ -364,6 +379,21 @@ public class ApiDefinitionExecResultService {
TestPlanReportExecuteCatch.updateTestPlanReport(testPlanReportId, caseReportMap, null);
}
/**
* 更新测试计划中, 关联接口测试的功能用例的状态
*
*/
public void updateTestCaseStates(String testPlanApiCaseId) {
try {
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(testPlanApiCaseId);
if (testPlanApiCase == null) return;
ApiTestCaseWithBLOBs apiTestCase = apiTestCaseService.get(testPlanApiCase.getApiCaseId());
testPlanTestCaseService.updateTestCaseStates(apiTestCase.getId(), apiTestCase.getName(), testPlanApiCase.getTestPlanId(), TrackCount.TESTCASE);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
public void deleteByResourceId(String resourceId) {
ApiDefinitionExecResultExample example = new ApiDefinitionExecResultExample();
example.createCriteria().andResourceIdEqualTo(resourceId);

View File

@ -22,10 +22,7 @@ import io.metersphere.base.mapper.ext.ExtApiScenarioReportDetailMapper;
import io.metersphere.base.mapper.ext.ExtApiScenarioReportMapper;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.exception.MSException;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.ServiceUtils;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.commons.utils.*;
import io.metersphere.dto.ApiReportCountDTO;
import io.metersphere.dto.NodeDTO;
import io.metersphere.dto.UserDTO;
@ -37,6 +34,8 @@ import io.metersphere.log.vo.api.ModuleReference;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.UserService;
import io.metersphere.track.request.testcase.TrackCount;
import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -46,6 +45,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Lazy;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -86,6 +86,12 @@ public class ApiScenarioReportService {
private EnvironmentGroupMapper environmentGroupMapper;
@Resource
private ApiTestEnvironmentMapper apiTestEnvironmentMapper;
@Resource
@Lazy
private TestPlanTestCaseService testPlanTestCaseService;
@Resource
@Lazy
private ApiAutomationService apiAutomationService;
public ApiScenarioReport complete(TestResult result, String runMode) {
// 更新场景
@ -269,7 +275,6 @@ public class ApiScenarioReportService {
String passRate = new DecimalFormat("0%").format((float) scenarioResult.getSuccess() / (scenarioResult.getSuccess() + scenarioResult.getError()));
testPlanApiScenario.setPassRate(passRate);
testPlanApiScenario.setReportId(report.getId());
report.setTestPlanScenarioId(testPlanApiScenario.getId());
report.setEndTime(System.currentTimeMillis());
testPlanApiScenario.setUpdateTime(report.getCreateTime());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
@ -294,6 +299,8 @@ public class ApiScenarioReportService {
// 发送通知
// sendNotice(scenario);
}
updatePlanTestCaseStates(testPlanApiScenario);
}
returnReport = report;
reportIds.add(report.getId());
@ -353,8 +360,6 @@ public class ApiScenarioReportService {
if(testPlanApiScenario != null){
//更新测试计划场景相关状态
report.setScenarioId(testPlanApiScenario.getApiScenarioId());
report.setTestPlanScenarioId(planScenarioId);
if (scenarioResult.getError() > 0) {
scenarioAndErrorMap.put(testPlanApiScenario.getId(), TestPlanApiExecuteStatus.FAILD.name());
testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name());
@ -367,6 +372,8 @@ public class ApiScenarioReportService {
testPlanApiScenario.setUpdateTime(System.currentTimeMillis());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
scenarioIdList.add(testPlanApiScenario.getApiScenarioId());
updatePlanTestCaseStates(testPlanApiScenario);
}else {
LogUtil.info("TestPlanReport_Id is null. scenario report id : ["+report.getId()+"]; planScenarioIdArr:["+report.getScenarioId()+"] DATA:"+JSON.toJSONString(scenarioResult));
}
@ -420,6 +427,19 @@ public class ApiScenarioReportService {
return lastReport;
}
/**
* 更新测试计划关联产经测试的功能用例的状态
*
*/
public void updatePlanTestCaseStates(TestPlanApiScenario testPlanApiScenario) {
try {
ApiScenarioWithBLOBs apiScenario = apiAutomationService.getApiScenario(testPlanApiScenario.getApiScenarioId());
testPlanTestCaseService.updateTestCaseStates(apiScenario.getId(), apiScenario.getName(), testPlanApiScenario.getTestPlanId(), TrackCount.AUTOMATION);
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
/**
* 批量更新状态防止重复刷新报告
*

View File

@ -9,6 +9,7 @@ import io.metersphere.base.mapper.*;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -29,6 +30,12 @@ public class RemakeReportService {
private TestCaseReviewApiCaseMapper testCaseReviewApiCaseMapper;
@Resource
private TestPlanApiScenarioMapper testPlanApiScenarioMapper;
@Resource
@Lazy
private ApiScenarioReportService apiScenarioReportService;
@Resource
@Lazy
private ApiDefinitionExecResultService apiDefinitionExecResultService;
public void remake(RunRequest runRequest, RunModeConfig config, String reportId) {
if (config != null && config.getAmassReport() != null && MessageCache.cache.get(config.getAmassReport()) != null
@ -50,6 +57,7 @@ public class RemakeReportService {
testPlanApiCase.setStatus("error");
testPlanApiCase.setUpdateTime(System.currentTimeMillis());
testPlanApiCaseMapper.updateByPrimaryKeySelective(testPlanApiCase);
apiDefinitionExecResultService.updateTestCaseStates(testPlanApiCase.getId());
}
TestCaseReviewApiCase testCaseReviewApiCase = testCaseReviewApiCaseMapper.selectByPrimaryKey(runRequest.getTestId());
if (testCaseReviewApiCase != null) {
@ -71,10 +79,10 @@ public class RemakeReportService {
testPlanApiScenario.setReportId(report.getId());
testPlanApiScenario.setUpdateTime(report.getCreateTime());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
report.setTestPlanScenarioId(testPlanApiScenario.getId());
}
apiScenarioReportMapper.updateByPrimaryKey(report);
apiScenarioReportService.updatePlanTestCaseStates(testPlanApiScenario);
}
} else if (StringUtils.equalsAny(runRequest.getRunMode(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(reportId);
@ -85,7 +93,6 @@ public class RemakeReportService {
TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(planScenarioId);
if (testPlanApiScenario != null) {
report.setScenarioId(testPlanApiScenario.getApiScenarioId());
report.setTestPlanScenarioId(planScenarioId);
report.setEndTime(System.currentTimeMillis());
testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name());
@ -95,6 +102,8 @@ public class RemakeReportService {
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
}
apiScenarioReportMapper.updateByPrimaryKeySelective(report);
apiScenarioReportService.updatePlanTestCaseStates(testPlanApiScenario);
}
} else {
ApiScenarioReport report = apiScenarioReportMapper.selectByPrimaryKey(reportId);
@ -129,7 +138,6 @@ public class RemakeReportService {
TestPlanApiScenario testPlanApiScenario = testPlanApiScenarioMapper.selectByPrimaryKey(testId);
if (testPlanApiScenario != null) {
report.setScenarioId(scenarioWithBLOBs.getId());
report.setTestPlanScenarioId(testId);
report.setEndTime(System.currentTimeMillis());
testPlanApiScenario.setLastResult(ScenarioStatus.Fail.name());
@ -137,6 +145,8 @@ public class RemakeReportService {
testPlanApiScenario.setReportId(report.getId());
testPlanApiScenario.setUpdateTime(report.getCreateTime());
testPlanApiScenarioMapper.updateByPrimaryKeySelective(testPlanApiScenario);
apiScenarioReportService.updatePlanTestCaseStates(testPlanApiScenario);
}
} else {
scenarioWithBLOBs.setLastResult("Fail");

View File

@ -3,7 +3,10 @@ package io.metersphere.api.service;
import io.metersphere.api.dto.automation.ApiTestReportVariable;
import io.metersphere.api.jmeter.TestResult;
import io.metersphere.base.domain.*;
import io.metersphere.commons.constants.*;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.constants.NoticeConstants;
import io.metersphere.commons.constants.ReportTriggerMode;
import io.metersphere.commons.constants.TriggerMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.LogUtil;
@ -12,19 +15,13 @@ import io.metersphere.i18n.Translator;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.service.SystemParameterService;
import io.metersphere.track.request.testcase.TrackCount;
import io.metersphere.track.service.TestPlanApiCaseService;
import io.metersphere.track.service.TestPlanScenarioCaseService;
import io.metersphere.track.service.TestPlanTestCaseService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@Service
@ -40,16 +37,10 @@ public class TestResultService {
private ApiTestCaseService apiTestCaseService;
@Resource
private ApiAutomationService apiAutomationService;
@Resource
private TestPlanApiCaseService testPlanApiCaseService;
@Resource
private TestPlanTestCaseService testPlanTestCaseService;
public void saveResult(TestResult testResult, String runMode, String debugReportId, String testId) {
try {
ApiTestReport report = null;
ApiTestReportVariable reportTask = null;
String planScenarioId = null;
if (StringUtils.equals(runMode, ApiRunMode.DEFINITION.name())) {
// 调试操作不需要存储结果
apiDefinitionService.addResult(testResult);
@ -118,16 +109,6 @@ public class TestResultService {
SystemParameterService systemParameterService = CommonBeanFactory.getBean(SystemParameterService.class);
assert systemParameterService != null;
testResult.setTestId(scenarioReport.getScenarioId());
planScenarioId = scenarioReport.getTestPlanScenarioId();
}
}
updateTestCaseStates(testResult, planScenarioId, runMode);
List<String> ids = testPlanTestCaseService.getTestPlanTestCaseIds(testResult.getTestId());
if (CollectionUtils.isNotEmpty(ids)) {
if (StringUtils.equals(APITestStatus.Success.name(), report.getStatus())) {
testPlanTestCaseService.updateTestCaseStates(ids, TestPlanTestCaseStatus.Pass.name());
} else {
testPlanTestCaseService.updateTestCaseStates(ids, TestPlanTestCaseStatus.Failure.name());
}
}
if (reportTask != null) {
@ -141,39 +122,6 @@ public class TestResultService {
}
}
/**
* 更新测试计划关联接口测试的功能用例的状态
*
* @param testResult
*/
private void updateTestCaseStates(TestResult testResult, String testPlanScenarioId, String runMode) {
try {
if (StringUtils.equalsAny(runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(),
ApiRunMode.JENKINS_API_PLAN.name(), ApiRunMode.SCENARIO_PLAN.name(), ApiRunMode.SCHEDULE_SCENARIO_PLAN.name(), ApiRunMode.JENKINS_SCENARIO_PLAN.name())) {
testResult.getScenarios().forEach(scenarioResult -> {
if (scenarioResult != null && CollectionUtils.isNotEmpty(scenarioResult.getRequestResults())) {
scenarioResult.getRequestResults().forEach(item -> {
if (StringUtils.equalsAny(runMode, ApiRunMode.API_PLAN.name(), ApiRunMode.SCHEDULE_API_PLAN.name(),
ApiRunMode.JENKINS_API_PLAN.name())) {
TestPlanApiCase testPlanApiCase = testPlanApiCaseService.getById(item.getName());
ApiTestCaseWithBLOBs apiTestCase = apiTestCaseService.get(testPlanApiCase.getApiCaseId());
testPlanTestCaseService.updateTestCaseStates(apiTestCase.getId(), apiTestCase.getName(), testPlanApiCase.getTestPlanId(), TrackCount.TESTCASE);
} else {
TestPlanScenarioCaseService testPlanScenarioCaseService = CommonBeanFactory.getBean(TestPlanScenarioCaseService.class);
TestPlanApiScenario testPlanApiScenario = testPlanScenarioCaseService.get(testPlanScenarioId);
ApiScenarioWithBLOBs apiScenario = apiAutomationService.getApiScenario(testPlanApiScenario.getApiScenarioId());
testPlanTestCaseService.updateTestCaseStates(apiScenario.getId(), apiScenario.getName(), testPlanApiScenario.getTestPlanId(), TrackCount.AUTOMATION);
}
});
}
});
}
} catch (Exception e) {
LogUtil.error(e.getMessage(), e);
}
}
private void sendTask(ApiTestReportVariable report, TestResult testResult) {
if (report == null) {
return;

View File

@ -34,8 +34,6 @@ public class ApiScenarioReport implements Serializable {
private String description;
private String testPlanScenarioId;
private Long endTime;
private static final long serialVersionUID = 1L;

View File

@ -34,8 +34,6 @@ public interface ExtTestPlanTestCaseMapper {
int updateTestCaseStates(@Param("ids") List<String> ids, @Param("reportStatus") String reportStatus);
List<String> getTestPlanTestCaseIds(String testId);
/**
* 根据项目 ids 查询 TestPlanCaseDTO 列表
* @param ids project id list

View File

@ -359,15 +359,6 @@
from test_plan_test_case
where plan_id = #{planId}
</select>
<select id="getTestPlanTestCaseIds" resultType="java.lang.String">
select c.id from test_case a left join test_plan_test_case c on a.id=c.case_id
<where>
<if test="testId!=null">
and a.test_id=#{testId}
</if>
</where>
</select>
<select id="get" resultType="io.metersphere.track.dto.TestPlanCaseDTO">
select test_case.remark,
test_plan_test_case.id as id,

View File

@ -206,6 +206,12 @@ public class TestPlanApiCaseService {
return testPlanApiCaseMapper.selectByExample(example);
}
public List<TestPlanApiCase> getCases(String planId) {
TestPlanApiCaseExample example = new TestPlanApiCaseExample();
example.createCriteria().andTestPlanIdEqualTo(planId);
return testPlanApiCaseMapper.selectByExample(example);
}
public TestPlanApiCase getById(String id) {
return testPlanApiCaseMapper.selectByPrimaryKey(id);
}

View File

@ -217,14 +217,6 @@ public class TestPlanTestCaseService {
testPlanTestCaseMapper.deleteByExample(example);
}
public List<String> getTestPlanTestCaseIds(String testId) {
return extTestPlanTestCaseMapper.getTestPlanTestCaseIds(testId);
}
public int updateTestCaseStates(List<String> ids, String reportStatus) {
return extTestPlanTestCaseMapper.updateTestCaseStates(ids, reportStatus);
}
/**
* 更新测试计划关联接口测试的功能用例的状态
*