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,6 +25,7 @@ import javax.annotation.Resource; | ||
| 25 | import java.util.*; | 25 | import java.util.*; |
| 26 | 26 | ||
| 27 | import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; | 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 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS; | 29 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS; |
| 29 | import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS; | 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,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | ||
| 85 | 86 | ||
| 86 | private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) { | 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 | // 初始化默认的 defaultSensitiveWordTrie | 91 | // 初始化默认的 defaultSensitiveWordTrie |
| 91 | this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName)); | 92 | this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName)); |
| @@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | @@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | ||
| 107 | @Override | 108 | @Override |
| 108 | public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) { | 109 | public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) { |
| 109 | // 校验唯一性 | 110 | // 校验唯一性 |
| 110 | - checkSensitiveWordNameUnique(null, createReqVO.getName()); | 111 | + validateSensitiveWordNameUnique(null, createReqVO.getName()); |
| 112 | + | ||
| 111 | // 插入 | 113 | // 插入 |
| 112 | SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO); | 114 | SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO); |
| 113 | sensitiveWordMapper.insert(sensitiveWord); | 115 | sensitiveWordMapper.insert(sensitiveWord); |
| @@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | @@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | ||
| 119 | @Override | 121 | @Override |
| 120 | public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) { | 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 | SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO); | 128 | SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO); |
| 126 | sensitiveWordMapper.updateById(updateObj); | 129 | sensitiveWordMapper.updateById(updateObj); |
| @@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | @@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | ||
| 131 | @Override | 134 | @Override |
| 132 | public void deleteSensitiveWord(Long id) { | 135 | public void deleteSensitiveWord(Long id) { |
| 133 | // 校验存在 | 136 | // 校验存在 |
| 134 | - checkSensitiveWordExists(id); | 137 | + validateSensitiveWordExists(id); |
| 135 | // 删除 | 138 | // 删除 |
| 136 | sensitiveWordMapper.deleteById(id); | 139 | sensitiveWordMapper.deleteById(id); |
| 137 | // 发送消息,刷新缓存 | 140 | // 发送消息,刷新缓存 |
| 138 | sensitiveWordProducer.sendSensitiveWordRefreshMessage(); | 141 | sensitiveWordProducer.sendSensitiveWordRefreshMessage(); |
| 139 | } | 142 | } |
| 140 | 143 | ||
| 141 | - private void checkSensitiveWordNameUnique(Long id, String name) { | 144 | + private void validateSensitiveWordNameUnique(Long id, String name) { |
| 142 | SensitiveWordDO word = sensitiveWordMapper.selectByName(name); | 145 | SensitiveWordDO word = sensitiveWordMapper.selectByName(name); |
| 143 | if (word == null) { | 146 | if (word == null) { |
| 144 | return; | 147 | return; |
| @@ -152,7 +155,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService { | @@ -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 | if (sensitiveWordMapper.selectById(id) == null) { | 159 | if (sensitiveWordMapper.selectById(id) == null) { |
| 157 | throw exception(SENSITIVE_WORD_NOT_EXISTS); | 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,13 +16,12 @@ import org.springframework.boot.test.mock.mockito.MockBean; | ||
| 16 | import org.springframework.context.annotation.Import; | 16 | import org.springframework.context.annotation.Import; |
| 17 | 17 | ||
| 18 | import javax.annotation.Resource; | 18 | import javax.annotation.Resource; |
| 19 | -import java.time.LocalDateTime; | ||
| 20 | import java.util.Arrays; | 19 | import java.util.Arrays; |
| 21 | import java.util.List; | 20 | import java.util.List; |
| 22 | 21 | ||
| 22 | +import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime; | ||
| 23 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; | 23 | import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; |
| 24 | import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; | 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 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; | 25 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; |
| 27 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; | 26 | import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; |
| 28 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; | 27 | import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; |
| @@ -136,6 +135,36 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | @@ -136,6 +135,36 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | ||
| 136 | } | 135 | } |
| 137 | 136 | ||
| 138 | @Test | 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 | public void testGetSensitiveWordPage() { | 168 | public void testGetSensitiveWordPage() { |
| 140 | // mock 数据 | 169 | // mock 数据 |
| 141 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 | 170 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| @@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | @@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | ||
| 156 | reqVO.setName("笨"); | 185 | reqVO.setName("笨"); |
| 157 | reqVO.setTag("论坛"); | 186 | reqVO.setTag("论坛"); |
| 158 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | 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 | PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO); | 191 | PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO); |
| @@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | @@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | ||
| 167 | } | 196 | } |
| 168 | 197 | ||
| 169 | @Test | 198 | @Test |
| 170 | - public void testGetSensitiveWordList() { | 199 | + public void testGetSensitiveWordList_export() { |
| 171 | // mock 数据 | 200 | // mock 数据 |
| 172 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 | 201 | SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 |
| 173 | o.setName("笨蛋"); | 202 | o.setName("笨蛋"); |
| @@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | @@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest { | ||
| 187 | reqVO.setName("笨"); | 216 | reqVO.setName("笨"); |
| 188 | reqVO.setTag("论坛"); | 217 | reqVO.setTag("论坛"); |
| 189 | reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); | 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 | List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO); | 222 | List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO); |