feat(消息管理): 增加消息管理查询功能

This commit is contained in:
guoyuqi 2023-09-14 19:00:30 +08:00 committed by 刘瑞斌
parent b5ed0ab0f0
commit ae5839b653
7 changed files with 387 additions and 51 deletions

View File

@ -17,10 +17,6 @@ public class MessageTaskRequest {
@NotBlank(message = "{message_task.project_id.not_blank}", groups = {Created.class, Updated.class})
public String projectId;
@Schema(description = "消息配置ID", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{message_task.id.not_blank}", groups = {Updated.class})
public String id;
@Schema(description = "消息配置功能", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{message_task.taskType.not_blank}", groups = {Created.class, Updated.class})
public String taskType;
@ -37,15 +33,13 @@ public class MessageTaskRequest {
public String testId;
@Schema(description = "消息配置机器人id")
@NotBlank(message = "{message_task.robotId.not_blank}", groups = {Created.class, Updated.class})
public String robotId;
@Schema(description = "消息配置机器人是否开启")
@NotNull(message = "{message_task.enable.not_blank}", groups = {Created.class, Updated.class})
public Boolean enable;
@Schema(description = "消息配置消息模版")
@NotBlank(message = "{message_task.robotId.not_blank}", groups = {Created.class, Updated.class})
@NotBlank(message = "{message_task.template.not_blank}", groups = {Created.class, Updated.class})
public String template;
}

View File

@ -1,6 +1,7 @@
package io.metersphere.project.controller;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.service.NoticeMessageTaskService;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
@ -12,6 +13,7 @@ import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Tag(name = "项目管理-消息设置")
@ -21,11 +23,17 @@ public class NoticeMessageTaskController {
@Resource
private NoticeMessageTaskService noticeMessageTaskService;
@PostMapping("message/task/add")
@PostMapping("message/task/save")
@Operation(summary = "项目管理-消息设置-保存消息设置")
public ResultHolder saveMessage(@Validated({Created.class}) @RequestBody MessageTaskRequest messageTaskRequest) {
return noticeMessageTaskService.saveMessageTask(messageTaskRequest, SessionUtils.getUserId());
}
@GetMapping("message/task/get/{projectId}")
@Operation(summary = "项目管理-消息设置-获取消息设置")
public List<MessageTaskDTO> getMessageList(@PathVariable String projectId) {
return noticeMessageTaskService.getMessageList(projectId);
}
}

View File

@ -0,0 +1,27 @@
package io.metersphere.project.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.List;
@Data
@EqualsAndHashCode(callSuper = false)
public class MessageTaskDTO {
@Schema(description = "消息配置所在项目ID")
public String projectId;
@Schema(description = "消息配置功能")
public String taskType;
@Schema(description = "消息配置场景")
public String event;
@Schema(description = "消息配置接收人")
private List<String> receiverIds;
@Schema(description = "消息配置机器人设置")
private List<ProjectRobotConfigDTO> projectRobotConfigList;
}

View File

@ -0,0 +1,19 @@
package io.metersphere.project.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
import lombok.EqualsAndHashCode;
@Data
@EqualsAndHashCode(callSuper = false)
public class ProjectRobotConfigDTO {
@Schema(description = "消息配置机器人id")
public String robotId;
@Schema(description = "消息配置机器人是否开启")
public Boolean enable;
@Schema(description = "消息配置机器人发送模版")
public String template;
}

View File

@ -1,11 +1,15 @@
package io.metersphere.project.service;
import io.metersphere.project.domain.MessageTask;
import io.metersphere.project.domain.MessageTaskBlob;
import io.metersphere.project.domain.*;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.dto.ProjectRobotConfigDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.result.ProjectResultCode;
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.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.sdk.exception.MSException;
@ -27,10 +31,8 @@ import org.mybatis.spring.SqlSessionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.stream.Collectors;
@Service
@Transactional(rollbackFor = Exception.class)
@ -42,6 +44,15 @@ public class NoticeMessageTaskService {
private SqlSessionFactory sqlSessionFactory;
@Resource
private UserRoleRelationMapper userRoleRelationMapper;
@Resource
private ProjectRobotMapper projectRobotMapper;
@Resource
private MessageTaskMapper messageTaskMapper;
@Resource
private MessageTaskBlobMapper messageTaskBlobMapper;
@Resource
private ProjectMapper projectMapper;
public static final String USER_IDS = "user_ids";
@ -53,33 +64,22 @@ public class NoticeMessageTaskService {
public ResultHolder saveMessageTask(MessageTaskRequest messageTaskRequest, String userId) {
String projectId = messageTaskRequest.getProjectId();
checkProjectExist(projectId);
SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH);
MessageTaskMapper mapper = sqlSession.getMapper(MessageTaskMapper.class);
MessageTaskBlobMapper blobMapper = sqlSession.getMapper(MessageTaskBlobMapper.class);
//检查用户是否存在
Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), messageTaskRequest.getProjectId());
for (String receiverId : stringListMap.get(USER_IDS)) {
MessageTask messageTask = new MessageTask();
String insertId = UUID.randomUUID().toString();
messageTask.setId(insertId);
messageTask.setTaskType(messageTaskRequest.getTaskType());
messageTask.setEvent(messageTaskRequest.getEvent());
messageTask.setReceiver(receiverId);
messageTask.setProjectId(messageTaskRequest.getProjectId());
messageTask.setProjectRobotId(messageTaskRequest.getRobotId());
String testId = messageTaskRequest.getTestId() == null ? "NONE" : messageTaskRequest.getTestId();
messageTask.setTestId(testId);
messageTask.setCreateUser(userId);
messageTask.setCreateTime(System.currentTimeMillis());
messageTask.setUpdateUser(userId);
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setEnable(messageTaskRequest.getEnable());
mapper.insert(messageTask);
MessageTaskBlob messageTaskBlob = new MessageTaskBlob();
messageTaskBlob.setId(messageTask.getId());
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
blobMapper.insert(messageTaskBlob);
}
Map<String, List<String>> stringListMap = checkUserExistProject(messageTaskRequest.getReceiverIds(), projectId);
List<String> existUserIds = stringListMap.get(USER_IDS);
//如果只选了用户没有选机器人默认机器人为站内信
String robotId = setDefaultRobot(messageTaskRequest.getProjectId(), messageTaskRequest.getRobotId());
messageTaskRequest.setRobotId(robotId);
//检查设置的通知是否存在如果存在则更新
List<MessageTask> messageTasks = updateMessageTasks(messageTaskRequest, userId, mapper, blobMapper, existUserIds);
//保存消息任务
List<String> messageTaskReceivers = CollectionUtils.isEmpty(messageTasks) ? new ArrayList<>() : messageTasks.stream().map(MessageTask::getReceiver).toList();
insertMessageTask(messageTaskRequest, userId, mapper, blobMapper, existUserIds, messageTaskReceivers);
sqlSession.flushStatements();
SqlSessionUtils.closeSqlSession(sqlSession, sqlSessionFactory);
if (CollectionUtils.isNotEmpty(stringListMap.get(NO_USER_NAMES))) {
@ -89,6 +89,112 @@ public class NoticeMessageTaskService {
return ResultHolder.success("OK");
}
private void checkProjectExist(String projectId) {
Project project = projectMapper.selectByPrimaryKey(projectId);
if (project == null) {
throw new MSException(Translator.get("project_is_not_exist"));
}
}
/**
* 新增MessageTask
* @param messageTaskRequest 入参
* @param userId 当前用户i的
* @param mapper MessageTaskMapper
* @param blobMapper MessageTaskBlobMapper
* @param existUserIds 系统中还存在的入参传过来的接收人
* @param messageTaskReceivers 更新过后还有多少接收人需要保存
*/
private static void insertMessageTask(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List<String> existUserIds, List<String> messageTaskReceivers) {
String testId = messageTaskRequest.getTestId() == null ? "NONE" : messageTaskRequest.getTestId();
boolean enable = messageTaskRequest.getEnable() != null && messageTaskRequest.getEnable();
for (String receiverId : existUserIds) {
if (CollectionUtils.isNotEmpty(messageTaskReceivers) && messageTaskReceivers.contains(receiverId)) {
continue;
}
MessageTask messageTask = new MessageTask();
buildMessageTask(messageTaskRequest, userId, messageTaskRequest.getRobotId(), testId, enable, receiverId, messageTask);
mapper.insert(messageTask);
MessageTaskBlob messageTaskBlob = new MessageTaskBlob();
messageTaskBlob.setId(messageTask.getId());
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
blobMapper.insert(messageTaskBlob);
}
}
/**
* 查询默认机器人id
* @param projectId 项目id
* @param robotId 机器人id
* @return String
*/
private String setDefaultRobot(String projectId, String robotId) {
if (StringUtils.isBlank(robotId)) {
ProjectRobotExample projectRobotExample = new ProjectRobotExample();
projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andPlatformEqualTo(ProjectRobotPlatform.IN_SITE.toString());
List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample);
robotId = projectRobots.get(0).getId();
}
return robotId;
}
/**
* 检查数据库是否有同类型数据有则更新
* @param messageTaskRequest 入参
* @param userId 当前用户ID
* @param mapper MessageTaskMapper
* @param blobMapper MessageTaskBlobMapper
* @param existUserIds 系统中还存在的入参传过来的接收人
* @return List<MessageTask>
*/
private List<MessageTask> updateMessageTasks(MessageTaskRequest messageTaskRequest, String userId, MessageTaskMapper mapper, MessageTaskBlobMapper blobMapper, List<String> existUserIds) {
boolean enable = messageTaskRequest.getEnable() != null && messageTaskRequest.getEnable();
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andReceiverIn(existUserIds).andProjectIdEqualTo(messageTaskRequest.getProjectId())
.andProjectRobotIdEqualTo(messageTaskRequest.getRobotId()).andTaskTypeEqualTo(messageTaskRequest.getTaskType()).andEventEqualTo(messageTaskRequest.getEvent());
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
if (CollectionUtils.isEmpty(messageTasks)) {
return new ArrayList<>();
}
for (MessageTask messageTask : messageTasks) {
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setUpdateUser(userId);
messageTask.setEnable(enable);
mapper.updateByPrimaryKeySelective(messageTask);
}
List<String> messageTaskIds = messageTasks.stream().map(MessageTask::getId).toList();
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(messageTaskIds);
List<MessageTaskBlob> messageTaskBlobs = messageTaskBlobMapper.selectByExample(messageTaskBlobExample);
for (MessageTaskBlob messageTaskBlob : messageTaskBlobs) {
messageTaskBlob.setTemplate(messageTaskRequest.getTemplate());
blobMapper.updateByPrimaryKeySelective(messageTaskBlob);
}
return messageTasks;
}
private static void buildMessageTask(MessageTaskRequest messageTaskRequest, String userId, String robotId, String testId, boolean enable, String receiverId, MessageTask messageTask) {
String insertId = UUID.randomUUID().toString();
messageTask.setId(insertId);
messageTask.setTaskType(messageTaskRequest.getTaskType());
messageTask.setEvent(messageTaskRequest.getEvent());
messageTask.setReceiver(receiverId);
messageTask.setProjectId(messageTaskRequest.getProjectId());
messageTask.setProjectRobotId(robotId);
messageTask.setTestId(testId);
messageTask.setCreateUser(userId);
messageTask.setCreateTime(System.currentTimeMillis());
messageTask.setUpdateUser(userId);
messageTask.setUpdateTime(System.currentTimeMillis());
messageTask.setEnable(enable);
}
/**
* 检查用户是否存在
* @param receiverIds 接收人ids
* @param projectId 项目id
* @return Map<String, List<String>>
*/
private Map<String, List<String>> checkUserExistProject(List<String> receiverIds, String projectId) {
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdIn(receiverIds).andSourceIdEqualTo(projectId);
@ -113,4 +219,44 @@ public class NoticeMessageTaskService {
return map;
}
public List<MessageTaskDTO> getMessageList(String projectId) {
checkProjectExist(projectId);
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectIdEqualTo(projectId);
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
if (CollectionUtils.isEmpty(messageTasks)) {
return new ArrayList<>();
}
List<String> messageTaskIds = messageTasks.stream().map(MessageTask::getId).toList();
MessageTaskBlobExample messageTaskBlobExample = new MessageTaskBlobExample();
messageTaskBlobExample.createCriteria().andIdIn(messageTaskIds);
List<MessageTaskBlob> messageTaskBlobs = messageTaskBlobMapper.selectByExample(messageTaskBlobExample);
Map<String, MessageTaskBlob> messageTaskBlobMap = messageTaskBlobs.stream().collect(Collectors.toMap(MessageTaskBlob::getId, item -> item));
Map<String, List<MessageTask>> messageMap = messageTasks.stream().collect(Collectors.groupingBy(t -> (t.getTaskType() + "-" + t.getEvent())));
List<MessageTaskDTO> list = new ArrayList<>();
messageMap.forEach((key, messageTaskList) -> {
MessageTaskDTO messageTaskDTO = new MessageTaskDTO();
int i = key.indexOf("-");
String taskType = key.substring(0, i);
String event = key.substring(i+1);
messageTaskDTO.setProjectId(projectId);
messageTaskDTO.setTaskType(taskType);
messageTaskDTO.setEvent(event);
Set<String>receiverIds = new HashSet<>();
List<ProjectRobotConfigDTO>projectRobotConfigList = new ArrayList<>();
for (MessageTask messageTask : messageTaskList) {
MessageTaskBlob messageTaskBlob = messageTaskBlobMap.get(messageTask.getId());
receiverIds.add(messageTask.getReceiver());
ProjectRobotConfigDTO projectRobotConfigDTO = new ProjectRobotConfigDTO();
projectRobotConfigDTO.setRobotId(messageTask.getProjectRobotId());
projectRobotConfigDTO.setEnable(messageTask.getEnable());
projectRobotConfigDTO.setTemplate(messageTaskBlob.getTemplate());
projectRobotConfigList.add(projectRobotConfigDTO);
}
messageTaskDTO.setReceiverIds(new ArrayList<>(receiverIds));
messageTaskDTO.setProjectRobotConfigList(projectRobotConfigList);
list.add(messageTaskDTO);
});
return list;
}
}

View File

@ -1,13 +1,18 @@
package io.metersphere.project.controller;
import io.metersphere.project.domain.MessageTask;
import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.dto.MessageTaskDTO;
import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.sdk.base.BaseTest;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.request.MessageTaskRequest;
import io.metersphere.sdk.notice.constants.NoticeConstants;
import io.metersphere.sdk.util.JSON;
import jakarta.annotation.Resource;
import org.apache.commons.collections.CollectionUtils;
import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
@ -30,10 +35,13 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class NoticeMessageTaskControllerTests extends BaseTest {
@Resource
private MessageTaskMapper messageTaskMapper;
@Test
@Order(1)
@Sql(scripts = {"/dml/init_project_message.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void addMessageTaskHalfSuccess() throws Exception {
public void addMessageTaskUserNoExistSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
@ -46,7 +54,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/add")
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
@ -60,7 +68,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
@Test
@Order(2)
public void addMessageTaskSuccess() throws Exception {
public void addMessageTaskCheckUserSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test-1");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
@ -72,7 +80,7 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/add")
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
@ -86,19 +94,19 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
@Test
@Order(3)
public void addMessageTaskFile() throws Exception {
public void addMessageTaskCheckUserFile() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test-1");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("project-message-user-5");
userIds.add("project-message-user-6");
userIds.add("project-message-user-X");
userIds.add("project-message-user-Y");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(true);
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/add")
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
@ -110,4 +118,128 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
Assertions.assertEquals(100500, resultHolder.getCode());
}
@Test
@Order(4)
public void addMessageTaskCheckRobotSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test-1");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("project-message-user-7");
userIds.add("project-message-user-8");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
.contentType(MediaType.APPLICATION_JSON))
.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);
Assertions.assertEquals(100200, resultHolder.getCode());
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK)
.andEventEqualTo(NoticeConstants.Event.CREATE).andReceiverEqualTo("project-message-user-8").andProjectIdEqualTo("project-message-test-1");
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertEquals("test_message_robot1", messageTasks.get(0).getProjectRobotId());
Assertions.assertEquals(false, messageTasks.get(0).getEnable());
}
@Test
@Order(5)
public void addMessageTaskCheckOldExistSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test-1");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("project-message-user-7");
userIds.add("project-message-user-8");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot1");
messageTaskRequest.setEnable(true);
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
.contentType(MediaType.APPLICATION_JSON))
.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);
Assertions.assertEquals(100200, resultHolder.getCode());
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andTaskTypeEqualTo(NoticeConstants.TaskType.API_DEFINITION_TASK)
.andEventEqualTo(NoticeConstants.Event.CREATE).andReceiverEqualTo("project-message-user-8").andProjectIdEqualTo("project-message-test-1");
List<MessageTask> messageTasks = messageTaskMapper.selectByExample(messageTaskExample);
Assertions.assertEquals(1, messageTasks.size());
Assertions.assertEquals(true, messageTasks.get(0).getEnable());
}
@Test
@Order(6)
public void addMessageTaskCheckProjectExistFail() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test-3");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("project-message-user-7");
userIds.add("project-message-user-8");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot1");
messageTaskRequest.setEnable(true);
messageTaskRequest.setTemplate("发送消息测试");
mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(messageTaskRequest))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().is5xxServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
}
@Test
@Order(7)
public void getMessageListSuccess() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/project-message-test-1")
.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);
List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
Assertions.assertTrue(CollectionUtils.isNotEmpty(messageTaskDTOList));
Assertions.assertEquals(messageTaskDTOList.get(0).getTaskType(),NoticeConstants.TaskType.API_DEFINITION_TASK);
}
@Test
@Order(8)
public void getMessageListProjectFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/project-message-test-3")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().is5xxServerError())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
@Order(9)
public void getMessageListEmpty() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get("/notice/message/task/get/project-message-test-2")
.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);
List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
Assertions.assertTrue(CollectionUtils.isEmpty(messageTaskDTOList));
}
}

View File

@ -3,7 +3,8 @@ INSERT INTO organization(id, num, name, description, create_time, update_time, c
('organization-message-test', null, 'organization-message-test', 'organization-message-test', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, 'admin', 'admin', 0, null, null);
INSERT INTO project (id, num, organization_id, name, description, create_user, update_user, create_time, update_time) VALUES
('project-message-test', null, 'organization-message-test', '默认项目', '系统默认创建的项目', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000),
('project-message-test-1', null, 'organization-message-test-1', '默认项目1', '系统默认创建的项目1', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
('project-message-test-1', null, 'organization-message-test-1', '默认项目1', '系统默认创建的项目1', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000),
('project-message-test-2', null, 'organization-message-test-2', '默认项目2', '系统默认创建的项目1', 'admin', 'admin', UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000);
INSERT INTO user(id, name, email, password, create_time, update_time, language, last_organization_id, phone, source, last_project_id, create_user, update_user, deleted) VALUES
@ -11,19 +12,28 @@ INSERT INTO user(id, name, email, password, create_time, update_time, language,
('project-message-user-2', 'project-message-user-2', 'project-message-member2@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-3', 'project-message-user-3', 'project-message-member3@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-4', 'project-message-user-4', 'project-message-member4@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-5', 'project-message-user-5', 'project-message-member5@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-6', 'project-message-user-6', 'project-message-member6@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-7', 'project-message-user-7', 'project-message-member7@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-8', 'project-message-user-8', 'project-message-member8@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 0),
('project-message-user-del', 'project-message-user-del', 'project-message-member-del@metersphere.io', MD5('metersphere'), UNIX_TIMESTAMP() * 1000, UNIX_TIMESTAMP() * 1000, NULL, NUll, '', 'LOCAL', NULL, 'admin', 'admin', 1);
INSERT INTO user_role_relation (id, user_id, role_id, source_id, organization_id, create_time, create_user) VALUES
(UUID(), 'project-message-user-1', 'project_member', 'project-message-test', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-2', 'project_member', 'project-message-test', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-3', 'project_member', 'project-message-test-1', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-4', 'project_member', 'project-message-test-1', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin');
(UUID(), 'project-message-user-4', 'project_member', 'project-message-test-1', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-5', 'project_member', 'project-message-test', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-6', 'project_member', 'project-message-test', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-7', 'project_member', 'project-message-test-1', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin'),
(UUID(), 'project-message-user-8', 'project_member', 'project-message-test-1', 'organization-message-test', UNIX_TIMESTAMP() * 1000, 'admin');
replace INTO project_robot(id, project_id, name, platform, webhook, type, app_key, app_secret, enable, create_user, create_time, update_user, update_time, description) VALUES ('test_message_robot1', 'test', '测试机器人1', 'IN_SITE', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null);
replace INTO project_robot(id, project_id, name, platform, webhook, type, app_key, app_secret, enable, create_user, create_time, update_user, update_time, description) VALUES ('test_message_robot2', 'test', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null);
INSERT INTO project_robot(id, project_id, name, platform, webhook, type, app_key, app_secret, enable, create_user, create_time, update_user, update_time, description) VALUES ('test_message_robot1', 'project-message-test-1', '测试机器人1', 'IN_SITE', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null),
('test_message_robot2', 'project-message-test-1', '测试机器人2', 'MAIL', 'NONE', null, null, null, true, 'admin', unix_timestamp() * 1000,'admin', unix_timestamp() * 1000, null);