fix(测试跟踪): 修复测试计划运行接口用例时时执行结果没有写入测试计划与接口用例关联表的问题

--bug=1020245 --user=宋天阳
【测试计划】计划中关联1400+的接口用例和20条场景用例,执行该测试计划成功后-报告统计中未更新
https://www.tapd.cn/55049933/s/1304670
This commit is contained in:
song-tianyang 2022-11-24 17:11:52 +08:00 committed by 建国
parent 3a977e134b
commit 6956335481
3 changed files with 33 additions and 1 deletions

View File

@ -68,7 +68,6 @@ public class MsApiBackendListener extends AbstractBackendListenerClient implemen
sampleResults = RetryResultUtil.clearLoops(sampleResults);
JMeterBase.resultFormatting(sampleResults, dto);
testResultService.saveResults(dto);
resultVO = ReportStatusUtil.getStatus(dto, resultVO);
dto.getArbitraryData().put(CommonConstants.LOCAL_STATUS_KEY, resultVO);
sampleResults.clear();

View File

@ -14,6 +14,7 @@ import io.metersphere.commons.enums.ApiReportStatus;
import io.metersphere.commons.enums.ExecutionExecuteTypeEnum;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.DateUtils;
import io.metersphere.commons.utils.HttpHeaderUtils;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.ResultDTO;
@ -26,6 +27,7 @@ import io.metersphere.service.scenario.ApiScenarioReportStructureService;
import io.metersphere.service.scenario.ApiScenarioService;
import org.apache.commons.beanutils.BeanMap;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@ -86,6 +88,17 @@ public class TestResultService {
* @param dto 执行结果
*/
public void saveResults(ResultDTO dto) {
String userId = null;
if (MapUtils.isNotEmpty(dto.getExtendedParameters()) && dto.getExtendedParameters().containsKey("userId")) {
userId = dto.getExtendedParameters().get("userId").toString();
}
if (StringUtils.isNotEmpty(userId)) {
User user = new User();
user.setId(userId);
user.setName(userId);
HttpHeaderUtils.runAsUser(user);
}
// 处理环境
List<String> environmentList = new LinkedList<>();
if (dto.getArbitraryData() != null && dto.getArbitraryData().containsKey("ENV")) {
@ -106,6 +119,10 @@ public class TestResultService {
// 场景报告结果处理
apiScenarioReportService.saveResult(dto);
}
if (StringUtils.isNotEmpty(userId)) {
HttpHeaderUtils.clearUser();
}
}
/**

View File

@ -31,6 +31,7 @@ import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;
@ -133,6 +134,17 @@ public class ApiDefinitionExecResultService {
ApiTestCaseMapper batchApiTestCaseMapper = sqlSession.getMapper(ApiTestCaseMapper.class);
for (ResultDTO dto : resultDTOS) {
String userId = null;
if (MapUtils.isNotEmpty(dto.getExtendedParameters()) && dto.getExtendedParameters().containsKey("userId")) {
userId = dto.getExtendedParameters().get("userId").toString();
}
if (StringUtils.isNotEmpty(userId)) {
User user = new User();
user.setId(userId);
user.setName(userId);
HttpHeaderUtils.runAsUser(user);
}
this.mergeRetryResults(dto);
if (CollectionUtils.isNotEmpty(dto.getRequestResults())) {
for (RequestResult item : dto.getRequestResults()) {
@ -156,6 +168,10 @@ public class ApiDefinitionExecResultService {
}
}
}
if (StringUtils.isNotEmpty(userId)) {
HttpHeaderUtils.clearUser();
}
}
sqlSession.flushStatements();
if (sqlSession != null && sqlSessionFactory != null) {