diff --git a/src/test/java/com/gamzabat/algohub/feature/studygroup/controller/StudyGroupControllerTest.java b/src/test/java/com/gamzabat/algohub/feature/studygroup/controller/StudyGroupControllerTest.java index 87e072ea..84fdea5e 100644 --- a/src/test/java/com/gamzabat/algohub/feature/studygroup/controller/StudyGroupControllerTest.java +++ b/src/test/java/com/gamzabat/algohub/feature/studygroup/controller/StudyGroupControllerTest.java @@ -301,7 +301,7 @@ void deleteGroupFailed_2() throws Exception { @Test @DisplayName("그룹 탈퇴 성공") - void leaveGroup() throws Exception { + void exitGroup() throws Exception { // given doNothing().when(studyGroupService).exitGroup(user, groupId); // when, then @@ -315,32 +315,32 @@ void leaveGroup() throws Exception { @Test @DisplayName("그룹 탈퇴 실패 : 존재하지 않는 그룹") - void leaveGroupFailed_1() throws Exception { + void exitGroupFailed_1() throws Exception { // given - doThrow(new StudyGroupValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 그룹 입니다.")).when( + doThrow(new CannotFoundGroupException("존재하지 않는 그룹입니다.")).when( studyGroupService).exitGroup(user, groupId); // when, then mockMvc.perform(delete("/api/groups/{groupId}/members/me", groupId) .header("Authorization", token) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()) - .andExpect(jsonPath("$.error").value("존재하지 않는 그룹 입니다.")); + .andExpect(jsonPath("$.error").value("존재하지 않는 그룹입니다.")); verify(studyGroupService, times(1)).exitGroup(any(User.class), anyLong()); } @Test @DisplayName("그룹 탈퇴 실패 : 이미 참여 안한 그룹") - void leaveGroupFailed_2() throws Exception { + void exitGroupFailed_2() throws Exception { // given - doThrow(new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(), "이미 참여하지 않은 그룹 입니다.")).when( + doThrow(new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(), "참여하지 않은 그룹 입니다.")).when( studyGroupService).exitGroup(user, groupId); // when, then mockMvc.perform(delete("/api/groups/{groupId}/members/me", groupId) .header("Authorization", token) .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isBadRequest()) - .andExpect(jsonPath("$.error").value("이미 참여하지 않은 그룹 입니다.")); + .andExpect(jsonPath("$.error").value("참여하지 않은 그룹 입니다.")); verify(studyGroupService, times(1)).exitGroup(any(User.class), anyLong()); } @@ -359,7 +359,7 @@ void deleteUser() throws Exception { } @Test - @DisplayName("그룹 탈퇴 실패 : 참여하지 않은 그룹") + @DisplayName("멤버 삭제 실패 : 참여하지 않은 그룹") void deleteMemberFailed_1() throws Exception { // given doThrow(new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(), @@ -374,7 +374,7 @@ void deleteMemberFailed_1() throws Exception { } @Test - @DisplayName("그룹 탈퇴 실패 : 권한 없음") + @DisplayName("멤버 삭제 실패 : 권한 없음") void deleteMemberFailed_2() throws Exception { // given doThrow(new UserValidationException("멤버를 삭제 할 권한이 없습니다.")).when( diff --git a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java index f54d2dff..b92eb8b8 100644 --- a/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java +++ b/src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java @@ -49,6 +49,7 @@ import com.gamzabat.algohub.feature.group.studygroup.service.StudyGroupService; import com.gamzabat.algohub.feature.image.service.ImageService; 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; import com.gamzabat.algohub.feature.problem.domain.Problem; @@ -77,6 +78,8 @@ class StudyGroupServiceTest { @Mock private UserRepository userRepository; @Mock + private NotificationRepository notificationRepository; + @Mock private NotificationSettingRepository notificationSettingRepository; @Mock private RankingRepository rankingRepository; @@ -263,6 +266,7 @@ void deleteGroup() { verify(rankingRepository, times(1)).deleteAllByStudyGroup(group); verify(notificationSettingRepository, times(1)).deleteAllByStudyGroup(group); verify(groupMemberRepository, times(1)).deleteAllByStudyGroup(group); + verify(notificationRepository, times(1)).deleteAllByStudyGroup(group); verify(studyGroupRepository, times(1)).delete(group); } @@ -290,6 +294,49 @@ void deleteGroupFailed_2() { .hasFieldOrPropertyWithValue("error", "참여하지 않은 그룹입니다."); } + @Test + @DisplayName("그룹 탈퇴 성공 (방장)") + void exitGroup() { + // given + when(studyGroupRepository.findById(10L)).thenReturn(Optional.of(group)); + when(groupMemberRepository.findByUserAndStudyGroup(user, group)).thenReturn(Optional.ofNullable(groupMember1)); + when(groupMemberRepository.findAllByStudyGroup(group)).thenReturn( + List.of(groupMember2, groupMember3)); + when(studyGroupServiceObjectProvider.getObject()).thenReturn(studyGroupService); + // when + studyGroupService.exitGroup(user, 10L); + // then + verify(rankingRepository, times(1)).deleteByMember(groupMember1); + verify(notificationSettingRepository, times(1)).deleteByMember(groupMember1); + verify(groupMemberRepository, times(1)).delete(groupMember1); + verify(notificationRepository, times(1)).deleteAllByStudyGroup(group); + assertThat(groupMember3.getRole()).isEqualTo(RoleOfGroupMember.OWNER); + } + + @Test + @DisplayName("그룹 탈퇴 실패 : 존재하지 않는 그룹") + void exitGroupFailed_1() { + // given + when(studyGroupRepository.findById(groupId)).thenReturn(Optional.empty()); + // when, then + assertThatThrownBy(() -> studyGroupService.exitGroup(user, groupId)) + .isInstanceOf(CannotFoundGroupException.class) + .hasFieldOrPropertyWithValue("errors", "존재하지 않는 그룹입니다."); + } + + @Test + @DisplayName("그룹 탈퇴 실패 : 이미 참여하지 않은 그룹") + void exitGroupFailed_2() { + // given + when(studyGroupRepository.findById(10L)).thenReturn(Optional.ofNullable(group)); + when(groupMemberRepository.findByUserAndStudyGroup(user2, group)).thenReturn(Optional.empty()); + // when, then + assertThatThrownBy(() -> studyGroupService.exitGroup(user2, 10L)) + .isInstanceOf(GroupMemberValidationException.class) + .hasFieldOrPropertyWithValue("code", HttpStatus.BAD_REQUEST.value()) + .hasFieldOrPropertyWithValue("error", "참여하지 않은 그룹입니다."); + } + @Test @DisplayName("그룹 목록 조회") void getGroupList() {