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

View File

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

View File

@ -129,6 +129,8 @@ public class NoticeMessageTaskControllerTests extends BaseTest {
userIds.add("project-message-user-7"); userIds.add("project-message-user-7");
userIds.add("project-message-user-8"); userIds.add("project-message-user-8");
messageTaskRequest.setReceiverIds(userIds); messageTaskRequest.setReceiverIds(userIds);
messageTaskRequest.setEnable(null);
messageTaskRequest.setTestId("testId");
messageTaskRequest.setTemplate("发送消息测试"); messageTaskRequest.setTemplate("发送消息测试");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save") MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/notice/message/task/save")
.header(SessionConstants.HEADER_TOKEN, sessionId) .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); List<MessageTaskDTO> messageTaskDTOList = JSON.parseArray(JSON.toJSONString(resultHolder.getData()), MessageTaskDTO.class);
Assertions.assertTrue(CollectionUtils.isEmpty(messageTaskDTOList)); 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 @Test
@Order(21) @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 { void setEnableFail() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/no_id") mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/no_id")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -332,6 +346,8 @@ public class ProjectRobotControllerTests extends BaseTest {
} }
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);
; ;