diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationProjectController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationProjectController.java index ab309730f6..af854d2192 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationProjectController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationProjectController.java @@ -4,23 +4,20 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; -import io.metersphere.system.dto.sdk.OptionDTO; -import io.metersphere.system.dto.user.UserExtendDTO; -import io.metersphere.system.security.CheckOwner; -import io.metersphere.system.utils.PageUtils; -import io.metersphere.system.utils.Pager; import io.metersphere.system.dto.AddProjectRequest; import io.metersphere.system.dto.ProjectDTO; import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectRequest; +import io.metersphere.system.dto.request.*; +import io.metersphere.system.dto.sdk.OptionDTO; +import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.dto.request.OrganizationProjectRequest; -import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; -import io.metersphere.system.dto.request.ProjectAddMemberRequest; -import io.metersphere.system.dto.request.ProjectMemberRequest; +import io.metersphere.system.security.CheckOwner; import io.metersphere.system.service.OrganizationProjectLogService; import io.metersphere.system.service.OrganizationProjectService; +import io.metersphere.system.utils.PageUtils; +import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; @@ -164,15 +161,15 @@ public class OrganizationProjectController { @RequiresPermissions(PermissionConstants.ORGANIZATION_PROJECT_READ) public List getUserMemberList(@PathVariable String organizationId, @PathVariable String projectId, @Schema(description = "查询关键字,根据邮箱和用户名查询") - @RequestParam(value = "keyword", required = false) String keyword) { + @RequestParam(value = "keyword", required = false) String keyword) { return organizationProjectService.getUserMemberList(organizationId, projectId, keyword); } - @GetMapping("/pool-options/{organizationId}") + @PostMapping("/pool-options") @Operation(summary = "系统设置-组织-项目-获取资源池下拉选项") @RequiresPermissions(PermissionConstants.ORGANIZATION_PROJECT_READ) - public List getProjectOptions(@PathVariable String organizationId) { - return organizationProjectService.getTestResourcePoolOptions(organizationId); + public List getProjectOptions(@Validated @RequestBody ProjectPoolRequest request) { + return organizationProjectService.getTestResourcePoolOptions(request); } @PostMapping("/rename") diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java index e92506832a..8846eb90d7 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemProjectController.java @@ -4,25 +4,22 @@ package io.metersphere.system.controller; import com.github.pagehelper.Page; import com.github.pagehelper.PageHelper; import io.metersphere.sdk.constants.PermissionConstants; -import io.metersphere.system.dto.sdk.OptionDTO; -import io.metersphere.system.dto.user.UserExtendDTO; -import io.metersphere.system.security.CheckOwner; -import io.metersphere.system.utils.PageUtils; -import io.metersphere.system.utils.Pager; import io.metersphere.system.domain.User; import io.metersphere.system.dto.AddProjectRequest; import io.metersphere.system.dto.ProjectDTO; import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectRequest; +import io.metersphere.system.dto.request.*; +import io.metersphere.system.dto.sdk.OptionDTO; +import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.log.annotation.Log; import io.metersphere.system.log.constants.OperationLogType; -import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; -import io.metersphere.system.dto.request.ProjectAddMemberRequest; -import io.metersphere.system.dto.request.ProjectMemberRequest; -import io.metersphere.system.dto.request.ProjectRequest; +import io.metersphere.system.security.CheckOwner; import io.metersphere.system.service.SystemProjectLogService; import io.metersphere.system.service.SystemProjectService; import io.metersphere.system.service.UserService; +import io.metersphere.system.utils.PageUtils; +import io.metersphere.system.utils.Pager; import io.metersphere.system.utils.SessionUtils; import io.metersphere.validation.groups.Created; import io.metersphere.validation.groups.Updated; @@ -101,7 +98,7 @@ public class SystemProjectController { @Parameter(name = "id", description = "项目", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)) @CheckOwner(resourceId = "#id", resourceType = "project") public int revokeProject(@PathVariable String id) { - return systemProjectService.revoke(id, SessionUtils.getUserId()); + return systemProjectService.revoke(id, SessionUtils.getUserId()); } @GetMapping("/enable/{id}") @@ -160,15 +157,15 @@ public class SystemProjectController { @Operation(summary = "系统设置-系统-组织与项目-项目-系统-组织及项目, 获取管理员下拉选项") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) public List getUserList(@Schema(description = "查询关键字,根据邮箱和用户名查询") - @RequestParam(value = "keyword", required = false) String keyword) { + @RequestParam(value = "keyword", required = false) String keyword) { return userService.getUserList(keyword); } - @GetMapping("/pool-options") + @PostMapping("/pool-options") @Operation(summary = "系统设置-系统-组织与项目-项目-获取资源池下拉选项") @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) - public List getProjectOptions(@RequestParam(value = "organizationId",required = false) String organizationId) { - return systemProjectService.getTestResourcePoolOptions(organizationId); + public List getProjectOptions(@Validated @RequestBody ProjectPoolRequest request) { + return systemProjectService.getTestResourcePoolOptions(request); } @PostMapping("/rename") diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/ProjectPoolRequest.java b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/ProjectPoolRequest.java new file mode 100644 index 0000000000..c0c3ded9d3 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/dto/request/ProjectPoolRequest.java @@ -0,0 +1,20 @@ +package io.metersphere.system.dto.request; + +import io.swagger.v3.oas.annotations.media.Schema; +import jakarta.validation.constraints.NotEmpty; +import jakarta.validation.constraints.Size; +import lombok.Data; + +import java.io.Serializable; +import java.util.List; + +@Data +public class ProjectPoolRequest implements Serializable { + @Schema(description = "组织id") + @Size(min = 0, max = 50, message = "project.organization_id.length_range") + private String organizationId; + + @Schema(description = "项目开启的模块id集合") + @NotEmpty + private List modulesIds; +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java index 045f03fbed..bbf35b7ad6 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/CommonProjectService.java @@ -18,6 +18,7 @@ import io.metersphere.sdk.util.Translator; import io.metersphere.system.domain.*; import io.metersphere.system.dto.*; import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; +import io.metersphere.system.dto.request.ProjectPoolRequest; import io.metersphere.system.dto.sdk.OptionDTO; import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.invoker.ProjectServiceInvoker; @@ -74,6 +75,9 @@ public class CommonProjectService { @Resource private TestResourcePoolService testResourcePoolService; public static final Integer DEFAULT_REMAIN_DAY_COUNT = 30; + public static final String API_TEST = "apiTest"; + public static final String UI_TEST = "uiTest"; + public static final String LOAD_TEST = "loadTest"; @Autowired public CommonProjectService(ProjectServiceInvoker serviceInvoker) { @@ -612,12 +616,13 @@ public class CommonProjectService { projectMapper.updateByPrimaryKeySelective(project); } - public List getTestResourcePoolOptions(String organizationId) { + public List getTestResourcePoolOptions(ProjectPoolRequest request) { + List optionDTOS = new ArrayList<>(); //获取制定组织的资源池 和全部组织的资源池 List testResourcePools = new ArrayList<>(); - if (StringUtils.isNotBlank(organizationId)) { + if (StringUtils.isNotBlank(request.getOrganizationId())) { TestResourcePoolOrganizationExample example = new TestResourcePoolOrganizationExample(); - example.createCriteria().andOrgIdEqualTo(organizationId); + example.createCriteria().andOrgIdEqualTo(request.getOrganizationId()); List orgPools = testResourcePoolOrganizationMapper.selectByExample(example); if (CollectionUtils.isNotEmpty(orgPools)) { List poolIds = orgPools.stream().map(TestResourcePoolOrganization::getTestResourcePoolId).toList(); @@ -632,9 +637,19 @@ public class CommonProjectService { testResourcePools.addAll(testResourcePoolMapper.selectByExample(poolExample)); testResourcePools = testResourcePools.stream().filter(Objects::nonNull).distinct().collect(Collectors.toList()); - return testResourcePools.stream().map(testResourcePool -> - new OptionDTO(testResourcePool.getId(), testResourcePool.getName()) - ).toList(); + //这里需要获取项目开启的模块 判断资源池开启的使用范围的模块是否在项目开启的模块中 + List moduleIds = request.getModulesIds(); + testResourcePools.forEach(pool -> { + if ((moduleIds.contains(API_TEST) && pool.getApiTest()) || + (moduleIds.contains(LOAD_TEST) && pool.getLoadTest()) || + (moduleIds.contains(UI_TEST) && pool.getUiTest())) { + OptionDTO optionDTO = new OptionDTO(); + optionDTO.setId(pool.getId()); + optionDTO.setName(pool.getName()); + optionDTOS.add(optionDTO); + } + }); + return optionDTOS; } public void rename(UpdateProjectNameRequest request, String userId) { @@ -653,6 +668,7 @@ public class CommonProjectService { /** * 校验该项目是否有权限使用该资源池 + * * @param resourcePool * @return */ @@ -683,6 +699,7 @@ public class CommonProjectService { /** * 剩余天数 + * * @param deleteTime 删除时间 * @return 剩余天数 */ diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectService.java index b129d963d6..7560bd997f 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationProjectService.java @@ -1,7 +1,5 @@ package io.metersphere.system.service; -import io.metersphere.system.dto.sdk.OptionDTO; -import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.sdk.exception.MSException; import io.metersphere.sdk.util.BeanUtils; import io.metersphere.sdk.util.Translator; @@ -11,15 +9,14 @@ import io.metersphere.system.dto.AddProjectRequest; import io.metersphere.system.dto.ProjectDTO; import io.metersphere.system.dto.UpdateProjectNameRequest; import io.metersphere.system.dto.UpdateProjectRequest; +import io.metersphere.system.dto.request.*; +import io.metersphere.system.dto.sdk.OptionDTO; +import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.mapper.ExtSystemProjectMapper; import io.metersphere.system.mapper.OrganizationMapper; import io.metersphere.system.mapper.UserRoleRelationMapper; -import io.metersphere.system.dto.request.OrganizationProjectRequest; -import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; -import io.metersphere.system.dto.request.ProjectMemberRequest; -import io.metersphere.system.dto.request.ProjectRequest; import jakarta.annotation.Resource; import org.apache.commons.collections.CollectionUtils; import org.apache.commons.lang3.StringUtils; @@ -131,8 +128,8 @@ public class OrganizationProjectService { } } - public List getTestResourcePoolOptions(String organizationId) { - return commonProjectService.getTestResourcePoolOptions(organizationId); + public List getTestResourcePoolOptions(ProjectPoolRequest request) { + return commonProjectService.getTestResourcePoolOptions(request); } public void rename(UpdateProjectNameRequest project, String userId) { diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java index b2b6a23b13..307150f1d0 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/SystemProjectService.java @@ -1,16 +1,17 @@ package io.metersphere.system.service; import io.metersphere.project.domain.Project; -import io.metersphere.system.dto.sdk.OptionDTO; -import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.sdk.util.Translator; import io.metersphere.system.dto.*; +import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; +import io.metersphere.system.dto.request.ProjectMemberRequest; +import io.metersphere.system.dto.request.ProjectPoolRequest; +import io.metersphere.system.dto.request.ProjectRequest; +import io.metersphere.system.dto.sdk.OptionDTO; +import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.log.constants.OperationLogModule; import io.metersphere.system.log.constants.OperationLogType; import io.metersphere.system.mapper.ExtSystemProjectMapper; -import io.metersphere.system.dto.request.ProjectAddMemberBatchRequest; -import io.metersphere.system.dto.request.ProjectMemberRequest; -import io.metersphere.system.dto.request.ProjectRequest; import jakarta.annotation.Resource; import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; @@ -96,8 +97,8 @@ public class SystemProjectService { commonProjectService.disable(id, updateUser); } - public List getTestResourcePoolOptions(String organizationId) { - return commonProjectService.getTestResourcePoolOptions(organizationId); + public List getTestResourcePoolOptions(ProjectPoolRequest request) { + return commonProjectService.getTestResourcePoolOptions(request); } diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java index 450a6aad18..96b4d2834b 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationProjectControllerTests.java @@ -19,6 +19,7 @@ import io.metersphere.system.dto.*; import io.metersphere.system.dto.request.OrganizationProjectRequest; import io.metersphere.system.dto.request.ProjectAddMemberRequest; import io.metersphere.system.dto.request.ProjectMemberRequest; +import io.metersphere.system.dto.request.ProjectPoolRequest; import io.metersphere.system.dto.user.UserDTO; import io.metersphere.system.dto.user.UserExtendDTO; import io.metersphere.system.invoker.ProjectServiceInvoker; @@ -50,7 +51,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 OrganizationProjectControllerTests extends BaseTest { @@ -128,8 +129,9 @@ public class OrganizationProjectControllerTests extends BaseTest { .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); } + private void responseGet(String url, ResultMatcher resultMatcher) throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get(url) + mockMvc.perform(MockMvcRequestBuilders.get(url) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .contentType(MediaType.APPLICATION_JSON)) @@ -148,6 +150,7 @@ public class OrganizationProjectControllerTests extends BaseTest { } return null; } + public static void compareProjectDTO(Project currentProject, Project result) { Assertions.assertNotNull(currentProject); Assertions.assertNotNull(result); @@ -173,6 +176,7 @@ public class OrganizationProjectControllerTests extends BaseTest { addProjectDTO.setUserIds(userIds); return addProjectDTO; } + public UpdateProjectRequest generatorUpdate(String organizationId, String projectId, String name, @@ -285,6 +289,7 @@ public class OrganizationProjectControllerTests extends BaseTest { serviceInvoker.invokeCreateServices(initProject.getId()); } } + @Test @Order(1) @Sql(scripts = {"/dml/init_org_project.sql"}, @@ -295,7 +300,7 @@ public class OrganizationProjectControllerTests extends BaseTest { */ public void testAddProjectSuccess() throws Exception { initData(); - AddProjectRequest project = this.generatorAdd("organizationId","organization-name", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd("organizationId", "organization-name", "description", true, List.of("admin")); MvcResult mvcResult = this.responsePost(addProject, project); ProjectDTO result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); ProjectExample projectExample = new ProjectExample(); @@ -312,7 +317,7 @@ public class OrganizationProjectControllerTests extends BaseTest { List userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin")); userRoleRelationExample.createCriteria().andSourceIdEqualTo("organizationId").andRoleIdEqualTo(InternalUserRole.ORG_MEMBER.getValue()); - userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); + userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin")); projectId = result.getId(); Project projectExtend = projectMapper.selectByPrimaryKey(projectId); @@ -321,7 +326,7 @@ public class OrganizationProjectControllerTests extends BaseTest { checkLog(projectId, OperationLogType.ADD); //userId为空的时候 - project = this.generatorAdd("organizationId","organization-userIdIsNull", "description", true, new ArrayList<>()); + project = this.generatorAdd("organizationId", "organization-userIdIsNull", "description", true, new ArrayList<>()); mvcResult = this.responsePost(addProject, project); result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); projectExample = new ProjectExample(); @@ -391,7 +396,6 @@ public class OrganizationProjectControllerTests extends BaseTest { Assertions.assertTrue(projectTestResourcePools.stream().map(ProjectTestResourcePool::getTestResourcePoolId).toList().contains("resourcePoolId")); - project.setName("organization-testAddProjectSuccess1"); project.setOrganizationId(getDefault().getId()); // @@校验权限 @@ -404,10 +408,10 @@ public class OrganizationProjectControllerTests extends BaseTest { * 测试添加项目失败的用例 */ public void testAddProjectError() throws Exception { - AddProjectRequest project = this.generatorAdd("organizationId","organization-nameError", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd("organizationId", "organization-nameError", "description", true, List.of("admin")); this.responsePost(addProject, project); //项目名称存在 500 - project = this.generatorAdd("organizationId","organization-nameError", "description", true, List.of("admin")); + project = this.generatorAdd("organizationId", "organization-nameError", "description", true, List.of("admin")); this.requestPost(addProject, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 project = this.generatorAdd(null, null, null, true, List.of("admin")); @@ -424,10 +428,11 @@ public class OrganizationProjectControllerTests extends BaseTest { this.requestPost(addProject, project, ERROR_REQUEST_MATCHER); } + @Test @Order(3) public void testGetProject() throws Exception { - AddProjectRequest project = this.generatorAdd("organizationId","organization-getName", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd("organizationId", "organization-getName", "description", true, List.of("admin")); MvcResult mvcResult = this.responsePost(addProject, project); ProjectDTO result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); assert result != null; @@ -448,6 +453,7 @@ public class OrganizationProjectControllerTests extends BaseTest { // @@校验权限 requestGetPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, getProject + projectId); } + @Test @Order(4) public void testGetProjectError() throws Exception { @@ -565,7 +571,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(7) public void testUpdateProject() throws Exception { - UpdateProjectRequest project = this.generatorUpdate("organizationId", projectId,"organization-TestName", "Edit name", true, List.of("admin1")); + UpdateProjectRequest project = this.generatorUpdate("organizationId", projectId, "organization-TestName", "Edit name", true, List.of("admin1")); Project projectExtend = projectMapper.selectByPrimaryKey(projectId); List moduleIds = new ArrayList<>(); if (StringUtils.isNotBlank(projectExtend.getModuleSetting())) { @@ -625,7 +631,7 @@ public class OrganizationProjectControllerTests extends BaseTest { //设置资源池 project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId3", "org-updatePools", "org-updatePools", true, new ArrayList<>()); - project.setResourcePoolIds(List.of("resourcePoolId","resourcePoolId1")); + project.setResourcePoolIds(List.of("resourcePoolId", "resourcePoolId1")); mvcResult = this.responsePost(updateProject, project); result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); currentProject = projectMapper.selectByPrimaryKey(project.getId()); @@ -650,7 +656,7 @@ public class OrganizationProjectControllerTests extends BaseTest { UpdateProjectRequest project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1", "org-Module", "description", true, List.of("admin")); this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 - project = this.generatorUpdate(null, "projectId",null, null, true , List.of("admin")); + project = this.generatorUpdate(null, "projectId", null, null, true, List.of("admin")); this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); //项目Id为空 project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, null, null, null, true, List.of("admin")); @@ -714,7 +720,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(13) - public void testAddProjectMember() throws Exception{ + public void testAddProjectMember() throws Exception { ProjectAddMemberRequest projectAddMemberRequest = new ProjectAddMemberRequest(); projectAddMemberRequest.setProjectId(projectId); List userIds = List.of("admin1", "admin2"); @@ -725,7 +731,7 @@ public class OrganizationProjectControllerTests extends BaseTest { List userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().containsAll(userIds)); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().containsAll(userIds)); - userRoleRelations.forEach(item ->{ + userRoleRelations.forEach(item -> { try { checkLog(item.getId(), OperationLogType.ADD); } catch (Exception e) { @@ -738,7 +744,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(14) - public void testAddProjectMemberError() throws Exception{ + public void testAddProjectMemberError() throws Exception { //项目Id为空 ProjectAddMemberRequest projectAddMemberRequest = new ProjectAddMemberRequest(); projectAddMemberRequest.setProjectId(null); @@ -831,7 +837,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(17) - public void testRemoveProjectMember() throws Exception{ + public void testRemoveProjectMember() throws Exception { String userId = "admin1"; UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample(); userRoleRelationExample.createCriteria().andSourceIdEqualTo(projectId).andUserIdEqualTo(userId); @@ -847,7 +853,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(18) - public void testRemoveProjectMemberError() throws Exception{ + public void testRemoveProjectMemberError() throws Exception { String userId = "admin1"; MvcResult mvcResult = this.responseGet(removeProjectMember + projectId + "/" + userId); int count = parseObjectFromMvcResult(mvcResult, Integer.class); @@ -867,7 +873,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Order(19) public void disableSuccess() throws Exception { String id = "projectId"; - this.responseGet(disableProject + id,status().isOk()); + this.responseGet(disableProject + id, status().isOk()); Project currentProject = projectMapper.selectByPrimaryKey(id); Assertions.assertEquals(currentProject.getEnable(), false); checkLog(id, OperationLogType.UPDATE); @@ -886,7 +892,7 @@ public class OrganizationProjectControllerTests extends BaseTest { @Order(19) public void enableSuccess() throws Exception { String id = "projectId"; - this.responseGet(enableProject + id,status().isOk()); + this.responseGet(enableProject + id, status().isOk()); Project currentProject = projectMapper.selectByPrimaryKey(id); Assertions.assertEquals(currentProject.getEnable(), true); checkLog(id, OperationLogType.UPDATE); @@ -959,9 +965,12 @@ public class OrganizationProjectControllerTests extends BaseTest { @Test @Order(23) public void testGetOptions() throws Exception { - this.requestGetWithOkAndReturn(getPoolOptions + "organizationId"); + ProjectPoolRequest projectPoolRequest = new ProjectPoolRequest(); + projectPoolRequest.setModulesIds(List.of("apiTest", "uiTest", "loadTest")); + projectPoolRequest.setOrganizationId(DEFAULT_ORGANIZATION_ID); + this.responsePost(getPoolOptions, projectPoolRequest); // @@校验权限 - requestGetPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, getPoolOptions + DEFAULT_ORGANIZATION_ID); + requestPostPermissionTest(PermissionConstants.ORGANIZATION_PROJECT_READ, getPoolOptions, projectPoolRequest); } @Test diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java index b1066651d6..07ce0fbff0 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemProjectControllerTests.java @@ -54,7 +54,7 @@ import java.util.stream.Collectors; 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 SystemProjectControllerTests extends BaseTest { @@ -149,8 +149,9 @@ public class SystemProjectControllerTests extends BaseTest { .andExpect(status().isOk()) .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); } + private void responseGet(String url, ResultMatcher resultMatcher) throws Exception { - mockMvc.perform(MockMvcRequestBuilders.get(url) + mockMvc.perform(MockMvcRequestBuilders.get(url) .header(SessionConstants.HEADER_TOKEN, sessionId) .header(SessionConstants.CSRF_TOKEN, csrfToken) .contentType(MediaType.APPLICATION_JSON)) @@ -169,6 +170,7 @@ public class SystemProjectControllerTests extends BaseTest { } return null; } + public static void compareProjectDTO(Project currentProject, Project result) { Assertions.assertNotNull(currentProject); Assertions.assertNotNull(result); @@ -194,6 +196,7 @@ public class SystemProjectControllerTests extends BaseTest { addProjectDTO.setUserIds(userIds); return addProjectDTO; } + public UpdateProjectRequest generatorUpdate(String organizationId, String projectId, String name, @@ -308,6 +311,7 @@ public class SystemProjectControllerTests extends BaseTest { serviceInvoker.invokeCreateServices(initProject.getId()); } } + @Test @Order(1) /** @@ -319,7 +323,7 @@ public class SystemProjectControllerTests extends BaseTest { public void testAddProjectSuccess() throws Exception { initData(); addMultipleCustomField(); - AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"name", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "name", "description", true, List.of("admin")); MvcResult mvcResult = this.responsePost(addProject, project); ProjectDTO result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); ProjectExample projectExample = new ProjectExample(); @@ -336,7 +340,7 @@ public class SystemProjectControllerTests extends BaseTest { List userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin")); userRoleRelationExample.createCriteria().andSourceIdEqualTo(DEFAULT_ORGANIZATION_ID).andRoleIdEqualTo(InternalUserRole.ORG_MEMBER.getValue()); - userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); + userRoleRelations = userRoleRelationMapper.selectByExample(userRoleRelationExample); Assertions.assertTrue(userRoleRelations.stream().map(UserRoleRelation::getUserId).toList().contains("admin")); Project currentProject = projectMapper.selectByPrimaryKey(projectId); Assertions.assertNull(currentProject.getModuleSetting()); @@ -345,7 +349,7 @@ public class SystemProjectControllerTests extends BaseTest { assertionsRefStatusFlowSetting(projectId); //userId为空的时候 - project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"userIdIsNull", "description", true, new ArrayList<>()); + project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "userIdIsNull", "description", true, new ArrayList<>()); // 开启项目模板 changeOrgTemplateEnable(false); mvcResult = this.responsePost(addProject, project); @@ -604,10 +608,10 @@ public class SystemProjectControllerTests extends BaseTest { * 测试添加项目失败的用例 */ public void testAddProjectError() throws Exception { - AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"nameError", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "nameError", "description", true, List.of("admin")); this.responsePost(addProject, project); //项目名称存在 500 - project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"nameError", "description", true, List.of("admin")); + project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "nameError", "description", true, List.of("admin")); this.requestPost(addProject, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 project = this.generatorAdd(null, null, null, true, List.of("admin")); @@ -624,10 +628,11 @@ public class SystemProjectControllerTests extends BaseTest { this.requestPost(addProject, project, ERROR_REQUEST_MATCHER); } + @Test @Order(3) public void testGetProject() throws Exception { - AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID,"getName", "description", true, List.of("admin")); + AddProjectRequest project = this.generatorAdd(DEFAULT_ORGANIZATION_ID, "getName", "description", true, List.of("admin")); List moduleIds = new ArrayList<>(); moduleIds.add("apiTest"); moduleIds.add("uiTest"); @@ -643,6 +648,7 @@ public class SystemProjectControllerTests extends BaseTest { // @@校验权限 requestGetPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, getProject + projectId); } + @Test @Order(4) public void testGetProjectError() throws Exception { @@ -760,7 +766,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(7) public void testUpdateProject() throws Exception { - UpdateProjectRequest project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1","TestName", "Edit name", true, List.of("admin", "admin1")); + UpdateProjectRequest project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId1", "TestName", "Edit name", true, List.of("admin", "admin1")); MvcResult mvcResult = this.responsePost(updateProject, project); ProjectDTO result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); Project currentProject = projectMapper.selectByPrimaryKey(project.getId()); @@ -809,7 +815,7 @@ public class SystemProjectControllerTests extends BaseTest { //设置资源池 project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId5", "updatePools", "updatePools", true, new ArrayList<>()); - project.setResourcePoolIds(List.of("resourcePoolId","resourcePoolId1")); + project.setResourcePoolIds(List.of("resourcePoolId", "resourcePoolId1")); mvcResult = this.responsePost(updateProject, project); result = parseObjectFromMvcResult(mvcResult, ProjectDTO.class); currentProject = projectMapper.selectByPrimaryKey(project.getId()); @@ -830,22 +836,22 @@ public class SystemProjectControllerTests extends BaseTest { @Order(8) public void testUpdateProjectError() throws Exception { //项目名称存在 500 - UpdateProjectRequest project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2","TestName2", "description", true, List.of("admin")); + UpdateProjectRequest project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "TestName2", "description", true, List.of("admin")); this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); //参数组织Id为空 - project = this.generatorUpdate(null, "projectId",null, null, true , List.of("admin")); + project = this.generatorUpdate(null, "projectId", null, null, true, List.of("admin")); this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); //项目Id为空 - project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, null,null, null, true, List.of("admin")); + project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, null, null, null, true, List.of("admin")); this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); //项目名称为空 - project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId",null, null, true, List.of("admin")); + project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId", null, null, true, List.of("admin")); this.requestPost(updateProject, project, BAD_REQUEST_MATCHER); //项目不存在 - project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "1111","123", null, true, List.of("admin")); + project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "1111", "123", null, true, List.of("admin")); this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); //资源池不存在 - project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2","pool-edit", "description", true, List.of("admin")); + project = this.generatorUpdate(DEFAULT_ORGANIZATION_ID, "projectId2", "pool-edit", "description", true, List.of("admin")); project.setResourcePoolIds(List.of("resourcePoolId3")); this.requestPost(updateProject, project, ERROR_REQUEST_MATCHER); @@ -894,10 +900,9 @@ public class SystemProjectControllerTests extends BaseTest { } - @Test @Order(13) - public void testAddProjectMember() throws Exception{ + public void testAddProjectMember() throws Exception { ProjectAddMemberRequest projectAddMemberRequest = new ProjectAddMemberRequest(); projectAddMemberRequest.setProjectId("projectId"); List userIds = List.of("admin1", "admin2"); @@ -914,7 +919,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(14) - public void testAddProjectMemberError() throws Exception{ + public void testAddProjectMemberError() throws Exception { //项目Id为空 ProjectAddMemberRequest projectAddMemberRequest = new ProjectAddMemberRequest(); projectAddMemberRequest.setProjectId(null); @@ -1008,7 +1013,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(17) - public void testRemoveProjectMember() throws Exception{ + public void testRemoveProjectMember() throws Exception { String projectId = "projectId1"; String userId = "admin1"; UserRoleRelationExample userRoleRelationExample = new UserRoleRelationExample(); @@ -1023,7 +1028,7 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(18) - public void testRemoveProjectMemberError() throws Exception{ + public void testRemoveProjectMemberError() throws Exception { String projectId = "projectId1"; String userId = "admin1"; MvcResult mvcResult = this.responseGet(removeProjectMember + projectId + "/" + userId); @@ -1044,7 +1049,7 @@ public class SystemProjectControllerTests extends BaseTest { @Order(19) public void disableSuccess() throws Exception { String id = "projectId"; - this.responseGet(disableProject + id,status().isOk()); + this.responseGet(disableProject + id, status().isOk()); Project currentProject = projectMapper.selectByPrimaryKey(id); Assertions.assertEquals(currentProject.getEnable(), false); checkLog(id, OperationLogType.UPDATE); @@ -1063,7 +1068,7 @@ public class SystemProjectControllerTests extends BaseTest { @Order(19) public void enableSuccess() throws Exception { String id = "projectId"; - this.responseGet(enableProject + id,status().isOk()); + this.responseGet(enableProject + id, status().isOk()); Project currentProject = projectMapper.selectByPrimaryKey(id); Assertions.assertEquals(currentProject.getEnable(), true); checkLog(id, OperationLogType.UPDATE); @@ -1091,10 +1096,13 @@ public class SystemProjectControllerTests extends BaseTest { @Test @Order(22) public void testGetOptions() throws Exception { - this.requestGetWithOkAndReturn(getPoolOptions); - this.requestGetWithOkAndReturn(getPoolOptions + "?organizationId=" + DEFAULT_ORGANIZATION_ID); + ProjectPoolRequest projectPoolRequest = new ProjectPoolRequest(); + projectPoolRequest.setModulesIds(List.of("apiTest", "uiTest", "loadTest")); + this.responsePost(getPoolOptions, projectPoolRequest); + projectPoolRequest.setOrganizationId(DEFAULT_ORGANIZATION_ID); + this.responsePost(getPoolOptions, projectPoolRequest); // @@校验权限 - requestGetPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, getPoolOptions + "?organizationId=" + DEFAULT_ORGANIZATION_ID); + requestPostPermissionTest(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, getPoolOptions, projectPoolRequest); } @Test