refactor: add validate group

This commit is contained in:
guoyuqi 2023-07-21 13:37:24 +08:00 committed by Yuki Guo
parent 5ed1a983aa
commit 500982ccb9
3 changed files with 58 additions and 38 deletions

View File

@ -17,7 +17,7 @@ public class TestResourcePoolRequest {
private String id; private String id;
@Schema(title = "名称", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(title = "名称", requiredMode = Schema.RequiredMode.REQUIRED)
@NotBlank(message = "{test_resource_pool.name.not_blank}", groups = {Created.class}) @NotBlank(message = "{test_resource_pool.name.not_blank}", groups = {Created.class, Updated.class})
@Size(min = 1, max = 255, message = "{test_resource_pool.name.length_range}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 255, message = "{test_resource_pool.name.length_range}", groups = {Created.class, Updated.class})
private String name; private String name;
@ -25,7 +25,7 @@ public class TestResourcePoolRequest {
private String description; private String description;
@Schema(title = "类型", requiredMode = Schema.RequiredMode.REQUIRED, allowableValues = { "Node","Kubernetes"}) @Schema(title = "类型", requiredMode = Schema.RequiredMode.REQUIRED, allowableValues = { "Node","Kubernetes"})
@NotBlank(message = "{test_resource_pool.type.not_blank}", groups = {Created.class}) @NotBlank(message = "{test_resource_pool.type.not_blank}", groups = {Created.class, Updated.class})
@Size(min = 1, max = 30, message = "{test_resource_pool.type.length_range}", groups = {Created.class, Updated.class}) @Size(min = 1, max = 30, message = "{test_resource_pool.type.length_range}", groups = {Created.class, Updated.class})
private String type; private String type;
@ -45,7 +45,7 @@ public class TestResourcePoolRequest {
private String serverUrl; private String serverUrl;
@Schema(title = "资源池应用类型(组织/全部)", requiredMode = Schema.RequiredMode.REQUIRED) @Schema(title = "资源池应用类型(组织/全部)", requiredMode = Schema.RequiredMode.REQUIRED)
@NotNull(message = "{test_resource_pool.all_org.not_blank}", groups = {Created.class}) @NotNull(message = "{test_resource_pool.all_org.not_blank}", groups = {Created.class, Updated.class})
private Boolean allOrg; private Boolean allOrg;
@Schema(title = "其余配置") @Schema(title = "其余配置")

View File

@ -16,6 +16,8 @@ import io.metersphere.sdk.util.Pager;
import io.metersphere.sdk.util.SessionUtils; import io.metersphere.sdk.util.SessionUtils;
import io.metersphere.system.consul.CacheNode; import io.metersphere.system.consul.CacheNode;
import io.metersphere.system.domain.TestResourcePool; import io.metersphere.system.domain.TestResourcePool;
import io.metersphere.validation.groups.Created;
import io.metersphere.validation.groups.Updated;
import io.swagger.v3.oas.annotations.Operation; import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag; import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
@ -37,7 +39,7 @@ public class TestResourcePoolController {
@Operation(summary = "添加资源池") @Operation(summary = "添加资源池")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_ADD) @RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_ADD)
@Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = TestResourcePoolService.class) @Log(type = OperationLogType.ADD, expression = "#msClass.addLog(#request)", msClass = TestResourcePoolService.class)
public TestResourcePool addTestResourcePool(@Validated @RequestBody TestResourcePoolRequest request) { public TestResourcePool addTestResourcePool(@Validated({Created.class}) @RequestBody TestResourcePoolRequest request) {
String userId = SessionUtils.getUserId(); String userId = SessionUtils.getUserId();
TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO(); TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO();
BeanUtils.copyBean(testResourcePool, request); BeanUtils.copyBean(testResourcePool, request);
@ -60,7 +62,7 @@ public class TestResourcePoolController {
@Operation(summary = "更新资源池") @Operation(summary = "更新资源池")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_UPDATE) @RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request.getId())", msClass = TestResourcePoolService.class) @Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request.getId())", msClass = TestResourcePoolService.class)
public void updateTestResourcePool(@Validated @RequestBody TestResourcePoolRequest request) { public void updateTestResourcePool(@Validated({Updated.class}) @RequestBody TestResourcePoolRequest request) {
TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO(); TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO();
BeanUtils.copyBean(testResourcePool, request); BeanUtils.copyBean(testResourcePool, request);
testResourcePool.setCreateUser(null); testResourcePool.setCreateUser(null);

View File

@ -225,23 +225,7 @@ class TestResourcePoolControllerTests extends BaseTest {
config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED), config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED),
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)*/ executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)*/
void listResourcePoolsWidthSearch() throws Exception { void listResourcePoolsWidthSearch() throws Exception {
QueryResourcePoolRequest request = new QueryResourcePoolRequest(); listByKeyWord("test_pool_1");
request.setCurrent(1);
request.setPageSize(5);
request.setKeyword("test_pool_1");
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/page")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JsonUtils.parseObject(sortData, ResultHolder.class);
Pager<?> sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class);
// 返回值中取出第一条ID最大的数据, 并判断是否是default-admin
TestResourcePoolDTO testResourcePoolDTO = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), TestResourcePoolDTO.class).get(0);
Assertions.assertTrue(StringUtils.equals(testResourcePoolDTO.getName(), "test_pool_1"));
} }
@Test @Test
@ -314,8 +298,12 @@ class TestResourcePoolControllerTests extends BaseTest {
@Test @Test
@Order(15) @Order(15)
void updateTestResourcePool() throws Exception { void updateTestResourcePool() throws Exception {
MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob2", false, true, true, false, false, ResourcePoolTypeEnum.K8S.name());
TestResourcePoolRequest testResourcePoolRequest1 = getResult(testPoolBlob);
String id = testResourcePoolRequest1.getId();
TestResourcePoolRequest testResourcePoolRequest = new TestResourcePoolRequest(); TestResourcePoolRequest testResourcePoolRequest = new TestResourcePoolRequest();
testResourcePoolRequest.setName("test_pool"); testResourcePoolRequest.setId(id);
testResourcePoolRequest.setName("test_pool_update");
testResourcePoolRequest.setType(ResourcePoolTypeEnum.NODE.name()); testResourcePoolRequest.setType(ResourcePoolTypeEnum.NODE.name());
setResources(testResourcePoolRequest,false); setResources(testResourcePoolRequest,false);
testResourcePoolRequest.setApiTest(true); testResourcePoolRequest.setApiTest(true);
@ -329,6 +317,7 @@ class TestResourcePoolControllerTests extends BaseTest {
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()) .andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
listByKeyWord("test_pool_update");
} }
private static void setResources(TestResourcePoolRequest testResourcePoolDTO, boolean isPart) { private static void setResources(TestResourcePoolRequest testResourcePoolDTO, boolean isPart) {
@ -349,52 +338,56 @@ class TestResourcePoolControllerTests extends BaseTest {
private void dealTestResourcePoolFiled(String urlType) throws Exception { private void dealTestResourcePoolFiled(String urlType) throws Exception {
String url; String url;
String id = "";
if (StringUtils.equals(urlType,"ADD")) { if (StringUtils.equals(urlType,"ADD")) {
url = TEST_RESOURCE_POOL_ADD; url = TEST_RESOURCE_POOL_ADD;
} else { } else {
MvcResult testPoolBlob = this.addTestResourcePoolSuccess("test_pool_blob2", false, true, true, false, false, ResourcePoolTypeEnum.K8S.name());
TestResourcePoolRequest testResourcePoolRequest1 = getResult(testPoolBlob);
id = testResourcePoolRequest1.getId();
url = TEST_RESOURCE_POOL_UPDATE; url = TEST_RESOURCE_POOL_UPDATE;
} }
TestResourcePoolRequest testResourcePoolRequest = generatorDto(true,true, false, false, false, false,false,false,false); TestResourcePoolRequest testResourcePoolRequest = generatorDto(true,true, false, false, false, false,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, status().isBadRequest());
//资源池类型为空 //资源池类型为空
testResourcePoolRequest = generatorDto(true, false, true, false, false, false,false,false,false); testResourcePoolRequest = generatorDto(true, false, true, false, false, false,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, status().isBadRequest());
//api 类型 资源池节点集合为空 //api 类型 资源池节点集合为空
testResourcePoolRequest = generatorDto(true, false, false, true, false, false,false,false,false); testResourcePoolRequest = generatorDto(true, false, false, true, false, false,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 ip 为空 //资源池节点不为空但是内容为空 ip 为空
testResourcePoolRequest = generatorDto(true, false, false, true, false, true,false,false,false); testResourcePoolRequest = generatorDto(true, false, false, true, false, true,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 port 为空 //资源池节点不为空但是内容为空 port 为空
testResourcePoolRequest = generatorDto(true, false, false, true, false, false,true,false,false); testResourcePoolRequest = generatorDto(true, false, false, true, false, false,true,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 最大线程数 为空 //资源池节点不为空但是内容为空 最大线程数 为空
testResourcePoolRequest = generatorDto(true, false, false, true, false, false,false,false,true); testResourcePoolRequest = generatorDto(true, false, false, true, false, false,false,false,true);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//性能测试类型 资源池节点集合为空 //性能测试类型 资源池节点集合为空
testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,false,false); testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空ip为空 //资源池节点不为空但是内容为空ip为空
testResourcePoolRequest = generatorDto(false, false, false, true, false, true,false,false,false); testResourcePoolRequest = generatorDto(false, false, false, true, false, true,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 port 为空 //资源池节点不为空但是内容为空 port 为空
testResourcePoolRequest = generatorDto(false, false, false, true, false, false,true,false,false); testResourcePoolRequest = generatorDto(false, false, false, true, false, false,true,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 port 为空 //资源池节点不为空但是内容为空 port 为空
testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,true,false); testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,true,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//资源池节点不为空但是内容为空 最大线程数 为空 //资源池节点不为空但是内容为空 最大线程数 为空
testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,false,true); testResourcePoolRequest = generatorDto(false, false, false, true, false, false,false,false,true);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
//应用组织 //应用组织
testResourcePoolRequest = generatorDto(true,false, false, false, true, false,false,false,false); testResourcePoolRequest = generatorDto(true,false, false, false, true, false,false,false,false);
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, status().isBadRequest());
//部分组织 //部分组织
testResourcePoolRequest = generatorDto(true,false, false, false, false, false, false,false,false); testResourcePoolRequest = generatorDto(true,false, false, false, false, false, false,false,false);
testResourcePoolRequest.setAllOrg(false); testResourcePoolRequest.setAllOrg(false);
testResourcePoolRequest.setTestResourceDTO(JSON.parseObject(configurationWidthOutOrgIds, TestResourceDTO.class)); testResourcePoolRequest.setTestResourceDTO(JSON.parseObject(configurationWidthOutOrgIds, TestResourceDTO.class));
this.requestPost(url, testResourcePoolRequest, ERROR_REQUEST_MATCHER); this.requestPost(urlType, url, id, testResourcePoolRequest, ERROR_REQUEST_MATCHER);
} }
@Test @Test
@ -445,11 +438,35 @@ class TestResourcePoolControllerTests extends BaseTest {
} }
private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception {
void listByKeyWord(String keyWord) throws Exception {
QueryResourcePoolRequest request = new QueryResourcePoolRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setKeyword(keyWord);
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/page")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(request))
.contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk()).andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn();
String sortData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8);
ResultHolder sortHolder = JsonUtils.parseObject(sortData, ResultHolder.class);
Pager<?> sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class);
// 返回值中取出第一条ID最大的数据, 并判断是否是default-admin
TestResourcePoolDTO testResourcePoolDTO = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), TestResourcePoolDTO.class).get(0);
Assertions.assertTrue(StringUtils.equals(testResourcePoolDTO.getName(), keyWord));
}
private void requestPost(String urlType, String url, String id, TestResourcePoolRequest testResourcePoolRequest, ResultMatcher resultMatcher) throws Exception {
if (!StringUtils.equals(urlType,"ADD")) {
testResourcePoolRequest.setId(id);
}
mockMvc.perform(MockMvcRequestBuilders.post(url) mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken) .header(SessionConstants.CSRF_TOKEN, csrfToken)
.content(JSON.toJSONString(param)) .content(JSON.toJSONString(testResourcePoolRequest))
.contentType(MediaType.APPLICATION_JSON)) .contentType(MediaType.APPLICATION_JSON))
.andExpect(resultMatcher).andDo(print()) .andExpect(resultMatcher).andDo(print())
.andExpect(content().contentType(MediaType.APPLICATION_JSON)); .andExpect(content().contentType(MediaType.APPLICATION_JSON));
@ -462,6 +479,7 @@ class TestResourcePoolControllerTests extends BaseTest {
testResourcePoolDTO.setName("test_pool_test"); testResourcePoolDTO.setName("test_pool_test");
} }
//没类型 //没类型
if (!noType) { if (!noType) {
testResourcePoolDTO.setType(ResourcePoolTypeEnum.NODE.name()); testResourcePoolDTO.setType(ResourcePoolTypeEnum.NODE.name());
} }