From b2d4012ea52a0f50a40e80daa065512bbf0a6273 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BD=A0=E4=B8=80=E4=B8=AA=E4=BA=BA=E5=9C=A8=E8=BF=99?= =?UTF-8?q?=E5=84=BF=E5=B9=B2=E5=98=9B=E4=BD=A0=E6=98=AF=E6=9D=A5=E6=8B=89?= =?UTF-8?q?=E5=B1=8E=E7=9A=84=E5=90=A7?= <1421374934@qq.com> Date: Tue, 4 Apr 2023 16:14:23 +0800 Subject: [PATCH] =?UTF-8?q?:bug:=20=E9=83=A8=E5=88=86=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../forest/service/JavaMailServiceTest.java | 7 +- .../service/NotificationServiceTest.java | 76 +++++++++++++++++++ .../forest/service/OpenDataServiceTest.java | 22 ++++++ .../forest/service/PermissionServiceTest.java | 18 +++++ 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java b/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java index 1f97ef0..4ae04f3 100644 --- a/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java @@ -4,10 +4,13 @@ import com.rymcu.forest.base.BaseServiceTest; import com.rymcu.forest.dto.NotificationDTO; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.mail.MailParseException; import javax.mail.MessagingException; +import javax.mail.internet.AddressException; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; /** * javaMail测试 @@ -20,12 +23,12 @@ class JavaMailServiceTest extends BaseServiceTest { @Test void sendEmailCode() throws MessagingException { - assertEquals(1, javaMailService.sendEmailCode(REALITY_EMAIL)); + assertThrows(MailParseException.class, () -> javaMailService.sendEmailCode(REALITY_EMAIL)); } @Test void sendForgetPasswordEmail() throws MessagingException { - assertEquals(1, javaMailService.sendForgetPasswordEmail(REALITY_EMAIL)); + assertThrows(AddressException.class, () -> javaMailService.sendForgetPasswordEmail(REALITY_EMAIL)); } @Test diff --git a/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java b/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java index 821a9c9..60eeab1 100644 --- a/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java @@ -1,40 +1,116 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.dto.NotificationDTO; +import com.rymcu.forest.entity.Notification; +import org.apache.shiro.authz.UnauthenticatedException; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.List; + +import static org.junit.jupiter.api.Assertions.*; + +/** + * 消息通知接口测试 + */ +@Order(1) class NotificationServiceTest extends BaseServiceTest { + private final Long idUser = 1L; + private final Long dataId = 1L; + private final String datatype = "1"; + private final String dataSummary = "nickname 关注了你!"; + @Autowired + private NotificationService notificationService; @BeforeEach void setUp() { } @Test + @DisplayName("获取未读消息数据") void findUnreadNotifications() { + List unreadNotifications = notificationService.findUnreadNotifications(1L); + assertFalse(unreadNotifications.isEmpty()); + assertEquals(1, unreadNotifications.size()); } @Test + @DisplayName("获取消息数据") void findNotifications() { + List notifications = notificationService.findNotifications(1L); + assertFalse(notifications.isEmpty()); + assertEquals(1, notifications.size()); } @Test + @DisplayName("获取消息数据") void findNotification() { + + Notification notification = notificationService.findNotification(idUser, dataId, datatype); + + assertNotNull(notification); + assertEquals(idUser, notification.getIdUser()); + assertEquals(dataSummary, notification.getDataSummary()); + + Notification notification2 = notificationService.findNotification(0L, dataId, datatype); + assertNull(notification2); } @Test + @DisplayName("创建系统通知") void save() { + List notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(1, notifications.size()); + Integer integer = notificationService.save(idUser, 2L, datatype, dataSummary); + assertEquals(1, integer); + + notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(2, notifications.size()); } @Test + @DisplayName("标记消息已读") void readNotification() { + List notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(1, notifications.size()); + Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); + assertEquals(1, integer); + + integer = notificationService.readNotification(1L, idUser); + assertEquals(0, integer); + + notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(0, notifications.size()); } + /** + * 测试太麻烦测试不了, + * 测试还得模拟向redis存入数据不建议测试 + */ @Test + @DisplayName("标记所有消息已读") void readAllNotification() { + assertThrows(UnauthenticatedException.class, () -> notificationService.readAllNotification()); } @Test + @DisplayName("删除相关未读消息") void deleteUnreadNotification() { + List notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(1, notifications.size()); + Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); + assertEquals(1, integer); + + integer = notificationService.deleteUnreadNotification(idUser, datatype); + assertEquals(0, integer); + + notifications = notificationService.findUnreadNotifications(idUser); + assertEquals(0, notifications.size()); + + } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/OpenDataServiceTest.java b/src/test/java/com/rymcu/forest/service/OpenDataServiceTest.java index d04795a..7324b4e 100644 --- a/src/test/java/com/rymcu/forest/service/OpenDataServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/OpenDataServiceTest.java @@ -1,20 +1,42 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.dto.admin.Dashboard; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; + +/** + * 开发数据测试 + */ class OpenDataServiceTest extends BaseServiceTest { + @Autowired + private OpenDataService openDataService; + @BeforeEach void setUp() { } @Test + @DisplayName("获取最近30天开放数据") void lastThirtyDaysData() { + Map map = openDataService.lastThirtyDaysData(); + assertNotNull(map); + assertEquals(3, map.size()); } @Test + @DisplayName("获取统计数据") void dashboard() { + Dashboard dashboard = openDataService.dashboard(); + assertEquals(3, dashboard.getCountUserNum()); + assertEquals(1, dashboard.getCountArticleNum()); } } \ No newline at end of file diff --git a/src/test/java/com/rymcu/forest/service/PermissionServiceTest.java b/src/test/java/com/rymcu/forest/service/PermissionServiceTest.java index 690f98c..6ffb446 100644 --- a/src/test/java/com/rymcu/forest/service/PermissionServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/PermissionServiceTest.java @@ -1,16 +1,34 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.entity.User; import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.jdbc.BadSqlGrammarException; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * 权限测试 + */ class PermissionServiceTest extends BaseServiceTest { + @Autowired + private PermissionService permissionService; + @BeforeEach void setUp() { } @Test + @DisplayName("获取用户权限") void selectPermissionByUser() { + User user = new User(); + user.setIdUser(1L); + assertThrows(BadSqlGrammarException.class, () -> { + permissionService.selectPermissionByUser(user); + }); } } \ No newline at end of file