fix(接口测试): 接口用例执行消息通知变量缺失

--bug=1045218 --user=陈建星 【消息管理】配置接口CASE执行成功or失败的消息模板,有一些参数没有获取到值 https://www.tapd.cn/55049933/s/1563620
This commit is contained in:
AgAngle 2024-08-15 20:14:27 +08:00 committed by 刘瑞斌
parent b2ecefd97f
commit e56f978d7d
3 changed files with 80 additions and 28 deletions

View File

@ -2,10 +2,7 @@ package io.metersphere.api.service;
import io.metersphere.api.constants.ApiDefinitionStatus; import io.metersphere.api.constants.ApiDefinitionStatus;
import io.metersphere.api.constants.ApiScenarioStatus; import io.metersphere.api.constants.ApiScenarioStatus;
import io.metersphere.api.domain.ApiReport; import io.metersphere.api.domain.*;
import io.metersphere.api.domain.ApiScenario;
import io.metersphere.api.domain.ApiScenarioReport;
import io.metersphere.api.domain.ApiTestCase;
import io.metersphere.api.dto.share.ApiReportShareRequest; import io.metersphere.api.dto.share.ApiReportShareRequest;
import io.metersphere.api.dto.share.ShareInfoDTO; import io.metersphere.api.dto.share.ShareInfoDTO;
import io.metersphere.api.mapper.*; import io.metersphere.api.mapper.*;
@ -20,6 +17,7 @@ import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.CommonBeanFactory; import io.metersphere.sdk.util.CommonBeanFactory;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.domain.User; import io.metersphere.system.domain.User;
import io.metersphere.system.dto.sdk.ApiDefinitionCaseDTO;
import io.metersphere.system.dto.sdk.BaseSystemConfigDTO; import io.metersphere.system.dto.sdk.BaseSystemConfigDTO;
import io.metersphere.system.mapper.UserMapper; import io.metersphere.system.mapper.UserMapper;
import io.metersphere.system.notice.NoticeModel; import io.metersphere.system.notice.NoticeModel;
@ -59,6 +57,8 @@ public class ApiReportSendNoticeService {
private ExtApiScenarioMapper extApiScenarioMapper; private ExtApiScenarioMapper extApiScenarioMapper;
@Resource @Resource
private ExtApiTestCaseMapper extApiTestCaseMapper; private ExtApiTestCaseMapper extApiTestCaseMapper;
@Resource
private ApiDefinitionMapper apiDefinitionMapper;
public void sendNotice(ApiNoticeDTO noticeDTO) { public void sendNotice(ApiNoticeDTO noticeDTO) {
String noticeType = null; String noticeType = null;
@ -122,7 +122,21 @@ public class ApiReportSendNoticeService {
return; return;
} }
beanMap = new BeanMap(testCase); ApiDefinition apiDefinition = apiDefinitionMapper.selectByPrimaryKey(testCase.getApiDefinitionId());
ApiDefinitionCaseDTO caseDTO = BeanUtils.copyBean(new ApiDefinitionCaseDTO(), testCase);
caseDTO.setCaseName(testCase.getName());
caseDTO.setCaseStatus(getTranslateStatus(testCase.getStatus()));
caseDTO.setCaseCreateTime(testCase.getCreateTime());
caseDTO.setCaseCreateUser(testCase.getCreateUser());
caseDTO.setCaseUpdateTime(testCase.getUpdateTime());
caseDTO.setCaseUpdateUser(testCase.getUpdateUser());
caseDTO.setLastReportStatus(getTranslateReportStatus(report.getStatus()));
caseDTO.setPath(apiDefinition.getPath());
caseDTO.setMethod(apiDefinition.getMethod());
beanMap = new BeanMap(caseDTO);
// TODO 是否需要区分场景和用例 // TODO 是否需要区分场景和用例
noticeType = NoticeConstants.TaskType.API_DEFINITION_TASK; noticeType = NoticeConstants.TaskType.API_DEFINITION_TASK;
@ -145,29 +159,12 @@ public class ApiReportSendNoticeService {
Map paramMap = new HashMap<>(beanMap); Map paramMap = new HashMap<>(beanMap);
noticeSendService.setLanguage(user.getLanguage()); noticeSendService.setLanguage(user.getLanguage());
paramMap.put(NoticeConstants.RelatedUser.OPERATOR, user != null ? user.getName() : ""); paramMap.put(NoticeConstants.RelatedUser.OPERATOR, user != null ? user.getName() : "");
// TODO 是否需要国际化 根据状态判断给不同的key
String status = paramMap.containsKey("status") ? paramMap.get("status").toString() : null;
if (StringUtils.isNotBlank(status)) {
if (List.of(ApiScenarioStatus.UNDERWAY.name(), ApiDefinitionStatus.PROCESSING.name()).contains(status)) {
status = Translator.get("api_definition.status.ongoing");
} else if (List.of(ApiScenarioStatus.COMPLETED.name(), ApiDefinitionStatus.DONE.name()).contains(status)) {
status = Translator.get("api_definition.status.completed");
} else if (StringUtils.equals(ApiScenarioStatus.DEPRECATED.name(), status)) {
status = Translator.get("api_definition.status.abandoned");
} else if (StringUtils.equals(ApiDefinitionStatus.DEBUGGING.name(), status)) {
status = Translator.get("api_definition.status.continuous");
}
}
String status = paramMap.containsKey("status") ? paramMap.get("status").toString() : null;
status = getTranslateStatus(status);
String reportStatus = report.getStatus(); String reportStatus = report.getStatus();
if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.SUCCESS.name())) { reportStatus = getTranslateReportStatus(reportStatus);
reportStatus = Translator.get("report.status.success");
} else if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.FAKE_ERROR.name())) {
reportStatus = Translator.get("report.status.fake_error");
} else {
reportStatus = Translator.get("report.status.error");
}
paramMap.put("status", status); paramMap.put("status", status);
paramMap.put("reportName", report.getName()); paramMap.put("reportName", report.getName());
@ -187,8 +184,6 @@ public class ApiReportSendNoticeService {
paramMap.put("requestPassRate", report.getRequestPassRate()); paramMap.put("requestPassRate", report.getRequestPassRate());
paramMap.put("assertionPassRate", report.getAssertionPassRate()); paramMap.put("assertionPassRate", report.getAssertionPassRate());
// TODO 这里状态是否是国际化 还有分享链接需要补充
// TODO 暂时取一个环境处理 // TODO 暂时取一个环境处理
String environmentId = noticeDTO.getRunModeConfig().getEnvironmentId(); String environmentId = noticeDTO.getRunModeConfig().getEnvironmentId();
if (StringUtils.isNotEmpty(environmentId)) { if (StringUtils.isNotEmpty(environmentId)) {
@ -202,6 +197,7 @@ public class ApiReportSendNoticeService {
paramMap.put("reportUrl", reportUrl); paramMap.put("reportUrl", reportUrl);
paramMap.put("scenarioShareUrl", shareUrl); paramMap.put("scenarioShareUrl", shareUrl);
paramMap.put("shareUrl", shareUrl);
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
String template = defaultTemplateMap.get(noticeType + "_" + event); String template = defaultTemplateMap.get(noticeType + "_" + event);
@ -212,4 +208,30 @@ public class ApiReportSendNoticeService {
noticeSendService.send(project, noticeType, noticeModel); noticeSendService.send(project, noticeType, noticeModel);
} }
private String getTranslateReportStatus(String reportStatus) {
if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.SUCCESS.name())) {
reportStatus = Translator.get("report.status.success");
} else if (StringUtils.endsWithIgnoreCase(reportStatus, ResultStatus.FAKE_ERROR.name())) {
reportStatus = Translator.get("report.status.fake_error");
} else {
reportStatus = Translator.get("report.status.error");
}
return reportStatus;
}
private String getTranslateStatus(String status) {
if (StringUtils.isNotBlank(status)) {
if (List.of(ApiScenarioStatus.UNDERWAY.name(), ApiDefinitionStatus.PROCESSING.name()).contains(status)) {
status = Translator.get("api_definition.status.ongoing");
} else if (List.of(ApiScenarioStatus.COMPLETED.name(), ApiDefinitionStatus.DONE.name()).contains(status)) {
status = Translator.get("api_definition.status.completed");
} else if (StringUtils.equals(ApiScenarioStatus.DEPRECATED.name(), status)) {
status = Translator.get("api_definition.status.abandoned");
} else if (StringUtils.equals(ApiDefinitionStatus.DEBUGGING.name(), status)) {
status = Translator.get("api_definition.status.continuous");
}
}
return status;
}
} }

View File

@ -769,6 +769,8 @@ public class ApiTestCaseService extends MoveNodeService {
// 设置环境 // 设置环境
apiParamConfig.setEnvConfig(environmentService.get(envId)); apiParamConfig.setEnvConfig(environmentService.get(envId));
taskRequest.getTaskInfo().getRunModeConfig().setEnvironmentId(envId);
// 设置 method 等信息 // 设置 method 等信息
apiCommonService.setApiDefinitionExecuteInfo(runRequest.getTestElement(), apiDefinition); apiCommonService.setApiDefinitionExecuteInfo(runRequest.getTestElement(), apiDefinition);

View File

@ -1,17 +1,23 @@
package io.metersphere.api.controller; package io.metersphere.api.controller;
import io.metersphere.api.constants.ApiConstants;
import io.metersphere.api.constants.ApiDefinitionStatus;
import io.metersphere.api.domain.*; import io.metersphere.api.domain.*;
import io.metersphere.api.mapper.ApiDefinitionMapper;
import io.metersphere.api.mapper.ApiScenarioMapper; import io.metersphere.api.mapper.ApiScenarioMapper;
import io.metersphere.api.mapper.ApiTestCaseMapper; import io.metersphere.api.mapper.ApiTestCaseMapper;
import io.metersphere.api.service.ApiReportSendNoticeService; import io.metersphere.api.service.ApiReportSendNoticeService;
import io.metersphere.api.service.definition.ApiReportService; import io.metersphere.api.service.definition.ApiReportService;
import io.metersphere.api.service.scenario.ApiScenarioReportService; import io.metersphere.api.service.scenario.ApiScenarioReportService;
import io.metersphere.sdk.constants.ApiExecuteResourceType; import io.metersphere.sdk.constants.ApiExecuteResourceType;
import io.metersphere.sdk.constants.ApplicationNumScope;
import io.metersphere.sdk.constants.ResultStatus; import io.metersphere.sdk.constants.ResultStatus;
import io.metersphere.sdk.domain.Environment; import io.metersphere.sdk.domain.Environment;
import io.metersphere.sdk.dto.api.notice.ApiNoticeDTO; import io.metersphere.sdk.dto.api.notice.ApiNoticeDTO;
import io.metersphere.sdk.mapper.EnvironmentMapper; import io.metersphere.sdk.mapper.EnvironmentMapper;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.uid.IDGenerator;
import io.metersphere.system.uid.NumGenerator;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.MethodOrderer; import org.junit.jupiter.api.MethodOrderer;
@ -45,6 +51,8 @@ public class ApiReportSendNoticeTests extends BaseTest {
private ApiReportSendNoticeService apiReportSendNoticeService; private ApiReportSendNoticeService apiReportSendNoticeService;
@Resource @Resource
private ApiScenarioMapper apiScenarioMapper; private ApiScenarioMapper apiScenarioMapper;
@Resource
private ApiDefinitionMapper apiDefinitionMapper;
@Test @Test
@Order(0) @Order(0)
@ -61,9 +69,29 @@ public class ApiReportSendNoticeTests extends BaseTest {
environment.setPos(1L); environment.setPos(1L);
environmentMapper.insertSelective(environment); environmentMapper.insertSelective(environment);
ApiDefinition apiDefinition = new ApiDefinition();
apiDefinition.setId(IDGenerator.nextStr());
apiDefinition.setProjectId(DEFAULT_PROJECT_ID);
apiDefinition.setName(StringUtils.join("接口定义", apiDefinition.getId()));
apiDefinition.setModuleId("case-moduleId");
apiDefinition.setProtocol(ApiConstants.HTTP_PROTOCOL);
apiDefinition.setMethod("GET");
apiDefinition.setStatus(ApiDefinitionStatus.DEBUGGING.name());
apiDefinition.setNum(NumGenerator.nextNum(DEFAULT_PROJECT_ID, ApplicationNumScope.API_DEFINITION));
apiDefinition.setPos(0L);
apiDefinition.setPath(StringUtils.join("api/definition/", apiDefinition.getId()));
apiDefinition.setLatest(true);
apiDefinition.setVersionId("1.0");
apiDefinition.setRefId(apiDefinition.getId());
apiDefinition.setCreateTime(System.currentTimeMillis());
apiDefinition.setUpdateTime(System.currentTimeMillis());
apiDefinition.setCreateUser("admin");
apiDefinition.setUpdateUser("admin");
apiDefinitionMapper.insertSelective(apiDefinition);
ApiTestCase apiTestCase = new ApiTestCase(); ApiTestCase apiTestCase = new ApiTestCase();
apiTestCase.setId("send-api-case-id"); apiTestCase.setId("send-api-case-id");
apiTestCase.setApiDefinitionId("api-definition-id"); apiTestCase.setApiDefinitionId(apiDefinition.getId());
apiTestCase.setProjectId(DEFAULT_PROJECT_ID); apiTestCase.setProjectId(DEFAULT_PROJECT_ID);
apiTestCase.setName(StringUtils.join("接口用例", apiTestCase.getId())); apiTestCase.setName(StringUtils.join("接口用例", apiTestCase.getId()));
apiTestCase.setPriority("P0"); apiTestCase.setPriority("P0");