1、WithMockLoginUserExtension补充isSuperAdmin的缺省false;2、dept service移除真实db,走纯mock;

This commit is contained in:
gaoqr
2026-01-21 10:28:43 +08:00
parent 6c48ec283c
commit f22f3bb2ba
3 changed files with 136 additions and 135 deletions
@@ -215,7 +215,7 @@ public class DeptServiceImpl implements DeptService {
if (CollUtil.isEmpty(ids)) {
return Collections.emptyList();
}
return deptMapper.selectBatchIds(ids);
return deptMapper.selectByIds(ids);
}
@Override
@@ -32,6 +32,7 @@ public class WithMockLoginUserExtension implements BeforeEachCallback, AfterEach
loginUser.setOrganId(ann.organId());
loginUser.setDeptId(ann.deptId());
loginUser.setNickname(ann.username());
loginUser.setIsSupAdmin(ann.isSuperAdmin());
} else {
return;
}
@@ -2,12 +2,12 @@ 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;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.framework.security.test.WithMockLoginUser;
import com.cf.imes.framework.test.core.ut.BaseDbAndRedisUnitTest;
import com.cf.imes.framework.test.core.ut.BaseMockitoUnitTest;
import com.cf.imes.module.system.common.WithMockLoginUserExtension;
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;
@@ -17,10 +17,9 @@ 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;
import org.springframework.context.annotation.Import;
import jakarta.annotation.Resource;
import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import java.util.Arrays;
import java.util.Collections;
@@ -32,7 +31,15 @@ import static com.cf.imes.framework.test.core.util.AssertUtils.assertServiceExce
import static com.cf.imes.framework.test.core.util.RandomUtils.*;
import static com.cf.imes.module.system.enums.ErrorCodeConstants.*;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.argThat;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
@@ -40,18 +47,19 @@ import static org.mockito.Mockito.when;
*
* @author niudehua
*/
@Import(DeptServiceImpl.class)
public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
@ExtendWith(WithMockLoginUserExtension.class)
class DeptServiceImplTest extends BaseMockitoUnitTest {
@Resource
@InjectMocks
private DeptServiceImpl deptService;
@Resource
@Mock
private DeptMapper deptMapper;
@MockitoBean
@Mock
private AdminUserService userService;
@MockitoBean
@Mock
private SystemRedisUtils systemRedisUtils;
@Test
@@ -63,55 +71,63 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
o.setStatus(randomCommonStatus());
});
// 模拟 Mapper 插入时生成 ID
doAnswer(invocation -> {
DeptDO deptDO = invocation.getArgument(0);
deptDO.setId(100L); // 模拟自增主键
return null;
}).when(deptMapper).insert(any(DeptDO.class));
// 调用
Long deptId = deptService.createDept(reqVO);
// 断言
assertNotNull(deptId);
// 校验记录的属性是否正确
DeptDO deptDO = deptMapper.selectById(deptId);
assertPojoEquals(reqVO, deptDO, "id");
assertEquals(100L, deptId);
verify(deptMapper).insert(argThat((DeptDO dept) ->
dept.getParentId().equals(reqVO.getParentId())
&& dept.getStatus().equals(reqVO.getStatus())
&& dept.getOrganId().equals(reqVO.getOrganId())
&& dept.getName().equals(reqVO.getName())
&& dept.getPhone().equals(reqVO.getPhone())
));
}
@Test
@WithMockLoginUser
void testUpdateDept_enable() {
// mock 数据
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
Long deptId = 100L;
// exist
when(deptMapper.selectById(deptId)).thenReturn(new DeptDO());
// 准备参数
DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> {
// 设置更新的 ID
o.setId(deptId);
o.setParentId(DeptDO.PARENT_ID_ROOT);
o.setId(dbDeptDO.getId());
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
// 调用
deptService.updateDept(reqVO);
// 校验是否更新正确
DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, deptDO);
// 校验是否更新
verify(deptMapper, times(1)).updateById(any(DeptDO.class));
}
@Test
@WithMockLoginUser
void testUpdateDept_disable() {
// mock 数据
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
Long deptId = 100L;
// exist
when(deptMapper.selectById(deptId)).thenReturn(new DeptDO());
// 准备参数
DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> {
// 设置更新的 ID
o.setId(deptId);
o.setParentId(DeptDO.PARENT_ID_ROOT);
o.setId(dbDeptDO.getId());
o.setStatus(CommonStatusEnum.DISABLE.getStatus());
});
// 调用
deptService.updateDept(reqVO);
// 校验是否更新正确
DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的
assertPojoEquals(reqVO, deptDO);
// 校验是否更新
verify(deptMapper, times(1)).updateById(any(DeptDO.class));
verify(systemRedisUtils, times(1)).scanAndCompareDeptAndDelToken(deptId);
}
@Test
@@ -124,8 +140,12 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
o.setId(loginUser.getDeptId());
o.setParentId(parentDeptDO.getId());
});
deptMapper.insert(parentDeptDO);
deptMapper.insert(dbDeptDO);
// exist
when(deptMapper.selectById(parentDeptDO.getId())).thenReturn(parentDeptDO);
// 轮训子部门
when(deptMapper.selectListByParentId(anyCollection())).thenReturn(List.of(dbDeptDO)).thenReturn(Collections.emptyList());
// 准备参数
DeptSaveReqVO reqVO = randomPojo(DeptSaveReqVO.class, o -> {
o.setParentId(DeptDO.PARENT_ID_ROOT);
@@ -139,16 +159,16 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
@Test
@WithMockLoginUser
void testDeleteDept_success() {
// mock 数据
DeptDO dbDeptDO = randomPojo(DeptDO.class);
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbDeptDO.getId();
Long deptId = 100L;
// exist
when(deptMapper.selectById(deptId)).thenReturn(new DeptDO());
// 调用
deptService.deleteDept(id);
deptService.deleteDept(deptId);
// 校验数据不存在了
assertNull(deptMapper.selectById(id));
verify(deptMapper, times(1)).deleteById(deptId);
verify(userService, times(1)).deleteDeptUsers(deptId);
verify(systemRedisUtils, times(1)).scanAndCompareDeptAndDelToken(deptId);
}
@Test
@@ -157,9 +177,8 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
LoginUser loginUser = SecurityFrameworkUtils.getLoginUser();
// mock 数据
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setId(loginUser.getDeptId()));
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbDeptDO.getId();
when(deptMapper.selectById(id)).thenReturn(dbDeptDO);
assertServiceException(() -> deptService.deleteDept(id),
DEPT_USER_OPER_NOT_ALLOW);
@@ -170,9 +189,8 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testDeleteDept_deptUserExists(){
// mock 数据
DeptDO dbDeptDO = randomPojo(DeptDO.class);
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
// 准备参数
Long id = dbDeptDO.getId();
when(deptMapper.selectById(id)).thenReturn(dbDeptDO);
when(userService.getUserListByDeptIds(anyList()))
.thenReturn(Collections.singletonList(new AdminUserDO()));
@@ -185,14 +203,11 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testDeleteDept_exitsChildren() {
// mock 数据
DeptDO parentDept = randomPojo(DeptDO.class);
deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据
// 准备参数
DeptDO childrenDeptDO = randomPojo(DeptDO.class, o -> {
o.setParentId(parentDept.getId());
o.setStatus(randomCommonStatus());
});
Long id = parentDept.getId();
when(deptMapper.selectById(id)).thenReturn(parentDept);
// 插入子部门
deptMapper.insert(childrenDeptDO);
when(deptMapper.selectCountByParentId(id)).thenReturn(1L);
// 调用, 并断言异常
assertServiceException(() -> deptService.deleteDept(parentDept.getId()), DEPT_EXITS_CHILDREN);
@@ -201,14 +216,14 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
@Test
void testValidateDeptExists_idIsNull() {
// 调用,不抛异常
deptService.validateDeptExists(null);
assertDoesNotThrow(() -> deptService.validateDeptExists(null));
}
@Test
void testValidateDeptExists_exists() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
when(deptMapper.selectById(deptDO.getId())).thenReturn(deptDO);
// 调用,不抛异常
deptService.validateDeptExists(deptDO.getId());
@@ -247,7 +262,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testValidateParentDept_parentDisabled() {
DeptDO parentDept = randomPojo(DeptDO.class);
parentDept.setStatus(CommonStatusEnum.DISABLE.getStatus());
deptMapper.insert(parentDept);
when(deptMapper.selectById(parentDept.getId())).thenReturn(parentDept);
Long id = randomLongId();
Long parentId = parentDept.getId();
@@ -261,11 +276,11 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
// 构造三级部门:grandParent -> parent -> child
DeptDO grandParent = randomPojo(DeptDO.class);
grandParent.setStatus(CommonStatusEnum.DISABLE.getStatus());
deptMapper.insert(grandParent);
when(deptMapper.selectById(grandParent.getId())).thenReturn(grandParent);
DeptDO parent = randomPojo(DeptDO.class, o -> o.setParentId(grandParent.getId()));
parent.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(parent);
when(deptMapper.selectById(parent.getId())).thenReturn(parent);
Long id = randomLongId();
Long parentId = parent.getId();
@@ -278,11 +293,11 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testValidateParentDept_validHierarchy() {
DeptDO grandParent = randomPojo(DeptDO.class);
grandParent.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(grandParent);
when(deptMapper.selectById(grandParent.getId())).thenReturn(grandParent);
DeptDO parent = randomPojo(DeptDO.class, o -> o.setParentId(grandParent.getId()));
parent.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(parent);
when(deptMapper.selectById(parent.getId())).thenReturn(parent);
Long id = randomLongId();
Long parentId = parent.getId();
@@ -296,13 +311,13 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
// mock 数据(父节点)
DeptDO parentDept = randomPojo(DeptDO.class);
parentDept.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(parentDept);
// mock 数据(子节点)
DeptDO childDept = randomPojo(DeptDO.class, o -> {
o.setParentId(parentDept.getId());
});
childDept.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(childDept);
when(deptMapper.selectById(anyLong())).thenReturn(parentDept).thenReturn(childDept).thenReturn(null);
// 准备参数
Long id = parentDept.getId();
@@ -316,7 +331,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testValidateNameUnique_duplicate() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
when(deptMapper.selectByParentIdAndName(anyLong(), anyString(), anyLong())).thenReturn(deptDO);
// 准备参数
Long id = randomLongId();
@@ -333,7 +348,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testValidateDeptNameUnique_newDeptDuplicate() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
when(deptMapper.selectByParentIdAndName(anyLong(), anyString(), anyLong())).thenReturn(deptDO);
// 准备参数:id = null,模拟新增
Long id = null;
@@ -346,28 +361,11 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
DEPT_NAME_DUPLICATE);
}
@Test
void testValidateDeptNameUnique_updateOtherDeptDuplicate() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
// 准备参数:id 不同,模拟更新到重名部门
Long id = randomLongId(); // 与 deptDO 的 id 不同
Long parentId = deptDO.getParentId();
String name = deptDO.getName();
Long organId = deptDO.getOrganId();
// 调用,并断言异常
assertServiceException(() -> deptService.validateDeptNameUnique(id, parentId, name, organId),
DEPT_NAME_DUPLICATE);
}
@Test
void testValidateDeptNameUnique_updateSameDept() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
when(deptMapper.selectByParentIdAndName(anyLong(), anyString(), anyLong())).thenReturn(deptDO);
// 准备参数:id 相同,表示更新自己
Long id = deptDO.getId();
@@ -384,7 +382,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testGetDept() {
// mock 数据
DeptDO deptDO = randomPojo(DeptDO.class);
deptMapper.insert(deptDO);
when(deptMapper.selectById(deptDO.getId())).thenReturn(deptDO);
// 准备参数
Long id = deptDO.getId();
@@ -398,9 +396,9 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testGetDeptList_ids() {
// mock 数据
DeptDO deptDO01 = randomPojo(DeptDO.class);
deptMapper.insert(deptDO01);
DeptDO deptDO02 = randomPojo(DeptDO.class);
deptMapper.insert(deptDO02);
when(deptMapper.selectByIds(anyList())).thenReturn(List.of(deptDO01, deptDO02));
// 准备参数
List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId());
@@ -427,17 +425,15 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setOrganId(organId);
});
deptMapper.insert(dept);
// 测试 name 不匹配
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setName("")));
// 测试 status 不匹配
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
DeptListReqVO reqVO = new DeptListReqVO();
reqVO.setName("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setOrganId(organId);
when(deptMapper.selectList(reqVO, organId)).thenReturn(Arrays.asList(dept));
// 调用
List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO);
// 断言
@@ -455,16 +451,14 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setOrganId(organId);
});
deptMapper.insert(dept);
// 测试 name 不匹配
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setName("")));
// 测试 status 不匹配
deptMapper.insert(ObjectUtils.cloneIgnoreId(dept, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 准备参数
DeptListReqVO reqVO = new DeptListReqVO();
reqVO.setName("");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
when(deptMapper.selectList(reqVO, organId)).thenReturn(Arrays.asList(dept));
// 调用
List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO);
// 断言
@@ -476,21 +470,19 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testGetChildDeptList() {
// mock 数据(1 级别子节点)
DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1"));
deptMapper.insert(dept1);
DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2"));
deptMapper.insert(dept2);
// mock 数据(2 级子节点)
DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId()));
deptMapper.insert(dept1a);
DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId()));
deptMapper.insert(dept2a);
when(deptMapper.selectListByParentId(any())).thenReturn(Arrays.asList(dept1, dept1a)).thenReturn(null);
// 准备参数
Long id = dept1.getParentId();
// 调用
List<DeptDO> result = deptService.getChildDeptList(id);
// 断言
assertEquals(result.size(), 2);
assertEquals(2, result.size());
assertPojoEquals(dept1, result.get(0));
assertPojoEquals(dept1a, result.get(1));
}
@@ -500,20 +492,19 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
// mock 数据(1 级别子节点)
DeptDO dept1 = randomPojo(DeptDO.class, o -> o.setName("1"));
deptMapper.insert(dept1);
DeptDO dept2 = randomPojo(DeptDO.class, o -> o.setName("2"));
deptMapper.insert(dept2);
// mock 数据(2 级子节点)
DeptDO dept1a = randomPojo(DeptDO.class, o -> o.setName("1-a").setParentId(dept1.getId()));
deptMapper.insert(dept1a);
DeptDO dept2a = randomPojo(DeptDO.class, o -> o.setName("2-a").setParentId(dept2.getId()));
deptMapper.insert(dept2a);
when(deptMapper.selectListByParentId(any())).thenReturn(Arrays.asList(dept1, dept1a)).thenReturn(null);
// 准备参数
Long id = dept1.getParentId();
// 调用
Set<Long> result = deptService.getChildDeptIdListFromCache(id);
// 断言
assertEquals(result.size(), 2);
assertEquals(2, result.size());
assertTrue(result.contains(dept1.getId()));
assertTrue(result.contains(dept1a.getId()));
}
@@ -528,7 +519,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testValidDept_Disabled() {
// 部门状态不可用
DeptDO disableDbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
deptMapper.insert(disableDbDeptDO);
when(deptMapper.selectById(disableDbDeptDO.getId())).thenReturn(disableDbDeptDO);
assertServiceException(() -> deptService.validDept(disableDbDeptDO.getId()), DEPT_DISABLE, disableDbDeptDO.getName());
}
@@ -537,40 +528,38 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
DeptDO enabledbDeptDO = randomPojo(DeptDO.class, o -> {
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
});
deptMapper.insert(enabledbDeptDO);
when(deptMapper.selectById(enabledbDeptDO.getId())).thenReturn(enabledbDeptDO);
assertServiceException(() -> deptService.validDept(enabledbDeptDO.getId()), DEPT_PARENT_NOT_EXITS);
}
@Test
void testValidDept_ParentDisabled() {
DeptDO disabledParentDetpDO = randomPojo(DeptDO.class, o -> {
o.setId(null);
o.setStatus(CommonStatusEnum.DISABLE.getStatus());
});
deptMapper.insert(disabledParentDetpDO);
DeptDO enabledbDeptDO = randomPojo(DeptDO.class, o -> {
o.setId(null);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setParentId(disabledParentDetpDO.getId());
});
deptMapper.insert(enabledbDeptDO);
when(deptMapper.selectById(any())).thenReturn(enabledbDeptDO).thenReturn(disabledParentDetpDO);
assertServiceException(() -> deptService.validDept(enabledbDeptDO.getId()), DEPT_DISABLE, disabledParentDetpDO.getName());
}
@Test
void testValidDept_success() {
DeptDO disabledParentDetpDO = randomPojo(DeptDO.class, o -> {
DeptDO parentDetpDO = randomPojo(DeptDO.class, o -> {
o.setId(null);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setParentId(DeptDO.PARENT_ID_ROOT);
});
deptMapper.insert(disabledParentDetpDO);
DeptDO enabledbDeptDO = randomPojo(DeptDO.class, o -> {
o.setId(null);
o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setParentId(disabledParentDetpDO.getId());
o.setParentId(parentDetpDO.getId());
});
deptMapper.insert(enabledbDeptDO);
when(deptMapper.selectById(any())).thenReturn(enabledbDeptDO).thenReturn(parentDetpDO);
// 调用,无需断言
assertDoesNotThrow(()-> deptService.validDept(enabledbDeptDO.getId()));
@@ -579,7 +568,7 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
@Test
void removeUnowndDept_empty() {
List<DeptDO> deptDOS = deptService.removeUnowndDept(Collections.emptyList());
assertEquals(deptDOS.size(), 0);
assertEquals(0, deptDOS.size());
}
@Test
@@ -629,8 +618,10 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
});
List<DeptDO> deptDOS = List.of(oneParent, one, two, three);
// 批量插入
deptMapper.insertBatch(deptDOS);
when(deptMapper.selectList(any())).thenReturn(deptDOS);
long disableCount = deptDOS.stream().filter(o -> CommonStatusEnum.DISABLE.getStatus().equals(o.getStatus())).count();
Set<Long> allInvalidDeptIds = deptService.getAllInvalidDeptIds(organId);
assertEquals(disableCount, allInvalidDeptIds.size());
@@ -654,10 +645,12 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
DeptDO dept = new DeptDO();
dept.setName("测试部-禁用");
dept.setStatus(CommonStatusEnum.DISABLE.getStatus());
deptMapper.insert(dept);
Long id = dept.getId();
when(deptMapper.selectById(id)).thenReturn(dept);
ServiceException ex = assertThrows(ServiceException.class,
() -> deptService.validUserLoginDept(dept.getId()));
() -> deptService.validUserLoginDept(id));
assertEquals(ErrorCodeConstants.DEPT_NOT_ALLOWED_LOGIN.getCode(), ex.getCode());
assertTrue(ex.getArgs()[0].toString().contains("测试部-禁用"));
@@ -669,28 +662,32 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
DeptDO dept = new DeptDO();
dept.setName("测试部-启用");
dept.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(dept);
assertDoesNotThrow(() -> deptService.validUserLoginDept(dept.getId()));
Long id = dept.getId();
when(deptMapper.selectById(id)).thenReturn(dept);
assertDoesNotThrow(() -> deptService.validUserLoginDept(id));
}
@Test
void testParentDeptDisabled_shouldThrow() {
// 上级部门禁用
DeptDO parent = new DeptDO();
parent.setId(100L);
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);
when(deptMapper.selectById(any())).thenReturn(child).thenReturn(parent);
Long childId = child.getId();
ServiceException ex = assertThrows(ServiceException.class,
() -> deptService.validUserLoginDept(child.getId()));
() -> deptService.validUserLoginDept(childId));
assertEquals(ErrorCodeConstants.DEPT_DISABLE.getCode(), ex.getCode());
assertTrue(ex.getArgs()[0].toString().contains("上级部门-禁用"));
@@ -700,16 +697,16 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testParentDeptEnabled_shouldPass() {
// 上级部门启用
DeptDO parent = new DeptDO();
parent.setId(100L);
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);
when(deptMapper.selectById(any())).thenReturn(child).thenReturn(parent);
assertDoesNotThrow(() -> deptService.validUserLoginDept(child.getId()));
}
@@ -718,26 +715,28 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testMultiLevelParentDeptDisabled_shouldThrow() {
// 顶级部门禁用
DeptDO top = new DeptDO();
top.setId(100L);
top.setName("顶级部门-禁用");
top.setStatus(CommonStatusEnum.DISABLE.getStatus());
deptMapper.insert(top);
// 中间部门启用,指向顶级
DeptDO middle = new DeptDO();
middle.setId(200L);
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);
when(deptMapper.selectById(any())).thenReturn(child).thenReturn(middle).thenReturn(top);
Long childId = child.getId();
ServiceException ex = assertThrows(ServiceException.class,
() -> deptService.validUserLoginDept(child.getId()));
() -> deptService.validUserLoginDept(childId));
assertEquals(ErrorCodeConstants.DEPT_DISABLE.getCode(), ex.getCode());
assertTrue(ex.getArgs()[0].toString().contains("顶级部门-禁用"));
@@ -747,23 +746,24 @@ public class DeptServiceImplTest extends BaseDbAndRedisUnitTest {
void testMultiLevelAllEnabled_shouldPass() {
// 顶级部门启用
DeptDO top = new DeptDO();
top.setId(100L);
top.setName("顶级部门-启用");
top.setStatus(CommonStatusEnum.ENABLE.getStatus());
deptMapper.insert(top);
// 中间部门启用
DeptDO middle = new DeptDO();
middle.setId(200L);
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);
when(deptMapper.selectById(any())).thenReturn(child).thenReturn(middle).thenReturn(top);
// 方法正常执行,不抛异常
assertDoesNotThrow(() -> deptService.validUserLoginDept(child.getId()));