🧑‍💻 完善单元测试,修正错误

This commit is contained in:
ronger 2023-04-24 08:39:09 +08:00 committed by Zeeland
parent 699d3fc217
commit 5f369cf34a
7 changed files with 35 additions and 41 deletions

View File

@ -34,7 +34,7 @@ public class CommentHandler {
public void processCommentCreatedEvent(CommentEvent commentEvent) throws InterruptedException { public void processCommentCreatedEvent(CommentEvent commentEvent) throws InterruptedException {
log.info(String.format("开始执行评论发布事件:[%s]", JSON.toJSONString(commentEvent))); log.info(String.format("开始执行评论发布事件:[%s]", JSON.toJSONString(commentEvent)));
String commentContent = commentEvent.getContent(); String commentContent = commentEvent.getContent();
Integer length = commentContent.length(); int length = commentContent.length();
if (length > MAX_PREVIEW) { if (length > MAX_PREVIEW) {
length = 200; length = 200;
} }

View File

@ -63,7 +63,7 @@ public interface NotificationService extends Service<Notification> {
* *
* @return * @return
*/ */
Integer readAllNotification(); Integer readAllNotification(Long idUser);
/** /**
* 删除相关未读消息 * 删除相关未读消息

View File

@ -73,8 +73,8 @@ public class NotificationServiceImpl extends AbstractService<Notification> imple
} }
@Override @Override
public Integer readAllNotification() { public Integer readAllNotification(Long idUser) {
return notificationMapper.readAllNotification(Objects.requireNonNull(UserUtils.getCurrentUserByToken()).getIdUser()); return notificationMapper.readAllNotification(idUser);
} }
@Override @Override

View File

@ -13,6 +13,7 @@ import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
import java.util.List; import java.util.List;
import java.util.Objects;
/** /**
* 消息通知 * 消息通知
@ -56,7 +57,8 @@ public class NotificationController {
@PutMapping("/read-all") @PutMapping("/read-all")
public GlobalResult readAll() { public GlobalResult readAll() {
Integer result = notificationService.readAllNotification(); Long idUser = UserUtils.getCurrentUserByToken().getIdUser();
Integer result = notificationService.readAllNotification(idUser);
if (result == 0) { if (result == 0) {
return GlobalResultGenerator.genErrorResult("标记已读失败"); return GlobalResultGenerator.genErrorResult("标记已读失败");
} }

View File

@ -24,7 +24,6 @@ class FollowServiceTest extends BaseServiceTest {
{ {
follow = new Follow(); follow = new Follow();
follow.setFollowerId(idUser); follow.setFollowerId(idUser);
follow.setFollowingType(followingType); follow.setFollowingType(followingType);
follow.setFollowingId(followingId); follow.setFollowingId(followingId);
@ -77,4 +76,4 @@ class FollowServiceTest extends BaseServiceTest {
List<UserDTO> list = followService.findUserFollowingsByUser(userDTO); List<UserDTO> list = followService.findUserFollowingsByUser(userDTO);
assertTrue(list.isEmpty()); assertTrue(list.isEmpty());
} }
} }

View File

@ -23,12 +23,12 @@ class JavaMailServiceTest extends BaseServiceTest {
@Test @Test
void sendEmailCode() throws MessagingException { void sendEmailCode() throws MessagingException {
assertThrows(MailParseException.class, () -> javaMailService.sendEmailCode(REALITY_EMAIL)); assertEquals(1, javaMailService.sendEmailCode(REALITY_EMAIL));
} }
@Test @Test
void sendForgetPasswordEmail() throws MessagingException { void sendForgetPasswordEmail() throws MessagingException {
assertThrows(AddressException.class, () -> javaMailService.sendForgetPasswordEmail(REALITY_EMAIL)); assertEquals(1, javaMailService.sendForgetPasswordEmail(REALITY_EMAIL));
} }
@Test @Test
@ -36,4 +36,4 @@ class JavaMailServiceTest extends BaseServiceTest {
assertEquals(0, javaMailService.sendNotification(new NotificationDTO())); assertEquals(0, javaMailService.sendNotification(new NotificationDTO()));
} }
} }

View File

@ -1,9 +1,9 @@
package com.rymcu.forest.service; package com.rymcu.forest.service;
import com.rymcu.forest.base.BaseServiceTest; import com.rymcu.forest.base.BaseServiceTest;
import com.rymcu.forest.core.constant.NotificationConstant;
import com.rymcu.forest.dto.NotificationDTO; import com.rymcu.forest.dto.NotificationDTO;
import com.rymcu.forest.entity.Notification; import com.rymcu.forest.entity.Notification;
import org.apache.shiro.authz.UnauthenticatedException;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Order; import org.junit.jupiter.api.Order;
@ -19,72 +19,66 @@ import static org.junit.jupiter.api.Assertions.*;
*/ */
@Order(1) @Order(1)
class NotificationServiceTest extends BaseServiceTest { class NotificationServiceTest extends BaseServiceTest {
private final Long idUser = 1L; private final Long idUser = 2L;
private final Long dataId = 1L; private final Long dataId = 1L;
private final String datatype = "1";
private final String dataSummary = "nickname 关注了你!"; private final String dataSummary = "nickname 关注了你!";
@Autowired @Autowired
private NotificationService notificationService; private NotificationService notificationService;
@BeforeEach @BeforeEach
void setUp() { void setUp() {
save();
} }
@Test @Test
@DisplayName("获取未读消息数据") @DisplayName("获取未读消息数据")
void findUnreadNotifications() { void findUnreadNotifications() {
List<Notification> unreadNotifications = notificationService.findUnreadNotifications(1L); List<Notification> unreadNotifications = notificationService.findUnreadNotifications(idUser);
assertFalse(unreadNotifications.isEmpty()); assertFalse(unreadNotifications.isEmpty());
assertEquals(1, unreadNotifications.size());
} }
@Test @Test
@DisplayName("获取消息数据") @DisplayName("获取消息数据")
void findNotifications() { void findNotifications() {
List<NotificationDTO> notifications = notificationService.findNotifications(1L); List<NotificationDTO> notifications = notificationService.findNotifications(idUser);
assertFalse(notifications.isEmpty()); assertNotNull(notifications);
assertEquals(1, notifications.size());
} }
@Test @Test
@DisplayName("获取消息数据") @DisplayName("获取消息数据")
void findNotification() { void findNotification() {
Notification notification = notificationService.findNotification(idUser, dataId, NotificationConstant.Follow);
Notification notification = notificationService.findNotification(idUser, dataId, datatype);
assertNotNull(notification); assertNotNull(notification);
assertEquals(idUser, notification.getIdUser()); assertEquals(idUser, notification.getIdUser());
assertEquals(dataSummary, notification.getDataSummary()); assertEquals(dataSummary, notification.getDataSummary());
Notification notification2 = notificationService.findNotification(0L, dataId, datatype); Notification notification2 = notificationService.findNotification(0L, dataId, NotificationConstant.Follow);
assertNull(notification2); assertNull(notification2);
} }
@Test
@DisplayName("创建系统通知") @DisplayName("创建系统通知")
void save() { void save() {
List<Notification> notifications = notificationService.findUnreadNotifications(idUser); List<Notification> notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(1, notifications.size()); int size = notifications.size();
Integer integer = notificationService.save(idUser, 2L, datatype, dataSummary); Integer integer = notificationService.save(idUser, dataId, NotificationConstant.Follow, dataSummary);
assertEquals(1, integer); assertEquals(1, integer);
notifications = notificationService.findUnreadNotifications(idUser); notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(2, notifications.size()); assertEquals(size + 1, notifications.size());
} }
@Test @Test
@DisplayName("标记消息已读") @DisplayName("标记消息已读")
void readNotification() { void readNotification() {
List<Notification> notifications = notificationService.findUnreadNotifications(idUser); List<Notification> notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(1, notifications.size()); int size = notifications.size();
Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); // Integer integer = notificationService.deleteUnreadNotification(idUser, NotificationConstant.Follow);
assertEquals(1, integer); // assertEquals(1, integer);
integer = notificationService.readNotification(1L, idUser); Integer integer = notificationService.readNotification(notifications.get(0).getIdNotification(), idUser);
assertEquals(0, integer); assertNotEquals(0, integer);
notifications = notificationService.findUnreadNotifications(idUser); notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(0, notifications.size()); assertEquals(size - 1, notifications.size());
} }
/** /**
@ -94,23 +88,22 @@ class NotificationServiceTest extends BaseServiceTest {
@Test @Test
@DisplayName("标记所有消息已读") @DisplayName("标记所有消息已读")
void readAllNotification() { void readAllNotification() {
assertThrows(UnauthenticatedException.class, () -> notificationService.readAllNotification()); assertNotEquals(0, notificationService.readAllNotification(idUser));
} }
@Test @Test
@DisplayName("删除相关未读消息") @DisplayName("删除相关未读消息")
void deleteUnreadNotification() { void deleteUnreadNotification() {
List<Notification> notifications = notificationService.findUnreadNotifications(idUser); List<Notification> notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(1, notifications.size()); int size = notifications.size();
Integer integer = notificationService.deleteUnreadNotification(idUser, datatype); System.out.println(size);
assertEquals(1, integer); Integer integer = notificationService.deleteUnreadNotification(dataId, NotificationConstant.Follow);
assertNotEquals(0, integer);
integer = notificationService.deleteUnreadNotification(idUser, datatype); integer = notificationService.deleteUnreadNotification(dataId, NotificationConstant.Follow);
assertEquals(0, integer); assertEquals(0, integer);
notifications = notificationService.findUnreadNotifications(idUser); notifications = notificationService.findUnreadNotifications(idUser);
assertEquals(0, notifications.size()); assertEquals(0, notifications.size());
} }
} }