77import com .somemore .community .repository .board .CommunityBoardRepository ;
88import com .somemore .volunteer .domain .Volunteer ;
99import com .somemore .volunteer .repository .VolunteerRepository ;
10+ import org .junit .jupiter .api .BeforeEach ;
1011import org .junit .jupiter .api .DisplayName ;
1112import org .junit .jupiter .api .Test ;
1213import org .springframework .beans .factory .annotation .Autowired ;
14+ import org .springframework .data .domain .Page ;
15+ import org .springframework .data .domain .PageRequest ;
16+ import org .springframework .data .domain .Pageable ;
1317import org .springframework .transaction .annotation .Transactional ;
1418
15- import java .util .List ;
19+ import static com .somemore .common .fixture .CommunityBoardFixture .createCommunityBoard ;
20+
1621import java .util .Optional ;
1722import java .util .UUID ;
1823
@@ -26,17 +31,37 @@ class CommunityBoardRepositoryTest extends IntegrationTestSupport {
2631 @ Autowired
2732 private VolunteerRepository volunteerRepository ;
2833
34+ private UUID writerId ;
35+
36+ @ BeforeEach
37+ void setUp () {
38+ String oAuthId1 = "example-oauth-id1" ;
39+ Volunteer volunteer1 = Volunteer .createDefault (OAuthProvider .NAVER , oAuthId1 );
40+ volunteerRepository .save (volunteer1 );
41+ writerId = volunteer1 .getId ();
42+
43+ String oAuthId2 = "example-oauth-id2" ;
44+ Volunteer volunteer2 = Volunteer .createDefault (OAuthProvider .NAVER , oAuthId2 );
45+ volunteerRepository .save (volunteer2 );
46+
47+ for (int i = 1 ; i <= 8 ; i ++) {
48+ String title = "제목" + i ;
49+ CommunityBoard communityBoard = createCommunityBoard (title , writerId );
50+ communityBoardRepository .save (communityBoard );
51+ }
52+
53+ for (int i = 1 ; i <= 20 ; i ++) {
54+ String title = "제목" + i ;
55+ CommunityBoard communityBoard = createCommunityBoard (title , volunteer2 .getId ());
56+ communityBoardRepository .save (communityBoard );
57+ }
58+ }
59+
2960 @ DisplayName ("커뮤니티 게시글 id로 커뮤니티 상세 정보를 조회할 수 있다. (Repository)" )
3061 @ Test
3162 void getCommunityBoardById () {
3263 //given
33- CommunityBoard communityBoard = CommunityBoard .builder ()
34- .title ("테스트 커뮤니티 게시글 제목" )
35- .content ("테스트 커뮤니티 게시글 내용" )
36- .imgUrl ("http://community.example.com/123" )
37- .writerId (UUID .randomUUID ())
38- .build ();
39-
64+ CommunityBoard communityBoard = createCommunityBoard (UUID .randomUUID ());
4065 communityBoardRepository .save (communityBoard );
4166
4267 //when
@@ -54,14 +79,9 @@ void getCommunityBoardById() {
5479 @ Test
5580 void getCommunityBoardByDeletedId () {
5681 //given
57- CommunityBoard communityBoard = CommunityBoard .builder ()
58- .title ("테스트 커뮤니티 게시글 제목" )
59- .content ("테스트 커뮤니티 게시글 내용" )
60- .imgUrl ("http://community.example.com/123" )
61- .writerId (UUID .randomUUID ())
62- .build ();
63-
82+ CommunityBoard communityBoard = createCommunityBoard (UUID .randomUUID ());
6483 communityBoardRepository .save (communityBoard );
84+
6585 communityBoardRepository .deleteAllInBatch ();
6686
6787 //when
@@ -71,114 +91,54 @@ void getCommunityBoardByDeletedId() {
7191 assertThat (foundCommunityBoard ).isEmpty ();
7292 }
7393
74- @ DisplayName ("저장된 전체 커뮤니티 게시글을 목록으로 조회할 수 있다. (Repository)" )
94+ @ DisplayName ("저장된 전체 커뮤니티 게시글을 페이지로 조회할 수 있다. (Repository)" )
7595 @ Test
7696 void getCommunityBoards () {
97+
7798 //given
78- String oAuthId = "example-oauth-id" ;
79- Volunteer volunteer = Volunteer .createDefault (OAuthProvider .NAVER , oAuthId );
80- volunteerRepository .save (volunteer );
81-
82- CommunityBoard communityBoard1 = CommunityBoard .builder ()
83- .title ("테스트 커뮤니티 게시글 제목1" )
84- .content ("테스트 커뮤니티 게시글 내용1" )
85- .imgUrl ("http://community.example.com/123" )
86- .writerId (volunteer .getId ())
87- .build ();
88-
89- CommunityBoard communityBoard2 = CommunityBoard .builder ()
90- .title ("테스트 커뮤니티 게시글 제목2" )
91- .content ("테스트 커뮤니티 게시글 내용2" )
92- .imgUrl ("http://community.example.com/12" )
93- .writerId (volunteer .getId ())
94- .build ();
95-
96- communityBoardRepository .save (communityBoard1 );
97- communityBoardRepository .save (communityBoard2 );
99+ Pageable pageable = getPageable ();
98100
99101 //when
100- List <CommunityBoardView > communityBoards = communityBoardRepository .getCommunityBoards ( );
102+ Page <CommunityBoardView > communityBoards = communityBoardRepository .findCommunityBoards ( pageable );
101103
102104 //then
103- assertThat (communityBoards ).hasSize (2 );
104- assertThat (communityBoards .getFirst ().communityBoard ().getId ()).isEqualTo (communityBoard2 .getId ());
105- assertThat (communityBoards .getFirst ().communityBoard ().getTitle ()).isEqualTo (communityBoard2 .getTitle ());
106- assertThat (communityBoards .getFirst ().writerNickname ()).isEqualTo (volunteer .getNickname ());
107- assertThat (communityBoards .getFirst ().communityBoard ().getCreatedAt ()).isEqualTo (communityBoard2 .getCreatedAt ());
108- assertThat (communityBoards .getLast ().communityBoard ().getId ()).isEqualTo (communityBoard1 .getId ());
109- assertThat (communityBoards .getLast ().communityBoard ().getTitle ()).isEqualTo (communityBoard1 .getTitle ());
110- assertThat (communityBoards .getLast ().writerNickname ()).isEqualTo (volunteer .getNickname ());
111- assertThat (communityBoards .getLast ().communityBoard ().getCreatedAt ()).isEqualTo (communityBoard1 .getCreatedAt ());
105+ assertThat (communityBoards ).isNotNull ();
106+ assertThat (communityBoards .getTotalElements ()).isEqualTo (28 );
107+ assertThat (communityBoards .getSize ()).isEqualTo (10 );
108+ assertThat (communityBoards .getNumber ()).isZero ();
112109 }
113110
114111 @ DisplayName ("저장된 커뮤니티 게시글 리스트를 작성자별로 조회할 수 있다. (Repository)" )
115112 @ Test
116113 void getCommunityBoardsByWriterId () {
117114 //given
118- String oAuthId = "example-oauth-id" ;
119- Volunteer volunteer = Volunteer .createDefault (OAuthProvider .NAVER , oAuthId );
120- volunteerRepository .save (volunteer );
121-
122- CommunityBoard communityBoard1 = CommunityBoard .builder ()
123- .title ("테스트 커뮤니티 게시글 제목1" )
124- .content ("테스트 커뮤니티 게시글 내용1" )
125- .imgUrl ("http://community.example.com/123" )
126- .writerId (volunteer .getId ())
127- .build ();
128-
129- CommunityBoard communityBoard2 = CommunityBoard .builder ()
130- .title ("테스트 커뮤니티 게시글 제목2" )
131- .content ("테스트 커뮤니티 게시글 내용2" )
132- .imgUrl ("http://community.example.com/12" )
133- .writerId (volunteer .getId ())
134- .build ();
135-
136- CommunityBoard communityBoard3 = CommunityBoard .builder ()
137- .title ("테스트 커뮤니티 게시글 제목3" )
138- .content ("테스트 커뮤니티 게시글 내용3" )
139- .imgUrl ("http://community.example.com/1" )
140- .writerId (UUID .randomUUID ())
141- .build ();
142-
143- communityBoardRepository .save (communityBoard1 );
144- communityBoardRepository .save (communityBoard2 );
145- communityBoardRepository .save (communityBoard3 );
115+ Pageable pageable = getPageable ();
146116
147117 //when
148- List <CommunityBoardView > communityBoards = communityBoardRepository .findByWriterId (volunteer . getId () );
118+ Page <CommunityBoardView > communityBoards = communityBoardRepository .findByWriterId (writerId , pageable );
149119
150120 //then
151- assertThat (communityBoards ).hasSize (2 );
152- assertThat (communityBoards .getFirst ().communityBoard ().getId ()).isEqualTo (communityBoard2 .getId ());
153- assertThat (communityBoards .getFirst ().communityBoard ().getTitle ()).isEqualTo (communityBoard2 .getTitle ());
154- assertThat (communityBoards .getFirst ().writerNickname ()).isEqualTo (volunteer .getNickname ());
155- assertThat (communityBoards .getFirst ().communityBoard ().getCreatedAt ()).isEqualTo (communityBoard2 .getCreatedAt ());
156- assertThat (communityBoards .getLast ().communityBoard ().getId ()).isEqualTo (communityBoard1 .getId ());
157- assertThat (communityBoards .getLast ().communityBoard ().getTitle ()).isEqualTo (communityBoard1 .getTitle ());
158- assertThat (communityBoards .getLast ().writerNickname ()).isEqualTo (volunteer .getNickname ());
159- assertThat (communityBoards .getLast ().communityBoard ().getCreatedAt ()).isEqualTo (communityBoard1 .getCreatedAt ());
121+ assertThat (communityBoards ).isNotNull ();
122+ assertThat (communityBoards .getTotalElements ()).isEqualTo (8 );
123+ assertThat (communityBoards .getSize ()).isEqualTo (10 );
124+ assertThat (communityBoards .getNumber ()).isZero ();
160125 }
161126
162127 @ DisplayName ("게시글 id로 게시글이 존재하는지 확인할 수 있다." )
163128 @ Test
164129 void existsById () {
165-
166130 //given
167- UUID writerId = UUID .randomUUID ();
168-
169- CommunityBoard communityBoard = CommunityBoard .builder ()
170- .title ("테스트 커뮤니티 게시글 제목" )
171- .content ("테스트 커뮤니티 게시글 내용" )
172- .imgUrl ("http://community.example.com/123" )
173- .writerId (writerId )
174- .build ();
175-
176- CommunityBoard savedComment = communityBoardRepository .save (communityBoard );
131+ CommunityBoard communityBoard = createCommunityBoard ();
132+ communityBoardRepository .save (communityBoard );
177133
178134 //when
179- boolean isExist = communityBoardRepository .existsById (savedComment .getId ());
135+ boolean isExist = communityBoardRepository .existsById (communityBoard .getId ());
180136
181137 //then
182138 assertThat (isExist ).isTrue ();
183139 }
140+
141+ private Pageable getPageable () {
142+ return PageRequest .of (0 , 10 );
143+ }
184144}
0 commit comments