diff --git a/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java b/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java index e4ade0d0..59a6ec9b 100644 --- a/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java +++ b/src/main/java/com/gamzabat/algohub/feature/group/studygroup/service/StudyGroupService.java @@ -46,8 +46,7 @@ import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; import com.gamzabat.algohub.feature.image.service.ImageService; import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; -import com.gamzabat.algohub.feature.notification.enums.NotificationMessage; -import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException; +import com.gamzabat.algohub.feature.notification.enums.NotificationCategory; import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; import com.gamzabat.algohub.feature.notification.service.NotificationService; import com.gamzabat.algohub.feature.problem.domain.Problem; @@ -142,7 +141,7 @@ public void joinGroupWithCode(User user, String code) { .build() ); - sendNotification(studyGroup, member); + sendNewMemberNotification(studyGroup, member); log.info("success to join study group"); } @@ -452,27 +451,17 @@ public void editStudyGroupVisibility(User user, EditGroupVisibilityRequest reque log.info("success to update group visibility ( userId : {} )", user.getId()); } - private void sendNotification(StudyGroup studyGroup, GroupMember newMember) { - List members = groupMemberRepository.findAllByStudyGroup(studyGroup); - - List users = new ArrayList<>(); - for (GroupMember member : members) { - if (member.getUser().getId().equals(newMember.getUser().getId())) - continue; - - NotificationSetting setting = notificationSettingRepository.findByMember(member) - .orElseThrow(() -> new CannotFoundNotificationSettingException("그룹 멤버의 알림 정보를 조회할 수 없습니다.")); - - if (setting.isAllNotifications() && setting.isNewMember()) - users.add(member.getUser().getEmail()); - } + private void sendNewMemberNotification(StudyGroup studyGroup, GroupMember newMember) { + List members = groupMemberRepository.findAllByStudyGroup(studyGroup) + .stream() + .filter(member -> !member.getId().equals(newMember.getId())) + .toList(); - try { - String message = NotificationMessage.NEW_MEMBER_JOINED.format(newMember.getUser().getNickname(), - studyGroup.getName()); - notificationService.sendList(users, message, studyGroup, null); - } catch (Exception e) { - log.warn("failed to send notification", e); - } + notificationService.sendNotificationToMembers( + studyGroup, + members, + NotificationCategory.NEW_MEMBER_JOINED, + NotificationCategory.NEW_MEMBER_JOINED.getMessage(newMember.getUser().getNickname()) + ); } } diff --git a/src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationMessage.java b/src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationCategory.java similarity index 60% rename from src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationMessage.java rename to src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationCategory.java index b716e226..fd48e4f5 100644 --- a/src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationMessage.java +++ b/src/main/java/com/gamzabat/algohub/feature/notification/enums/NotificationCategory.java @@ -1,19 +1,19 @@ package com.gamzabat.algohub.feature.notification.enums; -public enum NotificationMessage { +public enum NotificationCategory { PROBLEM_STARTED("[%s] 문제가 시작되었습니다! 지금 도전해보세요!"), NEW_SOLUTION_POSTED("%s님이 새로운 풀이를 등록했습니다! 풀이를 확인하고 의견을 나눠보세요."), - NEW_MEMBER_JOINED("%s님이 %s 스터디에 합류했습니다!"), + NEW_MEMBER_JOINED("%s님이 스터디에 합류했습니다!"), NEW_COMMENT_POSTED("%s님이 내 풀이에 코멘트를 남겼습니다! 어떤 리뷰인지 확인해보세요."), - PROBLEM_DEADLINE_REACHED("[%s] 문제의 마감이 얼마 남지 않았습니다! 아직 해결하지 못했다면 지금 도전해보세요!"); + PROBLEM_DEADLINE_REACHED("[%s] 문제의 마감이 오늘입니다! 아직 해결하지 못했다면 지금 도전해보세요!"); private final String message; - NotificationMessage(String message) { + NotificationCategory(String message) { this.message = message; } - public String format(Object... args) { + public String getMessage(Object... args) { return String.format(message, args); } } diff --git a/src/main/java/com/gamzabat/algohub/feature/notification/service/NotificationService.java b/src/main/java/com/gamzabat/algohub/feature/notification/service/NotificationService.java index 9a139f94..0977b926 100644 --- a/src/main/java/com/gamzabat/algohub/feature/notification/service/NotificationService.java +++ b/src/main/java/com/gamzabat/algohub/feature/notification/service/NotificationService.java @@ -13,11 +13,18 @@ import org.springframework.web.servlet.mvc.method.annotation.SseEmitter; import com.gamzabat.algohub.exception.UserValidationException; +import com.gamzabat.algohub.feature.group.studygroup.domain.GroupMember; +import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup; +import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; +import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; import com.gamzabat.algohub.feature.notification.domain.Notification; +import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; import com.gamzabat.algohub.feature.notification.dto.GetNotificationResponse; +import com.gamzabat.algohub.feature.notification.enums.NotificationCategory; +import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException; import com.gamzabat.algohub.feature.notification.repository.EmitterRepositoryImpl; import com.gamzabat.algohub.feature.notification.repository.NotificationRepository; -import com.gamzabat.algohub.feature.group.studygroup.domain.StudyGroup; +import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; import com.gamzabat.algohub.feature.user.domain.User; import com.gamzabat.algohub.feature.user.repository.UserRepository; @@ -30,6 +37,9 @@ public class NotificationService { private final EmitterRepositoryImpl emitterRepository; private final NotificationRepository notificationRepository; + private final NotificationSettingRepository notificationSettingRepository; + private final StudyGroupRepository studyGroupRepository; + private final GroupMemberRepository groupMemberRepository; private final UserRepository userRepository; @Transactional @@ -98,8 +108,7 @@ public void send(String receiver, String message, StudyGroup studyGroup, String ); } - @Transactional - public void sendList(List receiverList, String message, StudyGroup studyGroup, String subContent) { + private void sendList(List receiverList, String message, StudyGroup studyGroup, String subContent) { List notifications = new ArrayList<>(); Map sseEmitters; for (int i = 0; i < receiverList.size(); i++) { @@ -157,4 +166,33 @@ public void updateIsRead(User user) { }); log.info("success to read status"); } + + @Transactional + public void sendNotificationToMembers(StudyGroup group, List receiver, + NotificationCategory category, String message) { + List users = new ArrayList<>(); + for (GroupMember member : receiver) { + NotificationSetting setting = notificationSettingRepository.findByMember(member) + .orElseThrow(() -> new CannotFoundNotificationSettingException("그룹 멤버의 알림 정보를 조회할 수 없습니다.")); + + if (setting.isAllNotifications() && isSettingOn(setting, category)) + users.add(member.getUser().getEmail()); + } + + try { + sendList(users, message, group, null); + } catch (Exception e) { + log.warn("failed to send notification", e); + } + } + + private boolean isSettingOn(NotificationSetting setting, NotificationCategory category) { + return switch (category) { + case NotificationCategory.PROBLEM_STARTED -> setting.isNewProblem(); + case NotificationCategory.PROBLEM_DEADLINE_REACHED -> setting.isDeadlineReached(); + case NotificationCategory.NEW_COMMENT_POSTED -> setting.isNewComment(); + case NotificationCategory.NEW_MEMBER_JOINED -> setting.isNewMember(); + case NotificationCategory.NEW_SOLUTION_POSTED -> setting.isNewSolution(); + }; + } } diff --git a/src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java b/src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java index d94209e2..cf12b2eb 100644 --- a/src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java +++ b/src/main/java/com/gamzabat/algohub/feature/problem/service/ProblemService.java @@ -28,10 +28,7 @@ import com.gamzabat.algohub.feature.group.studygroup.etc.RoleOfGroupMember; import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; -import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; -import com.gamzabat.algohub.feature.notification.enums.NotificationMessage; -import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException; -import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; +import com.gamzabat.algohub.feature.notification.enums.NotificationCategory; import com.gamzabat.algohub.feature.notification.service.NotificationService; import com.gamzabat.algohub.feature.problem.domain.Problem; import com.gamzabat.algohub.feature.problem.dto.CreateProblemRequest; @@ -57,7 +54,6 @@ public class ProblemService { private final GroupMemberRepository groupMemberRepository; private final NotificationService notificationService; private final RestTemplate restTemplate; - private final NotificationSettingRepository notificationSettingRepository; @Transactional public void createProblem(User user, CreateProblemRequest request) { @@ -87,7 +83,12 @@ public void createProblem(User user, CreateProblemRequest request) { .build()); if (request.startDate().equals(LocalDate.now())) - sendProblemNotification(group, title, NotificationMessage.PROBLEM_STARTED); + notificationService.sendNotificationToMembers( + group, + groupMemberRepository.findAllByStudyGroup(group), + NotificationCategory.PROBLEM_STARTED, + NotificationCategory.PROBLEM_STARTED.getMessage(title) + ); log.info("success to create problem"); } @@ -263,16 +264,24 @@ public void dailyProblemScheduler() { private void notifyProblemStartsToday(LocalDate now) { List problems = problemRepository.findAllByStartDate(now); for (Problem problem : problems) { - sendProblemNotification(problem.getStudyGroup(), problem.getTitle(), - NotificationMessage.PROBLEM_STARTED); + notificationService.sendNotificationToMembers( + problem.getStudyGroup(), + groupMemberRepository.findAllByStudyGroup(problem.getStudyGroup()), + NotificationCategory.PROBLEM_STARTED, + NotificationCategory.PROBLEM_STARTED.getMessage(problem.getTitle()) + ); } } private void notifyProblemEndsToday(LocalDate now) { List problems = problemRepository.findAllByEndDate(now); for (Problem problem : problems) { - sendProblemNotification(problem.getStudyGroup(), problem.getTitle(), - NotificationMessage.PROBLEM_DEADLINE_REACHED); + notificationService.sendNotificationToMembers( + problem.getStudyGroup(), + groupMemberRepository.findAllByStudyGroup(problem.getStudyGroup()), + NotificationCategory.PROBLEM_DEADLINE_REACHED, + NotificationCategory.PROBLEM_DEADLINE_REACHED.getMessage(problem.getTitle()) + ); } } @@ -346,37 +355,4 @@ private Integer calculateAccuracy(Integer submitMemberCount, Integer correctCoun private Boolean isInProgress(Problem problem) { return problem.getEndDate() != null && !LocalDate.now().isAfter(problem.getEndDate()); } - - private void sendProblemNotification(StudyGroup group, String title, NotificationMessage problemMessage) { - List members = groupMemberRepository.findAllByStudyGroup(group); - - List users = getMembersEmails(members, problemMessage); - - try { - String message = problemMessage.format(title); - notificationService.sendList(users, message, group, null); - } catch (Exception e) { - log.warn("failed to send notification", e); - } - } - - private List getMembersEmails(List members, NotificationMessage notificationMessage) { - List users = new ArrayList<>(); - for (GroupMember member : members) { - NotificationSetting setting = notificationSettingRepository.findByMember(member) - .orElseThrow(() -> new CannotFoundNotificationSettingException("그룹 멤버의 알림 정보를 조회할 수 없습니다.")); - - if (setting.isAllNotifications() && isSettingOn(setting, notificationMessage)) - users.add(member.getUser().getEmail()); - } - return users; - } - - private static boolean isSettingOn(NotificationSetting setting, NotificationMessage message) { - return switch (message) { - case NotificationMessage.PROBLEM_STARTED -> setting.isNewProblem(); - case NotificationMessage.PROBLEM_DEADLINE_REACHED -> setting.isDeadlineReached(); - default -> false; - }; - } } diff --git a/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionCommentService.java b/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionCommentService.java index 760c7013..9d9e98eb 100644 --- a/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionCommentService.java +++ b/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionCommentService.java @@ -19,9 +19,7 @@ import com.gamzabat.algohub.feature.group.studygroup.exception.GroupMemberValidationException; import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; -import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; -import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException; -import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; +import com.gamzabat.algohub.feature.notification.enums.NotificationCategory; import com.gamzabat.algohub.feature.notification.service.NotificationService; import com.gamzabat.algohub.feature.problem.domain.Problem; import com.gamzabat.algohub.feature.problem.repository.ProblemRepository; @@ -46,7 +44,6 @@ public class SolutionCommentService implements CommentService new GroupMemberValidationException(HttpStatus.NOT_FOUND.value(), "참여하지 않은 스터디 그룹입니다.")); - NotificationSetting setting = notificationSettingRepository.findByMember(member) - .orElseThrow(() -> new CannotFoundNotificationSettingException("그룹 멤버의 알림 정보를 조회할 수 없습니다.")); - - if (!setting.isAllNotifications() || !setting.isNewComment()) - return; - - String message = content.length() < 35 ? content : content.substring(0, 35) + "..."; - - try { - notificationService.send(solution.getUser().getEmail(), - commenter.getNickname() + "님이 코멘트를 남겼습니다.", - solution.getProblem().getStudyGroup(), - message); - } catch (Exception e) { - log.info("failed to send comment notification", e); - } + + notificationService.sendNotificationToMembers( + solution.getProblem().getStudyGroup(), + List.of(member), + NotificationCategory.NEW_COMMENT_POSTED, + NotificationCategory.NEW_COMMENT_POSTED.getMessage(commenter.getNickname()) + ); } @Override diff --git a/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionService.java b/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionService.java index d4bad48d..5acda9d7 100644 --- a/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionService.java +++ b/src/main/java/com/gamzabat/algohub/feature/solution/service/SolutionService.java @@ -2,7 +2,6 @@ import java.time.LocalDate; import java.time.LocalDateTime; -import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.Optional; @@ -23,9 +22,7 @@ import com.gamzabat.algohub.feature.group.studygroup.exception.GroupMemberValidationException; import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; -import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; -import com.gamzabat.algohub.feature.notification.enums.NotificationMessage; -import com.gamzabat.algohub.feature.notification.exception.CannotFoundNotificationSettingException; +import com.gamzabat.algohub.feature.notification.enums.NotificationCategory; import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; import com.gamzabat.algohub.feature.notification.service.NotificationService; import com.gamzabat.algohub.feature.problem.domain.Problem; @@ -141,31 +138,17 @@ public void createSolution(CreateSolutionRequest request) { rankingUpdateService.updateRanking(studyGroup); } - sendNotification(studyGroup, member.get()); + sendNewSolutionNotification(studyGroup, member.get()); } } - private void sendNotification(StudyGroup group, GroupMember solver) { - List members = groupMemberRepository.findAllByStudyGroup(group); - - List users = new ArrayList<>(); - for (GroupMember member : members) { - if (member.getUser().getId().equals(solver.getUser().getId())) - continue; - - NotificationSetting setting = notificationSettingRepository.findByMember(member) - .orElseThrow(() -> new CannotFoundNotificationSettingException("그룹 멤버의 알림 정보를 조회할 수 없습니다.")); - - if (setting.isAllNotifications() && setting.isNewSolution()) - users.add(member.getUser().getEmail()); - } - - try { - String message = NotificationMessage.NEW_SOLUTION_POSTED.format(solver.getUser().getNickname()); - notificationService.sendList(users, message, group, null); - } catch (Exception e) { - log.warn("failed to send notification", e); - } + private void sendNewSolutionNotification(StudyGroup group, GroupMember solver) { + notificationService.sendNotificationToMembers( + group, + groupMemberRepository.findAllByStudyGroup(group), + NotificationCategory.NEW_SOLUTION_POSTED, + NotificationCategory.NEW_SOLUTION_POSTED.getMessage(solver.getUser().getNickname()) + ); } private boolean isCorrect(String result) { diff --git a/src/test/java/com/gamzabat/algohub/service/ProblemServiceTest.java b/src/test/java/com/gamzabat/algohub/service/ProblemServiceTest.java index 33226d3a..aaa05ddf 100644 --- a/src/test/java/com/gamzabat/algohub/service/ProblemServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/ProblemServiceTest.java @@ -35,8 +35,6 @@ import com.gamzabat.algohub.feature.group.studygroup.etc.RoleOfGroupMember; import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; -import com.gamzabat.algohub.feature.notification.domain.Notification; -import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; import com.gamzabat.algohub.feature.notification.repository.NotificationRepository; import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; import com.gamzabat.algohub.feature.notification.service.NotificationService; @@ -178,7 +176,7 @@ void createProblem_SuccessByADMIN() { assertThat(result.getLevel()).isEqualTo(1); assertThat(result.getStartDate()).isEqualTo(LocalDate.now()); assertThat(result.getEndDate()).isEqualTo(LocalDate.now().plusDays(10)); - verify(notificationService, times(1)).sendList(any(), any(), any(), any()); + verify(notificationService, times(1)).sendNotificationToMembers(any(), any(), any(), any()); } @Test @@ -280,30 +278,6 @@ void createProblemFailed_6() { .hasFieldOrPropertyWithValue("error", "solved.ac API로부터 예상치 못한 응답을 받았습니다."); } - @Test - @DisplayName("문제 생성 성공, 알림 전송 실패") - void createProblemSuccess_NotificationFailed() { - // given - CreateProblemRequest request = CreateProblemRequest.builder() - .groupId(10L) - .link("https://www.acmicpc.net/problem/1000") - .startDate(LocalDate.now()) - .endDate(LocalDate.now().plusDays(10)) - .build(); - when(groupRepository.findById(10L)).thenReturn(Optional.ofNullable(group)); - String apiResult = "[{\"titleKo\":\"A+B\",\"level\":1}]"; - ResponseEntity responseEntity = new ResponseEntity<>(apiResult, HttpStatus.OK); - when(restTemplate.getForEntity(anyString(), eq(String.class))).thenReturn(responseEntity); - when(groupMemberRepository.findByUserAndStudyGroup(user, group)).thenReturn(Optional.ofNullable(groupMember1)); - doThrow(new RuntimeException()).when(notificationService).sendList(any(), any(), any(), any()); - // when - problemService.createProblem(user, request); - // then - verify(problemRepository, times(1)).save(any(Problem.class)); - verify(notificationService, times(1)).sendList(any(), any(), any(), any()); - verify(notificationRepository, never()).save(any(Notification.class)); - } - @Test @DisplayName("문제 정보 수정 성공") void editProblem() { @@ -779,23 +753,14 @@ void sendProblemNotification() { .build()); } - NotificationSetting setting1 = NotificationSetting.builder().member(groupMember1).build(); - NotificationSetting setting3 = NotificationSetting.builder().member(groupMember3).build(); - NotificationSetting setting4 = NotificationSetting.builder().member(groupMember4).build(); - NotificationSetting setting11 = NotificationSetting.builder().member(groupMember11).build(); - when(problemRepository.findAllByStartDate(LocalDate.now())).thenReturn(startProblems); when(problemRepository.findAllByEndDate(LocalDate.now())).thenReturn(endProblems); when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn(group1Members); when(groupMemberRepository.findAllByStudyGroup(group2)).thenReturn(group2Members); - when(notificationSettingRepository.findByMember(groupMember1)).thenReturn(Optional.ofNullable(setting1)); - when(notificationSettingRepository.findByMember(groupMember3)).thenReturn(Optional.ofNullable(setting3)); - when(notificationSettingRepository.findByMember(groupMember4)).thenReturn(Optional.ofNullable(setting4)); - when(notificationSettingRepository.findByMember(groupMember11)).thenReturn(Optional.ofNullable(setting11)); // when problemService.dailyProblemScheduler(); // then - verify(notificationService, times(20)).sendList(anyList(), anyString(), any(StudyGroup.class), eq(null)); + verify(notificationService, times(20)).sendNotificationToMembers(any(), any(), any(), any()); } } \ No newline at end of file diff --git a/src/test/java/com/gamzabat/algohub/service/SolutionCommentServiceTest.java b/src/test/java/com/gamzabat/algohub/service/SolutionCommentServiceTest.java index 203b69d1..7637fe22 100644 --- a/src/test/java/com/gamzabat/algohub/service/SolutionCommentServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/SolutionCommentServiceTest.java @@ -118,7 +118,10 @@ void setUp() throws NoSuchFieldException, IllegalAccessException { @DisplayName("댓글 작성 성공") void createComment_1() { // given - CreateSolutionCommentRequest request = CreateSolutionCommentRequest.builder().solutionId(10L).content("content").build(); + CreateSolutionCommentRequest request = CreateSolutionCommentRequest.builder() + .solutionId(10L) + .content("content") + .build(); GroupMember member = GroupMember.builder() .user(user) .studyGroup(studyGroup) @@ -132,7 +135,6 @@ void createComment_1() { when(groupMemberRepository.existsByUserAndStudyGroup(user2, studyGroup)).thenReturn(true); when(commentRepository.save(any(SolutionComment.class))).thenReturn(comment); when(groupMemberRepository.findByUserAndStudyGroup(user, studyGroup)).thenReturn(Optional.ofNullable(member)); - when(notificationSettingRepository.findByMember(member)).thenReturn(Optional.of(setting)); // when commentService.createComment(user2, request); // then @@ -141,7 +143,7 @@ void createComment_1() { assertThat(result.getContent()).isEqualTo("content"); assertThat(result.getUser()).isEqualTo(user2); assertThat(result.getSolution()).isEqualTo(solution); - verify(notificationService, times(1)).send(any(), any(), any(), any()); + verify(notificationService, times(1)).sendNotificationToMembers(any(), any(), any(), any()); } @Test @@ -217,37 +219,6 @@ void createCommentFailed_4() { verify(notificationService, never()).send(any(), any(), any(), any()); } - @Test - @DisplayName("댓글 작성 성공, 알림 전송 실패") - void createCommentSuccess_NotificationFailed() { - // given - CreateSolutionCommentRequest request = CreateSolutionCommentRequest.builder() - .solutionId(10L) - .content("content") - .build(); - GroupMember member = GroupMember.builder() - .user(user) - .studyGroup(studyGroup) - .role(RoleOfGroupMember.PARTICIPANT) - .build(); - NotificationSetting setting = new NotificationSetting(member); - - when(solutionRepository.findById(10L)).thenReturn(Optional.ofNullable(solution)); - when(problemRepository.findById(20L)).thenReturn(Optional.ofNullable(problem)); - when(studyGroupRepository.findById(30L)).thenReturn(Optional.ofNullable(studyGroup)); - when(groupMemberRepository.existsByUserAndStudyGroup(user2, studyGroup)).thenReturn(true); - when(commentRepository.save(any(SolutionComment.class))).thenReturn(comment); - when(groupMemberRepository.findByUserAndStudyGroup(user, studyGroup)).thenReturn(Optional.ofNullable(member)); - when(notificationSettingRepository.findByMember(member)).thenReturn(Optional.of(setting)); - doThrow(new RuntimeException()).when(notificationService).send(any(), any(), any(), any()); - // when - commentService.createComment(user2, request); - // then - verify(commentRepository, times(1)).save(any(SolutionComment.class)); - verify(notificationService, times(1)).send(any(), any(), any(), any()); - verify(notificationRepository, never()).save(any()); - } - @Test @DisplayName("댓글 조회 성공") void getComment_1() { diff --git a/src/test/java/com/gamzabat/algohub/service/SolutionServiceTest.java b/src/test/java/com/gamzabat/algohub/service/SolutionServiceTest.java index 294840bd..cd196131 100644 --- a/src/test/java/com/gamzabat/algohub/service/SolutionServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/SolutionServiceTest.java @@ -34,8 +34,6 @@ import com.gamzabat.algohub.feature.group.studygroup.exception.GroupMemberValidationException; import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository; import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository; -import com.gamzabat.algohub.feature.notification.domain.NotificationSetting; -import com.gamzabat.algohub.feature.notification.repository.NotificationSettingRepository; import com.gamzabat.algohub.feature.notification.service.NotificationService; import com.gamzabat.algohub.feature.problem.domain.Problem; import com.gamzabat.algohub.feature.problem.repository.ProblemRepository; @@ -64,11 +62,9 @@ class SolutionServiceTest { @Mock private GroupMemberRepository groupMemberRepository; @Mock - private SolutionCommentRepository commentRepository; + private SolutionCommentRepository solutionCommentRepository; @Mock private UserRepository userRepository; - @Mock - private NotificationSettingRepository notificationSettingRepository; private User user, user2; private Problem problem; private StudyGroup group; @@ -438,21 +434,17 @@ void createSolution() { .endDate(LocalDate.now().plusDays(30)) .build(); - NotificationSetting setting2 = NotificationSetting.builder().member(member2).build(); - List members = List.of(member1, member2); when(problemRepository.findAllByNumber(300)).thenReturn(List.of(problem)); when(userRepository.findByBjNickname("bjNickname")).thenReturn(Optional.of(user)); when(groupMemberRepository.findByUserAndStudyGroup(user, group)).thenReturn(Optional.of(member1)); when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn(members); - when(notificationSettingRepository.findByMember(member2)).thenReturn(Optional.of(setting2)); - List users = List.of(user2.getEmail()); // when solutionService.createSolution(request); // then verify(solutionRepository, times(1)).save(any(Solution.class)); - verify(notificationService, times(1)).sendList(eq(users), anyString(), eq(group), eq(null)); + verify(notificationService, times(1)).sendNotificationToMembers(any(), any(), any(), any()); } } \ No newline at end of file diff --git a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java index 661918d9..1b27f5b4 100644 --- a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java @@ -177,6 +177,12 @@ void setUp() throws NoSuchFieldException, IllegalAccessException { Field groupId = StudyGroup.class.getDeclaredField("id"); groupId.setAccessible(true); groupId.set(group, 10L); + + Field memberId = GroupMember.class.getDeclaredField("id"); + memberId.setAccessible(true); + memberId.set(groupMember1, 100L); + memberId.set(groupMember2, 200L); + memberId.set(groupMember3, 300L); } @Test @@ -208,15 +214,9 @@ void createGroup() { void joinGroupWithCode() { // given List members = List.of(groupMember1, groupMember2, groupMember3); - NotificationSetting setting1 = new NotificationSetting(groupMember1); - NotificationSetting setting3 = new NotificationSetting(groupMember3); when(studyGroupRepository.findByGroupCode("code")).thenReturn(Optional.ofNullable(group)); when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn(members); - when(notificationSettingRepository.findByMember(groupMember1)).thenReturn(Optional.of(setting1)); - when(notificationSettingRepository.findByMember(groupMember3)).thenReturn(Optional.of(setting3)); - - List users = List.of(user.getEmail(), user3.getEmail()); // when studyGroupService.joinGroupWithCode(user2, "code"); // then @@ -225,8 +225,7 @@ void joinGroupWithCode() { assertThat(result.getStudyGroup()).isEqualTo(group); assertThat(result.getUser()).isEqualTo(user2); verify(groupMemberRepository, times(1)).save(any(GroupMember.class)); - verify(notificationSettingRepository, times(1)).save(any(NotificationSetting.class)); - verify(notificationService, times(1)).sendList(eq(users), anyString(), eq(group), eq(null)); + verify(notificationService, times(1)).sendNotificationToMembers(any(), any(), any(), any()); } @Test