|
@@ -19,6 +19,7 @@ import org.springframework.boot.test.mock.mockito.MockBean;
|
|
|
import org.springframework.context.annotation.Import;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
+import java.util.Arrays;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
import java.util.function.Consumer;
|
|
@@ -28,8 +29,8 @@ import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEq
|
|
|
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
|
|
|
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.*;
|
|
|
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|
|
+import static java.util.Collections.singletonList;
|
|
|
import static org.junit.jupiter.api.Assertions.*;
|
|
|
-import static org.mockito.Mockito.times;
|
|
|
import static org.mockito.Mockito.verify;
|
|
|
|
|
|
/**
|
|
@@ -38,7 +39,7 @@ import static org.mockito.Mockito.verify;
|
|
|
* @author niudehua
|
|
|
*/
|
|
|
@Import(DeptServiceImpl.class)
|
|
|
-public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
+public class DeptServiceImplTest extends BaseDbUnitTest {
|
|
|
|
|
|
@Resource
|
|
|
private DeptServiceImpl deptService;
|
|
@@ -76,7 +77,7 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testListDepts() {
|
|
|
+ public void testListDepts() {
|
|
|
// mock 数据
|
|
|
DeptDO dept = randomPojo(DeptDO.class, o -> { // 等会查询到
|
|
|
o.setName("开发部");
|
|
@@ -91,21 +92,22 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
DeptListReqVO reqVO = new DeptListReqVO();
|
|
|
reqVO.setName("开");
|
|
|
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
|
+
|
|
|
// 调用
|
|
|
- List<DeptDO> sysDeptDOS = deptService.getSimpleDepts(reqVO);
|
|
|
+ List<DeptDO> sysDeptDOS = deptService.getDeptList(reqVO);
|
|
|
// 断言
|
|
|
assertEquals(1, sysDeptDOS.size());
|
|
|
assertPojoEquals(dept, sysDeptDOS.get(0));
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCreateDept_success() {
|
|
|
+ public void testCreateDept_success() {
|
|
|
// 准备参数
|
|
|
- DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
|
|
- o -> {
|
|
|
- o.setParentId(DeptIdEnum.ROOT.getId());
|
|
|
- o.setStatus(randomCommonStatus());
|
|
|
- });
|
|
|
+ DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
|
|
|
+ o.setParentId(DeptIdEnum.ROOT.getId());
|
|
|
+ o.setStatus(randomCommonStatus());
|
|
|
+ });
|
|
|
+
|
|
|
// 调用
|
|
|
Long deptId = deptService.createDept(reqVO);
|
|
|
// 断言
|
|
@@ -114,11 +116,11 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
DeptDO deptDO = deptMapper.selectById(deptId);
|
|
|
assertPojoEquals(reqVO, deptDO);
|
|
|
// 校验调用
|
|
|
- verify(deptProducer, times(1)).sendDeptRefreshMessage();
|
|
|
+ verify(deptProducer).sendDeptRefreshMessage();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testUpdateDept_success() {
|
|
|
+ public void testUpdateDept_success() {
|
|
|
// mock 数据
|
|
|
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
|
|
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
|
|
@@ -129,28 +131,34 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
o.setId(dbDeptDO.getId());
|
|
|
o.setStatus(randomCommonStatus());
|
|
|
});
|
|
|
+
|
|
|
// 调用
|
|
|
deptService.updateDept(reqVO);
|
|
|
// 校验是否更新正确
|
|
|
DeptDO deptDO = deptMapper.selectById(reqVO.getId()); // 获取最新的
|
|
|
assertPojoEquals(reqVO, deptDO);
|
|
|
+ // 校验调用
|
|
|
+ verify(deptProducer).sendDeptRefreshMessage();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testDeleteDept_success() {
|
|
|
+ public void testDeleteDept_success() {
|
|
|
// mock 数据
|
|
|
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
|
|
deptMapper.insert(dbDeptDO);// @Sql: 先插入出一条存在的数据
|
|
|
// 准备参数
|
|
|
Long id = dbDeptDO.getId();
|
|
|
+
|
|
|
// 调用
|
|
|
deptService.deleteDept(id);
|
|
|
// 校验数据不存在了
|
|
|
assertNull(deptMapper.selectById(id));
|
|
|
+ // 校验调用
|
|
|
+ verify(deptProducer).sendDeptRefreshMessage();
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_nameDuplicateForUpdate() {
|
|
|
+ public void testValidateDept_nameDuplicateForUpdate() {
|
|
|
// mock 数据
|
|
|
DeptDO deptDO = randomDeptDO();
|
|
|
// 设置根节点部门
|
|
@@ -162,37 +170,40 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
nameDeptDO.setParentId(DeptIdEnum.ROOT.getId());
|
|
|
deptMapper.insert(nameDeptDO);
|
|
|
// 准备参数
|
|
|
- DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
|
|
- o -> {
|
|
|
- // 设置根节点部门
|
|
|
- o.setParentId(DeptIdEnum.ROOT.getId());
|
|
|
- // 设置更新的 ID
|
|
|
- o.setId(deptDO.getId());
|
|
|
- // 模拟 name 重复
|
|
|
- o.setName(nameDeptDO.getName());
|
|
|
- });
|
|
|
+ DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
|
|
+ // 设置根节点部门
|
|
|
+ o.setParentId(DeptIdEnum.ROOT.getId());
|
|
|
+ // 设置更新的 ID
|
|
|
+ o.setId(deptDO.getId());
|
|
|
+ // 模拟 name 重复
|
|
|
+ o.setName(nameDeptDO.getName());
|
|
|
+ });
|
|
|
+
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_NAME_DUPLICATE);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_parentNotExitsForCreate() {
|
|
|
+ public void testValidateDept_parentNotExitsForCreate() {
|
|
|
+ // 准备参数
|
|
|
DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
|
|
o -> o.setStatus(randomCommonStatus()));
|
|
|
+
|
|
|
// 调用,并断言异常
|
|
|
assertServiceException(() -> deptService.createDept(reqVO), DEPT_PARENT_NOT_EXITS);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_notFoundForDelete() {
|
|
|
+ public void testValidateDept_notFoundForDelete() {
|
|
|
// 准备参数
|
|
|
Long id = randomLongId();
|
|
|
+
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> deptService.deleteDept(id), DEPT_NOT_FOUND);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_exitsChildrenForDelete() {
|
|
|
+ public void testValidateDept_exitsChildrenForDelete() {
|
|
|
// mock 数据
|
|
|
DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
|
|
deptMapper.insert(parentDept);// @Sql: 先插入出一条存在的数据
|
|
@@ -208,39 +219,39 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_parentErrorForUpdate() {
|
|
|
+ public void testValidateDept_parentErrorForUpdate() {
|
|
|
// mock 数据
|
|
|
DeptDO dbDeptDO = randomPojo(DeptDO.class, o -> o.setStatus(randomCommonStatus()));
|
|
|
deptMapper.insert(dbDeptDO);
|
|
|
// 准备参数
|
|
|
- DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
|
|
- o -> {
|
|
|
- // 设置自己为父部门
|
|
|
- o.setParentId(dbDeptDO.getId());
|
|
|
- // 设置更新的 ID
|
|
|
- o.setId(dbDeptDO.getId());
|
|
|
- });
|
|
|
+ DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
|
|
+ // 设置自己为父部门
|
|
|
+ o.setParentId(dbDeptDO.getId());
|
|
|
+ // 设置更新的 ID
|
|
|
+ o.setId(dbDeptDO.getId());
|
|
|
+ });
|
|
|
+
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_ERROR);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_notEnableForCreate() {
|
|
|
+ public void testValidateDept_notEnableForCreate() {
|
|
|
// mock 数据
|
|
|
DeptDO deptDO = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
|
|
|
deptMapper.insert(deptDO);
|
|
|
// 准备参数
|
|
|
- DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class,
|
|
|
- o -> {
|
|
|
- // 设置未启用的部门为副部门
|
|
|
- o.setParentId(deptDO.getId());
|
|
|
- });
|
|
|
+ DeptCreateReqVO reqVO = randomPojo(DeptCreateReqVO.class, o -> {
|
|
|
+ // 设置未启用的部门为父部门
|
|
|
+ o.setParentId(deptDO.getId());
|
|
|
+ });
|
|
|
+
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> deptService.createDept(reqVO), DEPT_NOT_ENABLE);
|
|
|
}
|
|
|
|
|
|
@Test
|
|
|
- void testCheckDept_parentIsChildForUpdate() {
|
|
|
+ public void testCheckDept_parentIsChildForUpdate() {
|
|
|
// mock 数据
|
|
|
DeptDO parentDept = randomPojo(DeptDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
|
|
|
deptMapper.insert(parentDept);
|
|
@@ -251,18 +262,84 @@ public class DeptServiceTest extends BaseDbUnitTest {
|
|
|
deptMapper.insert(childDept);
|
|
|
// 初始化本地缓存
|
|
|
deptService.initLocalCache();
|
|
|
+
|
|
|
// 准备参数
|
|
|
- DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class,
|
|
|
- o -> {
|
|
|
- // 设置自己的子部门为父部门
|
|
|
- o.setParentId(childDept.getId());
|
|
|
- // 设置更新的 ID
|
|
|
- o.setId(parentDept.getId());
|
|
|
- });
|
|
|
+ DeptUpdateReqVO reqVO = randomPojo(DeptUpdateReqVO.class, o -> {
|
|
|
+ // 设置自己的子部门为父部门
|
|
|
+ o.setParentId(childDept.getId());
|
|
|
+ // 设置更新的 ID
|
|
|
+ o.setId(parentDept.getId());
|
|
|
+ });
|
|
|
+
|
|
|
// 调用, 并断言异常
|
|
|
assertServiceException(() -> deptService.updateDept(reqVO), DEPT_PARENT_IS_CHILD);
|
|
|
}
|
|
|
|
|
|
+ @Test
|
|
|
+ public void testGetDeptList() {
|
|
|
+ // mock 数据
|
|
|
+ DeptDO deptDO01 = randomDeptDO();
|
|
|
+ deptMapper.insert(deptDO01);
|
|
|
+ DeptDO deptDO02 = randomDeptDO();
|
|
|
+ deptMapper.insert(deptDO02);
|
|
|
+ // 准备参数
|
|
|
+ List<Long> ids = Arrays.asList(deptDO01.getId(), deptDO02.getId());
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ List<DeptDO> deptDOList = deptService.getDeptList(ids);
|
|
|
+ // 断言
|
|
|
+ assertEquals(2, deptDOList.size());
|
|
|
+ assertEquals(deptDO01, deptDOList.get(0));
|
|
|
+ assertEquals(deptDO02, deptDOList.get(1));
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testGetDept() {
|
|
|
+ // mock 数据
|
|
|
+ DeptDO deptDO = randomDeptDO();
|
|
|
+ deptMapper.insert(deptDO);
|
|
|
+ // 准备参数
|
|
|
+ Long id = deptDO.getId();
|
|
|
+
|
|
|
+ // 调用
|
|
|
+ DeptDO dbDept = deptService.getDept(id);
|
|
|
+ // 断言
|
|
|
+ assertEquals(deptDO, dbDept);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidateDeptList_success() {
|
|
|
+ // mock 数据
|
|
|
+ DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
|
|
|
+ deptMapper.insert(deptDO);
|
|
|
+ // 准备参数
|
|
|
+ List<Long> ids = singletonList(deptDO.getId());
|
|
|
+
|
|
|
+ // 调用,无需断言
|
|
|
+ deptService.validateDeptList(ids);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidateDeptList_notFound() {
|
|
|
+ // 准备参数
|
|
|
+ List<Long> ids = singletonList(randomLongId());
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_FOUND);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Test
|
|
|
+ public void testValidateDeptList_notEnable() {
|
|
|
+ // mock 数据
|
|
|
+ DeptDO deptDO = randomDeptDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
|
|
|
+ deptMapper.insert(deptDO);
|
|
|
+ // 准备参数
|
|
|
+ List<Long> ids = singletonList(deptDO.getId());
|
|
|
+
|
|
|
+ // 调用, 并断言异常
|
|
|
+ assertServiceException(() -> deptService.validateDeptList(ids), DEPT_NOT_ENABLE);
|
|
|
+ }
|
|
|
+
|
|
|
@SafeVarargs
|
|
|
private static DeptDO randomDeptDO(Consumer<DeptDO>... consumers) {
|
|
|
Consumer<DeptDO> consumer = (o) -> {
|