refactor(消息管理): 优化消息设置

This commit is contained in:
guoyuqi 2023-10-11 12:00:29 +08:00 committed by 刘瑞斌
parent e0ee4cd184
commit 887cc6fcb3
12 changed files with 246 additions and 32 deletions

View File

@ -94,6 +94,10 @@ project_not_exist=项目不存在
#消息管理 #消息管理
save_message_task_user_no_exist=所选用户部分不存在 save_message_task_user_no_exist=所选用户部分不存在
# robot # robot
robot_in_site=站内信
robot_in_site_description=系统内置,在顶部导航栏显示消息通知
robot_mail=邮件
robot_mail_description=系统内置,以添加用户邮箱为通知方式
robot_is_null=当前机器人不存在 robot_is_null=当前机器人不存在
ding_type_is_null=钉钉机器人的类型不能为空 ding_type_is_null=钉钉机器人的类型不能为空
ding_app_key_is_null=钉钉的AppKey不能为空 ding_app_key_is_null=钉钉的AppKey不能为空

View File

@ -107,6 +107,10 @@ fake_error_name_exist=Fake error name already exists
#消息管理 #消息管理
save_message_task_user_no_exist=The selected user section does not exist save_message_task_user_no_exist=The selected user section does not exist
# robot # robot
robot_in_site=Site message
robot_in_site_description=Built-in system, message notifications are displayed in the top navigation bar
robot_mail=Mail
robot_mail_description=Built-in system, adding user email as notification method
robot_is_null=The current robot does not exist robot_is_null=The current robot does not exist
ding_type_is_null= DingTalk robot type is required ding_type_is_null= DingTalk robot type is required
ding_app_key_is_null=DingTalk AppKey is required ding_app_key_is_null=DingTalk AppKey is required

View File

@ -107,6 +107,10 @@ fake_error_name_exist=误报名称已存在
#消息管理 #消息管理
save_message_task_user_no_exist=所选用户部分不存在 save_message_task_user_no_exist=所选用户部分不存在
# robot # robot
robot_in_site=站内信
robot_in_site_description=系统内置,在顶部导航栏显示消息通知
robot_mail=邮件
robot_mail_description=系统内置,以添加用户邮箱为通知方式
robot_is_null=当前机器人不存在 robot_is_null=当前机器人不存在
ding_type_is_null=钉钉机器人的类型不能为空 ding_type_is_null=钉钉机器人的类型不能为空
ding_app_key_is_null=钉钉的AppKey不能为空 ding_app_key_is_null=钉钉的AppKey不能为空

View File

@ -107,6 +107,10 @@ fake_error_name_exist=誤報名稱已存在
#消息管理 #消息管理
save_message_task_user_no_exist=所選用戶部分不存在 save_message_task_user_no_exist=所選用戶部分不存在
# robot # robot
robot_in_site=站內信
robot_in_site_description=系統內置,在頂部導航欄顯示訊息通知
robot_mail=郵件
robot_mail_description=系統內置,以新增使用者信箱為通知方式
robot_is_null=當前機器人不存在 robot_is_null=當前機器人不存在
ding_type_is_null=釘釘機器人的類型不能為空 ding_type_is_null=釘釘機器人的類型不能為空
ding_app_key_is_null = 釘釘的AppKey不能為空 ding_app_key_is_null = 釘釘的AppKey不能為空

View File

@ -10,6 +10,12 @@ import java.util.List;
@EqualsAndHashCode(callSuper = false) @EqualsAndHashCode(callSuper = false)
public class MessageTemplateConfigDTO extends ProjectRobotConfigDTO{ public class MessageTemplateConfigDTO extends ProjectRobotConfigDTO{
@Schema(description = "消息配置功能名称")
public String taskTypeName ;
@Schema(description = "消息配置场景名称")
public String eventName;
@Schema(description = "消息配置接收人") @Schema(description = "消息配置接收人")
private List<String> receiverIds; private List<String> receiverIds;

View File

@ -1,22 +1,37 @@
package io.metersphere.project.service; package io.metersphere.project.service;
import io.metersphere.project.domain.MessageTask;
import io.metersphere.project.domain.MessageTaskBlobExample;
import io.metersphere.project.domain.MessageTaskExample; import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.sdk.util.LogUtils; import io.metersphere.sdk.util.LogUtils;
import io.metersphere.system.service.CleanupProjectResourceService; import io.metersphere.system.service.CleanupProjectResourceService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.List;
@Component @Component
public class CleanupMessageTaskService implements CleanupProjectResourceService { public class CleanupMessageTaskService implements CleanupProjectResourceService {
@Resource @Resource
private MessageTaskMapper messageTaskMapper; private MessageTaskMapper messageTaskMapper;
@Resource
private MessageTaskBlobMapper messageTaskBlobMapper;
@Override @Override
public void deleteResources(String projectId) { public void deleteResources(String projectId) {
MessageTaskExample messageTaskExample = new MessageTaskExample(); MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo(projectId); messageTaskExample.createCriteria().andProjectIdEqualTo(projectId);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
List<String> ids = messageTasks.stream().map(MessageTask::getId).toList();
if (CollectionUtils.isNotEmpty(ids)) {
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(ids);
messageTaskBlobMapper.deleteByExample(messageTaskBlobExample);
}
messageTaskMapper.deleteByExample(messageTaskExample); messageTaskMapper.deleteByExample(messageTaskExample);
LogUtils.info("删除当前项目[" + projectId + "]相关消息管理资源"); LogUtils.info("删除当前项目[" + projectId + "]相关消息管理资源");
} }

View File

@ -12,6 +12,7 @@ import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.dto.OptionDTO; import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Translator;
import io.metersphere.system.notice.constants.NoticeConstants; import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.service.CreateProjectResourceService; import io.metersphere.system.service.CreateProjectResourceService;
import io.metersphere.system.uid.UUID; import io.metersphere.system.uid.UUID;
@ -47,21 +48,25 @@ public class CreateRobotResourceService implements CreateProjectResourceService
String inSiteId = UUID.randomUUID().toString(); String inSiteId = UUID.randomUUID().toString();
projectRobot.setId(inSiteId); projectRobot.setId(inSiteId);
projectRobot.setProjectId(projectId); projectRobot.setProjectId(projectId);
projectRobot.setName("站内信"); projectRobot.setName(Translator.get("robot_in_site"));
projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString()); projectRobot.setPlatform(ProjectRobotPlatform.IN_SITE.toString());
projectRobot.setWebhook("NONE"); projectRobot.setWebhook("NONE");
projectRobot.setCreateUser("admin"); projectRobot.setCreateUser("admin");
projectRobot.setCreateTime(System.currentTimeMillis()); projectRobot.setCreateTime(System.currentTimeMillis());
projectRobot.setUpdateUser("admin"); projectRobot.setUpdateUser("admin");
projectRobot.setUpdateTime(System.currentTimeMillis()); projectRobot.setUpdateTime(System.currentTimeMillis());
projectRobot.setEnable(true);
projectRobot.setDescription(Translator.get("robot_in_site_description"));
list.add(projectRobot); list.add(projectRobot);
ProjectRobot projectRobotMail = new ProjectRobot(); ProjectRobot projectRobotMail = new ProjectRobot();
projectRobotMail.setId(UUID.randomUUID().toString()); projectRobotMail.setId(UUID.randomUUID().toString());
projectRobotMail.setProjectId(projectId); projectRobotMail.setProjectId(projectId);
projectRobotMail.setName("邮件"); projectRobotMail.setName(Translator.get("robot_mail"));
projectRobotMail.setPlatform(ProjectRobotPlatform.MAIL.toString()); projectRobotMail.setPlatform(ProjectRobotPlatform.MAIL.toString());
projectRobotMail.setWebhook("NONE"); projectRobotMail.setWebhook("NONE");
projectRobotMail.setCreateUser("admin"); projectRobotMail.setCreateUser("admin");
projectRobotMail.setEnable(true);
projectRobotMail.setDescription(Translator.get("robot_mail_description"));
projectRobotMail.setCreateTime(System.currentTimeMillis()); projectRobotMail.setCreateTime(System.currentTimeMillis());
projectRobotMail.setUpdateUser("admin"); projectRobotMail.setUpdateUser("admin");
projectRobotMail.setUpdateTime(System.currentTimeMillis()); projectRobotMail.setUpdateTime(System.currentTimeMillis());

View File

@ -79,7 +79,7 @@ public class NoticeMessageTaskService {
Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId); Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId);
List<String> existUserIds = stringListMap.get(USER_IDS); List<String> existUserIds = stringListMap.get(USER_IDS);
//如果只选了用户没有选机器人默认机器人为站内信 //如果只选了用户没有选机器人默认机器人为站内信
ProjectRobot projectRobot = setDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId()); ProjectRobot projectRobot = getDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId());
String robotId = projectRobot.getId(); String robotId = projectRobot.getId();
messageTaskRequest.setRobotId(robotId); messageTaskRequest.setRobotId(robotId);
//检查设置的通知是否存在如果存在则更新 //检查设置的通知是否存在如果存在则更新
@ -140,7 +140,7 @@ public class NoticeMessageTaskService {
* @param robotId 机器人id * @param robotId 机器人id
* @return String * @return String
*/ */
private ProjectRobot setDefaultRobot(String projectId, String robotId) { private ProjectRobot getDefaultRobot(String projectId, String robotId) {
if (StringUtils.isBlank(robotId)) { if (StringUtils.isBlank(robotId)) {
ProjectRobotExample projectRobotExample = new ProjectRobotExample(); ProjectRobotExample projectRobotExample = new ProjectRobotExample();
projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(ProjectRobotPlatform.IN_SITE.toString()); projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(ProjectRobotPlatform.IN_SITE.toString());
@ -324,7 +324,7 @@ public class NoticeMessageTaskService {
Map<String, String> eventMap = MessageTemplateUtils.getEventMap(); Map<String, String> eventMap = MessageTemplateUtils.getEventMap();
Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap(); Map<String, String> defaultTemplateMap = MessageTemplateUtils.getDefaultTemplateMap();
Map<String, String> defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap(); Map<String, String> defaultTemplateSubjectMap = MessageTemplateUtils.getDefaultTemplateSubjectMap();
ProjectRobot projectRobot = setDefaultRobot(projectId, null); ProjectRobot projectRobot = getDefaultRobot(projectId, null);
for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) { for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) {
messageTaskDTO.setProjectId(projectId); messageTaskDTO.setProjectId(projectId);
messageTaskDTO.setName(moduleMap.get(messageTaskDTO.getType())); messageTaskDTO.setName(moduleMap.get(messageTaskDTO.getType()));
@ -435,7 +435,12 @@ public class NoticeMessageTaskService {
ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot, messageTask, messageTaskBlob); ProjectRobotConfigDTO projectRobotConfigDTO = getProjectRobotConfigDTO(defaultTemplate, defaultSubject, projectRobot, messageTask, messageTaskBlob);
MessageTemplateConfigDTO messageTemplateConfigDTO = new MessageTemplateConfigDTO(); MessageTemplateConfigDTO messageTemplateConfigDTO = new MessageTemplateConfigDTO();
BeanUtils.copyBean(messageTemplateConfigDTO,projectRobotConfigDTO); BeanUtils.copyBean(messageTemplateConfigDTO,projectRobotConfigDTO);
Map<String, String> taskTypeMap = MessageTemplateUtils.getTaskTypeMap();
Map<String, String> eventMap = MessageTemplateUtils.getEventMap();
messageTemplateConfigDTO.setTaskTypeName(taskTypeMap.get(messageTask.getTaskType()));
messageTemplateConfigDTO.setEventName(eventMap.get(messageTask.getEvent()));
messageTemplateConfigDTO.setReceiverIds(receiverIds); messageTemplateConfigDTO.setReceiverIds(receiverIds);
return messageTemplateConfigDTO; return messageTemplateConfigDTO;
} }
} }

View File

@ -1,25 +1,28 @@
package io.metersphere.project.service; package io.metersphere.project.service;
import io.metersphere.project.domain.MessageTaskExample; import io.metersphere.project.domain.*;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.domain.ProjectRobotExample;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform; import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.ProjectRobotType; import io.metersphere.project.enums.ProjectRobotType;
import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper; import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator; import io.metersphere.sdk.util.Translator;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
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.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import java.util.List; import java.util.List;
import io.metersphere.system.uid.UUID;
@Service @Service
@Transactional @Transactional
public class ProjectRobotService { public class ProjectRobotService {
@ -30,9 +33,15 @@ public class ProjectRobotService {
@Resource @Resource
private MessageTaskMapper messageTaskMapper; private MessageTaskMapper messageTaskMapper;
@Resource
private MessageTaskBlobMapper messageTaskBlobMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
public void add(ProjectRobot projectRobot) { public void add(ProjectRobot projectRobot) {
projectRobot.setId(UUID.randomUUID().toString()); projectRobot.setId(UUID.randomUUID().toString());
projectRobot.setEnable(true); projectRobot.setEnable(projectRobot.getEnable());
checkDingTalk(projectRobot); checkDingTalk(projectRobot);
robotMapper.insert(projectRobot); robotMapper.insert(projectRobot);
} }
@ -71,6 +80,13 @@ public class ProjectRobotService {
checkRobotExist(id); checkRobotExist(id);
MessageTaskExample messageTaskExample = new MessageTaskExample(); MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectRobotIdEqualTo(id); messageTaskExample.createCriteria().andProjectRobotIdEqualTo(id);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
List<String> ids = messageTasks.stream().map(MessageTask::getId).toList();
if (CollectionUtils.isNotEmpty(ids)) {
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(ids);
messageTaskBlobMapper.deleteByExample(messageTaskBlobExample);
}
messageTaskMapper.deleteByExample(messageTaskExample); messageTaskMapper.deleteByExample(messageTaskExample);
robotMapper.deleteByPrimaryKey(id); robotMapper.deleteByPrimaryKey(id);
} }
@ -86,6 +102,17 @@ public class ProjectRobotService {
projectRobot.setCreateTime(null); projectRobot.setCreateTime(null);
projectRobot.setUpdateUser(updateUser); projectRobot.setUpdateUser(updateUser);
projectRobot.setUpdateTime(updateTime); projectRobot.setUpdateTime(updateTime);
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectRobotIdEqualTo(id);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
MessageTaskMapper mapper = sqlSession.getMapper(MessageTaskMapper.class);
for (MessageTask messageTask : messageTasks) {
messageTask.setEnable(projectRobot.getEnable());
mapper.updateByPrimaryKeySelective(messageTask);
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
robotMapper.updateByPrimaryKeySelective(projectRobot); robotMapper.updateByPrimaryKeySelective(projectRobot);
} }

View File

@ -20,6 +20,7 @@ import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.UUID;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@ -43,9 +44,10 @@ public class CreateRobotResourceTests extends BaseTest {
@Test @Test
@Order(1) @Order(1)
public void testCleanupResource() throws Exception { public void testCreateResource() throws Exception {
String id = UUID.randomUUID().toString();
Project project = new Project(); Project project = new Project();
project.setId("test_message"); project.setId(id);
project.setOrganizationId("organization-message-test"); project.setOrganizationId("organization-message-test");
project.setName("默认项目"); project.setName("默认项目");
project.setDescription("系统默认创建的项目"); project.setDescription("系统默认创建的项目");
@ -54,16 +56,16 @@ public class CreateRobotResourceTests extends BaseTest {
project.setCreateTime(System.currentTimeMillis()); project.setCreateTime(System.currentTimeMillis());
project.setUpdateTime(System.currentTimeMillis()); project.setUpdateTime(System.currentTimeMillis());
projectMapper.insertSelective(project); projectMapper.insertSelective(project);
serviceInvoker.invokeCreateServices("test_message"); serviceInvoker.invokeCreateServices(id);
List<ProjectRobot> projectRobotAfters = getList(); List<ProjectRobot> projectRobotAfters = getList(id);
Assertions.assertEquals(2, projectRobotAfters.size()); Assertions.assertEquals(2, projectRobotAfters.size());
List<MessageTaskDTO> messageList = getMessageList(); List<MessageTaskDTO> messageList = getMessageList(id);
Assertions.assertTrue(messageList.size() > 0); Assertions.assertTrue(messageList.size() > 0);
System.out.println(messageList); System.out.println(messageList);
} }
private List<ProjectRobot> getList() throws Exception { private List<ProjectRobot> getList(String id) throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_LIST + "test_message") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_LIST + id)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
@ -74,8 +76,8 @@ public class CreateRobotResourceTests extends BaseTest {
return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class); return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class);
} }
private List<MessageTaskDTO> getMessageList() throws Exception { private List<MessageTaskDTO> getMessageList(String id) throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/test_message") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/"+id)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)) .header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk()) .andExpect(status().isOk())

View File

@ -1,19 +1,32 @@
package io.metersphere.project.controller; package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.*;
import io.metersphere.project.domain.ProjectRobotExample; import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.MessageTaskDetailDTO;
import io.metersphere.project.dto.MessageTaskTypeDTO;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform; import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.ProjectRobotType; import io.metersphere.project.enums.ProjectRobotType;
import io.metersphere.project.mapper.MessageTaskBlobMapper;
import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.dto.OptionDTO;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.system.base.BaseTest; import io.metersphere.system.base.BaseTest;
import io.metersphere.system.controller.handler.ResultHolder; import io.metersphere.system.controller.handler.ResultHolder;
import io.metersphere.system.notice.constants.NoticeConstants;
import io.metersphere.system.uid.UUID;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
@ -21,7 +34,7 @@ import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.UnsupportedEncodingException; import java.io.*;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
@ -48,6 +61,12 @@ public class ProjectRobotControllerTests extends BaseTest {
@Resource @Resource
private ProjectRobotMapper projectRobotMapper; private ProjectRobotMapper projectRobotMapper;
@Resource
private ProjectMapper projectMapper;
@Resource
private SqlSessionFactory sqlSessionFactory;
@Test @Test
@Order(1) @Order(1)
@ -84,16 +103,26 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test @Test
@Order(3) @Order(3)
void addRobotSuccessDingCustom() throws Exception { void addRobotSuccessDingCustom() throws Exception {
setDingCustom("钉钉自定义机器人"); setDingCustom("test_project3","钉钉自定义机器人4");
checkName("test_project", "钉钉自定义机器人"); checkName("test_project3", "钉钉自定义机器人4");
} }
private void setDingCustom(String name) throws Exception { private void setInSiteCustom(String projectId,String name) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.IN_SITE.toString());
projectRobotDTO.setProjectId(projectId);
projectRobotDTO.setEnable(true);
projectRobotDTO.setWebhook("NONE");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
}
private void setDingCustom(String projectId,String name) throws Exception {
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString()); projectRobotDTO.setPlatform(ProjectRobotPlatform.DING_TALK.toString());
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId(projectId);
projectRobotDTO.setEnable(true);
projectRobotDTO.setType(ProjectRobotType.CUSTOM.toString()); projectRobotDTO.setType(ProjectRobotType.CUSTOM.toString());
projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=fd963136a4d7eebaaa68de261223089148e62d7519fbaf426626fe3157725b8a"); projectRobotDTO.setWebhook("https://oapi.dingtalk.com/robot/send?access_token=fd963136a4d7eebaaa68de261223089148e62d7519fbaf426626fe3157725b8a");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
@ -165,7 +194,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test @Test
@Order(9) @Order(9)
void updateRobotSuccessDingCus() throws Exception { void updateRobotSuccessDingCus() throws Exception {
setDingCustom("用于更新钉钉自定义机器人"); setDingCustom("test_project","用于更新钉钉自定义机器人");
ProjectRobot projectRobot = getRobot("test_project", "用于更新钉钉自定义机器人"); ProjectRobot projectRobot = getRobot("test_project", "用于更新钉钉自定义机器人");
checkUpdate(projectRobot, "更新钉钉自定义机器人", status().isOk()); checkUpdate(projectRobot, "更新钉钉自定义机器人", status().isOk());
} }
@ -199,7 +228,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test @Test
@Order(13) @Order(13)
void updateRobotFileNoDingType() throws Exception { void updateRobotFileNoDingType() throws Exception {
setDingCustom("测试更新没有Type失败"); setDingCustom("test_project","测试更新没有Type失败");
ProjectRobot projectRobot = getRobot("test_project", "测试更新没有Type失败"); ProjectRobot projectRobot = getRobot("test_project", "测试更新没有Type失败");
projectRobot.setType(null); projectRobot.setType(null);
checkUpdate(projectRobot, "测试更新没有Type失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试更新没有Type失败", status().is5xxServerError());
@ -324,6 +353,30 @@ public class ProjectRobotControllerTests extends BaseTest {
} }
@Test
@Order(21)
void deleteRobotWithMessage() throws Exception {
Project project = new Project();
project.setId("test_project1");
project.setOrganizationId("organization-message-test");
project.setName("默认项目");
project.setDescription("系统默认创建的项目");
project.setCreateUser("admin");
project.setUpdateUser("admin");
project.setCreateTime(System.currentTimeMillis());
project.setUpdateTime(System.currentTimeMillis());
projectMapper.insertSelective(project);
setInSiteCustom("test_project1","站内信1");
setDingCustom("test_project1","钉钉自定义机器人3");
ProjectRobot robot = getRobot("test_project1", "钉钉自定义机器人3");
setMessageTask("test_project1",robot.getId());
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DELETE + "/" + robot.getId())
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
private static ProjectRobot getResult(MvcResult mvcResult) throws UnsupportedEncodingException { private static ProjectRobot getResult(MvcResult mvcResult) throws UnsupportedEncodingException {
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
@ -348,18 +401,20 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setName(name); projectRobotDTO.setName(name);
projectRobotDTO.setPlatform(ProjectRobotPlatform.CUSTOM.toString()); projectRobotDTO.setPlatform(ProjectRobotPlatform.CUSTOM.toString());
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setEnable(true);
projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8"); projectRobotDTO.setWebhook("https://qyapi.weixin.qq.com/cgi-bin/webhook/send?key=2b67ccf4-e0da-4cd6-ae74-8d42657865f8");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
} }
private void getPostResult(ProjectRobotDTO projectRobotDTO, String url, ResultMatcher resultMatcher) throws Exception { private ProjectRobot getPostResult(ProjectRobotDTO projectRobotDTO, String url, ResultMatcher resultMatcher) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post(url) MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(projectRobotDTO)) .content(JSON.toJSONString(projectRobotDTO))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(resultMatcher) .andExpect(resultMatcher)
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
return getResult(mvcResult);
} }
private List<ProjectRobot> getList(String projectId) throws Exception { private List<ProjectRobot> getList(String projectId) throws Exception {
@ -381,5 +436,82 @@ public class ProjectRobotControllerTests extends BaseTest {
return projectRobots.get(0); return projectRobots.get(0);
} }
public void setMessageTask(String projectId, String defaultRobotId) throws Exception {
StringBuilder jsonStr = new StringBuilder();
InputStream inputStream = getClass().getResourceAsStream("/message_task.json");
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
try {
while ((line = reader.readLine()) != null) {
jsonStr.append(line);
}
reader.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
MessageTaskMapper mapper = sqlSession.getMapper(MessageTaskMapper.class);
MessageTaskBlobMapper blobMapper = sqlSession.getMapper(MessageTaskBlobMapper.class);
List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(jsonStr.toString(), MessageTaskDTO.class);
for (MessageTaskDTO messageTaskDTO : messageTaskDTOList) {
List<MessageTaskTypeDTO> messageTaskTypeDTOList = messageTaskDTO.getMessageTaskTypeDTOList();
for (MessageTaskTypeDTO messageTaskTypeDTO : messageTaskTypeDTOList) {
String taskType = messageTaskTypeDTO.getTaskType();
if (taskType.contains(NoticeConstants.Mode.SCHEDULE) || taskType.contains("AT") || taskType.contains("JENKINS")) {
continue;
}
List<MessageTaskDetailDTO> messageTaskDetailDTOList = messageTaskTypeDTO.getMessageTaskDetailDTOList();
for (MessageTaskDetailDTO messageTaskDetailDTO : messageTaskDetailDTOList) {
String event = messageTaskDetailDTO.getEvent();
List<OptionDTO> receivers = messageTaskDetailDTO.getReceivers();
if (StringUtils.equalsIgnoreCase(event, NoticeConstants.Event.CREATE) || StringUtils.equalsIgnoreCase(event, NoticeConstants.Event.CASE_CREATE) || CollectionUtils.isEmpty(receivers)) {
continue;
}
for (OptionDTO receiver : receivers) {
String id = UUID.randomUUID().toString();
MessageTask messageTask = new MessageTask();
messageTask.setId(id);
messageTask.setEvent(event);
messageTask.setTaskType(taskType);
messageTask.setReceiver(receiver.getId());
messageTask.setProjectId(projectId);
messageTask.setProjectRobotId(defaultRobotId);
messageTask.setEnable(true);
messageTask.setTestId("NONE");
messageTask.setCreateUser("admin");
messageTask.setCreateTime(System.currentTimeMillis());
messageTask.setUpdateUser("admin");
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setSubject("");
messageTask.setUseDefaultSubject(true);
messageTask.setUseDefaultTemplate(true);
MessageTaskBlob messageTaskBlob = new MessageTaskBlob();
messageTaskBlob.setId(id);
messageTaskBlob.setTemplate("");
mapper.insert(messageTask);
blobMapper.insert(messageTaskBlob);
}
}
}
}
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
List<MessageTaskDTO> testProject1 = getMessageList(projectId);
Assertions.assertTrue(testProject1.size()>0);
System.out.println(testProject1);
}
private List<MessageTaskDTO> getMessageList(String projectId) throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/"+projectId)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
return JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
}
} }

View File

@ -43,6 +43,12 @@ public class CleanupMessageTaskResourceTests extends BaseTest {
@Test @Test
@Order(2) @Order(2)
public void testCleanupResourceNoMessage() throws Exception {
resourceService.cleanReportResources("test1");
}
@Test
@Order(3)
public void testCleanupReportResource() throws Exception { public void testCleanupReportResource() throws Exception {
resourceService.cleanReportResources("test"); resourceService.cleanReportResources("test");
} }