提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.system.service.notice; |
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.notice.vo.NoticePageReqVO; |
|
7 |
import com.iailab.module.system.controller.admin.notice.vo.NoticeSaveReqVO; |
|
8 |
import com.iailab.module.system.dal.dataobject.notice.NoticeDO; |
|
9 |
import com.iailab.module.system.dal.mysql.notice.NoticeMapper; |
|
10 |
import org.junit.jupiter.api.Test; |
|
11 |
import org.springframework.context.annotation.Import; |
|
12 |
|
|
13 |
import javax.annotation.Resource; |
|
14 |
|
|
15 |
import static com.iailab.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
|
16 |
import static com.iailab.framework.test.core.util.AssertUtils.assertPojoEquals; |
|
17 |
import static com.iailab.framework.test.core.util.AssertUtils.assertServiceException; |
|
18 |
import static com.iailab.framework.test.core.util.RandomUtils.randomLongId; |
|
19 |
import static com.iailab.framework.test.core.util.RandomUtils.randomPojo; |
|
20 |
import static com.iailab.module.system.enums.ErrorCodeConstants.NOTICE_NOT_FOUND; |
|
21 |
import static org.junit.jupiter.api.Assertions.*; |
|
22 |
|
|
23 |
@Import(NoticeServiceImpl.class) |
|
24 |
class NoticeServiceImplTest extends BaseDbUnitTest { |
|
25 |
|
|
26 |
@Resource |
|
27 |
private NoticeServiceImpl noticeService; |
|
28 |
|
|
29 |
@Resource |
|
30 |
private NoticeMapper noticeMapper; |
|
31 |
|
|
32 |
@Test |
|
33 |
public void testGetNoticePage_success() { |
|
34 |
// 插入前置数据 |
|
35 |
NoticeDO dbNotice = randomPojo(NoticeDO.class, o -> { |
|
36 |
o.setTitle("尼古拉斯赵四来啦!"); |
|
37 |
o.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
38 |
}); |
|
39 |
noticeMapper.insert(dbNotice); |
|
40 |
// 测试 title 不匹配 |
|
41 |
noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); |
|
42 |
// 测试 status 不匹配 |
|
43 |
noticeMapper.insert(cloneIgnoreId(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); |
|
44 |
// 准备参数 |
|
45 |
NoticePageReqVO reqVO = new NoticePageReqVO(); |
|
46 |
reqVO.setTitle("尼古拉斯赵四来啦!"); |
|
47 |
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
|
48 |
|
|
49 |
// 调用 |
|
50 |
PageResult<NoticeDO> pageResult = noticeService.getNoticePage(reqVO); |
|
51 |
// 验证查询结果经过筛选 |
|
52 |
assertEquals(1, pageResult.getTotal()); |
|
53 |
assertEquals(1, pageResult.getList().size()); |
|
54 |
assertPojoEquals(dbNotice, pageResult.getList().get(0)); |
|
55 |
} |
|
56 |
|
|
57 |
@Test |
|
58 |
public void testGetNotice_success() { |
|
59 |
// 插入前置数据 |
|
60 |
NoticeDO dbNotice = randomPojo(NoticeDO.class); |
|
61 |
noticeMapper.insert(dbNotice); |
|
62 |
|
|
63 |
// 查询 |
|
64 |
NoticeDO notice = noticeService.getNotice(dbNotice.getId()); |
|
65 |
|
|
66 |
// 验证插入与读取对象是否一致 |
|
67 |
assertNotNull(notice); |
|
68 |
assertPojoEquals(dbNotice, notice); |
|
69 |
} |
|
70 |
|
|
71 |
@Test |
|
72 |
public void testCreateNotice_success() { |
|
73 |
// 准备参数 |
|
74 |
NoticeSaveReqVO reqVO = randomPojo(NoticeSaveReqVO.class) |
|
75 |
.setId(null); // 避免 id 被赋值 |
|
76 |
|
|
77 |
// 调用 |
|
78 |
Long noticeId = noticeService.createNotice(reqVO); |
|
79 |
// 校验插入属性是否正确 |
|
80 |
assertNotNull(noticeId); |
|
81 |
NoticeDO notice = noticeMapper.selectById(noticeId); |
|
82 |
assertPojoEquals(reqVO, notice, "id"); |
|
83 |
} |
|
84 |
|
|
85 |
@Test |
|
86 |
public void testUpdateNotice_success() { |
|
87 |
// 插入前置数据 |
|
88 |
NoticeDO dbNoticeDO = randomPojo(NoticeDO.class); |
|
89 |
noticeMapper.insert(dbNoticeDO); |
|
90 |
|
|
91 |
// 准备更新参数 |
|
92 |
NoticeSaveReqVO reqVO = randomPojo(NoticeSaveReqVO.class, o -> o.setId(dbNoticeDO.getId())); |
|
93 |
|
|
94 |
// 更新 |
|
95 |
noticeService.updateNotice(reqVO); |
|
96 |
// 检验是否更新成功 |
|
97 |
NoticeDO notice = noticeMapper.selectById(reqVO.getId()); |
|
98 |
assertPojoEquals(reqVO, notice); |
|
99 |
} |
|
100 |
|
|
101 |
@Test |
|
102 |
public void testDeleteNotice_success() { |
|
103 |
// 插入前置数据 |
|
104 |
NoticeDO dbNotice = randomPojo(NoticeDO.class); |
|
105 |
noticeMapper.insert(dbNotice); |
|
106 |
|
|
107 |
// 删除 |
|
108 |
noticeService.deleteNotice(dbNotice.getId()); |
|
109 |
|
|
110 |
// 检查是否删除成功 |
|
111 |
assertNull(noticeMapper.selectById(dbNotice.getId())); |
|
112 |
} |
|
113 |
|
|
114 |
@Test |
|
115 |
public void testValidateNoticeExists_success() { |
|
116 |
// 插入前置数据 |
|
117 |
NoticeDO dbNotice = randomPojo(NoticeDO.class); |
|
118 |
noticeMapper.insert(dbNotice); |
|
119 |
|
|
120 |
// 成功调用 |
|
121 |
noticeService.validateNoticeExists(dbNotice.getId()); |
|
122 |
} |
|
123 |
|
|
124 |
@Test |
|
125 |
public void testValidateNoticeExists_noExists() { |
|
126 |
assertServiceException(() -> |
|
127 |
noticeService.validateNoticeExists(randomLongId()), NOTICE_NOT_FOUND); |
|
128 |
} |
|
129 |
|
|
130 |
} |