From 5f369cf34a1b4b09f021e901e460121f1d73cdff Mon Sep 17 00:00:00 2001 From: ronger Date: Mon, 24 Apr 2023 08:39:09 +0800 Subject: [PATCH] =?UTF-8?q?:technologist:=20=E5=AE=8C=E5=96=84=E5=8D=95?= =?UTF-8?q?=E5=85=83=E6=B5=8B=E8=AF=95,=E4=BF=AE=E6=AD=A3=E9=94=99?= =?UTF-8?q?=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../rymcu/forest/handler/CommentHandler.java | 2 +- .../forest/service/NotificationService.java | 2 +- .../service/impl/NotificationServiceImpl.java | 4 +- .../notification/NotificationController.java | 4 +- .../forest/service/FollowServiceTest.java | 3 +- .../forest/service/JavaMailServiceTest.java | 6 +- .../service/NotificationServiceTest.java | 55 ++++++++----------- 7 files changed, 35 insertions(+), 41 deletions(-) diff --git a/src/main/java/com/rymcu/forest/handler/CommentHandler.java b/src/main/java/com/rymcu/forest/handler/CommentHandler.java index a558dd1..1ad1661 100644 --- a/src/main/java/com/rymcu/forest/handler/CommentHandler.java +++ b/src/main/java/com/rymcu/forest/handler/CommentHandler.java @@ -34,7 +34,7 @@ public class CommentHandler { public void processCommentCreatedEvent(CommentEvent commentEvent) throws InterruptedException { log.info(String.format("开始执行评论发布事件:[%s]", JSON.toJSONString(commentEvent))); String commentContent = commentEvent.getContent(); - Integer length = commentContent.length(); + int length = commentContent.length(); if (length > MAX_PREVIEW) { length = 200; } diff --git a/src/main/java/com/rymcu/forest/service/NotificationService.java b/src/main/java/com/rymcu/forest/service/NotificationService.java index cd517f9..90e3de0 100644 --- a/src/main/java/com/rymcu/forest/service/NotificationService.java +++ b/src/main/java/com/rymcu/forest/service/NotificationService.java @@ -63,7 +63,7 @@ public interface NotificationService extends Service { * * @return */ - Integer readAllNotification(); + Integer readAllNotification(Long idUser); /** * 删除相关未读消息 diff --git a/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java b/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java index 2711ea2..f7edf90 100644 --- a/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java +++ b/src/main/java/com/rymcu/forest/service/impl/NotificationServiceImpl.java @@ -73,8 +73,8 @@ public class NotificationServiceImpl extends AbstractService imple } @Override - public Integer readAllNotification() { - return notificationMapper.readAllNotification(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser()); + public Integer readAllNotification(Long idUser) { + return notificationMapper.readAllNotification(idUser); } @Override diff --git a/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java b/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java index 349a15c..016685a 100644 --- a/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java +++ b/src/main/java/com/rymcu/forest/web/api/notification/NotificationController.java @@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*; import javax.annotation.Resource; import java.util.List; +import java.util.Objects; /** * 消息通知 @@ -56,7 +57,8 @@ public class NotificationController { @PutMapping("/read-all") public GlobalResult readAll() { - Integer result = notificationService.readAllNotification(); + Long idUser = UserUtils.getCurrentUserByToken().getIdUser(); + Integer result = notificationService.readAllNotification(idUser); if (result == 0) { return GlobalResultGenerator.genErrorResult("标记已读失败"); } diff --git a/src/test/java/com/rymcu/forest/service/FollowServiceTest.java b/src/test/java/com/rymcu/forest/service/FollowServiceTest.java index a5be386..15850a0 100644 --- a/src/test/java/com/rymcu/forest/service/FollowServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/FollowServiceTest.java @@ -24,7 +24,6 @@ class FollowServiceTest extends BaseServiceTest { { follow = new Follow(); follow.setFollowerId(idUser); - follow.setFollowingType(followingType); follow.setFollowingId(followingId); @@ -77,4 +76,4 @@ class FollowServiceTest extends BaseServiceTest { List list = followService.findUserFollowingsByUser(userDTO); assertTrue(list.isEmpty()); } -} \ No newline at end of file +} diff --git a/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java b/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java index 4ae04f3..ce358ae 100644 --- a/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/JavaMailServiceTest.java @@ -23,12 +23,12 @@ class JavaMailServiceTest extends BaseServiceTest { @Test void sendEmailCode() throws MessagingException { - assertThrows(MailParseException.class, () -> javaMailService.sendEmailCode(REALITY_EMAIL)); + assertEquals(1, javaMailService.sendEmailCode(REALITY_EMAIL)); } @Test void sendForgetPasswordEmail() throws MessagingException { - assertThrows(AddressException.class, () -> javaMailService.sendForgetPasswordEmail(REALITY_EMAIL)); + assertEquals(1, javaMailService.sendForgetPasswordEmail(REALITY_EMAIL)); } @Test @@ -36,4 +36,4 @@ class JavaMailServiceTest extends BaseServiceTest { assertEquals(0, javaMailService.sendNotification(new NotificationDTO())); } -} \ No newline at end of file +} diff --git a/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java b/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java index 60eeab1..772c2ae 100644 --- a/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java +++ b/src/test/java/com/rymcu/forest/service/NotificationServiceTest.java @@ -1,9 +1,9 @@ package com.rymcu.forest.service; import com.rymcu.forest.base.BaseServiceTest; +import com.rymcu.forest.core.constant.NotificationConstant; 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; @@ -19,72 +19,66 @@ import static org.junit.jupiter.api.Assertions.*; */ @Order(1) class NotificationServiceTest extends BaseServiceTest { - private final Long idUser = 1L; + private final Long idUser = 2L; private final Long dataId = 1L; - private final String datatype = "1"; private final String dataSummary = "nickname 关注了你!"; @Autowired private NotificationService notificationService; @BeforeEach void setUp() { + save(); } @Test @DisplayName("获取未读消息数据") void findUnreadNotifications() { - List unreadNotifications = notificationService.findUnreadNotifications(1L); + List unreadNotifications = notificationService.findUnreadNotifications(idUser); assertFalse(unreadNotifications.isEmpty()); - assertEquals(1, unreadNotifications.size()); } @Test @DisplayName("获取消息数据") void findNotifications() { - List notifications = notificationService.findNotifications(1L); - assertFalse(notifications.isEmpty()); - assertEquals(1, notifications.size()); + List notifications = notificationService.findNotifications(idUser); + assertNotNull(notifications); } @Test @DisplayName("获取消息数据") void findNotification() { - - Notification notification = notificationService.findNotification(idUser, dataId, datatype); - + Notification notification = notificationService.findNotification(idUser, dataId, NotificationConstant.Follow); assertNotNull(notification); assertEquals(idUser, notification.getIdUser()); assertEquals(dataSummary, notification.getDataSummary()); - Notification notification2 = notificationService.findNotification(0L, dataId, datatype); + Notification notification2 = notificationService.findNotification(0L, dataId, NotificationConstant.Follow); assertNull(notification2); } - @Test @DisplayName("创建系统通知") void save() { List notifications = notificationService.findUnreadNotifications(idUser); - assertEquals(1, notifications.size()); - Integer integer = notificationService.save(idUser, 2L, datatype, dataSummary); + int size = notifications.size(); + Integer integer = notificationService.save(idUser, dataId, NotificationConstant.Follow, dataSummary); assertEquals(1, integer); - notifications = notificationService.findUnreadNotifications(idUser); - assertEquals(2, notifications.size()); + assertEquals(size + 1, notifications.size()); } @Test @DisplayName("标记消息已读") void readNotification() { List notifications = notificationService.findUnreadNotifications(idUser); - assertEquals(1, notifications.size()); - Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); - assertEquals(1, integer); + int size = notifications.size(); +// Integer integer = notificationService.deleteUnreadNotification(idUser, NotificationConstant.Follow); +// assertEquals(1, integer); - integer = notificationService.readNotification(1L, idUser); - assertEquals(0, integer); + Integer integer = notificationService.readNotification(notifications.get(0).getIdNotification(), idUser); + assertNotEquals(0, integer); notifications = notificationService.findUnreadNotifications(idUser); - assertEquals(0, notifications.size()); + assertEquals(size - 1, notifications.size()); } /** @@ -94,23 +88,22 @@ class NotificationServiceTest extends BaseServiceTest { @Test @DisplayName("标记所有消息已读") void readAllNotification() { - assertThrows(UnauthenticatedException.class, () -> notificationService.readAllNotification()); + assertNotEquals(0, notificationService.readAllNotification(idUser)); } @Test @DisplayName("删除相关未读消息") void deleteUnreadNotification() { List notifications = notificationService.findUnreadNotifications(idUser); - assertEquals(1, notifications.size()); - Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); - assertEquals(1, integer); + int size = notifications.size(); + System.out.println(size); + Integer integer = notificationService.deleteUnreadNotification(dataId, NotificationConstant.Follow); + assertNotEquals(0, integer); - integer = notificationService.deleteUnreadNotification(idUser, datatype); + integer = notificationService.deleteUnreadNotification(dataId, NotificationConstant.Follow); assertEquals(0, integer); notifications = notificationService.findUnreadNotifications(idUser); assertEquals(0, notifications.size()); - - } -} \ No newline at end of file +}