From 697375168a5925ef56b24a8b62c2143cb8a8d627 Mon Sep 17 00:00:00 2001 From: song-cc-rock Date: Tue, 18 Jul 2023 14:08:38 +0800 Subject: [PATCH] =?UTF-8?q?refactor(=E7=B3=BB=E7=BB=9F=E8=AE=BE=E7=BD=AE):?= =?UTF-8?q?=20=E7=B3=BB=E7=BB=9F=E7=BB=84=E7=BB=87=E5=8A=9F=E8=83=BD?= =?UTF-8?q?=E6=8B=86=E5=88=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../metersphere/sdk/dto/BasePageRequest.java | 6 +- .../io/metersphere/sdk/mapper/BaseMapper.java | 4 + .../io/metersphere/sdk/mapper/BaseMapper.xml | 50 +++ .../controller/OrganizationController.java | 86 +--- .../SystemOrganizationController.java | 101 +++++ .../system/mapper/ExtOrganizationMapper.java | 8 +- .../system/mapper/ExtOrganizationMapper.xml | 77 ++-- .../system/service/OrganizationService.java | 16 +- .../OrganizationControllerTests.java | 283 +------------ .../SystemOrganizationControllerTests.java | 385 ++++++++++++++++++ 10 files changed, 602 insertions(+), 414 deletions(-) create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.java create mode 100644 backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.xml create mode 100644 backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemOrganizationController.java create mode 100644 backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BasePageRequest.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BasePageRequest.java index 13c1b6b088..f58582c996 100644 --- a/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BasePageRequest.java +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/dto/BasePageRequest.java @@ -10,6 +10,7 @@ import jakarta.validation.constraints.Pattern; import lombok.Data; import org.apache.commons.lang3.StringUtils; +import java.util.List; import java.util.Map; @Data @@ -30,7 +31,10 @@ public class BasePageRequest { private Map<@Valid @Pattern(regexp = "^[A-Za-z]+$") String, @Valid @NotBlank String> sort; @Schema(title = "过滤字段") - private Map filter; + private Map> filter; + + @Schema(title = "高级搜索") + private Map combine; public String getSortString() { if (sort == null || sort.isEmpty()) { diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.java b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.java new file mode 100644 index 0000000000..aa32b06691 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.java @@ -0,0 +1,4 @@ +package io.metersphere.sdk.mapper; + +public interface BaseMapper { +} diff --git a/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.xml b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.xml new file mode 100644 index 0000000000..84743ff960 --- /dev/null +++ b/backend/framework/sdk/src/main/java/io/metersphere/sdk/mapper/BaseMapper.xml @@ -0,0 +1,50 @@ + + + + + + + like CONCAT('%', #{${object}.value},'%') + + + not like CONCAT('%', #{${object}.value},'%') + + + in + + #{v} + + + + not in + + #{v} + + + + between #{${object}.value[0]} and #{${object}.value[1]} + + + > #{${object}.value} + + + < #{${object}.value} + + + >= #{${object}.value} + + + <= #{${object}.value} + + + = #{${object}.value} + + + + + + + #{value} + + + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationController.java index 84ab904b2d..fc89645f11 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationController.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/OrganizationController.java @@ -1,30 +1,8 @@ package io.metersphere.system.controller; -import com.github.pagehelper.Page; -import com.github.pagehelper.PageHelper; -import io.metersphere.sdk.constants.PermissionConstants; -import io.metersphere.sdk.dto.ProjectDTO; -import io.metersphere.sdk.util.PageUtils; -import io.metersphere.sdk.util.Pager; -import io.metersphere.sdk.util.SessionUtils; -import io.metersphere.system.dto.OrganizationDTO; -import io.metersphere.system.dto.UserExtend; -import io.metersphere.system.request.OrganizationMemberRequest; -import io.metersphere.system.request.OrganizationRequest; -import io.metersphere.system.request.ProjectRequest; -import io.metersphere.system.service.OrganizationService; -import io.metersphere.system.service.SystemProjectService; -import io.swagger.v3.oas.annotations.Operation; -import io.swagger.v3.oas.annotations.Parameter; -import io.swagger.v3.oas.annotations.Parameters; -import io.swagger.v3.oas.annotations.media.Schema; import io.swagger.v3.oas.annotations.tags.Tag; -import jakarta.annotation.Resource; -import org.apache.shiro.authz.annotation.RequiresPermissions; -import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.*; - -import java.util.List; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; /** * @author song-cc-rock @@ -34,64 +12,4 @@ import java.util.List; @RequestMapping("/organization") public class OrganizationController { - @Resource - private SystemProjectService systemProjectService; - @Resource - private OrganizationService organizationService; - - @PostMapping("/list") - @Operation(summary = "获取组织列表") - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) - public Pager> list(@Validated @RequestBody OrganizationRequest organizationRequest) { - Page page = PageHelper.startPage(organizationRequest.getCurrent(), organizationRequest.getPageSize()); - return PageUtils.setPageInfo(page, organizationService.list(organizationRequest)); - } - - @PostMapping("/list-all") - @Operation(summary = "获取系统所有组织") - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) - public List listAll() { - return organizationService.listAll(); - } - - @PostMapping("/list-member") - @Operation(summary = "获取组织成员") - @RequiresPermissions(value = {PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ}) - public Pager> listMember(@Validated @RequestBody OrganizationRequest organizationRequest) { - Page page = PageHelper.startPage(organizationRequest.getCurrent(), organizationRequest.getPageSize()); - return PageUtils.setPageInfo(page, organizationService.listMember(organizationRequest)); - } - - @PostMapping("/add-member") - @Operation(summary = "添加组织成员") - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) - public void addMember(@Validated @RequestBody OrganizationMemberRequest organizationMemberRequest) { - organizationService.addMember(organizationMemberRequest, SessionUtils.getUserId()); - } - - @GetMapping("/remove-member/{organizationId}/{userId}") - @Operation(summary = "删除组织成员") - @Parameters({ - @Parameter(name = "organizationId", description = "组织ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)), - @Parameter(name = "userId", description = "成员ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)) - }) - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) - public void removeMember(@PathVariable String organizationId, @PathVariable String userId) { - organizationService.removeMember(organizationId, userId); - } - - @GetMapping("/default") - @Operation(summary = "获取系统默认组织") - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) - public OrganizationDTO getDefault() { - return organizationService.getDefault(); - } - - @PostMapping("/list-project") - @Operation(summary = "获取组织下的项目列表") - @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) - public Pager> listProject(@Validated @RequestBody ProjectRequest projectRequest) { - Page page = PageHelper.startPage(projectRequest.getCurrent(), projectRequest.getPageSize()); - return PageUtils.setPageInfo(page, systemProjectService.getProjectList(projectRequest)); - } } diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemOrganizationController.java b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemOrganizationController.java new file mode 100644 index 0000000000..f5c6267046 --- /dev/null +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/controller/SystemOrganizationController.java @@ -0,0 +1,101 @@ +package io.metersphere.system.controller; + +import com.github.pagehelper.Page; +import com.github.pagehelper.PageHelper; +import io.metersphere.sdk.constants.PermissionConstants; +import io.metersphere.sdk.dto.ProjectDTO; +import io.metersphere.sdk.util.PageUtils; +import io.metersphere.sdk.util.Pager; +import io.metersphere.sdk.util.SessionUtils; +import io.metersphere.system.dto.OrganizationDTO; +import io.metersphere.system.dto.UserExtend; +import io.metersphere.system.request.OrganizationMemberRequest; +import io.metersphere.system.request.OrganizationRequest; +import io.metersphere.system.request.ProjectRequest; +import io.metersphere.system.service.OrganizationService; +import io.metersphere.system.service.SystemProjectService; +import io.swagger.v3.oas.annotations.Operation; +import io.swagger.v3.oas.annotations.Parameter; +import io.swagger.v3.oas.annotations.Parameters; +import io.swagger.v3.oas.annotations.media.Schema; +import io.swagger.v3.oas.annotations.tags.Tag; +import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; +import org.apache.shiro.authz.annotation.RequiresPermissions; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +/** + * @author song-cc-rock + */ +@Tag(name = "系统-组织") +@RestController +@RequestMapping("/system/organization") +public class SystemOrganizationController { + + @Resource + private SystemProjectService systemProjectService; + @Resource + private OrganizationService organizationService; + + @PostMapping("/list") + @Operation(summary = "获取组织列表") + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) + public Pager> list(@Validated @RequestBody OrganizationRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); + return PageUtils.setPageInfo(page, organizationService.list(request)); + } + + @PostMapping("/list-all") + @Operation(summary = "获取系统所有组织") + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) + public List listAll() { + return organizationService.listAll(); + } + + @PostMapping("/list-member") + @Operation(summary = "获取组织成员") + @RequiresPermissions(value = {PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ}) + public Pager> listMember(@Validated @RequestBody OrganizationRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); + return PageUtils.setPageInfo(page, organizationService.listMember(request)); + } + + @PostMapping("/add-member") + @Operation(summary = "添加组织成员") + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) + public void addMember(@Validated @RequestBody OrganizationMemberRequest request) { + organizationService.addMember(request, SessionUtils.getUserId()); + } + + @GetMapping("/remove-member/{organizationId}/{userId}") + @Operation(summary = "删除组织成员") + @Parameters({ + @Parameter(name = "organizationId", description = "组织ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)), + @Parameter(name = "userId", description = "成员ID", schema = @Schema(requiredMode = Schema.RequiredMode.REQUIRED)) + }) + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ_UPDATE) + public void removeMember(@PathVariable String organizationId, @PathVariable String userId) { + organizationService.removeMember(organizationId, userId); + } + + @GetMapping("/default") + @Operation(summary = "获取系统默认组织") + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) + public OrganizationDTO getDefault() { + return organizationService.getDefault(); + } + + @PostMapping("/list-project") + @Operation(summary = "获取组织下的项目列表") + @RequiresPermissions(PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ) + public Pager> listProject(@Validated @RequestBody ProjectRequest request) { + Page page = PageHelper.startPage(request.getCurrent(), request.getPageSize(), + StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc"); + return PageUtils.setPageInfo(page, systemProjectService.getProjectList(request)); + } +} diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.java b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.java index d4c2ef10f9..6913c8ec35 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.java @@ -17,10 +17,10 @@ public interface ExtOrganizationMapper { /** * 查询组织列表 - * @param organizationRequest 列表请求参数 + * @param request 列表请求参数 * @return 组织列表数据 */ - List list(@Param("request") OrganizationRequest organizationRequest); + List list(@Param("request") OrganizationRequest request); /** * 获取系统下所有组织 @@ -49,10 +49,10 @@ public interface ExtOrganizationMapper { /** * 获取组织成员列表(角色) - * @param organizationRequest 组织成员列表请求参数 + * @param request 组织成员列表请求参数 * @return 组织成员列表数据 */ - List listMember(@Param("request") OrganizationRequest organizationRequest); + List listMember(@Param("request") OrganizationRequest request); /** * 获取组织管理员 diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.xml b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.xml index c1a6448a23..dfed344256 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.xml +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/mapper/ExtOrganizationMapper.xml @@ -8,11 +8,6 @@ from organization o left join user_role_relation ur on ur.source_id = o.id left join project p on p.organization_id = o.id - - - - - group by o.id @@ -20,37 +15,6 @@ select * from organization - - - - and ( - o.name like concat('%', #{request.keyword},'%') - or o.id like concat('%', #{request.keyword},'%') - ) - - - - - - - - - - - - - - - - - - - - - - - - update organization set deleted = true , delete_time = #{request.deleteTime} , delete_user = #{request.deleteUserId} where id = #{request.organizationId} @@ -94,4 +58,45 @@ + + + + + and ( + o.name like concat('%', #{request.keyword},'%') + or o.id like concat('%', #{request.keyword},'%') + ) + + + + + + + + + + + + + + + and o.create_user in + + + + + + + + + + + + and o.create_user + + + + + + \ No newline at end of file diff --git a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java index 9e9577cfc3..a915bdaf31 100644 --- a/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java +++ b/backend/services/system-setting/src/main/java/io/metersphere/system/service/OrganizationService.java @@ -40,8 +40,8 @@ public class OrganizationService{ @Resource UserMapper userMapper; - public List list(OrganizationRequest organizationRequest) { - List organizationDTOS = extOrganizationMapper.list(organizationRequest); + public List list(OrganizationRequest request) { + List organizationDTOS = extOrganizationMapper.list(request); return buildOrgAdminInfo(organizationDTOS); } @@ -59,20 +59,20 @@ public class OrganizationService{ return organizationDTO; } - public List listMember(OrganizationRequest organizationRequest) { - return extOrganizationMapper.listMember(organizationRequest); + public List listMember(OrganizationRequest request) { + return extOrganizationMapper.listMember(request); } - public void addMember(OrganizationMemberRequest organizationMemberRequest, String createUserId) { - Organization organization = organizationMapper.selectByPrimaryKey(organizationMemberRequest.getOrganizationId()); + public void addMember(OrganizationMemberRequest request, String createUserId) { + Organization organization = organizationMapper.selectByPrimaryKey(request.getOrganizationId()); if (organization == null) { throw new MSException(Translator.get("organization_not_exist")); } - for (String userId : organizationMemberRequest.getMemberIds()) { + for (String userId : request.getMemberIds()) { UserRoleRelation userRoleRelation = new UserRoleRelation(); userRoleRelation.setId(UUID.randomUUID().toString()); userRoleRelation.setUserId(userId); - userRoleRelation.setSourceId(organizationMemberRequest.getOrganizationId()); + userRoleRelation.setSourceId(request.getOrganizationId()); userRoleRelation.setRoleId(InternalUserRole.ORG_MEMBER.getValue()); userRoleRelation.setCreateTime(System.currentTimeMillis()); userRoleRelation.setCreateUser(createUserId); diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java index aa2c60efbd..17615261b7 100644 --- a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/OrganizationControllerTests.java @@ -2,32 +2,18 @@ package io.metersphere.system.controller; import base.BaseTest; import io.metersphere.sdk.constants.SessionConstants; -import io.metersphere.sdk.controller.handler.ResultHolder; import io.metersphere.sdk.util.JSON; -import io.metersphere.sdk.util.Pager; -import io.metersphere.system.dto.OrganizationDTO; -import io.metersphere.system.dto.UserExtend; -import io.metersphere.system.request.OrganizationMemberRequest; -import io.metersphere.system.request.OrganizationRequest; -import io.metersphere.system.request.ProjectRequest; -import io.metersphere.utils.JsonUtils; import jakarta.annotation.Resource; -import org.apache.commons.lang3.StringUtils; -import org.junit.jupiter.api.*; +import org.junit.jupiter.api.MethodOrderer; +import org.junit.jupiter.api.TestMethodOrder; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; -import org.springframework.test.context.jdbc.Sql; -import org.springframework.test.context.jdbc.SqlConfig; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; -import java.nio.charset.StandardCharsets; -import java.util.Arrays; -import java.util.Collections; - import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -40,271 +26,6 @@ public class OrganizationControllerTests extends BaseTest{ @Resource private MockMvc mockMvc; - public static final String ORGANIZATION_LIST = "/organization/list"; - public static final String ORGANIZATION_LIST_ALL = "/organization/list-all"; - public static final String ORGANIZATION_DEFAULT = "/organization/default"; - public static final String ORGANIZATION_LIST_MEMBER = "/organization/list-member"; - public static final String ORGANIZATION_ADD_MEMBER = "/organization/add-member"; - public static final String ORGANIZATION_REMOVE_MEMBER = "/organization/remove-member"; - public static final String ORGANIZATION_LIST_PROJECT = "/organization/list-project"; - - @Test - @Order(0) - @Sql(scripts = {"/dml/init_organization.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) - public void testListOrganizationSuccess() throws Exception { - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(10); - organizationRequest.setKeyword("default"); - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST, organizationRequest); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - // 返回值不为空 - Assertions.assertNotNull(pageData); - // 返回值的页码和当前页码相同 - Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); - // 返回的数据量不超过规定要返回的数据量相同 - Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); - // 返回值中取出第一条数据, 并判断是否包含关键字default - OrganizationDTO organizationDTO = JSON.parseArray(JSON.toJSONString(pageData.getList()), OrganizationDTO.class).get(0); - Assertions.assertTrue(StringUtils.contains(organizationDTO.getName(), organizationRequest.getKeyword()) - || StringUtils.contains(organizationDTO.getId(), organizationRequest.getKeyword())); - } - - @Test - @Order(1) - public void testListOrganizationEmptySuccess() throws Exception { - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(10); - organizationRequest.setKeyword("default-x"); - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST, organizationRequest); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - // 返回值不为空 - Assertions.assertNotNull(pageData); - // 返回值的页码和当前页码相同 - Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); - // 返回的数据量为0条 - Assertions.assertEquals(0, pageData.getTotal()); - } - - @Test - @Order(1) - public void testListOrganizationError() throws Exception { - // 页码有误 - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(0); - organizationRequest.setPageSize(10); - this.requestPost(ORGANIZATION_LIST, organizationRequest, status().isBadRequest()); - // 页数有误 - organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(1); - this.requestPost(ORGANIZATION_LIST, organizationRequest, status().isBadRequest()); - } - - @Test - @Order(2) - public void testListAllOrganizationSuccess() throws Exception { - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回值不为空 - Assertions.assertNotNull(resultHolder); - // 返回总条数是否为init_organization.sql中的数据总数 - Assertions.assertEquals(6, JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size()); - } - - @Test - @Order(3) - public void testListAllOrganizationError() throws Exception { - this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed()); - } - - @Test - @Order(4) - public void testListOrganizationMemberSuccess() throws Exception { - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(10); - organizationRequest.setKeyword("admin"); - organizationRequest.setOrganizationId("default-organization-2"); - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - // 返回值不为空 - Assertions.assertNotNull(pageData); - // 返回值的页码和当前页码相同 - Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); - // 返回的数据量不超过规定要返回的数据量相同 - Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); - // 返回值中取出第一条数据, 并判断是否包含关键字admin - UserExtend userExtend = JSON.parseArray(JSON.toJSONString(pageData.getList()), UserExtend.class).get(0); - Assertions.assertTrue(StringUtils.contains(userExtend.getName(), organizationRequest.getKeyword()) - || StringUtils.contains(userExtend.getEmail(), organizationRequest.getKeyword()) - || StringUtils.contains(userExtend.getPhone(), organizationRequest.getKeyword())); - } - - @Test - @Order(5) - public void testListOrganizationMemberError() throws Exception { - // 页码有误 - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(0); - organizationRequest.setPageSize(10); - organizationRequest.setKeyword("admin"); - organizationRequest.setOrganizationId("default-organization-2"); - this.requestPost(ORGANIZATION_LIST_MEMBER, organizationRequest, status().isBadRequest()); - // 页数有误 - organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(1); - organizationRequest.setKeyword("admin"); - organizationRequest.setOrganizationId("default-organization-2"); - this.requestPost(ORGANIZATION_LIST_MEMBER, organizationRequest, status().isBadRequest()); - } - - - @Test - @Order(6) - public void testAddOrganizationMemberSuccess() throws Exception { - OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); - organizationMemberRequest.setOrganizationId("default-organization-3"); - organizationMemberRequest.setMemberIds(Arrays.asList("admin", "default-admin")); - this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isOk()); - // 批量添加成员成功后, 验证是否添加成功 - OrganizationRequest organizationRequest = new OrganizationRequest(); - organizationRequest.setCurrent(1); - organizationRequest.setPageSize(10); - organizationRequest.setKeyword("admin"); - organizationRequest.setOrganizationId("default-organization-3"); - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - // 返回值不为空 - Assertions.assertNotNull(pageData); - // 返回值的页码和当前页码相同 - Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); - // 返回的数据量不超过规定要返回的数据量相同 - Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); - // 返回值中取出第一条数据, 并判断是否包含关键字admin - UserExtend userExtend = JSON.parseArray(JSON.toJSONString(pageData.getList()), UserExtend.class).get(0); - Assertions.assertTrue(StringUtils.contains(userExtend.getName(), organizationRequest.getKeyword()) - || StringUtils.contains(userExtend.getEmail(), organizationRequest.getKeyword()) - || StringUtils.contains(userExtend.getPhone(), organizationRequest.getKeyword())); - } - - @Test - @Order(7) - public void testAddOrganizationMemberError() throws Exception { - // 成员选择为空 - OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); - organizationMemberRequest.setOrganizationId("default-organization-3"); - organizationMemberRequest.setMemberIds(Collections.emptyList()); - this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isBadRequest()); - // 组织不存在 - organizationMemberRequest = new OrganizationMemberRequest(); - organizationMemberRequest.setOrganizationId("default-organization-x"); - organizationMemberRequest.setMemberIds(Arrays.asList("admin", "default-admin")); - this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().is5xxServerError()); - } - - @Test - @Order(8) - public void testRemoveOrganizationMemberSuccess() throws Exception { - this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin", status().isOk()); - } - - @Test - @Order(9) - public void testRemoveOrganizationMemberError() throws Exception { - // 组织不存在 - this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin", status().is5xxServerError()); - // 用户不存在 - this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin-x", status().is5xxServerError()); - } - - @Test - @Order(10) - public void testGetOrganizationProjectSuccess() throws Exception { - ProjectRequest projectRequest = new ProjectRequest(); - projectRequest.setCurrent(1); - projectRequest.setPageSize(10); - projectRequest.setOrganizationId("default-organization-2"); - MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_PROJECT, projectRequest); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); - // 返回值不为空 - Assertions.assertNotNull(pageData); - // 返回值的页码和当前页码相同 - Assertions.assertEquals(pageData.getCurrent(), projectRequest.getCurrent()); - // 返回的数据量不超过规定要返回的数据量相同 - Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= projectRequest.getPageSize()); - } - - @Test - @Order(11) - public void testGetOrganizationProjectError() throws Exception { - // 页码有误 - ProjectRequest projectRequest = new ProjectRequest(); - projectRequest.setCurrent(0); - projectRequest.setPageSize(10); - projectRequest.setOrganizationId("default-organization-2"); - this.requestPost(ORGANIZATION_LIST_PROJECT, projectRequest, status().isBadRequest()); - // 页数有误 - projectRequest = new ProjectRequest(); - projectRequest.setCurrent(1); - projectRequest.setPageSize(1); - projectRequest.setOrganizationId("default-organization-2"); - this.requestPost(ORGANIZATION_LIST_PROJECT, projectRequest, status().isBadRequest()); - } - - @Test - @Order(12) - public void testGetDefaultOrganizationSuccess() throws Exception { - MvcResult mvcResult = this.responseGet(OrganizationControllerTests.ORGANIZATION_DEFAULT); - // 获取返回值 - String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); - ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); - // 返回请求正常 - Assertions.assertNotNull(resultHolder); - OrganizationDTO defaultOrg = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), OrganizationDTO.class); - // 返回值不为空 - Assertions.assertNotNull(defaultOrg); - // 返回数据NUM是否为默认100001 - Assertions.assertEquals(defaultOrg.getNum(), 100001L); - } - - @Test - @Order(13) - public void testGetDefaultOrganizationError() throws Exception { - this.requestPost(ORGANIZATION_DEFAULT, null, status().isMethodNotAllowed()); - } - - - private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception { mockMvc.perform(MockMvcRequestBuilders.post(url) .header(SessionConstants.HEADER_TOKEN, sessionId) diff --git a/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java new file mode 100644 index 0000000000..ed41213f43 --- /dev/null +++ b/backend/services/system-setting/src/test/java/io/metersphere/system/controller/SystemOrganizationControllerTests.java @@ -0,0 +1,385 @@ +package io.metersphere.system.controller; + +import base.BaseTest; +import io.metersphere.sdk.constants.SessionConstants; +import io.metersphere.sdk.controller.handler.ResultHolder; +import io.metersphere.sdk.dto.ProjectDTO; +import io.metersphere.sdk.util.JSON; +import io.metersphere.sdk.util.Pager; +import io.metersphere.system.dto.OrganizationDTO; +import io.metersphere.system.dto.UserExtend; +import io.metersphere.system.request.OrganizationMemberRequest; +import io.metersphere.system.request.OrganizationRequest; +import io.metersphere.system.request.ProjectRequest; +import io.metersphere.utils.JsonUtils; +import jakarta.annotation.Resource; +import org.apache.commons.lang3.StringUtils; +import org.junit.jupiter.api.*; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.jdbc.Sql; +import org.springframework.test.context.jdbc.SqlConfig; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.MvcResult; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; + +import java.nio.charset.StandardCharsets; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +@SpringBootTest +@AutoConfigureMockMvc +@TestMethodOrder(MethodOrderer.OrderAnnotation.class) +public class SystemOrganizationControllerTests extends BaseTest{ + + @Resource + private MockMvc mockMvc; + + public static final String ORGANIZATION_LIST = "/system/organization/list"; + public static final String ORGANIZATION_LIST_ALL = "/system/organization/list-all"; + public static final String ORGANIZATION_DEFAULT = "/system/organization/default"; + public static final String ORGANIZATION_LIST_MEMBER = "/system/organization/list-member"; + public static final String ORGANIZATION_ADD_MEMBER = "/system/organization/add-member"; + public static final String ORGANIZATION_REMOVE_MEMBER = "/system/organization/remove-member"; + public static final String ORGANIZATION_LIST_PROJECT = "/system/organization/list-project"; + + @Test + @Order(0) + @Sql(scripts = {"/dml/init_organization.sql"}, config = @SqlConfig(encoding = "utf-8", transactionMode = SqlConfig.TransactionMode.ISOLATED)) + public void testListOrganizationSuccess() throws Exception { + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("default"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST, organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); + // 返回值中取出第一条数据, 并判断是否包含关键字default + OrganizationDTO organizationDTO = JSON.parseArray(JSON.toJSONString(pageData.getList()), OrganizationDTO.class).get(0); + Assertions.assertTrue(StringUtils.contains(organizationDTO.getName(), organizationRequest.getKeyword()) + || StringUtils.contains(organizationDTO.getId(), organizationRequest.getKeyword())); + + // sort不为空 + Map sort = new HashMap<>(); + sort.put("id", "desc"); + organizationRequest.setSort(sort); + MvcResult sortResult = this.responsePost(ORGANIZATION_LIST, organizationRequest); + String sortData = sortResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder sortHolder = JsonUtils.parseObject(sortData, ResultHolder.class); + Pager sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); + // 返回值中取出第一条ID最大的数据, 并判断是否是default-organization-6 + OrganizationDTO organizationDTO1 = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), OrganizationDTO.class).get(0); + Assertions.assertTrue(StringUtils.equals(organizationDTO1.getId(), "default-organization-6")); + } + + @Test + @Order(1) + public void testListOrganizationEmptySuccess() throws Exception { + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("default-x"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST, organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量为0条 + Assertions.assertEquals(0, pageData.getTotal()); + } + + @Test + @Order(1) + public void testListOrganizationError() throws Exception { + // 页码有误 + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(0); + organizationRequest.setPageSize(10); + this.requestPost(ORGANIZATION_LIST, organizationRequest, status().isBadRequest()); + // 页数有误 + organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(1); + this.requestPost(ORGANIZATION_LIST, organizationRequest, status().isBadRequest()); + } + + @Test + @Order(2) + public void testListAllOrganizationSuccess() throws Exception { + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_ALL, null); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回值不为空 + Assertions.assertNotNull(resultHolder); + // 返回总条数是否大于0 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(resultHolder.getData())).size() > 0); + } + + @Test + @Order(3) + public void testListAllOrganizationError() throws Exception { + this.requestGet(ORGANIZATION_LIST_ALL, status().isMethodNotAllowed()); + } + + @Test + @Order(4) + public void testListOrganizationMemberSuccess() throws Exception { + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("admin"); + organizationRequest.setOrganizationId("default-organization-2"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); + // 返回值中取出第一条数据, 并判断是否包含关键字admin + UserExtend userExtend = JSON.parseArray(JSON.toJSONString(pageData.getList()), UserExtend.class).get(0); + Assertions.assertTrue(StringUtils.contains(userExtend.getName(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getEmail(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getPhone(), organizationRequest.getKeyword())); + + // sort不为空 + Map sort = new HashMap<>(); + sort.put("id", "desc"); + organizationRequest.setSort(sort); + MvcResult sortResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); + String sortData = sortResult.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 + UserExtend userExtend1 = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), UserExtend.class).get(0); + Assertions.assertTrue(StringUtils.equals(userExtend1.getId(), "default-admin")); + } + + @Test + @Order(5) + public void testListOrganizationMemberError() throws Exception { + // 页码有误 + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(0); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("admin"); + organizationRequest.setOrganizationId("default-organization-2"); + this.requestPost(ORGANIZATION_LIST_MEMBER, organizationRequest, status().isBadRequest()); + // 页数有误 + organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(1); + organizationRequest.setKeyword("admin"); + organizationRequest.setOrganizationId("default-organization-2"); + this.requestPost(ORGANIZATION_LIST_MEMBER, organizationRequest, status().isBadRequest()); + } + + + @Test + @Order(6) + public void testAddOrganizationMemberSuccess() throws Exception { + OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-3"); + organizationMemberRequest.setMemberIds(Arrays.asList("admin", "default-admin")); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isOk()); + // 批量添加成员成功后, 验证是否添加成功 + OrganizationRequest organizationRequest = new OrganizationRequest(); + organizationRequest.setCurrent(1); + organizationRequest.setPageSize(10); + organizationRequest.setKeyword("admin"); + organizationRequest.setOrganizationId("default-organization-3"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_MEMBER, organizationRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), organizationRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= organizationRequest.getPageSize()); + // 返回值中取出第一条数据, 并判断是否包含关键字admin + UserExtend userExtend = JSON.parseArray(JSON.toJSONString(pageData.getList()), UserExtend.class).get(0); + Assertions.assertTrue(StringUtils.contains(userExtend.getName(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getEmail(), organizationRequest.getKeyword()) + || StringUtils.contains(userExtend.getPhone(), organizationRequest.getKeyword())); + } + + @Test + @Order(7) + public void testAddOrganizationMemberError() throws Exception { + // 成员选择为空 + OrganizationMemberRequest organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-3"); + organizationMemberRequest.setMemberIds(Collections.emptyList()); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().isBadRequest()); + // 组织不存在 + organizationMemberRequest = new OrganizationMemberRequest(); + organizationMemberRequest.setOrganizationId("default-organization-x"); + organizationMemberRequest.setMemberIds(Arrays.asList("admin", "default-admin")); + this.requestPost(ORGANIZATION_ADD_MEMBER, organizationMemberRequest, status().is5xxServerError()); + } + + @Test + @Order(8) + public void testRemoveOrganizationMemberSuccess() throws Exception { + this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin", status().isOk()); + } + + @Test + @Order(9) + public void testRemoveOrganizationMemberError() throws Exception { + // 组织不存在 + this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-x/admin", status().is5xxServerError()); + // 用户不存在 + this.requestGet(ORGANIZATION_REMOVE_MEMBER + "/default-organization-3/admin-x", status().is5xxServerError()); + } + + @Test + @Order(10) + public void testGetOrganizationProjectSuccess() throws Exception { + ProjectRequest projectRequest = new ProjectRequest(); + projectRequest.setCurrent(1); + projectRequest.setPageSize(10); + projectRequest.setOrganizationId("default-organization-2"); + MvcResult mvcResult = this.responsePost(ORGANIZATION_LIST_PROJECT, projectRequest); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + Pager pageData = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), Pager.class); + // 返回值不为空 + Assertions.assertNotNull(pageData); + // 返回值的页码和当前页码相同 + Assertions.assertEquals(pageData.getCurrent(), projectRequest.getCurrent()); + // 返回的数据量不超过规定要返回的数据量相同 + Assertions.assertTrue(JSON.parseArray(JSON.toJSONString(pageData.getList())).size() <= projectRequest.getPageSize()); + + // sort不为空 + Map sort = new HashMap<>(); + sort.put("id", "desc"); + projectRequest.setSort(sort); + MvcResult sortResult = this.responsePost(ORGANIZATION_LIST_PROJECT, projectRequest); + String sortData = sortResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder sortHolder = JsonUtils.parseObject(sortData, ResultHolder.class); + Pager sortPageData = JSON.parseObject(JSON.toJSONString(sortHolder.getData()), Pager.class); + // 返回值中取出第一条ID最大的数据, 并判断是否是default-project + ProjectDTO projectDTO = JSON.parseArray(JSON.toJSONString(sortPageData.getList()), ProjectDTO.class).get(0); + Assertions.assertTrue(StringUtils.equals(projectDTO.getId(), "default-project")); + } + + @Test + @Order(11) + public void testGetOrganizationProjectError() throws Exception { + // 页码有误 + ProjectRequest projectRequest = new ProjectRequest(); + projectRequest.setCurrent(0); + projectRequest.setPageSize(10); + projectRequest.setOrganizationId("default-organization-2"); + this.requestPost(ORGANIZATION_LIST_PROJECT, projectRequest, status().isBadRequest()); + // 页数有误 + projectRequest = new ProjectRequest(); + projectRequest.setCurrent(1); + projectRequest.setPageSize(1); + projectRequest.setOrganizationId("default-organization-2"); + this.requestPost(ORGANIZATION_LIST_PROJECT, projectRequest, status().isBadRequest()); + } + + @Test + @Order(12) + public void testGetDefaultOrganizationSuccess() throws Exception { + MvcResult mvcResult = this.responseGet(SystemOrganizationControllerTests.ORGANIZATION_DEFAULT); + // 获取返回值 + String returnData = mvcResult.getResponse().getContentAsString(StandardCharsets.UTF_8); + ResultHolder resultHolder = JsonUtils.parseObject(returnData, ResultHolder.class); + // 返回请求正常 + Assertions.assertNotNull(resultHolder); + OrganizationDTO defaultOrg = JSON.parseObject(JSON.toJSONString(resultHolder.getData()), OrganizationDTO.class); + // 返回值不为空 + Assertions.assertNotNull(defaultOrg); + // 返回数据NUM是否为默认100001 + Assertions.assertEquals(defaultOrg.getNum(), 100001L); + } + + @Test + @Order(13) + public void testGetDefaultOrganizationError() throws Exception { + this.requestPost(ORGANIZATION_DEFAULT, null, status().isMethodNotAllowed()); + } + + + + private void requestPost(String url, Object param, ResultMatcher resultMatcher) throws Exception { + mockMvc.perform(MockMvcRequestBuilders.post(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(param)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(resultMatcher).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + private MvcResult responsePost(String url, Object param) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.post(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .content(JSON.toJSONString(param)) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)) + .andReturn(); + } + + private void requestGet(String url, ResultMatcher resultMatcher) throws Exception { + mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(resultMatcher).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)); + } + + private MvcResult responseGet(String url) throws Exception { + return mockMvc.perform(MockMvcRequestBuilders.get(url) + .header(SessionConstants.HEADER_TOKEN, sessionId) + .header(SessionConstants.CSRF_TOKEN, csrfToken) + .contentType(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()).andDo(print()) + .andExpect(content().contentType(MediaType.APPLICATION_JSON)).andReturn(); + } +}