refactor(消息管理): 修改机器人获取列表方法

This commit is contained in:
guoyuqi 2023-09-26 18:37:44 +08:00 committed by Yuki Guo
parent bb7a36cd3b
commit 4b6e8c9ece
7 changed files with 77 additions and 158 deletions

View File

@ -25,7 +25,7 @@ public class ProjectRobot implements Serializable {
@Size(min = 1, max = 255, message = "{project_robot.name.length_range}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 255, message = "{project_robot.name.length_range}", groups = {Created.class, Updated.class})
private String name; private String name;
@Schema(description = "所属平台(飞书,钉钉,企业微信,自定义)", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(description = "所属平台(飞书:LARK,钉钉:DING_TALK,企业微信:WE_COM,自定义:CUSTOM, 站内信:IN_SITE, 邮件:MAIL", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{project_robot.platform.not_blank}", groups = {Created.class}) @NotBlank(message = "{project_robot.platform.not_blank}", groups = {Created.class})
@Size(min = 1, max = 50, message = "{project_robot.platform.length_range}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 50, message = "{project_robot.platform.length_range}", groups = {Created.class, Updated.class})
private String platform; private String platform;
@ -35,7 +35,7 @@ public class ProjectRobot implements Serializable {
@Size(min = 1, max = 255, message = "{project_robot.webhook.length_range}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 255, message = "{project_robot.webhook.length_range}", groups = {Created.class, Updated.class})
private String webhook; private String webhook;
@Schema(description = "自定义和内部") @Schema(description = "钉钉机器人的种类: 自定义:CUSTOM, 企业内部:ENTERPRISE")
private String type; private String type;
@Schema(description = "钉钉AppKey") @Schema(description = "钉钉AppKey")

View File

@ -1,15 +1,11 @@
package io.metersphere.project.controller; package io.metersphere.project.controller;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.dto.ProjectRobotDTO; import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.project.service.ProjectRobotService; import io.metersphere.project.service.ProjectRobotService;
import io.metersphere.sdk.constants.PermissionConstants; import io.metersphere.sdk.constants.PermissionConstants;
import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.PageUtils;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.utils.SessionUtils; import io.metersphere.system.utils.SessionUtils;
import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated; import io.metersphere.validation.groups.Updated;
@ -31,12 +27,11 @@ public class ProjectRobotController {
private ProjectRobotService projectRobotService; private ProjectRobotService projectRobotService;
@PostMapping("/list/page") @PostMapping("/list/{projectId}")
@Operation(summary = "项目管理-项目与权限-消息管理-获取机器人列表") @Operation(summary = "项目管理-项目与权限-消息管理-获取机器人列表")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ) @RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ)
public Pager<List<ProjectRobot>> listResourcePools(@Validated @RequestBody ProjectRobotRequest request) { public List<ProjectRobot> listResourcePools(@PathVariable(value = "projectId") String projectId) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), true); return projectRobotService.getList(projectId);
return PageUtils.setPageInfo(page, projectRobotService.getList(request));
} }
@PostMapping("add") @PostMapping("add")

View File

@ -1,15 +0,0 @@
package io.metersphere.project.request;
import io.metersphere.sdk.dto.BasePageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class ProjectRobotRequest extends BasePageRequest {
@Schema(description = "是否禁用")
private String projectId;
@Schema(description = "是否禁用")
private Boolean enable;
}

View File

@ -1,12 +1,13 @@
package io.metersphere.project.service; package io.metersphere.project.service;
import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.domain.ProjectRobotExample; 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.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.project.request.ProjectRobotRequest;
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;
@ -26,6 +27,9 @@ public class ProjectRobotService {
@Resource @Resource
private ProjectRobotMapper robotMapper; private ProjectRobotMapper robotMapper;
@Resource
private MessageTaskMapper messageTaskMapper;
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(true);
@ -63,9 +67,11 @@ public class ProjectRobotService {
return projectRobotInDB; return projectRobotInDB;
} }
public void delete(String id) { public void delete(String id) {
checkRobotExist(id); checkRobotExist(id);
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectRobotIdEqualTo(id);
messageTaskMapper.deleteByExample(messageTaskExample);
robotMapper.deleteByPrimaryKey(id); robotMapper.deleteByPrimaryKey(id);
} }
@ -83,29 +89,16 @@ public class ProjectRobotService {
robotMapper.updateByPrimaryKeySelective(projectRobot); robotMapper.updateByPrimaryKeySelective(projectRobot);
} }
public List<ProjectRobot> getList(ProjectRobotRequest request) { public List<ProjectRobot> getList(String projectId) {
ProjectRobotExample projectExample = new ProjectRobotExample(); ProjectRobotExample projectExample = new ProjectRobotExample();
ProjectRobotExample.Criteria criteria = projectExample.createCriteria(); ProjectRobotExample.Criteria criteria = projectExample.createCriteria();
criteria.andProjectIdEqualTo(projectId);
if (StringUtils.isNotBlank(request.getKeyword())) {
criteria.andNameLike(StringUtils.wrapIfMissing(request.getKeyword(), "%"));
}
if (request.getEnable() != null) {
criteria.andEnableEqualTo(request.getEnable());
}
if (request.getProjectId() != null) {
criteria.andProjectIdEqualTo(request.getProjectId());
}
projectExample.setOrderByClause("create_time desc"); projectExample.setOrderByClause("create_time desc");
return robotMapper.selectByExample(projectExample); return robotMapper.selectByExample(projectExample);
} }
public ProjectRobotDTO getDetail(String robotId) { public ProjectRobotDTO getDetail(String robotId) {
ProjectRobot projectRobotInDB = checkRobotExist(robotId); ProjectRobot projectRobotInDB = checkRobotExist(robotId);
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO(); ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();

View File

@ -1,11 +1,10 @@
package io.metersphere.project.controller; package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.project.service.CleanupRobotResourceService; import io.metersphere.project.service.CleanupRobotResourceService;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
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 jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -26,33 +25,24 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
@AutoConfigureMockMvc @AutoConfigureMockMvc
public class CleanupRobotResourceTests extends BaseTest { public class CleanupRobotResourceTests extends BaseTest {
@Resource @Resource
private CleanupRobotResourceService resourceService; private CleanupRobotResourceService resourceService;
public static final String ROBOT_LIST = "/project/robot/list/page"; public static final String ROBOT_LIST = "/project/robot/list/";
@Test @Test
@Order(1) @Order(1)
@Sql(scripts = {"/dml/init_project_robot.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) @Sql(scripts = {"/dml/init_project_robot.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED))
public void testCleanupResource() throws Exception { public void testCleanupResource() throws Exception {
ProjectRobotRequest request = new ProjectRobotRequest(); List<ProjectRobot> projectRobots = getList();
request.setCurrent(1);
request.setPageSize(5);
Pager<?> sortPageData = getPager(request);
List<ProjectRobot> projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class);
if (CollectionUtils.isNotEmpty(projectRobots)) { if (CollectionUtils.isNotEmpty(projectRobots)) {
resourceService.deleteResources("test"); resourceService.deleteResources("test");
} }
request = new ProjectRobotRequest(); List<ProjectRobot> projectRobotAfters = getList();
request.setCurrent(1);
request.setPageSize(5);
request.setKeyword("测试机器人");
Pager<?> sortPageDataAfter = getPager(request);
List<ProjectRobot> projectRobotAfters = JSON.parseArray(JSON.toJSONString(sortPageDataAfter.getList()), ProjectRobot.class);
Assertions.assertTrue(CollectionUtils.isEmpty(projectRobotAfters)); Assertions.assertTrue(CollectionUtils.isEmpty(projectRobotAfters));
} }
@ -62,17 +52,15 @@ public class CleanupRobotResourceTests extends BaseTest {
resourceService.cleanReportResources("test"); resourceService.cleanReportResources("test");
} }
private Pager<?> getPager(ProjectRobotRequest request) throws Exception { private List<ProjectRobot> getList() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST) MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST + "test")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class); ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
Pager<?> sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class);
return sortPageData;
} }
} }

View File

@ -1,10 +1,8 @@
package io.metersphere.project.controller; package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot; import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.util.JSON; import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.util.Pager;
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.invoker.ProjectServiceInvoker; import io.metersphere.system.invoker.ProjectServiceInvoker;
@ -29,7 +27,7 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
public class CreateRobotResourceTests extends BaseTest { public class CreateRobotResourceTests extends BaseTest {
private final ProjectServiceInvoker serviceInvoker; private final ProjectServiceInvoker serviceInvoker;
public static final String ROBOT_LIST = "/project/robot/list/page"; public static final String ROBOT_LIST = "/project/robot/list/";
@Autowired @Autowired
public CreateRobotResourceTests(ProjectServiceInvoker serviceInvoker) { public CreateRobotResourceTests(ProjectServiceInvoker serviceInvoker) {
@ -40,25 +38,19 @@ public class CreateRobotResourceTests extends BaseTest {
@Order(1) @Order(1)
public void testCleanupResource() throws Exception { public void testCleanupResource() throws Exception {
serviceInvoker.invokeCreateServices("test"); serviceInvoker.invokeCreateServices("test");
ProjectRobotRequest request = new ProjectRobotRequest(); List<ProjectRobot> projectRobotAfters = getList();
request.setCurrent(1);
request.setPageSize(5);
request.setProjectId("test");
Pager<?> sortPageDataAfter = getPager(request);
List<ProjectRobot> projectRobotAfters = JSON.parseArray(JSON.toJSONString(sortPageDataAfter.getList()), ProjectRobot.class);
Assertions.assertEquals(2, projectRobotAfters.size()); Assertions.assertEquals(2, projectRobotAfters.size());
} }
private Pager<?> getPager(ProjectRobotRequest request) throws Exception { private List<ProjectRobot> getList() throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST) MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST + "test")
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class); ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
return JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class);
} }
} }

View File

@ -1,16 +1,17 @@
package io.metersphere.project.controller; package io.metersphere.project.controller;
import io.metersphere.project.domain.ProjectRobot; 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.request.ProjectRobotRequest; import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.sdk.constants.SessionConstants; import io.metersphere.sdk.constants.SessionConstants;
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.sdk.util.Pager;
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 jakarta.annotation.Resource;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.junit.jupiter.api.*; import org.junit.jupiter.api.*;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
@ -27,7 +28,7 @@ import java.util.List;
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;
@SpringBootTest(webEnvironment= SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc @AutoConfigureMockMvc
@TestMethodOrder(MethodOrderer.OrderAnnotation.class) @TestMethodOrder(MethodOrderer.OrderAnnotation.class)
public class ProjectRobotControllerTests extends BaseTest { public class ProjectRobotControllerTests extends BaseTest {
@ -40,10 +41,13 @@ public class ProjectRobotControllerTests extends BaseTest {
public static final String ROBOT_ENABLE = "/project/robot/enable"; public static final String ROBOT_ENABLE = "/project/robot/enable";
public static final String ROBOT_LIST = "/project/robot/list/page"; public static final String ROBOT_LIST = "/project/robot/list/";
public static final String ROBOT_DETAIL = "/project/robot/get"; public static final String ROBOT_DETAIL = "/project/robot/get";
@Resource
private ProjectRobotMapper projectRobotMapper;
@Test @Test
@Order(1) @Order(1)
@ -54,10 +58,16 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
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());
listByKeyWord("企业微信机器人"); checkName("test_project", "企业微信机器人");
} }
private void checkName(String projectId, String name) throws Exception {
List<ProjectRobot> testProject = getList(projectId);
List<String> nameList = testProject.stream().map(ProjectRobot::getName).toList();
Assertions.assertTrue(nameList.contains(name));
}
@Test @Test
@Order(2) @Order(2)
void addRobotSuccessLark() throws Exception { void addRobotSuccessLark() throws Exception {
@ -67,14 +77,16 @@ public class ProjectRobotControllerTests extends BaseTest {
projectRobotDTO.setProjectId("test_project"); projectRobotDTO.setProjectId("test_project");
projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb"); projectRobotDTO.setWebhook("https://open.feishu.cn/open-apis/bot/v2/hook/a6024229-9d9d-41c2-8662-7bc3da1092cb");
getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk()); getPostResult(projectRobotDTO, ROBOT_ADD, status().isOk());
listByKeyWord("飞书机器人"); checkName("test_project", "飞书机器人");
} }
@Test @Test
@Order(3) @Order(3)
void addRobotSuccessDingCustom() throws Exception { void addRobotSuccessDingCustom() throws Exception {
setDingCustom("钉钉自定义机器人"); setDingCustom("钉钉自定义机器人");
listByKeyWord("钉钉自定义机器人"); checkName("test_project", "钉钉自定义机器人");
} }
private void setDingCustom(String name) throws Exception { private void setDingCustom(String name) throws Exception {
@ -91,7 +103,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(4) @Order(4)
void addRobotSuccessDingEn() throws Exception { void addRobotSuccessDingEn() throws Exception {
setDingEn("钉钉企业应用机器人"); setDingEn("钉钉企业应用机器人");
listByKeyWord("钉钉企业应用机器人"); checkName("test_project", "钉钉企业应用机器人");
} }
private void setDingEn(String name) throws Exception { private void setDingEn(String name) throws Exception {
@ -146,7 +158,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(8) @Order(8)
void updateRobotSuccessCusTom() throws Exception { void updateRobotSuccessCusTom() throws Exception {
setCustomRobot("用于更新自定义机器人"); setCustomRobot("用于更新自定义机器人");
ProjectRobot projectRobot = getRobot("用于更新自定义机器人"); ProjectRobot projectRobot = getRobot("test_project", "用于更新自定义机器人");
checkUpdate(projectRobot, "更新自定义机器人", status().isOk()); checkUpdate(projectRobot, "更新自定义机器人", status().isOk());
} }
@ -154,7 +166,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(9) @Order(9)
void updateRobotSuccessDingCus() throws Exception { void updateRobotSuccessDingCus() throws Exception {
setDingCustom("用于更新钉钉自定义机器人"); setDingCustom("用于更新钉钉自定义机器人");
ProjectRobot projectRobot = getRobot("用于更新钉钉自定义机器人"); ProjectRobot projectRobot = getRobot("test_project", "用于更新钉钉自定义机器人");
checkUpdate(projectRobot, "更新钉钉自定义机器人", status().isOk()); checkUpdate(projectRobot, "更新钉钉自定义机器人", status().isOk());
} }
@ -162,7 +174,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(10) @Order(10)
void updateRobotSuccessDingEn() throws Exception { void updateRobotSuccessDingEn() throws Exception {
setDingEn("用于更新钉钉企业机器人"); setDingEn("用于更新钉钉企业机器人");
ProjectRobot projectRobot = getRobot("用于更新钉钉企业机器人"); ProjectRobot projectRobot = getRobot("test_project", "用于更新钉钉企业机器人");
checkUpdate(projectRobot, "更新钉钉企业机器人", status().isOk()); checkUpdate(projectRobot, "更新钉钉企业机器人", status().isOk());
} }
@ -170,7 +182,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(11) @Order(11)
void updateRobotFileIdNotExist() throws Exception { void updateRobotFileIdNotExist() throws Exception {
setCustomRobot("测试没有ID失败"); setCustomRobot("测试没有ID失败");
ProjectRobot projectRobot = getRobot("测试没有ID失败"); ProjectRobot projectRobot = getRobot("test_project", "测试没有ID失败");
projectRobot.setId("noId"); projectRobot.setId("noId");
checkUpdate(projectRobot, "测试没有ID失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试没有ID失败", status().is5xxServerError());
} }
@ -179,7 +191,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(12) @Order(12)
void updateRobotFileIdNoId() throws Exception { void updateRobotFileIdNoId() throws Exception {
setCustomRobot("测试ID空失败"); setCustomRobot("测试ID空失败");
ProjectRobot projectRobot = getRobot("测试ID空失败"); ProjectRobot projectRobot = getRobot("test_project", "测试ID空失败");
projectRobot.setId(null); projectRobot.setId(null);
checkUpdate(projectRobot, "测试ID空失败", status().isBadRequest()); checkUpdate(projectRobot, "测试ID空失败", status().isBadRequest());
} }
@ -188,7 +200,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(13) @Order(13)
void updateRobotFileNoDingType() throws Exception { void updateRobotFileNoDingType() throws Exception {
setDingCustom("测试更新没有Type失败"); setDingCustom("测试更新没有Type失败");
ProjectRobot projectRobot = getRobot("测试更新没有Type失败"); ProjectRobot projectRobot = getRobot("test_project", "测试更新没有Type失败");
projectRobot.setType(null); projectRobot.setType(null);
checkUpdate(projectRobot, "测试更新没有Type失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试更新没有Type失败", status().is5xxServerError());
} }
@ -197,7 +209,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(14) @Order(14)
void updateRobotFileNoDingKey() throws Exception { void updateRobotFileNoDingKey() throws Exception {
setDingEn("测试更新没有key失败"); setDingEn("测试更新没有key失败");
ProjectRobot projectRobot = getRobot("测试更新没有key失败"); ProjectRobot projectRobot = getRobot("test_project", "测试更新没有key失败");
projectRobot.setAppKey(null); projectRobot.setAppKey(null);
checkUpdate(projectRobot, "测试更新没有key失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试更新没有key失败", status().is5xxServerError());
} }
@ -206,7 +218,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(15) @Order(15)
void updateRobotFileNoDingSecret() throws Exception { void updateRobotFileNoDingSecret() throws Exception {
setDingEn("测试更新没有Secret失败"); setDingEn("测试更新没有Secret失败");
ProjectRobot projectRobot = getRobot("测试更新没有Secret失败"); ProjectRobot projectRobot = getRobot("test_project", "测试更新没有Secret失败");
projectRobot.setAppSecret(null); projectRobot.setAppSecret(null);
checkUpdate(projectRobot, "测试更新没有Secret失败", status().is5xxServerError()); checkUpdate(projectRobot, "测试更新没有Secret失败", status().is5xxServerError());
} }
@ -215,7 +227,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(16) @Order(16)
void deleteRobotSuccess() throws Exception { void deleteRobotSuccess() throws Exception {
setCustomRobot("测试删除"); setCustomRobot("测试删除");
ProjectRobot projectRobot = getRobot("测试删除"); ProjectRobot projectRobot = getRobot("test_project", "测试删除");
String projectRobotId = projectRobot.getId(); String projectRobotId = projectRobot.getId();
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DELETE + "/" + projectRobotId) mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DELETE + "/" + projectRobotId)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
@ -254,7 +266,7 @@ public class ProjectRobotControllerTests extends BaseTest {
@Order(16) @Order(16)
void getDetailSuccess() throws Exception { void getDetailSuccess() throws Exception {
setCustomRobot("测试获取详情"); setCustomRobot("测试获取详情");
ProjectRobot projectRobot = getRobot("测试获取详情"); ProjectRobot projectRobot = getRobot("test_project", "测试获取详情");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DETAIL + "/" + projectRobot.getId()) MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_DETAIL + "/" + projectRobot.getId())
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)) .header(SessionConstants.CSRF_TOKEN, csrfToken))
@ -268,75 +280,41 @@ public class ProjectRobotControllerTests extends BaseTest {
@Test @Test
@Order(17) @Order(17)
void getListSuccessNoKeyword() throws Exception { void getListSuccessNoKeyword() throws Exception {
ProjectRobotRequest request = new ProjectRobotRequest(); List<ProjectRobot> projectRobots = getList("test_project");
request.setCurrent(1);
request.setPageSize(5);
Pager<?> sortPageData = getPager(request);
List<ProjectRobot> projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class);
Assertions.assertTrue(projectRobots.size() > 0); Assertions.assertTrue(projectRobots.size() > 0);
} }
@Test @Test
@Order(18) @Order(18)
void getListSuccessEnable() throws Exception {
setCustomRobot("测试集合");
ProjectRobotRequest request = new ProjectRobotRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setKeyword("测试集合");
request.setEnable(true);
Pager<?> sortPageData = getPager(request);
List<ProjectRobot> projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class);
ProjectRobot projectRobot = projectRobots.get(0);
Assertions.assertTrue(projectRobot.getEnable());
}
@Test
@Order(19)
void getListSuccessByProject() throws Exception {
setCustomRobot("测试集合");
ProjectRobotRequest request = new ProjectRobotRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setProjectId("test_project");
request.setEnable(true);
Pager<?> sortPageData = getPager(request);
List<ProjectRobot> projectRobots = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class);
ProjectRobot projectRobot = projectRobots.get(0);
Assertions.assertTrue(projectRobot.getEnable());
}
@Test
@Order(20)
void setEnableSuccess() throws Exception { void setEnableSuccess() throws Exception {
setCustomRobot("测试Enable"); setCustomRobot("测试Enable");
ProjectRobot projectRobot = getRobot("测试Enable"); ProjectRobot projectRobot = getRobot("test_project","测试Enable");
String projectRobotId = projectRobot.getId(); String projectRobotId = projectRobot.getId();
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/" + projectRobotId) mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/" + projectRobotId)
.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())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
ProjectRobot projectRobotEnable = getRobot("测试Enable"); ProjectRobot projectRobotEnable = getRobot("test_project","测试Enable");
Assertions.assertFalse(projectRobotEnable.getEnable()); Assertions.assertFalse(projectRobotEnable.getEnable());
} }
@Test @Test
@Order(21) @Order(19)
void setEnableFalseSuccess() throws Exception { void setEnableFalseSuccess() throws Exception {
ProjectRobot projectRobot = getRobot("测试Enable"); ProjectRobot projectRobot = getRobot("test_project","测试Enable");
String projectRobotId = projectRobot.getId(); String projectRobotId = projectRobot.getId();
mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/" + projectRobotId) mockMvc.perform(MockMvcRequestBuilders.get(ROBOT_ENABLE + "/" + projectRobotId)
.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())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
ProjectRobot projectRobotEnable = getRobot("测试Enable"); ProjectRobot projectRobotEnable = getRobot("test_project","测试Enable");
Assertions.assertTrue(projectRobotEnable.getEnable()); Assertions.assertTrue(projectRobotEnable.getEnable());
} }
@Test @Test
@Order(22) @Order(20)
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)
@ -347,10 +325,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);
;
ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class); ResultHolder resultHolder = JSON.parseObject(contentAsString, ResultHolder.class);
return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), ProjectRobot.class); return JSON.parseObject(JSON.toJSONString(resultHolder.getData()), ProjectRobot.class);
} }
@ -362,7 +338,7 @@ public class ProjectRobotControllerTests extends BaseTest {
BeanUtils.copyBean(projectRobotDTO, projectRobot); BeanUtils.copyBean(projectRobotDTO, projectRobot);
getPostResult(projectRobotDTO, ROBOT_UPDATE, resultMatcher); getPostResult(projectRobotDTO, ROBOT_UPDATE, resultMatcher);
if (resultMatcher.equals(status().isOk())) { if (resultMatcher.equals(status().isOk())) {
ProjectRobot projectRobotUpdate = getRobot(name); ProjectRobot projectRobotUpdate = getRobot(projectRobot.getProjectId(), name);
Assertions.assertTrue(StringUtils.equals(projectRobotUpdate.getName(), name)); Assertions.assertTrue(StringUtils.equals(projectRobotUpdate.getName(), name));
} }
} }
@ -386,33 +362,23 @@ public class ProjectRobotControllerTests extends BaseTest {
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
} }
void listByKeyWord(String keyWord) throws Exception { private List<ProjectRobot> getList(String projectId) throws Exception {
ProjectRobot projectRobot = getRobot(keyWord); MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST + projectId)
Assertions.assertTrue(StringUtils.equals(projectRobot.getName(), keyWord));
}
private ProjectRobot getRobot(String keyWord) throws Exception {
ProjectRobotRequest request = new ProjectRobotRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setKeyword(keyWord);
Pager<?> sortPageData = getPager(request);
ProjectRobot projectRobot = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectRobot.class).get(0);
return projectRobot;
}
private Pager<?> getPager(ProjectRobotRequest request) throws Exception {
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post(ROBOT_LIST)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class); ResultHolder sortHolder = JSON.parseObject(sortData, ResultHolder.class);
Pager<?> sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); return JSON.parseArray(JSON.toJSONString(sortHolder.getData()), ProjectRobot.class);
return sortPageData; }
private ProjectRobot getRobot(String projectId, String keyWord) throws Exception {
ProjectRobotExample projectRobotExample = new ProjectRobotExample();
projectRobotExample.createCriteria().andProjectIdEqualTo(projectId).andNameEqualTo(keyWord);
List<ProjectRobot> projectRobots = projectRobotMapper.selectByExample(projectRobotExample);
return projectRobots.get(0);
} }