fix(接口测试): 修复case执行时在任务中心提示不正确的问题

--bug=1012090 --user=宋天阳 [接口定义]github#12468接口-接口定义执行的case在任务中心提示不正确
https://www.tapd.cn/55049933/s/1137389
This commit is contained in:
song-tianyang 2022-04-18 14:33:28 +08:00 committed by TIanyang
parent 094a310ffe
commit fc4c403c9c
6 changed files with 62 additions and 13 deletions

View File

@ -37,6 +37,7 @@ import org.apache.commons.collections.MapUtils;
import org.apache.commons.collections4.comparators.FixedOrderComparator;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -63,6 +64,8 @@ public class ApiCaseExecuteService {
private ApiCaseResultService apiCaseResultService;
@Resource
private ApiScenarioReportStructureService apiScenarioReportStructureService;
@Resource
RedisTemplate<String, Object> redisTemplate;
/**
* 测试计划case执行
@ -109,7 +112,7 @@ public class ApiCaseExecuteService {
Map<String, String> planProjects = new HashMap<>();
for (TestPlanApiCase testPlanApiCase : planApiCases) {
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.addResult(request, testPlanApiCase, status, caseMap, resourcePoolId);
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.addResult(request, testPlanApiCase, status, caseMap, resourcePoolId, redisTemplate);
if (planProjects.containsKey(testPlanApiCase.getTestPlanId())) {
report.setProjectId(planProjects.get(testPlanApiCase.getTestPlanId()));
} else {
@ -230,7 +233,7 @@ public class ApiCaseExecuteService {
if (StringUtils.equals(request.getConfig().getReportType(), RunModeConstants.SET_REPORT.toString())
&& StringUtils.isNotEmpty(request.getConfig().getReportName())) {
serialReportId = UUID.randomUUID().toString();
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(null, APITestStatus.Running.name(), serialReportId, request.getConfig());
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(null, APITestStatus.Running.name(), serialReportId, request.getConfig(),redisTemplate);
report.setName(request.getConfig().getReportName());
report.setProjectId(request.getProjectId());
report.setReportType(ReportTypeConstants.API_INTEGRATED.name());
@ -262,7 +265,7 @@ public class ApiCaseExecuteService {
for (int i = 0; i < caseList.size(); i++) {
ApiTestCaseWithBLOBs caseWithBLOBs = caseList.get(i);
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, request.getConfig());
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.initBase(caseWithBLOBs.getId(), APITestStatus.Running.name(), null, request.getConfig(), redisTemplate);
report.setStatus(status);
report.setName(caseWithBLOBs.getName());
report.setProjectId(caseWithBLOBs.getProjectId());

View File

@ -36,6 +36,7 @@ import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.jorphan.collections.HashTree;
import org.apache.jorphan.collections.ListedHashTree;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
@ -62,6 +63,8 @@ public class ApiExecuteService {
private ExtApiTestCaseMapper extApiTestCaseMapper;
@Resource
private ObjectMapper mapper;
@Resource
private RedisTemplate<String, Object> redisTemplate;
public MsExecResponseDTO jenkinsRun(RunCaseRequest request) {
ApiTestCaseWithBLOBs caseWithBLOBs = null;
@ -81,7 +84,7 @@ public class ApiExecuteService {
request.setEnvironmentId(extApiTestCaseMapper.getApiCaseEnvironment(request.getCaseId()));
}
//提前生成报告
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.add(caseWithBLOBs.getId(), APITestStatus.Running.name(), request.getReportId());
ApiDefinitionExecResult report = ApiDefinitionExecResultUtil.add(caseWithBLOBs.getId(), APITestStatus.Running.name(), request.getReportId(),redisTemplate);
report.setName(caseWithBLOBs.getName());
report.setTriggerMode(ApiRunMode.JENKINS.name());
report.setType(ApiRunMode.JENKINS.name());

View File

@ -9,14 +9,16 @@ import io.metersphere.commons.constants.ReportTypeConstants;
import io.metersphere.commons.constants.TriggerMode;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.RunModeConfigDTO;
import io.metersphere.dto.UserDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.data.redis.core.RedisTemplate;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
public class ApiDefinitionExecResultUtil {
public static ApiDefinitionExecResult initBase(String resourceId, String status, String reportId, RunModeConfigDTO config) {
public static ApiDefinitionExecResult initBase(String resourceId, String status, String reportId, RunModeConfigDTO config,RedisTemplate<String, Object> redisTemplate) {
ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult();
if (StringUtils.isEmpty(reportId)) {
apiResult.setId(UUID.randomUUID().toString());
@ -32,6 +34,10 @@ public class ApiDefinitionExecResultUtil {
apiResult.setActuator(config.getResourcePoolId());
}
apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
//存储用户信息,执行完成后删除
if(redisTemplate != null){
redisTemplate.opsForValue().set(apiResult.getId(),SessionUtils.getUser());
}
apiResult.setResourceId(resourceId);
apiResult.setReportType(ReportTypeConstants.API_INDEPENDENT.name());
apiResult.setStartTime(System.currentTimeMillis());
@ -41,7 +47,7 @@ public class ApiDefinitionExecResultUtil {
}
public static ApiDefinitionExecResult addResult(BatchRunDefinitionRequest request, TestPlanApiCase key, String status,
Map<String, ApiTestCase> caseMap, String poolId) {
Map<String, ApiTestCase> caseMap, String poolId,RedisTemplate<String, Object> redisTemplate) {
ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult();
apiResult.setId(UUID.randomUUID().toString());
apiResult.setCreateTime(System.currentTimeMillis());
@ -62,9 +68,17 @@ public class ApiDefinitionExecResultUtil {
if (StringUtils.isEmpty(request.getUserId())) {
if (SessionUtils.getUser() != null) {
apiResult.setUserId(SessionUtils.getUser().getId());
if(redisTemplate != null){
redisTemplate.opsForValue().set(apiResult.getId(),SessionUtils.getUser());
}
}
} else {
apiResult.setUserId(request.getUserId());
UserDTO userDTO = new UserDTO();
userDTO.setId(request.getUserId());
if(redisTemplate != null){
redisTemplate.opsForValue().set(apiResult.getId(),userDTO);
}
}
apiResult.setResourceId(key.getApiCaseId());
@ -75,7 +89,7 @@ public class ApiDefinitionExecResultUtil {
return apiResult;
}
public static ApiDefinitionExecResult add(String resourceId, String status, String reportId) {
public static ApiDefinitionExecResult add(String resourceId, String status, String reportId,RedisTemplate<String, Object> redisTemplate) {
ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult();
if (StringUtils.isEmpty(reportId)) {
apiResult.setId(UUID.randomUUID().toString());
@ -89,6 +103,9 @@ public class ApiDefinitionExecResultUtil {
apiResult.setTriggerMode(TriggerMode.BATCH.name());
apiResult.setActuator("LOCAL");
apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
if(redisTemplate != null){
redisTemplate.opsForValue().set(apiResult.getId(),SessionUtils.getUser());
}
apiResult.setResourceId(resourceId);
apiResult.setStartTime(System.currentTimeMillis());
apiResult.setType(ApiRunMode.DEFINITION.name());

View File

@ -13,6 +13,7 @@ import io.metersphere.commons.constants.*;
import io.metersphere.commons.utils.*;
import io.metersphere.dto.RequestResult;
import io.metersphere.dto.ResultDTO;
import io.metersphere.dto.UserDTO;
import io.metersphere.notice.sender.NoticeModel;
import io.metersphere.notice.service.NoticeSendService;
import io.metersphere.track.dto.PlanReportCaseDTO;
@ -30,6 +31,7 @@ import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -68,6 +70,9 @@ public class ApiDefinitionExecResultService {
private ProjectMapper projectMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Resource
RedisTemplate<String, Object> redisTemplate;
public void saveApiResult(List<RequestResult> requestResults, ResultDTO dto) {
LoggerUtil.info("接收到API/CASE执行结果【 " + requestResults.size() + "");
@ -82,6 +87,8 @@ public class ApiDefinitionExecResultService {
// 发送通知
sendNotice(result);
}
//删除用例运行时存放在redis中的用户数据
redisTemplate.delete(dto.getReportId());
}
}
}
@ -113,6 +120,8 @@ public class ApiDefinitionExecResultService {
// 发送通知
sendNotice(result);
}
//删除用例运行时存放在redis中的用户数据
redisTemplate.delete(dto.getReportId());
}
}
if (isSchedule) {
@ -154,11 +163,19 @@ public class ApiDefinitionExecResultService {
status = "失败";
}
User user = null;
if (SessionUtils.getUser() != null && StringUtils.equals(SessionUtils.getUser().getId(), result.getUserId())) {
user = SessionUtils.getUser();
} else {
user = userMapper.selectByPrimaryKey(result.getUserId());
Object userObject = redisTemplate.opsForValue().get(result.getId());
if(userObject != null && userObject instanceof UserDTO){
user = (UserDTO) userObject;
}
if(user == null){
if (SessionUtils.getUser() != null && StringUtils.equals(SessionUtils.getUser().getId(), result.getUserId())) {
user = SessionUtils.getUser();
} else {
user = userMapper.selectByPrimaryKey(result.getUserId());
}
}
Map paramMap = new HashMap<>(beanMap);
paramMap.put("operator", user != null ? user.getName() : result.getUserId());
paramMap.put("status", result.getStatus());
@ -294,6 +311,8 @@ public class ApiDefinitionExecResultService {
RequestResultExpandDTO expandDTO = ResponseUtil.parseByRequestResult(item);
ApiDefinitionExecResult reportResult = this.editResult(item, dto.getReportId(), dto.getConsole(), dto.getRunMode(), dto.getTestId(), null);
redisTemplate.delete(reportResult.getId());
String status = item.isSuccess() ? "success" : "error";
if (reportResult != null) {
status = reportResult.getStatus();
@ -429,7 +448,6 @@ public class ApiDefinitionExecResultService {
saveResult.setContent(JSON.toJSONString(item));
}
saveResult.setType(type);
saveResult.setStatus(status);
saveResult.setResourceId(item.getName());
saveResult.setStartTime(item.getStartTime());

View File

@ -61,6 +61,7 @@ import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
@ -134,6 +135,8 @@ public class ApiDefinitionService {
private ExtProjectVersionMapper extProjectVersionMapper;
@Resource
private ProjectApplicationService projectApplicationService;
@Resource
private RedisTemplate<String, Object> redisTemplate;
private ThreadLocal<Long> currentApiOrder = new ThreadLocal<>();
private ThreadLocal<Long> currentApiCaseOrder = new ThreadLocal<>();
@ -1109,7 +1112,7 @@ public class ApiDefinitionService {
CollectionUtils.isNotEmpty(request.getTestElement().getHashTree()) &&
CollectionUtils.isNotEmpty(request.getTestElement().getHashTree().get(0).getHashTree()) ?
request.getTestElement().getHashTree().get(0).getHashTree().get(0).getName() : request.getId();
ApiDefinitionExecResult result = ApiDefinitionExecResultUtil.add(testId, APITestStatus.Running.name(), request.getId());
ApiDefinitionExecResult result = ApiDefinitionExecResultUtil.add(testId, APITestStatus.Running.name(), request.getId(), redisTemplate);
result.setProjectId(request.getProjectId());
result.setTriggerMode(TriggerMode.MANUAL.name());
apiDefinitionExecResultMapper.insert(result);

View File

@ -35,6 +35,7 @@ import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
@ -83,6 +84,8 @@ public class ApiScenarioReportService {
private ApiDefinitionExecResultMapper definitionExecResultMapper;
@Resource
private UiReportServiceProxy uiReportServiceProxy;
@Resource
RedisTemplate<String, Object> redisTemplate;
public void saveResult(List<RequestResult> requestResults, ResultDTO dto) {
// 报告详情内容
@ -349,6 +352,8 @@ public class ApiScenarioReportService {
// 更新场景状态
if (StringUtils.equalsIgnoreCase(runMode, ApiRunMode.DEFINITION.name())) {
ApiDefinitionExecResult result = definitionExecResultMapper.selectByPrimaryKey(reportId);
//删除执行时redis记录的数据
redisTemplate.delete(result.getId());
ApiDefinitionExecResultExample execResultExample = new ApiDefinitionExecResultExample();
execResultExample.createCriteria().andIntegratedReportIdEqualTo(reportId).andStatusEqualTo("Error");
long size = definitionExecResultMapper.countByExample(execResultExample);