From 3b628e9d9103d960760bb0eee25601f2a8b01e35 Mon Sep 17 00:00:00 2001 From: gaoqr <13665037151@163.com> Date: Fri, 10 Oct 2025 12:00:20 +0800 Subject: [PATCH] =?UTF-8?q?=E9=83=A8=E9=97=A8=E7=AE=A1=E7=90=86serviceImpl?= =?UTF-8?q?=E3=80=81controller=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E8=A1=A5=E5=85=85?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/dept/DeptControllerTest.java | 190 ++++++++++++++++ .../service/dept/DeptServiceImplTest.java | 206 ++++++++++++++---- 2 files changed, 359 insertions(+), 37 deletions(-) create mode 100644 cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/controller/admin/dept/DeptControllerTest.java diff --git a/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/controller/admin/dept/DeptControllerTest.java b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/controller/admin/dept/DeptControllerTest.java new file mode 100644 index 000000000..b48f383d4 --- /dev/null +++ b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/controller/admin/dept/DeptControllerTest.java @@ -0,0 +1,190 @@ +package com.cf.imes.module.system.controller.admin.dept; + +import com.cf.imes.framework.security.test.WithMockLoginUser; +import com.cf.imes.module.system.controller.admin.dept.vo.dept.DeptListReqVO; +import com.cf.imes.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; +import com.cf.imes.module.system.dal.dataobject.dept.DeptDO; +import com.cf.imes.module.system.service.dept.DeptService; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.bean.override.mockito.MockitoBean; +import org.springframework.test.web.servlet.MockMvc; + + +import java.util.List; + +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +/** + * @author Gqr + * @since 2025/10/10 10:32 + */ +@WebMvcTest(controllers = DeptController.class, excludeAutoConfiguration = SecurityAutoConfiguration.class) +public class DeptControllerTest { + @Autowired + private MockMvc mockMvc; + + @MockitoBean + private DeptService deptService; + + @Autowired + private ObjectMapper objectMapper; + + private DeptSaveReqVO deptSaveReqVO; + + @BeforeEach + void setup() { + deptSaveReqVO = new DeptSaveReqVO(); + deptSaveReqVO.setName("测试部门"); // @NotBlank + @Size + deptSaveReqVO.setParentId(1L); // @NotNull + deptSaveReqVO.setSort(1); // @NotNull + @NumberValid + deptSaveReqVO.setPhone("15601691000"); // @NotEmpty + @Mobile + deptSaveReqVO.setEmail("test@cf.com"); // @NotEmpty + @Email + deptSaveReqVO.setStatus(1); // @NotNull + @CommonStatus + deptSaveReqVO.setOrganId(100L); // 可选字段 + deptSaveReqVO.setLeader("负责人"); // 可选字段 + } + + @Test + @WithMockLoginUser(isSuperAdmin = true) + void testCreateDept() throws Exception { + when(deptService.createDept(any())).thenReturn(123L); + + mockMvc.perform(post("/system/dept/create") + .contentType(MediaType.APPLICATION_JSON) + .header("Authorization", "123") + .content(objectMapper.writeValueAsString(deptSaveReqVO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(123)); + } + + @Test + void testUpdateDept() throws Exception { + Mockito.doNothing().when(deptService).updateDept(any()); + + mockMvc.perform(put("/system/dept/update") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(deptSaveReqVO))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(true)); + } + + @Test + void testDeleteDept() throws Exception { + Mockito.doNothing().when(deptService).deleteDept(1024L); + + mockMvc.perform(delete("/system/dept/delete") + .param("id", "1024")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data").value(true)); + } + + @Test + void testGetDeptList() throws Exception { + DeptDO dept = new DeptDO().setId(1L).setName("测试部门").setStatus(1); + when(deptService.getDeptList(any(DeptListReqVO.class))) + .thenReturn(List.of(dept)); + + mockMvc.perform(get("/system/dept/list") + .param("organId", "100") + .param("status", "1")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data[0].name").value("测试部门")); + } + + @Test + void testGetAllSimpleDeptList_withOrganId() throws Exception { + DeptDO dept = new DeptDO().setId(2L).setName("精简部门").setStatus(1); + + // Mock getDeptList(DeptListReqVO) + when(deptService.getDeptList(any(DeptListReqVO.class))) + .thenReturn(List.of(dept)); + + // Mock removeUnowndDept + when(deptService.removeUnowndDept(any())) + .thenReturn(List.of(dept)); + + // 构造请求 VO,并设置 organId 不为 null + DeptListReqVO reqVO = new DeptListReqVO(); + reqVO.setOrganId(100L); + + mockMvc.perform(get("/system/dept/list-all-simple") + .param("organId", reqVO.getOrganId().toString())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data[0].name").value("精简部门")); + } + + @Test + void testGetAllSimpleDeptList_withoutOrganId() throws Exception { + DeptDO dept = new DeptDO().setId(2L).setName("精简部门").setStatus(1); + + when(deptService.getDeptList(any(DeptListReqVO.class))) + .thenReturn(List.of(dept)); + when(deptService.removeUnowndDept(any())) + .thenReturn(List.of(dept)); + + // 不传 organId,触发 else 分支 + mockMvc.perform(get("/system/dept/list-all-simple")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data[0].name").value("精简部门")); + } + + @Test + void testGetSimpleDeptList_withNullOrganId() throws Exception { + // 不传 organId,触发 if 分支 + mockMvc.perform(get("/system/dept/simple-list")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data").isArray()) + .andExpect(jsonPath("$.data").isEmpty()); + } + + @Test + void testGetSimpleDeptList_withOrganId() throws Exception { + DeptDO dept = new DeptDO().setId(3L).setName("简单部门").setStatus(1); + + // Mock getDeptList(DeptListReqVO) + when(deptService.getDeptList(any(DeptListReqVO.class))) + .thenReturn(List.of(dept)); + when(deptService.removeUnowndDept(any())) + .thenReturn(List.of(dept)); + + // 传入 organId,触发 else 分支 + mockMvc.perform(get("/system/dept/simple-list") + .param("organId", "100")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.data[0].name").value("简单部门")); + } + + @Test + void testGetDept() throws Exception { + // 准备 DeptDO 返回对象 + DeptDO dept = new DeptDO() + .setId(1024L) + .setName("测试部门") + .setStatus(1); + + // Mock deptService.getDept 方法 + when(deptService.getDept(1024L)).thenReturn(dept); + + // 发起 GET 请求 + mockMvc.perform(get("/system/dept/get") + .param("id", "1024")) // 注意 GET 查询参数 + .andExpect(status().isOk()) // 返回 200 + .andExpect(jsonPath("$.data.id").value(1024)) + .andExpect(jsonPath("$.data.name").value("测试部门")); + } + +} diff --git a/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/dept/DeptServiceImplTest.java b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/dept/DeptServiceImplTest.java index f832fab7f..1a0a7ca5e 100644 --- a/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/dept/DeptServiceImplTest.java +++ b/cf-module-system/cf-module-system-biz/src/test/java/com/cf/imes/module/system/service/dept/DeptServiceImplTest.java @@ -1,6 +1,7 @@ package com.cf.imes.module.system.service.dept; import com.cf.imes.framework.common.enums.CommonStatusEnum; +import com.cf.imes.framework.common.exception.ServiceException; import com.cf.imes.framework.common.util.object.ObjectUtils; import com.cf.imes.framework.organ.core.context.OrganContextHolder; import com.cf.imes.framework.security.core.LoginUser; @@ -12,6 +13,7 @@ import com.cf.imes.module.system.controller.admin.dept.vo.dept.DeptSaveReqVO; import com.cf.imes.module.system.dal.dataobject.dept.DeptDO; import com.cf.imes.module.system.dal.dataobject.user.AdminUserDO; import com.cf.imes.module.system.dal.mysql.dept.DeptMapper; +import com.cf.imes.module.system.enums.ErrorCodeConstants; import com.cf.imes.module.system.service.user.AdminUserService; import com.cf.imes.module.system.util.redis.SystemRedisUtils; import org.junit.jupiter.api.Test; @@ -53,7 +55,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { private SystemRedisUtils systemRedisUtils; @Test - public void testCreateDept() { + void testCreateDept() { // 准备参数 DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> { o.setId(null); // 防止 id 被设置 @@ -72,7 +74,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testUpdateDept_enable() { + void testUpdateDept_enable() { // mock 数据 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 @@ -93,7 +95,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testUpdateDept_disable() { + void testUpdateDept_disable() { // mock 数据 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus())); deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 @@ -114,7 +116,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testUpdateDept_parentDeptUserOperNotAllow() { + void testUpdateDept_parentDeptUserOperNotAllow() { LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); // mock 数据 DeptDO parentDeptDO = randomPojo(DeptDO.class, o -> o.setId(randomLongId())); @@ -136,7 +138,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testDeleteDept_success() { + void testDeleteDept_success() { // mock 数据 DeptDO dbDeptDO = randomPojo(DeptDO.class); deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 @@ -151,7 +153,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testDeleteDept_deptUserOperNotAllow() { + void testDeleteDept_deptUserOperNotAllow() { LoginUser loginUser = SecurityFrameworkUtils.getLoginUser(); // mock 数据 DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setId(loginUser.getDeptId())); @@ -165,7 +167,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testDeleteDept_deptUserExists(){ + void testDeleteDept_deptUserExists(){ // mock 数据 DeptDO dbDeptDO = randomPojo(DeptDO.class); deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据 @@ -180,7 +182,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testDeleteDept_exitsChildren() { + void testDeleteDept_exitsChildren() { // mock 数据 DeptDO parentDept = randomPojo(DeptDO.class); deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据 @@ -197,13 +199,13 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateDeptExists_idIsNull() { + void testValidateDeptExists_idIsNull() { // 调用,不抛异常 deptService.validateDeptExists(null); } @Test - public void testValidateDeptExists_exists() { + void testValidateDeptExists_exists() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -213,7 +215,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateDeptExists_notFound() { + void testValidateDeptExists_notFound() { // 准备参数 Long id = randomLongId(); @@ -222,17 +224,19 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateParentDept_parentError() { + void testValidateParentDept_parentError() { // 准备参数 Long id = randomLongId(); // 调用, 并断言异常 assertServiceException(() -> deptService.validateParentDept(id, id), DEPT_PARENT_ERROR); + + assertDoesNotThrow(() -> deptService.validateParentDept(id, null)); } @Test - public void testValidateParentDept_parentNotExists() { + void testValidateParentDept_parentNotExists() { Long id = randomLongId(); Long parentId = randomLongId(); // 假设不存在的 ID assertServiceException(() -> deptService.validateParentDept(id, parentId), @@ -240,7 +244,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateParentDept_parentDisabled() { + void testValidateParentDept_parentDisabled() { DeptDO parentDept = randomPojo(DeptDO.class); parentDept.setStatus(CommonStatusEnum.DISABLE.getStatus()); deptMapper.insert(parentDept); @@ -253,7 +257,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateParentDept_multiLevelParentDisabled() { + void testValidateParentDept_multiLevelParentDisabled() { // 构造三级部门:grandParent -> parent -> child DeptDO grandParent = randomPojo(DeptDO.class); grandParent.setStatus(CommonStatusEnum.DISABLE.getStatus()); @@ -271,7 +275,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateParentDept_validHierarchy() { + void testValidateParentDept_validHierarchy() { DeptDO grandParent = randomPojo(DeptDO.class); grandParent.setStatus(CommonStatusEnum.ENABLE.getStatus()); deptMapper.insert(grandParent); @@ -288,7 +292,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateParentDept_parentIsChild() { + void testValidateParentDept_parentIsChild() { // mock 数据(父节点) DeptDO parentDept = randomPojo(DeptDO.class); parentDept.setStatus(CommonStatusEnum.ENABLE.getStatus()); @@ -309,7 +313,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateNameUnique_duplicate() { + void testValidateNameUnique_duplicate() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -326,7 +330,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateDeptNameUnique_newDeptDuplicate() { + void testValidateDeptNameUnique_newDeptDuplicate() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -343,7 +347,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateDeptNameUnique_updateOtherDeptDuplicate() { + void testValidateDeptNameUnique_updateOtherDeptDuplicate() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -360,7 +364,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidateDeptNameUnique_updateSameDept() { + void testValidateDeptNameUnique_updateSameDept() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -377,7 +381,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test - public void testGetDept() { + void testGetDept() { // mock 数据 DeptDO deptDO = randomPojo(DeptDO.class); deptMapper.insert(deptDO); @@ -391,7 +395,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testGetDeptList_ids() { + void testGetDeptList_ids() { // mock 数据 DeptDO deptDO01 = randomPojo(DeptDO.class); deptMapper.insert(deptDO01); @@ -409,13 +413,13 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testGetDeptList_emptyids() { + void testGetDeptList_emptyids() { assertEquals(0, deptService.getDeptList(Collections.emptyList()).size()); } @Test @WithMockLoginUser(isSuperAdmin = true) - public void testGetDeptList_superadmin_reqVO() { + void testGetDeptList_superadmin_reqVO() { Long organId = SecurityFrameworkUtils.getUserOrganId(); // mock 数据 DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到 @@ -443,7 +447,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { @Test @WithMockLoginUser - public void testGetDeptList_commonuser_reqVO() { + void testGetDeptList_commonuser_reqVO() { Long organId = SecurityFrameworkUtils.getUserOrganId(); // mock 数据 DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到 @@ -469,7 +473,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testGetChildDeptList() { + void testGetChildDeptList() { // mock 数据(1 级别子节点) DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); deptMapper.insert(dept1); @@ -492,7 +496,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testGetChildDeptListFromCache() { + void testGetChildDeptListFromCache() { // mock 数据(1 级别子节点) DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1")); deptMapper.insert(dept1); @@ -515,13 +519,13 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidDept_NotFound() { + void testValidDept_NotFound() { // 部门不存在 assertServiceException(() -> deptService.validDept(1L), DEPT_NOT_FOUND); } @Test - public void testValidDept_Disabled() { + void testValidDept_Disabled() { // 部门状态不可用 DeptDO disableDbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); deptMapper.insert(disableDbDeptDO); @@ -529,7 +533,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidDept_ParentNotFound() { + void testValidDept_ParentNotFound() { DeptDO enabledbDeptDO = randomPojo(DeptDO.class, o -> { o.setStatus(CommonStatusEnum.ENABLE.getStatus()); }); @@ -538,7 +542,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidDept_ParentDisabled() { + void testValidDept_ParentDisabled() { DeptDO disabledParentDetpDO = randomPojo(DeptDO.class, o -> { o.setId(null); o.setStatus(CommonStatusEnum.DISABLE.getStatus()); @@ -554,7 +558,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void testValidDept_success() { + void testValidDept_success() { DeptDO disabledParentDetpDO = randomPojo(DeptDO.class, o -> { o.setId(null); o.setStatus(CommonStatusEnum.ENABLE.getStatus()); @@ -573,13 +577,13 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void removeUnowndDept_empty() { + void removeUnowndDept_empty() { List deptDOS = deptService.removeUnowndDept(Collections.emptyList()); assertEquals(deptDOS.size(), 0); } @Test - public void removeUnowndDept_success() { + void removeUnowndDept_success() { DeptDO oneParent = randomPojo(DeptDO.class, o -> o.setParentId(DeptDO.PARENT_ID_ROOT)); DeptDO onechild1 = randomPojo(DeptDO.class, o -> o.setParentId(oneParent.getId())); DeptDO onechild2 = randomPojo(DeptDO.class, o -> o.setParentId(oneParent.getId())); @@ -589,7 +593,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void removeUnowndDept_ParentNotFound() { + void removeUnowndDept_ParentNotFound() { DeptDO oneParent = randomPojo(DeptDO.class, o -> o.setParentId(DeptDO.PARENT_ID_ROOT)); DeptDO one = randomPojo(DeptDO.class, o -> o.setParentId(oneParent.getId())); DeptDO two = randomPojo(DeptDO.class); @@ -599,7 +603,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { } @Test - public void getAllInvalidDeptIds_success() { + void getAllInvalidDeptIds_success() { Long organId = randomLongId(); DeptDO oneParent = randomPojo(DeptDO.class, o -> { o.setId(randomLongId()); @@ -636,4 +640,132 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest { assertEquals(disableCount, noOrganAllInvalidDeptIds.size()); } + @Test + void testDeptNotExist_shouldReturn() { + // 不插入任何部门,确保 deptId 不存在 + Long nonExistDeptId = 9999L; + + assertDoesNotThrow(() -> deptService.validUserLoginDept(nonExistDeptId)); + } + + @Test + void testDeptDisabled_shouldThrow() { + // 插入禁用部门 + DeptDO dept = new DeptDO(); + dept.setName("测试部-禁用"); + dept.setStatus(CommonStatusEnum.DISABLE.getStatus()); + deptMapper.insert(dept); + + ServiceException ex = assertThrows(ServiceException.class, + () -> deptService.validUserLoginDept(dept.getId())); + + assertEquals(ErrorCodeConstants.DEPT_NOT_ALLOWED_LOGIN.getCode(), ex.getCode()); + assertTrue(ex.getMessage().contains("测试部-禁用")); + } + + @Test + void testDeptEnabled_shouldPass() { + // 插入启用部门 + DeptDO dept = new DeptDO(); + dept.setName("测试部-启用"); + dept.setStatus(CommonStatusEnum.ENABLE.getStatus()); + deptMapper.insert(dept); + + assertDoesNotThrow(() -> deptService.validUserLoginDept(dept.getId())); + } + + @Test + void testParentDeptDisabled_shouldThrow() { + // 上级部门禁用 + DeptDO parent = new DeptDO(); + parent.setName("上级部门-禁用"); + parent.setStatus(CommonStatusEnum.DISABLE.getStatus()); + deptMapper.insert(parent); + + // 子部门启用 + DeptDO child = new DeptDO(); + child.setName("子部门"); + child.setStatus(CommonStatusEnum.ENABLE.getStatus()); + child.setParentId(parent.getId()); + deptMapper.insert(child); + + ServiceException ex = assertThrows(ServiceException.class, + () -> deptService.validUserLoginDept(child.getId())); + + assertEquals(ErrorCodeConstants.DEPT_DISABLE.getCode(), ex.getCode()); + assertTrue(ex.getMessage().contains("上级部门-禁用")); + } + + @Test + void testParentDeptEnabled_shouldPass() { + // 上级部门启用 + DeptDO parent = new DeptDO(); + parent.setName("上级部门-启用"); + parent.setStatus(CommonStatusEnum.ENABLE.getStatus()); + deptMapper.insert(parent); + + // 子部门启用 + DeptDO child = new DeptDO(); + child.setName("子部门"); + child.setStatus(CommonStatusEnum.ENABLE.getStatus()); + child.setParentId(parent.getId()); + deptMapper.insert(child); + + assertDoesNotThrow(() -> deptService.validUserLoginDept(child.getId())); + } + + @Test + void testMultiLevelParentDeptDisabled_shouldThrow() { + // 顶级部门禁用 + DeptDO top = new DeptDO(); + top.setName("顶级部门-禁用"); + top.setStatus(CommonStatusEnum.DISABLE.getStatus()); + deptMapper.insert(top); + + // 中间部门启用,指向顶级 + DeptDO middle = new DeptDO(); + middle.setName("中间部门-启用"); + middle.setStatus(CommonStatusEnum.ENABLE.getStatus()); + middle.setParentId(top.getId()); + deptMapper.insert(middle); + + // 子部门启用,指向中间部门 + DeptDO child = new DeptDO(); + child.setName("子部门-启用"); + child.setStatus(CommonStatusEnum.ENABLE.getStatus()); + child.setParentId(middle.getId()); + deptMapper.insert(child); + + ServiceException ex = assertThrows(ServiceException.class, + () -> deptService.validUserLoginDept(child.getId())); + + assertEquals(ErrorCodeConstants.DEPT_DISABLE.getCode(), ex.getCode()); + assertTrue(ex.getMessage().contains("顶级部门-禁用")); + } + + @Test + void testMultiLevelAllEnabled_shouldPass() { + // 顶级部门启用 + DeptDO top = new DeptDO(); + top.setName("顶级部门-启用"); + top.setStatus(CommonStatusEnum.ENABLE.getStatus()); + deptMapper.insert(top); + + // 中间部门启用 + DeptDO middle = new DeptDO(); + middle.setName("中间部门-启用"); + middle.setStatus(CommonStatusEnum.ENABLE.getStatus()); + middle.setParentId(top.getId()); + deptMapper.insert(middle); + + // 子部门启用 + DeptDO child = new DeptDO(); + child.setName("子部门-启用"); + child.setStatus(CommonStatusEnum.ENABLE.getStatus()); + child.setParentId(middle.getId()); + deptMapper.insert(child); + + // 方法正常执行,不抛异常 + assertDoesNotThrow(() -> deptService.validUserLoginDept(child.getId())); + } }