提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.tenant; |
H |
2 |
|
|
3 |
import com.iailab.framework.common.enums.CommonStatusEnum; |
|
4 |
import com.iailab.framework.common.pojo.PageResult; |
|
5 |
import com.iailab.framework.test.core.ut.BaseDbUnitTest; |
|
6 |
import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackagePageReqVO; |
|
7 |
import com.iailab.module.system.controller.admin.tenant.vo.packages.TenantPackageSaveReqVO; |
|
8 |
import com.iailab.module.system.dal.dataobject.tenant.TenantDO; |
|
9 |
import com.iailab.module.system.dal.dataobject.tenant.TenantPackageDO; |
|
10 |
import com.iailab.module.system.dal.mysql.tenant.TenantPackageMapper; |
|
11 |
import org.junit.jupiter.api.Test; |
|
12 |
import org.springframework.boot.test.mock.mockito.MockBean; |
|
13 |
import org.springframework.context.annotation.Import; |
|
14 |
|
|
15 |
import javax.annotation.Resource; |
|
16 |
import java.util.List; |
|
17 |
|
|
18 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; |
|
19 |
import static com.iailab.framework.common.util.date.LocalDateTimeUtils.buildTime; |
|
20 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
21 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
22 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
23 |
import static com.iailab.framework.test.core.util.RandomUtils.*; |
|
24 |
import static com.iailab.module.system.enums.ErrorCodeConstants.*; |
|
25 |
import static java.util.Arrays.asList; |
|
26 |
import static org.junit.jupiter.api.Assertions.*; |
|
27 |
import static org.mockito.ArgumentMatchers.eq; |
|
28 |
import static org.mockito.Mockito.verify; |
|
29 |
import static org.mockito.Mockito.when; |
|
30 |
|
|
31 |
/** |
|
32 |
* {@link TenantPackageServiceImpl} 的单元测试类 |
|
33 |
* |
|
34 |
* @author iailab |
|
35 |
*/ |
|
36 |
@Import(TenantPackageServiceImpl.class) |
|
37 |
public class TenantPackageServiceImplTest extends BaseDbUnitTest { |
|
38 |
|
|
39 |
@Resource |
|
40 |
private TenantPackageServiceImpl tenantPackageService; |
|
41 |
|
|
42 |
@Resource |
|
43 |
private TenantPackageMapper tenantPackageMapper; |
|
44 |
|
|
45 |
@MockBean |
|
46 |
private TenantService tenantService; |
|
47 |
|
|
48 |
@Test |
|
49 |
public void testCreateTenantPackage_success() { |
|
50 |
// 准备参数 |
|
51 |
TenantPackageSaveReqVO reqVO = randomPojo(TenantPackageSaveReqVO.class, |
|
52 |
o -> o.setStatus(randomCommonStatus())) |
|
53 |
.setId(null); // 防止 id 被赋值 |
|
54 |
|
|
55 |
// 调用 |
|
56 |
Long tenantPackageId = tenantPackageService.createTenantPackage(reqVO); |
|
57 |
// 断言 |
|
58 |
assertNotNull(tenantPackageId); |
|
59 |
// 校验记录的属性是否正确 |
|
60 |
TenantPackageDO tenantPackage = tenantPackageMapper.selectById(tenantPackageId); |
|
61 |
assertPojoEquals(reqVO, tenantPackage, "id"); |
|
62 |
} |
|
63 |
|
|
64 |
@Test |
|
65 |
public void testUpdateTenantPackage_success() { |
|
66 |
// mock 数据 |
|
67 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, |
|
68 |
o -> o.setStatus(randomCommonStatus())); |
|
69 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
70 |
// 准备参数 |
|
71 |
TenantPackageSaveReqVO reqVO = randomPojo(TenantPackageSaveReqVO.class, o -> { |
|
72 |
o.setId(dbTenantPackage.getId()); // 设置更新的 ID |
|
73 |
o.setStatus(randomCommonStatus()); |
|
74 |
}); |
|
75 |
// mock 方法 |
|
76 |
Long tenantId01 = randomLongId(); |
|
77 |
Long tenantId02 = randomLongId(); |
|
78 |
when(tenantService.getTenantListByPackageId(eq(reqVO.getId()))).thenReturn( |
|
79 |
asList(randomPojo(TenantDO.class, o -> o.setId(tenantId01)), |
|
80 |
randomPojo(TenantDO.class, o -> o.setId(tenantId02)))); |
|
81 |
|
|
82 |
// 调用 |
|
83 |
tenantPackageService.updateTenantPackage(reqVO); |
|
84 |
// 校验是否更新正确 |
|
85 |
TenantPackageDO tenantPackage = tenantPackageMapper.selectById(reqVO.getId()); // 获取最新的 |
|
86 |
assertPojoEquals(reqVO, tenantPackage); |
|
87 |
// 校验调用租户的菜单 |
|
88 |
verify(tenantService).updateTenantRoleMenu(eq(tenantId01), eq(reqVO.getMenuIds())); |
|
89 |
verify(tenantService).updateTenantRoleMenu(eq(tenantId02), eq(reqVO.getMenuIds())); |
|
90 |
} |
|
91 |
|
|
92 |
@Test |
|
93 |
public void testUpdateTenantPackage_notExists() { |
|
94 |
// 准备参数 |
|
95 |
TenantPackageSaveReqVO reqVO = randomPojo(TenantPackageSaveReqVO.class); |
|
96 |
|
|
97 |
// 调用, 并断言异常 |
|
98 |
assertServiceException(() -> tenantPackageService.updateTenantPackage(reqVO), TENANT_PACKAGE_NOT_EXISTS); |
|
99 |
} |
|
100 |
|
|
101 |
@Test |
|
102 |
public void testDeleteTenantPackage_success() { |
|
103 |
// mock 数据 |
|
104 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class); |
|
105 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
106 |
// 准备参数 |
|
107 |
Long id = dbTenantPackage.getId(); |
|
108 |
// mock 租户未使用该套餐 |
|
109 |
when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(0L); |
|
110 |
|
|
111 |
// 调用 |
|
112 |
tenantPackageService.deleteTenantPackage(id); |
|
113 |
// 校验数据不存在了 |
|
114 |
assertNull(tenantPackageMapper.selectById(id)); |
|
115 |
} |
|
116 |
|
|
117 |
@Test |
|
118 |
public void testDeleteTenantPackage_notExists() { |
|
119 |
// 准备参数 |
|
120 |
Long id = randomLongId(); |
|
121 |
|
|
122 |
// 调用, 并断言异常 |
|
123 |
assertServiceException(() -> tenantPackageService.deleteTenantPackage(id), TENANT_PACKAGE_NOT_EXISTS); |
|
124 |
} |
|
125 |
|
|
126 |
@Test |
|
127 |
public void testDeleteTenantPackage_used() { |
|
128 |
// mock 数据 |
|
129 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class); |
|
130 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
131 |
// 准备参数 |
|
132 |
Long id = dbTenantPackage.getId(); |
|
133 |
// mock 租户在使用该套餐 |
|
134 |
when(tenantService.getTenantCountByPackageId(eq(id))).thenReturn(1L); |
|
135 |
|
|
136 |
// 调用, 并断言异常 |
|
137 |
assertServiceException(() -> tenantPackageService.deleteTenantPackage(id), TENANT_PACKAGE_USED); |
|
138 |
} |
|
139 |
|
|
140 |
@Test |
|
141 |
public void testGetTenantPackagePage() { |
|
142 |
// mock 数据 |
|
143 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, o -> { // 等会查询到 |
|
144 |
o.setName("iailab"); |
|
145 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
146 |
o.setRemark("源码解析"); |
|
147 |
o.setCreateTime(buildTime(2022, 10, 10)); |
|
148 |
}); |
|
149 |
tenantPackageMapper.insert(dbTenantPackage); |
|
150 |
// 测试 name 不匹配 |
|
151 |
tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, o -> o.setName("源码"))); |
|
152 |
// 测试 status 不匹配 |
|
153 |
tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
154 |
// 测试 remark 不匹配 |
|
155 |
tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, o -> o.setRemark("解析"))); |
|
156 |
// 测试 createTime 不匹配 |
|
157 |
tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, o -> o.setCreateTime(buildTime(2022, 11, 11)))); |
|
158 |
// 准备参数 |
|
159 |
TenantPackagePageReqVO reqVO = new TenantPackagePageReqVO(); |
|
160 |
reqVO.setName("平台"); |
|
161 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
162 |
reqVO.setRemark("源码"); |
|
163 |
reqVO.setCreateTime(buildBetweenTime(2022, 10, 9, 2022, 10, 11)); |
|
164 |
|
|
165 |
// 调用 |
|
166 |
PageResult<TenantPackageDO> pageResult = tenantPackageService.getTenantPackagePage(reqVO); |
|
167 |
// 断言 |
|
168 |
assertEquals(1, pageResult.getTotal()); |
|
169 |
assertEquals(1, pageResult.getList().size()); |
|
170 |
assertPojoEquals(dbTenantPackage, pageResult.getList().get(0)); |
|
171 |
} |
|
172 |
|
|
173 |
@Test |
|
174 |
public void testValidTenantPackage_success() { |
|
175 |
// mock 数据 |
|
176 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, |
|
177 |
o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|
178 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
179 |
|
|
180 |
// 调用 |
|
181 |
TenantPackageDO result = tenantPackageService.validTenantPackage(dbTenantPackage.getId()); |
|
182 |
// 断言 |
|
183 |
assertPojoEquals(dbTenantPackage, result); |
|
184 |
} |
|
185 |
|
|
186 |
@Test |
|
187 |
public void testValidTenantPackage_notExists() { |
|
188 |
// 准备参数 |
|
189 |
Long id = randomLongId(); |
|
190 |
|
|
191 |
// 调用, 并断言异常 |
|
192 |
assertServiceException(() -> tenantPackageService.validTenantPackage(id), TENANT_PACKAGE_NOT_EXISTS); |
|
193 |
} |
|
194 |
|
|
195 |
@Test |
|
196 |
public void testValidTenantPackage_disable() { |
|
197 |
// mock 数据 |
|
198 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, |
|
199 |
o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())); |
|
200 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
201 |
|
|
202 |
// 调用, 并断言异常 |
|
203 |
assertServiceException(() -> tenantPackageService.validTenantPackage(dbTenantPackage.getId()), |
|
204 |
TENANT_PACKAGE_DISABLE, dbTenantPackage.getName()); |
|
205 |
} |
|
206 |
|
|
207 |
@Test |
|
208 |
public void testGetTenantPackage() { |
|
209 |
// mock 数据 |
|
210 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class); |
|
211 |
tenantPackageMapper.insert(dbTenantPackage);// @Sql: 先插入出一条存在的数据 |
|
212 |
|
|
213 |
// 调用 |
|
214 |
TenantPackageDO result = tenantPackageService.getTenantPackage(dbTenantPackage.getId()); |
|
215 |
// 断言 |
|
216 |
assertPojoEquals(result, dbTenantPackage); |
|
217 |
} |
|
218 |
|
|
219 |
@Test |
|
220 |
public void testGetTenantPackageListByStatus() { |
|
221 |
// mock 数据 |
|
222 |
TenantPackageDO dbTenantPackage = randomPojo(TenantPackageDO.class, |
|
223 |
o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus())); |
|
224 |
tenantPackageMapper.insert(dbTenantPackage); |
|
225 |
// 测试 status 不匹配 |
|
226 |
tenantPackageMapper.insert(cloneIgnoreId(dbTenantPackage, |
|
227 |
o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
228 |
|
|
229 |
// 调用 |
|
230 |
List<TenantPackageDO> list = tenantPackageService.getTenantPackageListByStatus( |
|
231 |
CommonStatusEnum.ENABLE.getStatus()); |
|
232 |
assertEquals(1, list.size()); |
|
233 |
assertPojoEquals(dbTenantPackage, list.get(0)); |
|
234 |
} |
|
235 |
|
|
236 |
} |