refactor(系统设置): 系统组织功能拆分

This commit is contained in:
song-cc-rock 2023-07-18 14:08:38 +08:00 committed by fit2-zhao
parent 2866aa3268
commit 697375168a
10 changed files with 602 additions and 414 deletions

View File

@ -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<String, Object> filter;
private Map<String, List<String>> filter;
@Schema(title = "高级搜索")
private Map<String, Object> combine;
public String getSortString() {
if (sort == null || sort.isEmpty()) {

View File

@ -0,0 +1,4 @@
package io.metersphere.sdk.mapper;
public interface BaseMapper {
}

View File

@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="io.metersphere.sdk.mapper.BaseMapper">
<sql id="condition">
<choose>
<when test='${object}.operator == "like"'>
like CONCAT('%', #{${object}.value},'%')
</when>
<when test='${object}.operator == "not like"'>
not like CONCAT('%', #{${object}.value},'%')
</when>
<when test='${object}.operator == "in"'>
in
<foreach collection="${object}.value" item="v" separator="," open="(" close=")">
#{v}
</foreach>
</when>
<when test='${object}.operator == "not in"'>
not in
<foreach collection="${object}.value" item="v" separator="," open="(" close=")">
#{v}
</foreach>
</when>
<when test='${object}.operator == "between"'>
between #{${object}.value[0]} and #{${object}.value[1]}
</when>
<when test='${object}.operator == "gt"'>
&gt; #{${object}.value}
</when>
<when test='${object}.operator == "lt"'>
&lt; #{${object}.value}
</when>
<when test='${object}.operator == "ge"'>
&gt;= #{${object}.value}
</when>
<when test='${object}.operator == "le"'>
&lt;= #{${object}.value}
</when>
<otherwise>
= #{${object}.value}
</otherwise>
</choose>
</sql>
<sql id="filterInWrapper">
<foreach collection="values" item="value" separator="," open="(" close=")">
#{value}
</foreach>
</sql>
</mapper>

View File

@ -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<OrganizationDTO>> list(@Validated @RequestBody OrganizationRequest organizationRequest) {
Page<Object> 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<OrganizationDTO> listAll() {
return organizationService.listAll();
}
@PostMapping("/list-member")
@Operation(summary = "获取组织成员")
@RequiresPermissions(value = {PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ})
public Pager<List<UserExtend>> listMember(@Validated @RequestBody OrganizationRequest organizationRequest) {
Page<Object> 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<List<ProjectDTO>> listProject(@Validated @RequestBody ProjectRequest projectRequest) {
Page<Object> page = PageHelper.startPage(projectRequest.getCurrent(), projectRequest.getPageSize());
return PageUtils.setPageInfo(page, systemProjectService.getProjectList(projectRequest));
}
}

View File

@ -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<OrganizationDTO>> list(@Validated @RequestBody OrganizationRequest request) {
Page<Object> 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<OrganizationDTO> listAll() {
return organizationService.listAll();
}
@PostMapping("/list-member")
@Operation(summary = "获取组织成员")
@RequiresPermissions(value = {PermissionConstants.SYSTEM_ORGANIZATION_PROJECT_READ, PermissionConstants.SYSTEM_USER_READ})
public Pager<List<UserExtend>> listMember(@Validated @RequestBody OrganizationRequest request) {
Page<Object> 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<List<ProjectDTO>> listProject(@Validated @RequestBody ProjectRequest request) {
Page<Object> page = PageHelper.startPage(request.getCurrent(), request.getPageSize(),
StringUtils.isNotBlank(request.getSortString()) ? request.getSortString() : "create_time desc");
return PageUtils.setPageInfo(page, systemProjectService.getProjectList(request));
}
}

View File

@ -17,10 +17,10 @@ public interface ExtOrganizationMapper {
/**
* 查询组织列表
* @param organizationRequest 列表请求参数
* @param request 列表请求参数
* @return 组织列表数据
*/
List<OrganizationDTO> list(@Param("request") OrganizationRequest organizationRequest);
List<OrganizationDTO> list(@Param("request") OrganizationRequest request);
/**
* 获取系统下所有组织
@ -49,10 +49,10 @@ public interface ExtOrganizationMapper {
/**
* 获取组织成员列表(角色)
* @param organizationRequest 组织成员列表请求参数
* @param request 组织成员列表请求参数
* @return 组织成员列表数据
*/
List<UserExtend> listMember(@Param("request") OrganizationRequest organizationRequest);
List<UserExtend> listMember(@Param("request") OrganizationRequest request);
/**
* 获取组织管理员

View File

@ -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
<include refid="queryWhereCondition"/>
<!-- <if test="request.sort != null">-->
<!-- <include refid="sort">-->
<!-- <property name="orderMap" value="request.sort"/>-->
<!-- </include>-->
<!-- </if>-->
group by o.id
</select>
@ -20,37 +15,6 @@
select * from organization
</select>
<sql id="queryWhereCondition">
<where>
<if test="request.keyword != null">
and (
o.name like concat('%', #{request.keyword},'%')
or o.id like concat('%', #{request.keyword},'%')
)
</if>
<!-- <if test="request.filter != null">-->
<!-- <include refid="filter">-->
<!-- <property name="filterMap" value="request.filter"/>-->
<!-- </include>-->
<!-- </if>-->
</where>
</sql>
<!-- <sql id="filter">-->
<!-- <if test="filterMap.keySet().size() > 0">-->
<!-- </if>-->
<!-- </sql>-->
<!-- <sql id="sort">-->
<!-- <if test="orderMap.keySet().size() > 0">-->
<!-- order by-->
<!-- <foreach collection="orderMap.keySet()" separator="," item="orderKey">-->
<!-- orderKey ${orderMap[orderKey]}-->
<!-- </foreach>-->
<!-- </if>-->
<!-- </sql>-->
<update id="delete" parameterType="io.metersphere.system.request.OrganizationDeleteRequest">
update organization set deleted = true , delete_time = #{request.deleteTime} , delete_user = #{request.deleteUserId} where id = #{request.organizationId}
</update>
@ -94,4 +58,45 @@
<select id="selectOrganizationOptions" resultType="io.metersphere.system.dto.OrganizationProjectOptionsDto">
select id, name from organization order by create_time desc
</select>
<sql id="queryWhereCondition">
<where>
<if test="request.keyword != null">
and (
o.name like concat('%', #{request.keyword},'%')
or o.id like concat('%', #{request.keyword},'%')
)
</if>
<include refid="filter"/>
<include refid="combine">
<property name="condition" value="request.combine"/>
</include>
</where>
</sql>
<sql id="filter">
<if test="request.filter != null and request.filter.size() > 0">
<foreach collection="request.filter.entrySet()" index="key" item="values">
<if test="values != null and values.size() > 0">
<choose>
<when test="key=='createUser'">
and o.create_user in
<include refid="io.metersphere.sdk.mapper.BaseMapper.filterInWrapper"/>
</when>
</choose>
</if>
</foreach>
</if>
</sql>
<sql id="combine">
<if test="request.combine != null">
<if test='${condition}.createUser != null'>
and o.create_user
<include refid="io.metersphere.sdk.mapper.BaseMapper.condition">
<property name="object" value="${condition}.createUser"/>
</include>
</if>
</if>
</sql>
</mapper>

View File

@ -40,8 +40,8 @@ public class OrganizationService{
@Resource
UserMapper userMapper;
public List<OrganizationDTO> list(OrganizationRequest organizationRequest) {
List<OrganizationDTO> organizationDTOS = extOrganizationMapper.list(organizationRequest);
public List<OrganizationDTO> list(OrganizationRequest request) {
List<OrganizationDTO> organizationDTOS = extOrganizationMapper.list(request);
return buildOrgAdminInfo(organizationDTOS);
}
@ -59,20 +59,20 @@ public class OrganizationService{
return organizationDTO;
}
public List<UserExtend> listMember(OrganizationRequest organizationRequest) {
return extOrganizationMapper.listMember(organizationRequest);
public List<UserExtend> 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);

View File

@ -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)

View File

@ -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<String, String> 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<String, String> 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<String, String> 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();
}
}