完善 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 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_NOT_EXISTS;
@ -85,7 +86,7 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
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
this.defaultSensitiveWordTrie = new SimpleTrie(CollectionUtils.convertList(wordDOs, SensitiveWordDO::getName));
@ -107,7 +108,8 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override
public Long createSensitiveWord(SensitiveWordCreateReqVO createReqVO) {
// 校验唯一性
checkSensitiveWordNameUnique(null, createReqVO.getName());
validateSensitiveWordNameUnique(null, createReqVO.getName());
// 插入
SensitiveWordDO sensitiveWord = SensitiveWordConvert.INSTANCE.convert(createReqVO);
sensitiveWordMapper.insert(sensitiveWord);
@ -119,8 +121,9 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override
public void updateSensitiveWord(SensitiveWordUpdateReqVO updateReqVO) {
// 校验唯一性
checkSensitiveWordExists(updateReqVO.getId());
checkSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
validateSensitiveWordExists(updateReqVO.getId());
validateSensitiveWordNameUnique(updateReqVO.getId(), updateReqVO.getName());
// 更新
SensitiveWordDO updateObj = SensitiveWordConvert.INSTANCE.convert(updateReqVO);
sensitiveWordMapper.updateById(updateObj);
@ -131,14 +134,14 @@ public class SensitiveWordServiceImpl implements SensitiveWordService {
@Override
public void deleteSensitiveWord(Long id) {
// 校验存在
checkSensitiveWordExists(id);
validateSensitiveWordExists(id);
// 删除
sensitiveWordMapper.deleteById(id);
// 发送消息刷新缓存
sensitiveWordProducer.sendSensitiveWordRefreshMessage();
}
private void checkSensitiveWordNameUnique(Long id, String name) {
private void validateSensitiveWordNameUnique(Long id, String name) {
SensitiveWordDO word = sensitiveWordMapper.selectByName(name);
if (word == null) {
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) {
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 javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Arrays;
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.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.assertServiceException;
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);
}
@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
public void testGetSensitiveWordPage() {
// mock 数据
@ -156,7 +185,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
reqVO.setName("");
reqVO.setTag("论坛");
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);
@ -167,7 +196,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
}
@Test
public void testGetSensitiveWordList() {
public void testGetSensitiveWordList_export() {
// mock 数据
SensitiveWordDO dbSensitiveWord = randomPojo(SensitiveWordDO.class, o -> { // 等会查询到
o.setName("笨蛋");
@ -187,7 +216,7 @@ public class SensitiveWordServiceImplTest extends BaseDbUnitTest {
reqVO.setName("");
reqVO.setTag("论坛");
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);