diff --git a/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsApiBackendListener.java b/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsApiBackendListener.java index 6d2470e0d4..033e0160ad 100644 --- a/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsApiBackendListener.java +++ b/api-test/backend/src/main/java/io/metersphere/api/jmeter/MsApiBackendListener.java @@ -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(); diff --git a/api-test/backend/src/main/java/io/metersphere/service/TestResultService.java b/api-test/backend/src/main/java/io/metersphere/service/TestResultService.java index c1f67e62a1..e99828591f 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/TestResultService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/TestResultService.java @@ -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 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(); + } } /** diff --git a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionExecResultService.java b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionExecResultService.java index 7347c2e387..3c51d9384c 100644 --- a/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionExecResultService.java +++ b/api-test/backend/src/main/java/io/metersphere/service/definition/ApiDefinitionExecResultService.java @@ -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) {