feat(接口测试): 优化测试用例执行结束后发送通知的取值方式

优化测试用例执行结束后发送通知的取值方式,从redis中取值改为执行时直接将执行人作为参数传递进来
This commit is contained in:
song-tianyang 2022-04-29 12:31:41 +08:00 committed by TIanyang
parent 19ad193847
commit 97dde4eead
6 changed files with 50 additions and 25 deletions

View File

@ -19,6 +19,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
@ -48,6 +49,9 @@ public class ApiCaseParallelExecuteService {
runRequest.setReportType(executionQueue.getReportType());
runRequest.setRunType(RunModeConstants.PARALLEL.toString());
runRequest.setQueueId(executionQueue.getId());
Map<String,Object> extendedParameters = new HashMap<>();
extendedParameters.put("userId",result.getUserId());
runRequest.setExtendedParameters(extendedParameters);
if (MapUtils.isNotEmpty(executionQueue.getDetailMap())) {
runRequest.setPlatformUrl(GenerateHashTreeUtil.getPlatformUrl(baseInfo, runRequest, executionQueue.getDetailMap().get(result.getId())));
}

View File

@ -28,6 +28,7 @@ import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.FileUtils;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.SessionUtils;
import io.metersphere.dto.JmeterRunRequestDTO;
import io.metersphere.dto.MsExecResponseDTO;
import io.metersphere.plugin.core.MsTestElement;
@ -81,7 +82,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(),Objects.requireNonNull(SessionUtils.getUser()).getId());
report.setName(caseWithBLOBs.getName());
report.setTriggerMode(ApiRunMode.JENKINS.name());
report.setType(ApiRunMode.JENKINS.name());
@ -186,6 +187,7 @@ public class ApiExecuteService {
runRequest.setDebug(request.isDebug());
runRequest.setExtendedParameters(new HashMap<String, Object>() {{
this.put("SYN_RES", request.isSyncResult());
this.put("user", Objects.requireNonNull(SessionUtils.getUser()));
}});
// 开始执行
jMeterService.run(runRequest);

View File

@ -23,10 +23,7 @@ import io.metersphere.base.domain.*;
import io.metersphere.base.mapper.*;
import io.metersphere.commons.constants.APITestStatus;
import io.metersphere.commons.constants.ApiRunMode;
import io.metersphere.commons.utils.BeanUtils;
import io.metersphere.commons.utils.CommonBeanFactory;
import io.metersphere.commons.utils.HashTreeUtil;
import io.metersphere.commons.utils.LogUtil;
import io.metersphere.commons.utils.*;
import io.metersphere.constants.RunModeConstants;
import io.metersphere.dto.BaseSystemConfigDTO;
import io.metersphere.dto.JmeterRunRequestDTO;
@ -40,9 +37,7 @@ import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.*;
@Service
public class ApiScenarioSerialService {
@ -69,6 +64,12 @@ public class ApiScenarioSerialService {
public void serial(ApiExecutionQueue executionQueue, ApiExecutionQueueDetail queue) {
LoggerUtil.debug("Scenario run-执行脚本装载-进入串行准备");
String reportId = StringUtils.isNotEmpty(executionQueue.getReportId()) ? executionQueue.getReportId() : queue.getReportId();
if (!StringUtils.equalsAny(executionQueue.getRunMode(), ApiRunMode.SCENARIO.name())) {
reportId = queue.getReportId();
}
HashTree hashTree = null;
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(queue.getTestId(), reportId, executionQueue.getRunMode(), hashTree);
// 获取可以执行的资源池
BaseSystemConfigDTO baseInfo = CommonBeanFactory.getBean(SystemParameterService.class).getBaseInfo();
if (!StringUtils.equals(executionQueue.getReportType(), RunModeConstants.SET_REPORT.toString())
@ -79,11 +80,17 @@ public class ApiScenarioSerialService {
report.setStatus(APITestStatus.Running.name());
report.setCreateTime(System.currentTimeMillis());
report.setUpdateTime(System.currentTimeMillis());
runRequest.setExtendedParameters(new HashMap<String, Object>() {{
this.put("userId", report.getCreateUser());
}});
apiScenarioReportMapper.updateByPrimaryKey(report);
}
} else {
ApiDefinitionExecResult execResult = apiDefinitionExecResultMapper.selectByPrimaryKey(queue.getReportId());
if (execResult != null) {
runRequest.setExtendedParameters(new HashMap<String, Object>() {{
this.put("userId", execResult.getUserId());
}});
execResult.setStatus(APITestStatus.Running.name());
apiDefinitionExecResultMapper.updateByPrimaryKeySelective(execResult);
}
@ -91,12 +98,6 @@ public class ApiScenarioSerialService {
}
LoggerUtil.info("Scenario run-开始执行队列ID" + executionQueue.getReportId() + "");
String reportId = StringUtils.isNotEmpty(executionQueue.getReportId()) ? executionQueue.getReportId() : queue.getReportId();
if (!StringUtils.equalsAny(executionQueue.getRunMode(), ApiRunMode.SCENARIO.name())) {
reportId = queue.getReportId();
}
HashTree hashTree = null;
JmeterRunRequestDTO runRequest = new JmeterRunRequestDTO(queue.getTestId(), reportId, executionQueue.getRunMode(), hashTree);
runRequest.setReportType(executionQueue.getReportType());
runRequest.setPool(GenerateHashTreeUtil.isResourcePool(executionQueue.getPoolId()));
runRequest.setTestPlanReportId(executionQueue.getReportId());

View File

@ -75,7 +75,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, String userId) {
ApiDefinitionExecResult apiResult = new ApiDefinitionExecResult();
if (StringUtils.isEmpty(reportId)) {
apiResult.setId(UUID.randomUUID().toString());
@ -88,7 +88,7 @@ public class ApiDefinitionExecResultUtil {
apiResult.setEndTime(System.currentTimeMillis());
apiResult.setTriggerMode(TriggerMode.BATCH.name());
apiResult.setActuator("LOCAL");
apiResult.setUserId(Objects.requireNonNull(SessionUtils.getUser()).getId());
apiResult.setUserId(userId);
apiResult.setResourceId(resourceId);
apiResult.setStartTime(System.currentTimeMillis());
apiResult.setType(ApiRunMode.DEFINITION.name());

View File

@ -79,9 +79,19 @@ public class ApiDefinitionExecResultService {
if (!StringUtils.startsWithAny(item.getName(), "PRE_PROCESSOR_ENV_", "POST_PROCESSOR_ENV_")) {
ApiDefinitionExecResult result = this.editResult(item, dto.getReportId(), dto.getConsole(), dto.getRunMode(), dto.getTestId(), null);
if (result != null) {
User user = null;
if(MapUtils.isNotEmpty(dto.getExtendedParameters()) && dto.getExtendedParameters().containsKey("user")){
try {
user = JSONObject.parseObject(String.valueOf(dto.getExtendedParameters().get("user")),User.class);
}catch (Exception e){
LogUtil.error("解析用户信息出错!",e);
}
}else if(dto.getExtendedParameters().containsKey("userId")){
result.setUserId(dto.getExtendedParameters().get("userId").toString());
}
// 发送通知
result.setResourceId(dto.getTestId());
sendNotice(result);
sendNotice(result,user);
}
}
}
@ -111,9 +121,13 @@ public class ApiDefinitionExecResultService {
);
if (result != null && !StringUtils.startsWithAny(dto.getRunMode(), "SCHEDULE")) {
User user = null;
if(MapUtils.isNotEmpty(dto.getExtendedParameters()) && dto.getExtendedParameters().containsKey("user")&& dto.getExtendedParameters().get("user") instanceof User){
user = (User)dto.getExtendedParameters().get("user");
}
// 发送通知
result.setResourceId(dto.getTestId());
sendNotice(result);
sendNotice(result,user);
}
}
}
@ -136,7 +150,7 @@ public class ApiDefinitionExecResultService {
}
}
private void sendNotice(ApiDefinitionExecResult result) {
private void sendNotice(ApiDefinitionExecResult result, User user) {
try {
String resourceId = result.getResourceId();
ApiTestCaseWithBLOBs apiTestCaseWithBLOBs = apiTestCaseMapper.selectByPrimaryKey(resourceId);
@ -155,11 +169,12 @@ public class ApiDefinitionExecResultService {
event = NoticeConstants.Event.EXECUTE_FAILED;
status = "失败";
}
User user = null;
if (SessionUtils.getUser() != null && StringUtils.equals(SessionUtils.getUser().getId(), result.getUserId())) {
user = SessionUtils.getUser();
} else {
user = userMapper.selectByPrimaryKey(result.getUserId());
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());
@ -294,6 +309,9 @@ public class ApiDefinitionExecResultService {
RequestResultExpandDTO expandDTO = ResponseUtil.parseByRequestResult(item);
ApiDefinitionExecResult reportResult = this.editResult(item, dto.getReportId(), dto.getConsole(), dto.getRunMode(), dto.getTestId(), null);
if(MapUtils.isNotEmpty(dto.getExtendedParameters()) && dto.getExtendedParameters().containsKey("userId")){
reportResult.setUserId(String.valueOf(dto.getExtendedParameters().get("userId")));
}
String status = item.isSuccess() ? "success" : "error";
if (reportResult != null) {
status = reportResult.getStatus();

View File

@ -1108,7 +1108,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(), Objects.requireNonNull(SessionUtils.getUser()).getId());
result.setProjectId(request.getProjectId());
result.setTriggerMode(TriggerMode.MANUAL.name());
apiDefinitionExecResultMapper.insert(result);