完善 TenantServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-02 23:47:25 +08:00
parent 9c216e5b7d
commit 6474fba923
2 changed files with 13 additions and 15 deletions

View File

@ -7,7 +7,6 @@ import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.PageResult; import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils; import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.framework.common.util.date.DateUtils; import cn.iocoder.yudao.framework.common.util.date.DateUtils;
import cn.iocoder.yudao.framework.datapermission.core.annotation.DataPermission;
import cn.iocoder.yudao.framework.tenant.config.TenantProperties; import cn.iocoder.yudao.framework.tenant.config.TenantProperties;
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils; import cn.iocoder.yudao.framework.tenant.core.util.TenantUtils;
@ -97,7 +96,6 @@ public class TenantServiceImpl implements TenantService {
@Override @Override
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
@DataPermission(enable = false) // 租户创建不需要数据权限避免出现被数据权限拦截
public Long createTenant(TenantCreateReqVO createReqVO) { public Long createTenant(TenantCreateReqVO createReqVO) {
// 校验套餐被禁用 // 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId()); TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(createReqVO.getPackageId());
@ -140,7 +138,7 @@ public class TenantServiceImpl implements TenantService {
@Transactional(rollbackFor = Exception.class) @Transactional(rollbackFor = Exception.class)
public void updateTenant(TenantUpdateReqVO updateReqVO) { public void updateTenant(TenantUpdateReqVO updateReqVO) {
// 校验存在 // 校验存在
TenantDO tenant = checkUpdateTenant(updateReqVO.getId()); TenantDO tenant = validateUpdateTenant(updateReqVO.getId());
// 校验套餐被禁用 // 校验套餐被禁用
TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId()); TenantPackageDO tenantPackage = tenantPackageService.validTenantPackage(updateReqVO.getPackageId());
@ -181,12 +179,12 @@ public class TenantServiceImpl implements TenantService {
@Override @Override
public void deleteTenant(Long id) { public void deleteTenant(Long id) {
// 校验存在 // 校验存在
checkUpdateTenant(id); validateUpdateTenant(id);
// 删除 // 删除
tenantMapper.deleteById(id); tenantMapper.deleteById(id);
} }
private TenantDO checkUpdateTenant(Long id) { private TenantDO validateUpdateTenant(Long id) {
TenantDO tenant = tenantMapper.selectById(id); TenantDO tenant = tenantMapper.selectById(id);
if (tenant == null) { if (tenant == null) {
throw exception(TENANT_NOT_EXISTS); throw exception(TENANT_NOT_EXISTS);

View File

@ -34,7 +34,7 @@ import java.util.Collections;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet; import static cn.iocoder.yudao.framework.common.util.collection.SetUtils.asSet;
import static cn.iocoder.yudao.framework.common.util.date.DateUtils.buildLocalDateTime; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
@ -110,7 +110,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
public void testValidTenant_expired() { public void testValidTenant_expired() {
// mock 数据 // mock 数据
TenantDO tenant = randomPojo(TenantDO.class, o -> o.setId(1L).setStatus(CommonStatusEnum.ENABLE.getStatus()) TenantDO tenant = randomPojo(TenantDO.class, o -> o.setId(1L).setStatus(CommonStatusEnum.ENABLE.getStatus())
.setExpireTime(buildLocalDateTime(2020, 2, 2))); .setExpireTime(buildTime(2020, 2, 2)));
tenantMapper.insert(tenant); tenantMapper.insert(tenant);
// 调用并断言业务异常 // 调用并断言业务异常
@ -196,7 +196,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
role100.setTenantId(dbTenant.getId()); role100.setTenantId(dbTenant.getId());
RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L)); RoleDO role101 = randomPojo(RoleDO.class, o -> o.setId(101L));
role101.setTenantId(dbTenant.getId()); role101.setTenantId(dbTenant.getId());
when(roleService.getRoleList(isNull())).thenReturn(asList(role100, role101)); when(roleService.getRoleListByStatus(isNull())).thenReturn(asList(role100, role101));
// mock 每个角色的权限 // mock 每个角色的权限
when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L)); when(permissionService.getRoleMenuIds(eq(101L))).thenReturn(asSet(201L, 202L));
@ -291,7 +291,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
o.setContactName("芋艿"); o.setContactName("芋艿");
o.setContactMobile("15601691300"); o.setContactMobile("15601691300");
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(buildLocalDateTime(2020, 12, 12)); o.setCreateTime(buildTime(2020, 12, 12));
}); });
tenantMapper.insert(dbTenant); tenantMapper.insert(dbTenant);
// 测试 name 不匹配 // 测试 name 不匹配
@ -303,14 +303,14 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
// 测试 status 不匹配 // 测试 status 不匹配
tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildLocalDateTime(2021, 12, 12)))); tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildTime(2021, 12, 12))));
// 准备参数 // 准备参数
TenantPageReqVO reqVO = new TenantPageReqVO(); TenantPageReqVO reqVO = new TenantPageReqVO();
reqVO.setName("芋道"); reqVO.setName("芋道");
reqVO.setContactName(""); reqVO.setContactName("");
reqVO.setContactMobile("1560"); reqVO.setContactMobile("1560");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime(new LocalDateTime[]{buildLocalDateTime(2020, 12, 1),buildLocalDateTime(2020, 12, 24)}); reqVO.setCreateTime(new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)});
// 调用 // 调用
PageResult<TenantDO> pageResult = tenantService.getTenantPage(reqVO); PageResult<TenantDO> pageResult = tenantService.getTenantPage(reqVO);
@ -328,7 +328,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
o.setContactName("芋艿"); o.setContactName("芋艿");
o.setContactMobile("15601691300"); o.setContactMobile("15601691300");
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); o.setStatus(CommonStatusEnum.ENABLE.getStatus());
o.setCreateTime(buildLocalDateTime(2020, 12, 12)); o.setCreateTime(buildTime(2020, 12, 12));
}); });
tenantMapper.insert(dbTenant); tenantMapper.insert(dbTenant);
// 测试 name 不匹配 // 测试 name 不匹配
@ -340,14 +340,14 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
// 测试 status 不匹配 // 测试 status 不匹配
tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
// 测试 createTime 不匹配 // 测试 createTime 不匹配
tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildLocalDateTime(2021, 12, 12)))); tenantMapper.insert(cloneIgnoreId(dbTenant, o -> o.setCreateTime(buildTime(2021, 12, 12))));
// 准备参数 // 准备参数
TenantExportReqVO reqVO = new TenantExportReqVO(); TenantExportReqVO reqVO = new TenantExportReqVO();
reqVO.setName("芋道"); reqVO.setName("芋道");
reqVO.setContactName(""); reqVO.setContactName("");
reqVO.setContactMobile("1560"); reqVO.setContactMobile("1560");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime(new LocalDateTime[]{buildLocalDateTime(2020, 12, 1),buildLocalDateTime(2020, 12, 24)}); reqVO.setCreateTime(new LocalDateTime[]{buildTime(2020, 12, 1),buildTime(2020, 12, 24)});
// 调用 // 调用
List<TenantDO> list = tenantService.getTenantList(reqVO); List<TenantDO> list = tenantService.getTenantList(reqVO);
@ -454,7 +454,7 @@ public class TenantServiceImplTest extends BaseDbUnitTest {
TenantContextHolder.setTenantId(dbTenant.getId()); TenantContextHolder.setTenantId(dbTenant.getId());
// mock 菜单 // mock 菜单
when(menuService.getMenuList()).thenReturn(Arrays.asList(randomPojo(MenuDO.class, o -> o.setId(100L)), when(menuService.getMenuList()).thenReturn(Arrays.asList(randomPojo(MenuDO.class, o -> o.setId(100L)),
randomPojo(MenuDO.class, o -> o.setId(101L)))); randomPojo(MenuDO.class, o -> o.setId(101L))));
// 调用 // 调用
tenantService.handleTenantMenu(handler); tenantService.handleTenantMenu(handler);