完善 SensitiveWordServiceImpl 单元测试

This commit is contained in:
YunaiV 2023-02-02 21:03:00 +08:00
parent 9185e5d2a0
commit 458a042701
2 changed files with 44 additions and 12 deletions

View File

@ -25,6 +25,7 @@ import javax.annotation.Resource;
import java.util.*; import java.util.*;
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception; import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.filterList;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_EXISTS;
import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS; import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.SENSITIVE_WORD_NOT_EXISTS;
@ -85,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) { private void initSensitiveWordTrie(List<SensitiveWordDO> wordDOs) {
// 过滤禁用的敏感词 // 过滤禁用的敏感词
wordDOs = CollectionUtils.filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus())); wordDOs = filterList(wordDOs, word -> word.getStatus().equals(CommonStatusEnum.ENABLE.getStatus()));
// 初始化默认的 defaultSensitiveWordTrie // 初始化默认的 defaultSensitiveWordTrie
this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName)); this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName));
@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override @Override
public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) { public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) {
// 校验唯一性 // 校验唯一性
checkSensitiveWordNameUnique(null, createReqVO.getName()); validateSensitiveWordNameUnique(null, createReqVO.getName());
// 插入 // 插入
SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO); SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO);
sensitiveWordMapper.insert(sensitiveWord); sensitiveWordMapper.insert(sensitiveWord);
@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override @Override
public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) { public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) {
// 校验唯一性 // 校验唯一性
checkSensitiveWordExists(updateReqVO.getId()); validateSensitiveWordExists(updateReqVO.getId());
checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName()); validateSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
// 更新 // 更新
SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO); SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO);
sensitiveWordMapper.updateById(updateObj); sensitiveWordMapper.updateById(updateObj);
@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override @Override
public void deleteSensitiveWord(Long id) { public void deleteSensitiveWord(Long id) {
// 校验存在 // 校验存在
checkSensitiveWordExists(id); validateSensitiveWordExists(id);
// 删除 // 删除
sensitiveWordMapper.deleteById(id); sensitiveWordMapper.deleteById(id);
// 发送消息刷新缓存 // 发送消息刷新缓存
sensitiveWordProducer.sendSensitiveWordRefreshMessage(); sensitiveWordProducer.sendSensitiveWordRefreshMessage();
} }
private void checkSensitiveWordNameUnique(Long id, String name) { private void validateSensitiveWordNameUnique(Long id, String name) {
SensitiveWordDO word = sensitiveWordMapper.selectByName(name); SensitiveWordDO word = sensitiveWordMapper.selectByName(name);
if (word == null) { if (word == null) {
return; return;
@ -152,7 +155,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
} }
} }
private void checkSensitiveWordExists(Long id) { private void validateSensitiveWordExists(Long id) {
if (sensitiveWordMapper.selectById(id) == null) { if (sensitiveWordMapper.selectById(id) == null) {
throw exception(SENSITIVE_WORD_NOT_EXISTS); throw exception(SENSITIVE_WORD_NOT_EXISTS);
} }

View File

@ -16,13 +16,12 @@ import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildBetweenTime;
import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime; import static cn.iocoder.yudao.framework.common.util.date.LocalDateTimeUtils.buildTime;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId; import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.cloneIgnoreId;
import static cn.iocoder.yudao.framework.common.util.object.ObjectUtils.max;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertPojoEquals;
import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException; import static cn.iocoder.yudao.framework.test.core.util.AssertUtils.assertServiceException;
import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId; import static cn.iocoder.yudao.framework.test.core.util.RandomUtils.randomLongId;
@ -135,6 +134,36 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
assertServiceException(() -> sensitiveWordService.deleteSensitiveWord(id), SENSITIVE_WORD_NOT_EXISTS); assertServiceException(() -> sensitiveWordService.deleteSensitiveWord(id), SENSITIVE_WORD_NOT_EXISTS);
} }
@Test
public void testGetSensitiveWord() {
// mock 数据
SensitiveWordDO sensitiveWord = randomPojo(SensitiveWordDO.class);
sensitiveWordMapper.insert(sensitiveWord);
// 准备参数
Long id = sensitiveWord.getId();
// 调用
SensitiveWordDO dbSensitiveWord = sensitiveWordService.getSensitiveWord(id);
// 断言
assertPojoEquals(sensitiveWord, dbSensitiveWord);
}
@Test
public void testGetSensitiveWordList() {
// mock 数据
SensitiveWordDO sensitiveWord01 = randomPojo(SensitiveWordDO.class);
sensitiveWordMapper.insert(sensitiveWord01);
SensitiveWordDO sensitiveWord02 = randomPojo(SensitiveWordDO.class);
sensitiveWordMapper.insert(sensitiveWord02);
// 调用
List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList();
// 断言
assertEquals(2, list.size());
assertEquals(sensitiveWord01, list.get(0));
assertEquals(sensitiveWord02, list.get(1));
}
@Test @Test
public void testGetSensitiveWordPage() { public void testGetSensitiveWordPage() {
// mock 数据 // mock 数据
@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
reqVO.setName(""); reqVO.setName("");
reqVO.setTag("论坛"); reqVO.setTag("论坛");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12));
// 调用 // 调用
PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO); PageResult<SensitiveWordDO> pageResult = sensitiveWordService.getSensitiveWordPage(reqVO);
@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
} }
@Test @Test
public void testGetSensitiveWordList() { public void testGetSensitiveWordList_export() {
// mock 数据 // mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到 SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到
o.setName("笨蛋"); o.setName("笨蛋");
@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
reqVO.setName(""); reqVO.setName("");
reqVO.setTag("论坛"); reqVO.setTag("论坛");
reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus());
reqVO.setCreateTime((new LocalDateTime[]{buildTime(2022, 2, 1),buildTime(2022, 2, 12)})); reqVO.setCreateTime(buildBetweenTime(2022, 2, 1, 2022, 2, 12));
// 调用 // 调用
List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO); List<SensitiveWordDO> list = sensitiveWordService.getSensitiveWordList(reqVO);