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

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 {
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;
}

View File

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

View File

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

View File

@ -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("标记已读失败");
}

View File

@ -24,7 +24,6 @@ class FollowServiceTest extends BaseServiceTest {
{
follow = new Follow();
follow.setFollowerId(idUser);
follow.setFollowingType(followingType);
follow.setFollowingId(followingId);

View File

@ -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

View File

@ -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<Notification> unreadNotifications = notificationService.findUnreadNotifications(1L);
List<Notification> unreadNotifications = notificationService.findUnreadNotifications(idUser);
assertFalse(unreadNotifications.isEmpty());
assertEquals(1, unreadNotifications.size());
}
@Test
@DisplayName("获取消息数据")
void findNotifications() {
List<NotificationDTO> notifications = notificationService.findNotifications(1L);
assertFalse(notifications.isEmpty());
assertEquals(1, notifications.size());
List<NotificationDTO> 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<Notification> 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<Notification> 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<Notification> 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());
}
}