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})
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})
@Size(min = 1, max = 50, message = "{project_robot.platform.length_range}", groups = {Created.class, Updated.class})
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})
private String webhook;
@Schema(description = "自定义和内部")
@Schema(description = "钉钉机器人的种类: 自定义:CUSTOM, 企业内部:ENTERPRISE")
private String type;
@Schema(description = "钉钉AppKey")

View File

@ -1,15 +1,11 @@
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.dto.ProjectRobotDTO;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.project.service.ProjectRobotService;
import io.metersphere.sdk.constants.PermissionConstants;
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.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
@ -31,12 +27,11 @@ public class ProjectRobotController {
private ProjectRobotService projectRobotService;
@PostMapping("/list/page")
@PostMapping("/list/{projectId}")
@Operation(summary = "项目管理-项目与权限-消息管理-获取机器人列表")
@RequiresPermissions(PermissionConstants.PROJECT_MESSAGE_READ)
public Pager<List<ProjectRobot>> listResourcePools(@Validated @RequestBody ProjectRobotRequest request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), true);
return PageUtils.setPageInfo(page, projectRobotService.getList(request));
public List<ProjectRobot> listResourcePools(@PathVariable(value = "projectId") String projectId) {
return projectRobotService.getList(projectId);
}
@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;
import io.metersphere.project.domain.MessageTaskExample;
import io.metersphere.project.domain.ProjectRobot;
import io.metersphere.project.domain.ProjectRobotExample;
import io.metersphere.project.dto.ProjectRobotDTO;
import io.metersphere.project.enums.ProjectRobotPlatform;
import io.metersphere.project.enums.ProjectRobotType;
import io.metersphere.project.mapper.MessageTaskMapper;
import io.metersphere.project.mapper.ProjectRobotMapper;
import io.metersphere.project.request.ProjectRobotRequest;
import io.metersphere.sdk.exception.MSException;
import io.metersphere.sdk.util.BeanUtils;
import io.metersphere.sdk.util.Translator;
@ -26,6 +27,9 @@ public class ProjectRobotService {
@Resource
private ProjectRobotMapper robotMapper;
@Resource
private MessageTaskMapper messageTaskMapper;
public void add(ProjectRobot projectRobot) {
projectRobot.setId(UUID.randomUUID().toString());
projectRobot.setEnable(true);
@ -63,9 +67,11 @@ public class ProjectRobotService {
return projectRobotInDB;
}
public void delete(String id) {
checkRobotExist(id);
MessageTaskExample messageTaskExample = new MessageTaskExample();
messageTaskExample.createCriteria().andProjectRobotIdEqualTo(id);
messageTaskMapper.deleteByExample(messageTaskExample);
robotMapper.deleteByPrimaryKey(id);
}
@ -83,29 +89,16 @@ public class ProjectRobotService {
robotMapper.updateByPrimaryKeySelective(projectRobot);
}
public List<ProjectRobot> getList(ProjectRobotRequest request) {
public List<ProjectRobot> getList(String projectId) {
ProjectRobotExample projectExample = new ProjectRobotExample();
ProjectRobotExample.Criteria criteria = projectExample.createCriteria();
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());
}
criteria.andProjectIdEqualTo(projectId);
projectExample.setOrderByClause("create_time desc");
return robotMapper.selectByExample(projectExample);
}
public ProjectRobotDTO getDetail(String robotId) {
ProjectRobot projectRobotInDB = checkRobotExist(robotId);
ProjectRobotDTO projectRobotDTO = new ProjectRobotDTO();

View File

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

View File

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