refactor(消息管理): 补充测试用例

This commit is contained in:
guoyuqi 2023-09-15 17:44:08 +08:00 committed by fit2-zhao
parent 8d8e26e623
commit 62aac9fbde
4 changed files with 145 additions and 7 deletions

View File

@ -196,14 +196,26 @@ public class NoticeMessageTaskService {
* @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);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
List<String> userIds = userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().toList();
Map<String, List<String>> map = new HashMap<>();
if (CollectionUtils.isEmpty(userIds)) {
throw new MSException(Translator.get("user.not.exist"));
List<String>normalUserIds = new ArrayList<>();
for (String receiverId : receiverIds) {
if (!StringUtils.equalsIgnoreCase(receiverId, CREATOR) && !StringUtils.equalsIgnoreCase(receiverId, FOLLOW_PEOPLE)) {
normalUserIds.add(receiverId);
}
}
List<String> userIds = new ArrayList<>();
if (CollectionUtils.isNotEmpty(normalUserIds)) {
UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample();
userRoleRelationExample.createCriteria().andUserIdIn(normalUserIds).andSourceIdEqualTo(projectId);
List<UserRoleRelation> userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample);
userIds = new ArrayList<>(userRoleRelations.stream().map(UserRoleRelation::getUserId).distinct().toList());
}
if (receiverIds.contains(CREATOR)) {
userIds.add(CREATOR);
}
if (receiverIds.contains(FOLLOW_PEOPLE)) {
userIds.add(FOLLOW_PEOPLE);
}
Map<String, List<String>> map = new HashMap<>();
List<String> noUserNames = new ArrayList<>();
if (userIds.size() < receiverIds.size()) {
for (String receiverId : receiverIds) {

View File

@ -66,6 +66,7 @@ public class NoticeTemplateService {
addOptionDto(optionDTOList, allFields);
//TODO获取报告和自定义
}
default -> optionDTOList = new ArrayList<>();
}
return optionDTOList;
}

View File

@ -129,6 +129,8 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
userIds.add("project-message-user-7");
userIds.add("project-message-user-8");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setEnable(null);
messageTaskRequest.setTestId("testId");
messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId)
@ -242,4 +244,111 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
Assertions.assertTrue(CollectionUtils.isEmpty(messageTaskDTOList));
}
@Test
@Order(10)
public void addMessageTaskCheckSpecialUserSuccess() 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("CREATOR");
userIds.add("FOLLOW_PEOPLE");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
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());
}
@Test
@Order(11)
public void addMessageTaskCheckSpecialUserTwoSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("CREATOR");
userIds.add("FOLLOW_PEOPLE");
userIds.add("project-message-user-5");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
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());
}
@Test
@Order(12)
public void addMessageTaskCheckSpecialUserThreeSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("CREATOR");
userIds.add("FOLLOW_PEOPLE");
userIds.add("project-message-user-6");
userIds.add("project-message-user-del");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
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(102001, resultHolder.getCode());
}
@Test
@Order(13)
public void closeMessageTaskSuccess() throws Exception {
MessageTaskRequest messageTaskRequest = new MessageTaskRequest();
messageTaskRequest.setProjectId("project-message-test");
messageTaskRequest.setTaskType(NoticeConstants.TaskType.API_DEFINITION_TASK);
messageTaskRequest.setEvent(NoticeConstants.Event.CREATE);
List<String> userIds = new ArrayList<>();
userIds.add("CREATOR");
userIds.add("FOLLOW_PEOPLE");
messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setRobotId("test_message_robot2");
messageTaskRequest.setEnable(null);
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());
}
}

View File

@ -323,6 +323,20 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test
@Order(21)
void setEnableFalseSuccess() throws Exception {
ProjectRobot projectRobot = getRobot("测试Enable");
String projectRobotId = projectRobot.getId();
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/" + projectRobotId)
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
ProjectRobot projectRobotEnable = getRobot("测试Enable");
Assertions.assertTrue(projectRobotEnable.getEnable());
}
@Test
@Order(22)
void setEnableFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/no_id")
.header(SessionConstants.HEADER_TOKEN, sessionId)
@ -332,6 +346,8 @@ public class ProjectRobotControllerTests extends BaseTest {
}
private static ProjectRobot getResult(MvcResult mvcResult) throws UnsupportedEncodingException {
String contentAsString = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
;