Skip to content

Commit

Permalink
test : 그룹 탈퇴 API 테스트 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
rladmstn committed Nov 18, 2024
1 parent 8fe7d7a commit f51033f
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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());
}
Expand All @@ -359,7 +359,7 @@ void deleteUser() throws Exception {
}

@Test
@DisplayName("그룹 탈퇴 실패 : 참여하지 않은 그룹")
@DisplayName("멤버 삭제 실패 : 참여하지 않은 그룹")
void deleteMemberFailed_1() throws Exception {
// given
doThrow(new GroupMemberValidationException(HttpStatus.BAD_REQUEST.value(),
Expand All @@ -374,7 +374,7 @@ void deleteMemberFailed_1() throws Exception {
}

@Test
@DisplayName("그룹 탈퇴 실패 : 권한 없음")
@DisplayName("멤버 삭제 실패 : 권한 없음")
void deleteMemberFailed_2() throws Exception {
// given
doThrow(new UserValidationException("멤버를 삭제 할 권한이 없습니다.")).when(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -77,6 +78,8 @@ class StudyGroupServiceTest {
@Mock
private UserRepository userRepository;
@Mock
private NotificationRepository notificationRepository;
@Mock
private NotificationSettingRepository notificationSettingRepository;
@Mock
private RankingRepository rankingRepository;
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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() {
Expand Down

0 comments on commit f51033f

Please sign in to comment.