Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test : 그룸 멤버 전체 랭킹 API 테스트코드 추가 #61

Merged
merged 6 commits into from
Aug 23, 2024
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 97 additions & 6 deletions src/test/java/com/gamzabat/algohub/service/StudyGroupServiceTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
Expand All @@ -21,12 +24,17 @@

import com.gamzabat.algohub.enums.Role;
import com.gamzabat.algohub.exception.StudyGroupValidationException;
import com.gamzabat.algohub.exception.UserValidationException;
import com.gamzabat.algohub.feature.image.service.ImageService;
import com.gamzabat.algohub.feature.problem.domain.Problem;
import com.gamzabat.algohub.feature.solution.domain.Solution;
import com.gamzabat.algohub.feature.solution.repository.SolutionRepository;
import com.gamzabat.algohub.feature.studygroup.domain.BookmarkedStudyGroup;
import com.gamzabat.algohub.feature.studygroup.domain.GroupMember;
import com.gamzabat.algohub.feature.studygroup.domain.StudyGroup;
import com.gamzabat.algohub.feature.studygroup.dto.CreateGroupRequest;
import com.gamzabat.algohub.feature.studygroup.dto.EditGroupRequest;
import com.gamzabat.algohub.feature.studygroup.dto.GetRankingResponse;
import com.gamzabat.algohub.feature.studygroup.exception.CannotFoundGroupException;
import com.gamzabat.algohub.feature.studygroup.exception.GroupMemberValidationException;
import com.gamzabat.algohub.feature.studygroup.repository.BookmarkedStudyGroupRepository;
Expand All @@ -46,10 +54,17 @@ class StudyGroupServiceTest {
@Mock
private BookmarkedStudyGroupRepository bookmarkedStudyGroupRepository;
@Mock
private SolutionRepository solutionRepository;
@Mock
private ImageService imageService;
private User user;
private User user2;
private StudyGroup group;
private Problem problem1;
private Problem problem2;
private Solution solution1;
private Solution solution2;
private Solution solution3;
private final Long groupId = 10L;
@Captor
private ArgumentCaptor<StudyGroup> groupCaptor;
Expand All @@ -58,10 +73,10 @@ class StudyGroupServiceTest {

@BeforeEach
void setUp() throws NoSuchFieldException, IllegalAccessException {
user = User.builder().email("email1").password("password").nickname("nickname")
.role(Role.USER).profileImage("image").build();
user2 = User.builder().email("email2").password("password").nickname("nickname")
.role(Role.USER).profileImage("image").build();
user = User.builder().email("email1").password("password").nickname("nickname1")
.role(Role.USER).profileImage("image1").build();
user2 = User.builder().email("email2").password("password").nickname("nickname2")
.role(Role.USER).profileImage("image2").build();
group = StudyGroup.builder()
.name("name")
.owner(user)
Expand All @@ -70,7 +85,30 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
.groupImage("imageUrl")
.groupCode("code")
.build();

problem1 = Problem.builder()
.studyGroup(group)
.build();
problem2 = Problem.builder()
.studyGroup(group)
.build();
solution1 = Solution.builder()
.result("맞았습니다!!")
.solvedDateTime(LocalDateTime.now().minusDays(1))
.problem(problem1)
.user(user)
.build();
solution2 = Solution.builder()
.result("맞았습니다!!")
.solvedDateTime(LocalDateTime.now().minusDays(2))
.problem(problem2)
.user(user)
.build();
solution3 = Solution.builder()
.result("맞았습니다!!")
.solvedDateTime(LocalDateTime.now().minusDays(1))
.problem(problem1)
.user(user2)
.build();
Field userField = User.class.getDeclaredField("id");
userField.setAccessible(true);
userField.set(user, 1L);
Expand All @@ -79,6 +117,7 @@ void setUp() throws NoSuchFieldException, IllegalAccessException {
Field groupId = StudyGroup.class.getDeclaredField("id");
groupId.setAccessible(true);
groupId.set(group, 10L);

}

@Test
Expand Down Expand Up @@ -363,4 +402,56 @@ void updateBookmarkedStudyGroupFailed_2() {
.hasFieldOrPropertyWithValue("code", HttpStatus.BAD_REQUEST.value())
.hasFieldOrPropertyWithValue("error", "참여하지 않은 그룹 입니다.");
}
}

@Test
@DisplayName("전체랭킹 조회 성공")
void getAllRank_SuccessByOwner() {
//given
when(studyGroupRepository.findById(10L)).thenReturn(Optional.of(group));
when(groupMemberRepository.existsByUserAndStudyGroup(user2, group)).thenReturn(true);
List<GetRankingResponse> response = Arrays.asList(
new GetRankingResponse("nickname2", "image2", 1, 2L), // user2
new GetRankingResponse("nickname1", "image1", 2, 1L) // user1
);
when(solutionRepository.findTopUsersByGroup(group)).thenReturn(response);

//when
List<GetRankingResponse> result = studyGroupService.getAllRank(user2, 10L);

//then
assertThat(result.get(0).getProfileImage()).isEqualTo("image2");
assertThat(result.get(0).getSolvedCount()).isEqualTo(2L);
assertThat(result.get(0).getUserNickname()).isEqualTo("nickname2");
assertThat(result.get(0).getRank()).isEqualTo(1);

assertThat(result.get(1).getProfileImage()).isEqualTo("image1");
assertThat(result.get(1).getSolvedCount()).isEqualTo(1L);
assertThat(result.get(1).getUserNickname()).isEqualTo("nickname1");
assertThat((result.get(1).getRank())).isEqualTo(2);
}

@Test
@DisplayName("전체랭킹 조회 실패 : 그룹을 못 찾은 경우")
void getAllRank_FailedByCannotFoundGroup() {
//given
when(studyGroupRepository.findById(9L)).thenReturn(Optional.empty());

//then
assertThatThrownBy(() -> studyGroupService.getAllRank(user, 9L))
.isInstanceOf(CannotFoundGroupException.class)
.hasFieldOrPropertyWithValue("errors", "그룹을 찾을 수 없습니다.");
}

@Test
@DisplayName("전체랭킹 조회 실패 : 랭킹을 확인할 권한이 없는경우")
void getAllRank_FailedByAccess() {
//given
when(studyGroupRepository.findById(groupId)).thenReturn(Optional.of(group));
when(groupMemberRepository.existsByUserAndStudyGroup(user2, group)).thenReturn(false);

//then
assertThatThrownBy(() -> studyGroupService.getAllRank(user2, groupId))
.isInstanceOf(UserValidationException.class)
.hasFieldOrPropertyWithValue("errors", "랭킹을 확인할 권한이 없습니다.");
}
}