feat: add system resource pool service

This commit is contained in:
guoyuqi 2023-07-19 19:19:00 +08:00 committed by fit2-zhao
parent 61fd6ae3be
commit a8ee2d4621
8 changed files with 101 additions and 25 deletions

View File

@ -0,0 +1,15 @@
package io.metersphere.sdk.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;
@Data
public class OrgIdNameDTO {
@Schema(title = "关联的组织id")
private String id;
@Schema(title = "关联的组织名称")
private String name;
}

View File

@ -1,12 +1,12 @@
package io.metersphere.sdk.dto;
import io.metersphere.sdk.dto.BasePageRequest;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Getter;
import lombok.Setter;
@Getter
@Setter
public class QueryResourcePoolRequest extends BasePageRequest {
private String name;
@Schema(title = "是否禁用")
private Boolean enable;
}

View File

@ -81,6 +81,9 @@ public class TestResourceDTO {
@Schema(title = "UI测试的grid配置")
private String uiGrid;
@Schema(title = "grid最大线程数")
private String girdConcurrentNumber;
/**
* 关联的组织id集合
*/

View File

@ -5,7 +5,6 @@ import lombok.Getter;
import lombok.Setter;
import java.util.List;
import java.util.Map;
/**
*用来返回TestResourceBlob的结构
@ -84,6 +83,6 @@ public class TestResourceReturnDTO {
/**
* 关联的组织id集合
*/
@Schema(title = "关联的组织id和名称map")
private Map<String,String> orgIdNameMap;
@Schema(title = "关联的组织id和名称的集合")
private List<OrgIdNameDTO> orgIdNameMap;
}

View File

@ -52,13 +52,11 @@ public class TestResourcePoolService {
testResourcePoolBlob.setId(id);
TestResourceDTO testResourceDTO = testResourcePool.getTestResourceDTO();
checkAndSaveOrgRelation(testResourcePool, id, testResourceDTO);
checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType());
checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType());
checkUiConfig(testResourceDTO, testResourcePool);
String configuration = JSON.toJSONString(testResourceDTO);
testResourcePoolBlob.setConfiguration(configuration.getBytes());
buildTestPoolBaseInfo(testResourcePool, id);
testResourcePoolMapper.insert(testResourcePool);
testResourcePoolBlobMapper.insert(testResourcePoolBlob);
@ -178,14 +176,21 @@ public class TestResourcePoolService {
checkApiConfig(testResourceDTO, testResourcePool, testResourcePool.getType());
checkLoadConfig(testResourceDTO, testResourcePool, testResourcePool.getType());
checkUiConfig(testResourceDTO, testResourcePool);
testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool);
String configuration = JSON.toJSONString(testResourceDTO);
TestResourcePoolBlob testResourcePoolBlob = new TestResourcePoolBlob();
testResourcePoolBlob.setId(testResourcePool.getId());
testResourcePoolBlob.setConfiguration(configuration.getBytes());
testResourcePoolBlobMapper.updateByPrimaryKeyWithBLOBs(testResourcePoolBlob);
testResourcePoolMapper.updateByPrimaryKey(testResourcePool);
}
public List<TestResourcePoolDTO> listResourcePools(QueryResourcePoolRequest request) {
TestResourcePoolExample example = new TestResourcePoolExample();
TestResourcePoolExample.Criteria criteria = example.createCriteria();
if (StringUtils.isNotBlank(request.getName())) {
criteria.andNameLike(StringUtils.wrapIfMissing(request.getName(), "%"));
if (StringUtils.isNotBlank(request.getKeyword())) {
criteria.andNameLike(StringUtils.wrapIfMissing(request.getKeyword(), "%"));
}
if (request.getEnable() != null) {
criteria.andEnableEqualTo(request.getEnable());
@ -250,15 +255,17 @@ public class TestResourcePoolService {
TestResourceReturnDTO testResourceReturnDTO = new TestResourceReturnDTO();
BeanUtils.copyBean(testResourceReturnDTO, testResourceDTO);
List<String> orgIds = testResourceDTO.getOrgIds();
Map<String,String> orgIdNameMap = new HashMap<>();
List<OrgIdNameDTO>orgIdNameMap = new ArrayList<>();
if (CollectionUtils.isNotEmpty(orgIds)) {
for (String orgId : orgIds) {
OrgIdNameDTO orgIdNameDTO = new OrgIdNameDTO();
OrganizationMapper organizationMapper = CommonBeanFactory.getBean(OrganizationMapper.class);
Organization organization = organizationMapper.selectByPrimaryKey(orgId);
orgIdNameDTO.setId(orgId);
if (organization != null) {
orgIdNameMap.put(orgId,organization.getName());
orgIdNameDTO.setName(organization.getName());
} else {
orgIdNameMap.put(orgId,Translator.get("organization_not_exists"));
orgIdNameDTO.setName(Translator.get("organization_not_exists"));
}
}
}
@ -305,8 +312,8 @@ public class TestResourcePoolService {
return null;
}
public LogDTO updateLog(TestResourcePoolRequest request) {
TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(request.getId());
public LogDTO updateLog(String resourcePoolId) {
TestResourcePool pool = testResourcePoolMapper.selectByPrimaryKey(resourcePoolId);
if (pool != null) {
LogDTO dto = new LogDTO(
"system",
@ -324,4 +331,18 @@ public class TestResourcePoolService {
}
return null;
}
public void unableTestResourcePool(String testResourcePoolId) {
TestResourcePool testResourcePool = testResourcePoolMapper.selectByPrimaryKey(testResourcePoolId);
if (testResourcePool == null) {
throw new MSException(Translator.get("test_resource_pool_not_exists"));
}
if (testResourcePool.getEnable()) {
testResourcePool.setEnable(false);
} else {
testResourcePool.setEnable(true);
}
testResourcePoolMapper.updateByPrimaryKeySelective(testResourcePool);
}
}

View File

@ -59,7 +59,7 @@ public class TestResourcePoolController {
@CacheNode // 把监控节点缓存起来
@Operation(summary = "更新资源池")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request)", msClass = TestResourcePoolService.class)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#request.getId())", msClass = TestResourcePoolService.class)
public void updateTestResourcePool(@Validated @RequestBody TestResourcePoolRequest request) {
TestResourcePoolDTO testResourcePool = new TestResourcePoolDTO();
BeanUtils.copyBean(testResourcePool, request);
@ -83,5 +83,13 @@ public class TestResourcePoolController {
return testResourcePoolService.getTestResourcePoolDetail(testResourcePoolId);
}
@PostMapping("/set/enable/{poolId}")
@Operation(summary = "资源池禁用")
@RequiresPermissions(PermissionConstants.SYSTEM_TEST_RESOURCE_POOL_READ_UPDATE)
@Log(type = OperationLogType.UPDATE, expression = "#msClass.updateLog(#testResourcePoolId)", msClass = TestResourcePoolService.class)
public void unableTestResourcePool(@PathVariable(value = "poolId") String testResourcePoolId) {
testResourcePoolService.unableTestResourcePool(testResourcePoolId);
}
}

View File

@ -5,11 +5,9 @@ import base.BaseTest;
import io.metersphere.sdk.constants.ResourcePoolTypeEnum;
import io.metersphere.sdk.constants.SessionConstants;
import io.metersphere.sdk.controller.handler.ResultHolder;
import io.metersphere.sdk.dto.TestResourceDTO;
import io.metersphere.sdk.dto.TestResourceNodeDTO;
import io.metersphere.sdk.dto.TestResourcePoolRequest;
import io.metersphere.sdk.dto.*;
import io.metersphere.sdk.util.JSON;
import io.metersphere.sdk.dto.QueryResourcePoolRequest;
import io.metersphere.sdk.util.Pager;
import io.metersphere.system.domain.TestResourcePoolOrganization;
import io.metersphere.system.domain.TestResourcePoolOrganizationExample;
import io.metersphere.system.mapper.TestResourcePoolOrganizationMapper;
@ -29,6 +27,7 @@ import org.springframework.test.web.servlet.ResultMatcher;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@ -222,18 +221,27 @@ class TestResourcePoolControllerTests extends BaseTest {
@Test
@Order(10)
/*@Sql(scripts = {"/dml/init_test_resource_pool.sql"},
config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED),
executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD)*/
void listResourcePoolsWidthSearch() throws Exception {
QueryResourcePoolRequest request = new QueryResourcePoolRequest();
request.setCurrent(1);
request.setPageSize(5);
request.setName("test_pool");
mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/page")
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));
.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
@ -415,6 +423,28 @@ class TestResourcePoolControllerTests extends BaseTest {
}
@Test
@Order(19)
void unableTestResourcePoolSuccess() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/104")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
@Test
@Order(20)
void unableTestResourcePoolFiled() throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post("/test/resource/pool/set/enable/105")
.header(SessionConstants.HEADER_TOKEN, sessionId)
.header(SessionConstants.CSRF_TOKEN, csrfToken))
.andExpect(ERROR_REQUEST_MATCHER)
.andExpect(content().contentType(MediaType.APPLICATION_JSON));
}
private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception {
mockMvc.perform(MockMvcRequestBuilders.post(url)
.header(SessionConstants.HEADER_TOKEN, sessionId)

View File

@ -1,4 +1,4 @@
# 插入测试数据
INSERT INTO `test_resource_pool` VALUES ('102','test_pool_one', 'node', '1', 1, '1686634885000', '1686634885000', 'TCP', 0, 0, 0, NULL, 1,0);
INSERT INTO `test_resource_pool` VALUES ('103','test_pool_one', 'node', '1', 1, '1686634885000', '1686634885000', 'TCP', 0, 0, 0, NULL, 1,0);
INSERT INTO `test_resource_pool` VALUES ('104','test_pool_one', 'node', '1', 1, '1686634885000', '1686634885000', 'TCP', 0, 0, 0, NULL, 1,0);
INSERT INTO `test_resource_pool` VALUES ('103','test_pool_two', 'node', '1', 1, '1686634885000', '1686634885000', 'TCP', 0, 0, 0, NULL, 1,0);
INSERT INTO `test_resource_pool` VALUES ('104','test_pool_three', 'node', '1', 1, '1686634885000', '1686634885000', 'TCP', 0, 0, 0, NULL, 1,0);