33import com .somemore .IntegrationTestSupport ;
44import com .somemore .auth .oauth .OAuthProvider ;
55import com .somemore .volunteer .domain .Volunteer ;
6+ import com .somemore .volunteer .repository .mapper .VolunteerOverviewForRankingByHours ;
67import org .junit .jupiter .api .BeforeEach ;
78import org .junit .jupiter .api .DisplayName ;
89import org .junit .jupiter .api .Test ;
910import org .springframework .beans .factory .annotation .Autowired ;
1011import org .springframework .transaction .annotation .Transactional ;
1112
13+ import java .util .List ;
1214import java .util .Optional ;
1315import java .util .UUID ;
1416
@@ -76,4 +78,57 @@ void findByOauthId() {
7678 assertThat (foundVolunteer .get ().getOauthId ()).isEqualTo (oAuthId );
7779 assertThat (foundVolunteer .get ().getNickname ()).isEqualTo (volunteer .getNickname ());
7880 }
81+
82+ @ DisplayName ("봉사 시간 기준 상위 4명을 조회한다." )
83+ @ Test
84+ void findRankingByVolunteerHours_top4 () {
85+ // given
86+ for (int i = 1 ; i <= 5 ; i ++) {
87+ createVolunteerAndUpdateVolunteerStats (i );
88+ }
89+
90+ // when
91+ List <VolunteerOverviewForRankingByHours > rankings = volunteerRepository .findRankingByVolunteerHours ();
92+
93+ // then
94+ assertThat (rankings ).hasSize (4 );
95+ assertThat (rankings .get (0 ).totalVolunteerHours ()).isGreaterThan (rankings .get (1 ).totalVolunteerHours ());
96+ }
97+
98+ @ DisplayName ("등록된 봉사자가 없는 경우 빈 리스트를 반환한다." )
99+ @ Test
100+ void findRankingByVolunteerHours_noVolunteers () {
101+ // given
102+ volunteerRepository .deleteAllInBatch ();
103+
104+ // when
105+ List <VolunteerOverviewForRankingByHours > rankings = volunteerRepository .findRankingByVolunteerHours ();
106+
107+ // then
108+ assertThat (rankings ).isEmpty ();
109+ }
110+
111+ @ DisplayName ("등록된 봉사자가 4명 이하인 경우 전체 봉사자를 반환한다." )
112+ @ Test
113+ void findRankingByVolunteerHours_lessThan4Volunteers () {
114+ // given
115+ volunteerRepository .deleteAllInBatch ();
116+
117+ for (int i = 1 ; i <= 3 ; i ++) {
118+ createVolunteerAndUpdateVolunteerStats (i );
119+ }
120+
121+ // when
122+ List <VolunteerOverviewForRankingByHours > rankings = volunteerRepository .findRankingByVolunteerHours ();
123+
124+ // then
125+ assertThat (rankings ).hasSize (3 );
126+ assertThat (rankings .get (0 ).totalVolunteerHours ()).isGreaterThan (rankings .get (1 ).totalVolunteerHours ());
127+ }
128+
129+ private void createVolunteerAndUpdateVolunteerStats (int i ) {
130+ Volunteer volunteer = Volunteer .createDefault (OAuthProvider .NAVER , "oauth-id-" + i );
131+ volunteer .updateVolunteerStats (i * 10 , i );
132+ volunteerRepository .save (volunteer );
133+ }
79134}
0 commit comments