houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.iailab.module.system.service.dept;
H 2
3 import com.iailab.framework.common.enums.CommonStatusEnum;
4 import com.iailab.framework.common.pojo.PageResult;
5 import com.iailab.framework.common.util.collection.ArrayUtils;
6 import com.iailab.framework.test.core.ut.BaseDbUnitTest;
7 import com.iailab.module.system.controller.admin.dept.vo.post.PostPageReqVO;
8 import com.iailab.module.system.controller.admin.dept.vo.post.PostSaveReqVO;
9 import com.iailab.module.system.dal.dataobject.dept.PostDO;
10 import com.iailab.module.system.dal.mysql.dept.PostMapper;
11 import org.junit.jupiter.api.Test;
12 import org.springframework.context.annotation.Import;
13
14 import javax.annotation.Resource;
15 import java.util.Arrays;
16 import java.util.List;
17 import java.util.function.Consumer;
18
19 import static cn.hutool.core.util.RandomUtil.randomEle;
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.Collections.singletonList;
26 import static org.junit.jupiter.api.Assertions.*;
27
28 /**
29  * {@link PostServiceImpl} 的单元测试类
30  *
31  * @author niudehua
32  */
33 @Import(PostServiceImpl.class)
34 public class PostServiceImplTest extends BaseDbUnitTest {
35
36     @Resource
37     private PostServiceImpl postService;
38
39     @Resource
40     private PostMapper postMapper;
41
42     @Test
43     public void testCreatePost_success() {
44         // 准备参数
45         PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class,
46                 o -> o.setStatus(randomEle(CommonStatusEnum.values()).getStatus()))
47                 .setId(null); // 防止 id 被设置
48         // 调用
49         Long postId = postService.createPost(reqVO);
50
51         // 断言
52         assertNotNull(postId);
53         // 校验记录的属性是否正确
54         PostDO post = postMapper.selectById(postId);
55         assertPojoEquals(reqVO, post, "id");
56     }
57
58     @Test
59     public void testUpdatePost_success() {
60         // mock 数据
61         PostDO postDO = randomPostDO();
62         postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
63         // 准备参数
64         PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> {
65             // 设置更新的 ID
66             o.setId(postDO.getId());
67             o.setStatus(randomEle(CommonStatusEnum.values()).getStatus());
68         });
69
70         // 调用
71         postService.updatePost(reqVO);
72         // 校验是否更新正确
73         PostDO post = postMapper.selectById(reqVO.getId());
74         assertPojoEquals(reqVO, post);
75     }
76
77     @Test
78     public void testDeletePost_success() {
79         // mock 数据
80         PostDO postDO = randomPostDO();
81         postMapper.insert(postDO);
82         // 准备参数
83         Long id = postDO.getId();
84
85         // 调用
86         postService.deletePost(id);
87         assertNull(postMapper.selectById(id));
88     }
89
90     @Test
91     public void testValidatePost_notFoundForDelete() {
92         // 准备参数
93         Long id = randomLongId();
94
95         // 调用, 并断言异常
96         assertServiceException(() -> postService.deletePost(id), POST_NOT_FOUND);
97     }
98
99     @Test
100     public void testValidatePost_nameDuplicateForCreate() {
101         // mock 数据
102         PostDO postDO = randomPostDO();
103         postMapper.insert(postDO);// @Sql: 先插入出一条存在的数据
104         // 准备参数
105         PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class,
106             // 模拟 name 重复
107             o -> o.setName(postDO.getName()));
108         assertServiceException(() -> postService.createPost(reqVO), POST_NAME_DUPLICATE);
109     }
110
111     @Test
112     public void testValidatePost_codeDuplicateForUpdate() {
113         // mock 数据
114         PostDO postDO = randomPostDO();
115         postMapper.insert(postDO);
116         // mock 数据:稍后模拟重复它的 code
117         PostDO codePostDO = randomPostDO();
118         postMapper.insert(codePostDO);
119         // 准备参数
120         PostSaveReqVO reqVO = randomPojo(PostSaveReqVO.class, o -> {
121             // 设置更新的 ID
122             o.setId(postDO.getId());
123             // 模拟 code 重复
124             o.setCode(codePostDO.getCode());
125         });
126
127         // 调用, 并断言异常
128         assertServiceException(() -> postService.updatePost(reqVO), POST_CODE_DUPLICATE);
129     }
130
131     @Test
132     public void testGetPostPage() {
133         // mock 数据
134         PostDO postDO = randomPojo(PostDO.class, o -> {
135             o.setName("码仔");
136             o.setStatus(CommonStatusEnum.ENABLE.getStatus());
137         });
138         postMapper.insert(postDO);
139         // 测试 name 不匹配
140         postMapper.insert(cloneIgnoreId(postDO, o -> o.setName("程序员")));
141         // 测试 status 不匹配
142         postMapper.insert(cloneIgnoreId(postDO, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus())));
143         // 准备参数
144         PostPageReqVO reqVO = new PostPageReqVO();
145         reqVO.setName("码");
146         reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
147
148         // 调用
149         PageResult<PostDO> pageResult = postService.getPostPage(reqVO);
150         // 断言
151         assertEquals(1, pageResult.getTotal());
152         assertEquals(1, pageResult.getList().size());
153         assertPojoEquals(postDO, pageResult.getList().get(0));
154     }
155
156     @Test
157     public void testGetPostList() {
158         // mock 数据
159         PostDO postDO01 = randomPojo(PostDO.class);
160         postMapper.insert(postDO01);
161         // 测试 id 不匹配
162         PostDO postDO02 = randomPojo(PostDO.class);
163         postMapper.insert(postDO02);
164         // 准备参数
165         List<Long> ids = singletonList(postDO01.getId());
166
167         // 调用
168         List<PostDO> list = postService.getPostList(ids);
169         // 断言
170         assertEquals(1, list.size());
171         assertPojoEquals(postDO01, list.get(0));
172     }
173
174     @Test
175     public void testGetPostList_idsAndStatus() {
176         // mock 数据
177         PostDO postDO01 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.ENABLE.getStatus()));
178         postMapper.insert(postDO01);
179         // 测试 status 不匹配
180         PostDO postDO02 = randomPojo(PostDO.class, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()));
181         postMapper.insert(postDO02);
182         // 准备参数
183         List<Long> ids = Arrays.asList(postDO01.getId(), postDO02.getId());
184
185         // 调用
186         List<PostDO> list = postService.getPostList(ids, singletonList(CommonStatusEnum.ENABLE.getStatus()));
187         // 断言
188         assertEquals(1, list.size());
189         assertPojoEquals(postDO01, list.get(0));
190     }
191
192     @Test
193     public void testGetPost() {
194         // mock 数据
195         PostDO dbPostDO = randomPostDO();
196         postMapper.insert(dbPostDO);
197         // 准备参数
198         Long id = dbPostDO.getId();
199         // 调用
200         PostDO post = postService.getPost(id);
201         // 断言
202         assertNotNull(post);
203         assertPojoEquals(dbPostDO, post);
204     }
205
206     @Test
207     public void testValidatePostList_success() {
208         // mock 数据
209         PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.ENABLE.getStatus());
210         postMapper.insert(postDO);
211         // 准备参数
212         List<Long> ids = singletonList(postDO.getId());
213
214         // 调用,无需断言
215         postService.validatePostList(ids);
216     }
217
218     @Test
219     public void testValidatePostList_notFound() {
220         // 准备参数
221         List<Long> ids = singletonList(randomLongId());
222
223         // 调用, 并断言异常
224         assertServiceException(() -> postService.validatePostList(ids), POST_NOT_FOUND);
225     }
226
227     @Test
228     public void testValidatePostList_notEnable() {
229         // mock 数据
230         PostDO postDO = randomPostDO().setStatus(CommonStatusEnum.DISABLE.getStatus());
231         postMapper.insert(postDO);
232         // 准备参数
233         List<Long> ids = singletonList(postDO.getId());
234
235         // 调用, 并断言异常
236         assertServiceException(() -> postService.validatePostList(ids), POST_NOT_ENABLE,
237                 postDO.getName());
238     }
239
240     @SafeVarargs
241     private static PostDO randomPostDO(Consumer<PostDO>... consumers) {
242         Consumer<PostDO> consumer = (o) -> {
243             o.setStatus(randomCommonStatus()); // 保证 status 的范围
244         };
245         return randomPojo(PostDO.class, ArrayUtils.append(consumer, consumers));
246     }
247
248 }