From 4438b6d4aba6a9cf7b8643a42881e41e89902b1b Mon Sep 17 00:00:00 2001 From: budliang Date: Sat, 6 Mar 2021 19:57:39 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E5=85=AC=E5=91=8A=E6=A8=A1=E5=9D=97=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notice/SysNoticeServiceImplTest.java | 111 ++++++++++++++++++ src/test/resources/sql/create_tables.sql | 14 +++ 2 files changed, 125 insertions(+) create mode 100644 src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java new file mode 100644 index 000000000..065c07e37 --- /dev/null +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java @@ -0,0 +1,111 @@ +package cn.iocoder.dashboard.modules.system.service.notice; + +import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.common.enums.CommonStatusEnum; +import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeUpdateReqVO; +import cn.iocoder.dashboard.modules.system.dal.dataobject.notice.SysNoticeDO; +import cn.iocoder.dashboard.modules.system.dal.mysql.notice.SysNoticeMapper; +import cn.iocoder.dashboard.modules.system.enums.notice.SysNoticeTypeEnum; +import org.junit.jupiter.api.Test; + +import javax.annotation.Resource; + +import java.util.function.Consumer; + +import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; + + +class SysNoticeServiceImplTest extends BaseSpringBootUnitTest { + + @Resource + private SysNoticeService sysNoticeService; + + @Resource + private SysNoticeMapper sysNoticeMapper; + + @Test + void testPageNotices_success() { + // todo: 待更新 + } + + @Test + void testGetNotice_success() { + // 插入前置数据 + int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + + // 查询 + assertNotNull(sysNoticeService.getNotice(new Long(noticeId))); + } + + @Test + void testCreateNotice_success() { + // 准备参数 + SysNoticeCreateReqVO reqVO = randomSysNoticeCreateReqVO(); + + // 校验插入是否成功 + Long noticeId = sysNoticeService.createNotice(reqVO); + assertNotNull(noticeId); + + // 校验插入属性是否正确 + SysNoticeDO notice = sysNoticeMapper.selectById(noticeId); + assertPojoEquals(reqVO, notice); + } + + @Test + void testUpdateNotice_success() { + // 插入前置数据 + int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + + // 准备更新参数 + SysNoticeUpdateReqVO reqVO = randomSysNoticeUpdateReqVO(o -> o.setId(new Long(noticeId))); + + // 更新 + sysNoticeService.updateNotice(reqVO); + + // 检验是否更新成功 + SysNoticeDO notice = sysNoticeMapper.selectById(noticeId); + assertPojoEquals(reqVO, notice); + } + + @Test + void testDeleteNotice_success() { + // 插入前置数据 + int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + + // 删除 + sysNoticeService.deleteNotice(new Long(noticeId)); + + // 检查是否删除成功 + assertNull(sysNoticeMapper.selectById(noticeId)); + } + + @SafeVarargs + private static SysNoticeDO randomSysNoticeDO(Consumer... consumers) { + SysNoticeDO notice = randomPojo(SysNoticeDO.class, consumers); + notice.setType(randomEle(SysNoticeTypeEnum.values()).getType()); + notice.setStatus(CommonStatusEnum.ENABLE.getStatus()); + return notice; + } + + @SafeVarargs + private static SysNoticeUpdateReqVO randomSysNoticeUpdateReqVO(Consumer... consumers) { + SysNoticeUpdateReqVO reqVO = randomPojo(SysNoticeUpdateReqVO.class, consumers); + reqVO.setType(randomEle(SysNoticeTypeEnum.values()).getType()); + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + return reqVO; + } + + private static SysNoticeCreateReqVO randomSysNoticeCreateReqVO() { + SysNoticeCreateReqVO reqVO = randomPojo(SysNoticeCreateReqVO.class); + reqVO.setType(randomEle(SysNoticeTypeEnum.values()).getType()); + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + return reqVO; + } + + +} \ No newline at end of file diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 183554519..611e38d99 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -100,3 +100,17 @@ CREATE TABLE IF NOT EXISTS "sys_menu" ( "deleted" bit NOT NULL DEFAULT FALSE, PRIMARY KEY ("id") ) COMMENT '菜单权限表'; + +CREATE TABLE IF NOT EXISTS "sys_notice" ( + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "title" varchar(50) NOT NULL, + "content" text NOT NULL, + "notice_type" tinyint(4) NOT NULL, + "status" tinyint(4) NOT NULL DEFAULT '0', + "create_by" varchar(64) DEFAULT '', + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP, + "update_by" varchar(64) DEFAULT '', + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, + "deleted" bit NOT NULL DEFAULT '0', + PRIMARY KEY ("id") +) COMMENT='通知公告表'; From ccba513f3bcfb4ec64029c810e2641f7795f4431 Mon Sep 17 00:00:00 2001 From: budliang Date: Wed, 10 Mar 2021 00:43:15 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=9B=B4=E6=96=B0=E9=80=9A=E7=9F=A5?= =?UTF-8?q?=E5=85=AC=E5=91=8A=E6=A8=A1=E5=9D=97=E5=8D=95=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../notice/impl/SysNoticeServiceImpl.java | 4 +- .../notice/SysNoticeServiceImplTest.java | 99 ++++++++++++++----- src/test/resources/sql/create_tables.sql | 15 +++ 3 files changed, 94 insertions(+), 24 deletions(-) diff --git a/src/main/java/cn/iocoder/dashboard/modules/system/service/notice/impl/SysNoticeServiceImpl.java b/src/main/java/cn/iocoder/dashboard/modules/system/service/notice/impl/SysNoticeServiceImpl.java index 4f157269d..b2e083f78 100644 --- a/src/main/java/cn/iocoder/dashboard/modules/system/service/notice/impl/SysNoticeServiceImpl.java +++ b/src/main/java/cn/iocoder/dashboard/modules/system/service/notice/impl/SysNoticeServiceImpl.java @@ -9,6 +9,7 @@ import cn.iocoder.dashboard.modules.system.convert.notice.SysNoticeConvert; import cn.iocoder.dashboard.modules.system.dal.mysql.notice.SysNoticeMapper; import cn.iocoder.dashboard.modules.system.dal.dataobject.notice.SysNoticeDO; import cn.iocoder.dashboard.modules.system.service.notice.SysNoticeService; +import com.google.common.annotations.VisibleForTesting; import org.springframework.stereotype.Service; import javax.annotation.Resource; @@ -60,7 +61,8 @@ public class SysNoticeServiceImpl implements SysNoticeService { noticeMapper.deleteById(id); } - private void checkNoticeExists(Long id) { + @VisibleForTesting + public void checkNoticeExists(Long id) { if (id == null) { return; } diff --git a/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java b/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java index 065c07e37..801ecea8c 100644 --- a/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java +++ b/src/test/java/cn/iocoder/dashboard/modules/system/service/notice/SysNoticeServiceImplTest.java @@ -1,49 +1,85 @@ package cn.iocoder.dashboard.modules.system.service.notice; -import cn.iocoder.dashboard.BaseSpringBootUnitTest; +import cn.iocoder.dashboard.BaseDbUnitTest; import cn.iocoder.dashboard.common.enums.CommonStatusEnum; +import cn.iocoder.dashboard.common.pojo.PageResult; import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeCreateReqVO; +import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticePageReqVO; import cn.iocoder.dashboard.modules.system.controller.notice.vo.SysNoticeUpdateReqVO; import cn.iocoder.dashboard.modules.system.dal.dataobject.notice.SysNoticeDO; import cn.iocoder.dashboard.modules.system.dal.mysql.notice.SysNoticeMapper; import cn.iocoder.dashboard.modules.system.enums.notice.SysNoticeTypeEnum; +import cn.iocoder.dashboard.modules.system.service.notice.impl.SysNoticeServiceImpl; +import cn.iocoder.dashboard.util.object.ObjectUtils; import org.junit.jupiter.api.Test; +import org.springframework.context.annotation.Import; import javax.annotation.Resource; import java.util.function.Consumer; import static cn.hutool.core.util.RandomUtil.randomEle; +import static cn.iocoder.dashboard.modules.system.enums.SysErrorCodeConstants.NOTICE_NOT_FOUND; import static cn.iocoder.dashboard.util.AssertUtils.assertPojoEquals; +import static cn.iocoder.dashboard.util.AssertUtils.assertServiceException; +import static cn.iocoder.dashboard.util.RandomUtils.randomLongId; import static cn.iocoder.dashboard.util.RandomUtils.randomPojo; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.*; - -class SysNoticeServiceImplTest extends BaseSpringBootUnitTest { +@Import(SysNoticeServiceImpl.class) +class SysNoticeServiceImplTest extends BaseDbUnitTest { @Resource - private SysNoticeService sysNoticeService; + private SysNoticeServiceImpl sysNoticeService; @Resource private SysNoticeMapper sysNoticeMapper; @Test - void testPageNotices_success() { - // todo: 待更新 - } - - @Test - void testGetNotice_success() { + public void testPageNotices_success() { // 插入前置数据 - int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + SysNoticeDO dbNotice = randomPojo(SysNoticeDO.class, o -> { + o.setTitle("尼古拉斯赵四来啦!"); + o.setStatus(CommonStatusEnum.ENABLE.getStatus()); + o.setType(randomEle(SysNoticeTypeEnum.values()).getType()); + }); + sysNoticeMapper.insert(dbNotice); + + // 测试 title 不匹配 + sysNoticeMapper.insert(ObjectUtils.clone(dbNotice, o -> o.setTitle("尼古拉斯凯奇也来啦!"))); + // 测试 status 不匹配 + sysNoticeMapper.insert(ObjectUtils.clone(dbNotice, o -> o.setStatus(CommonStatusEnum.DISABLE.getStatus()))); + // 查询 - assertNotNull(sysNoticeService.getNotice(new Long(noticeId))); + SysNoticePageReqVO reqVO = new SysNoticePageReqVO(); + reqVO.setTitle("尼古拉斯赵四来啦!"); + reqVO.setStatus(CommonStatusEnum.ENABLE.getStatus()); + PageResult pageResult = sysNoticeService.pageNotices(reqVO); + + // 验证查询结果经过筛选 + assertEquals(1, pageResult.getTotal()); + assertEquals(1, pageResult.getList().size()); + assertPojoEquals(dbNotice, pageResult.getList().get(0)); + } @Test - void testCreateNotice_success() { + public void testGetNotice_success() { + // 插入前置数据 + SysNoticeDO dbNotice = randomSysNoticeDO(); + sysNoticeMapper.insert(dbNotice); + + // 查询 + SysNoticeDO notice = sysNoticeService.getNotice(dbNotice.getId()); + + // 验证插入与读取对象是否一致 + assertNotNull(notice); + assertPojoEquals(dbNotice, notice); + } + + @Test + public void testCreateNotice_success() { // 准备参数 SysNoticeCreateReqVO reqVO = randomSysNoticeCreateReqVO(); @@ -57,31 +93,48 @@ class SysNoticeServiceImplTest extends BaseSpringBootUnitTest { } @Test - void testUpdateNotice_success() { + public void testUpdateNotice_success() { // 插入前置数据 - int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + SysNoticeDO dbNoticeDO = randomSysNoticeDO(); + sysNoticeMapper.insert(dbNoticeDO); // 准备更新参数 - SysNoticeUpdateReqVO reqVO = randomSysNoticeUpdateReqVO(o -> o.setId(new Long(noticeId))); + SysNoticeUpdateReqVO reqVO = randomSysNoticeUpdateReqVO(o -> o.setId(dbNoticeDO.getId())); // 更新 sysNoticeService.updateNotice(reqVO); // 检验是否更新成功 - SysNoticeDO notice = sysNoticeMapper.selectById(noticeId); + SysNoticeDO notice = sysNoticeMapper.selectById(reqVO.getId()); assertPojoEquals(reqVO, notice); } @Test - void testDeleteNotice_success() { + public void testDeleteNotice_success() { // 插入前置数据 - int noticeId = sysNoticeMapper.insert(randomSysNoticeDO()); + SysNoticeDO dbNotice = randomSysNoticeDO(); + sysNoticeMapper.insert(dbNotice); // 删除 - sysNoticeService.deleteNotice(new Long(noticeId)); + sysNoticeService.deleteNotice(dbNotice.getId()); // 检查是否删除成功 - assertNull(sysNoticeMapper.selectById(noticeId)); + assertNull(sysNoticeMapper.selectById(dbNotice.getId())); + } + + @Test + public void checkNoticeExists_success() { + // 插入前置数据 + SysNoticeDO dbNotice = randomSysNoticeDO(); + sysNoticeMapper.insert(dbNotice); + + // 成功调用 + sysNoticeService.checkNoticeExists(dbNotice.getId()); + } + + @Test + public void checkNoticeExists_noExists() { + assertServiceException(() -> sysNoticeService.checkNoticeExists(randomLongId()), NOTICE_NOT_FOUND); } @SafeVarargs diff --git a/src/test/resources/sql/create_tables.sql b/src/test/resources/sql/create_tables.sql index 8675ef978..2bfced6ff 100644 --- a/src/test/resources/sql/create_tables.sql +++ b/src/test/resources/sql/create_tables.sql @@ -129,3 +129,18 @@ CREATE TABLE `sys_user_session` ( "deleted" bit NOT NULL DEFAULT FALSE, PRIMARY KEY (`id`) ) COMMENT '用户在线 Session'; + +CREATE TABLE IF NOT EXISTS "sys_notice" ( + "id" bigint NOT NULL GENERATED BY DEFAULT AS IDENTITY, + "title" varchar(50) NOT NULL COMMENT '公告标题', + "content" text NOT NULL COMMENT '公告内容', + "notice_type" tinyint NOT NULL COMMENT '公告类型(1通知 2公告)', + "status" tinyint NOT NULL DEFAULT '0' COMMENT '公告状态(0正常 1关闭)', + "creator" varchar(64) DEFAULT '' COMMENT '创建者', + "create_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间', + "updater" varchar(64) DEFAULT '' COMMENT '更新者', + "update_time" datetime NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间', + "deleted" bit NOT NULL DEFAULT 0 COMMENT '是否删除', + PRIMARY KEY("id") +) COMMENT '通知公告表'; +