Commit d7f86afd6cfb51264085b5164d904165c5c29158
1 parent
96e8fa42
完善 SensitiveWordServiceImpl 单元测试
Showing
2 changed files
with
44 additions
and
12 deletions
yudao-module-system/yudao-module-system-biz/src/main/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImpl.java
| ... | ... | @@ -25,6 +25,7 @@ import javax.annotation.Resource; |
| 25 | 25 | import java.util.*; |
| 26 | 26 | |
| 27 | 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; |
| 28 | +import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList; | |
| 28 | 29 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS; |
| 29 | 30 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS; |
| 30 | 31 | |
| ... | ... | @@ -85,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 85 | 86 | |
| 86 | 87 | private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) { |
| 87 | 88 | // 过滤禁用的敏感词 |
| 88 | - wordDOs = CollectionUtils.filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())); | |
| 89 | + wordDOs = filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())); | |
| 89 | 90 | |
| 90 | 91 | // 初始化默认的 defaultSensitiveWordTrie |
| 91 | 92 | this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName)); |
| ... | ... | @@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 107 | 108 | @Override |
| 108 | 109 | public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) { |
| 109 | 110 | // 校验唯一性 |
| 110 | - checkSensitiveWordNameUnique(null, createReqVO.getName()); | |
| 111 | + validateSensitiveWordNameUnique(null, createReqVO.getName()); | |
| 112 | + | |
| 111 | 113 | // 插入 |
| 112 | 114 | SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO); |
| 113 | 115 | sensitiveWordMapper.insert(sensitiveWord); |
| ... | ... | @@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 119 | 121 | @Override |
| 120 | 122 | public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) { |
| 121 | 123 | // 校验唯一性 |
| 122 | - checkSensitiveWordExists(updateReqVO.getId()); | |
| 123 | - checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName()); | |
| 124 | + validateSensitiveWordExists(updateReqVO.getId()); | |
| 125 | + validateSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName()); | |
| 126 | + | |
| 124 | 127 | // 更新 |
| 125 | 128 | SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO); |
| 126 | 129 | sensitiveWordMapper.updateById(updateObj); |
| ... | ... | @@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 131 | 134 | @Override |
| 132 | 135 | public void deleteSensitiveWord(Long id) { |
| 133 | 136 | // 校验存在 |
| 134 | - checkSensitiveWordExists(id); | |
| 137 | + validateSensitiveWordExists(id); | |
| 135 | 138 | // 删除 |
| 136 | 139 | sensitiveWordMapper.deleteById(id); |
| 137 | 140 | // 发送消息,刷新缓存 |
| 138 | 141 | sensitiveWordProducer.sendSensitiveWordRefreshMessage(); |
| 139 | 142 | } |
| 140 | 143 | |
| 141 | - private void checkSensitiveWordNameUnique(Long id, String name) { | |
| 144 | + private void validateSensitiveWordNameUnique(Long id, String name) { | |
| 142 | 145 | SensitiveWordDO word = sensitiveWordMapper.selectByName(name); |
| 143 | 146 | if (word == null) { |
| 144 | 147 | return; |
| ... | ... | @@ -152,7 +155,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { |
| 152 | 155 | } |
| 153 | 156 | } |
| 154 | 157 | |
| 155 | - private void checkSensitiveWordExists(Long id) { | |
| 158 | + private void validateSensitiveWordExists(Long id) { | |
| 156 | 159 | if (sensitiveWordMapper.selectById(id) == null) { |
| 157 | 160 | throw exception(SENSITIVE_WORD_NOT_EXISTS); |
| 158 | 161 | } | ... | ... |
yudao-module-system/yudao-module-system-biz/src/test/java/cn/iocoder/yudao/module/system/service/sensitiveword/SensitiveWordServiceImplTest.java
| ... | ... | @@ -16,13 +16,12 @@ import org.springframework.boot.test.mock.mockito.MockBean; |
| 16 | 16 | import org.springframework.context.annotation.Import; |
| 17 | 17 | |
| 18 | 18 | import javax.annotation.Resource; |
| 19 | -import java.time.LocalDateTime; | |
| 20 | 19 | import java.util.Arrays; |
| 21 | 20 | import java.util.List; |
| 22 | 21 | |
| 22 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | |
| 23 | 23 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 24 | 24 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; |
| 25 | -import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max; | |
| 26 | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| ... | ... | @@ -136,6 +135,36 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 136 | 135 | } |
| 137 | 136 | |
| 138 | 137 | @Test |
| 138 | + public void testGetSensitiveWord() { | |
| 139 | + // mock 数据 | |
| 140 | + SensitiveWordDO sensitiveWord = randomPojo(SensitiveWordDO.class); | |
| 141 | + sensitiveWordMapper.insert(sensitiveWord); | |
| 142 | + // 准备参数 | |
| 143 | + Long id = sensitiveWord.getId(); | |
| 144 | + | |
| 145 | + // 调用 | |
| 146 | + SensitiveWordDO dbSensitiveWord = sensitiveWordService.getSensitiveWord(id); | |
| 147 | + // 断言 | |
| 148 | + assertPojoEquals(sensitiveWord, dbSensitiveWord); | |
| 149 | + } | |
| 150 | + | |
| 151 | + @Test | |
| 152 | + public void testGetSensitiveWordList() { | |
| 153 | + // mock 数据 | |
| 154 | + SensitiveWordDO sensitiveWord01 = randomPojo(SensitiveWordDO.class); | |
| 155 | + sensitiveWordMapper.insert(sensitiveWord01); | |
| 156 | + SensitiveWordDO sensitiveWord02 = randomPojo(SensitiveWordDO.class); | |
| 157 | + sensitiveWordMapper.insert(sensitiveWord02); | |
| 158 | + | |
| 159 | + // 调用 | |
| 160 | + List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(); | |
| 161 | + // 断言 | |
| 162 | + assertEquals(2, list.size()); | |
| 163 | + assertEquals(sensitiveWord01, list.get(0)); | |
| 164 | + assertEquals(sensitiveWord02, list.get(1)); | |
| 165 | + } | |
| 166 | + | |
| 167 | + @Test | |
| 139 | 168 | public void testGetSensitiveWordPage() { |
| 140 | 169 | // mock 数据 |
| 141 | 170 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| ... | ... | @@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 156 | 185 | reqVO.setName("笨"); |
| 157 | 186 | reqVO.setTag("论坛"); |
| 158 | 187 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 159 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 188 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 160 | 189 | |
| 161 | 190 | // 调用 |
| 162 | 191 | PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO); |
| ... | ... | @@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 167 | 196 | } |
| 168 | 197 | |
| 169 | 198 | @Test |
| 170 | - public void testGetSensitiveWordList() { | |
| 199 | + public void testGetSensitiveWordList_export() { | |
| 171 | 200 | // mock 数据 |
| 172 | 201 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| 173 | 202 | o.setName("笨蛋"); |
| ... | ... | @@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { |
| 187 | 216 | reqVO.setName("笨"); |
| 188 | 217 | reqVO.setTag("论坛"); |
| 189 | 218 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); |
| 190 | - reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); | |
| 219 | + reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12)); | |
| 191 | 220 | |
| 192 | 221 | // 调用 |
| 193 | 222 | List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO); | ... | ... |