Skip to content

Commit aa4c16d

Browse files
committed
test(search): community 게시글 검색 테스트 추가
1 parent 0a99d88 commit aa4c16d

File tree

5 files changed

+217
-0
lines changed

5 files changed

+217
-0
lines changed

src/test/java/com/somemore/community/controller/CommunityBoardQueryApiControllerTest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import com.somemore.ControllerTestSupport;
1212
import com.somemore.community.dto.response.CommunityBoardDetailResponseDto;
1313
import com.somemore.community.dto.response.CommunityBoardResponseDto;
14+
import com.somemore.community.usecase.board.CommunityBoardDocumentUseCase;
1415
import com.somemore.community.usecase.board.CommunityBoardQueryUseCase;
1516
import java.util.Collections;
1617
import java.util.UUID;
@@ -32,6 +33,9 @@ public class CommunityBoardQueryApiControllerTest extends ControllerTestSupport
3233
@MockBean
3334
private CommunityBoardQueryUseCase communityBoardQueryUseCase;
3435

36+
@MockBean
37+
private CommunityBoardDocumentUseCase communityBoardDocumentUseCase;
38+
3539
@Test
3640
@DisplayName("커뮤니티 게시글 전체 조회 성공")
3741
void getAll() throws Exception {
@@ -101,4 +105,27 @@ void getById() throws Exception {
101105
verify(communityBoardQueryUseCase, times(1))
102106
.getCommunityBoardDetail(any());
103107
}
108+
109+
@Test
110+
@DisplayName("커뮤니티 게시글 검색 조회 성공")
111+
void getBySearch() throws Exception {
112+
// given
113+
Page<CommunityBoardResponseDto> page = new PageImpl<>(Collections.emptyList());
114+
115+
given(communityBoardDocumentUseCase.getCommunityBoardBySearch(any(), anyInt()))
116+
.willReturn(page);
117+
118+
// when
119+
// then
120+
mockMvc.perform(get("/api/community-boards/search")
121+
.param("keyword", "봉사")
122+
.accept(MediaType.APPLICATION_JSON))
123+
.andExpect(status().isOk())
124+
.andExpect(jsonPath("$.code").value(200))
125+
.andExpect(jsonPath("$.data").exists())
126+
.andExpect(jsonPath("$.message").value("커뮤니티 게시글 검색 리스트 조회 성공"));
127+
128+
verify(communityBoardDocumentUseCase, times(1)).getCommunityBoardBySearch(
129+
any(), anyInt());
130+
}
104131
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package com.somemore.community.repository;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.auth.oauth.OAuthProvider;
5+
import com.somemore.community.domain.CommunityBoard;
6+
import com.somemore.community.repository.board.CommunityBoardRepository;
7+
import com.somemore.community.repository.mapper.CommunityBoardView;
8+
import com.somemore.volunteer.domain.Volunteer;
9+
import com.somemore.volunteer.repository.VolunteerRepository;
10+
import org.junit.jupiter.api.BeforeEach;
11+
import org.junit.jupiter.api.DisplayName;
12+
import org.junit.jupiter.api.Test;
13+
import 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;
17+
import org.springframework.transaction.annotation.Transactional;
18+
19+
20+
import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard;
21+
import static org.assertj.core.api.Assertions.assertThat;
22+
23+
@Transactional
24+
public class CommunityBoardDocumentRepositoryTest extends IntegrationTestSupport {
25+
26+
@Autowired
27+
private CommunityBoardRepository communityBoardRepository;
28+
@Autowired
29+
private VolunteerRepository volunteerRepository;
30+
31+
@BeforeEach
32+
void setUp() {
33+
String oAuthId = "example-oauth-id2";
34+
Volunteer volunteer = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId);
35+
volunteerRepository.save(volunteer);
36+
37+
for (int i = 1; i <= 20; i++) {
38+
String title = "제목" + i;
39+
CommunityBoard communityBoard = createCommunityBoard(title, volunteer.getId());
40+
communityBoardRepository.save(communityBoard);
41+
}
42+
}
43+
44+
@DisplayName("검색 키워드가 포함된 게시글을 조회할 수 있다. (repository)")
45+
@Test
46+
void findByCommunityBoardsContaining() throws InterruptedException {
47+
//given
48+
Pageable pageable = getPageable();
49+
50+
//when
51+
Page<CommunityBoardView> findBoards = communityBoardRepository.findByCommunityBoardsContaining("봉사", pageable);
52+
53+
//then
54+
assertThat(findBoards).isNotNull();
55+
assertThat(findBoards.getTotalElements()).isEqualTo(9);
56+
assertThat(findBoards.getSize()).isEqualTo(10);
57+
assertThat(findBoards.getTotalPages()).isEqualTo(1);
58+
}
59+
60+
private Pageable getPageable() {
61+
return PageRequest.of(0, 10);
62+
}
63+
}

src/test/java/com/somemore/community/repository/CommunityBoardRepositoryTest.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard;
2020

21+
import java.util.ArrayList;
22+
import java.util.List;
2123
import java.util.Optional;
2224
import java.util.UUID;
2325

@@ -138,6 +140,34 @@ void existsById() {
138140
assertThat(isExist).isTrue();
139141
}
140142

143+
@DisplayName("게시글을 elastic search index에 저장할 수 있다. (repository)")
144+
@Test
145+
void saveDocuments() {
146+
//given
147+
Pageable pageable = getPageable();
148+
149+
List<CommunityBoard> communityBoards = new ArrayList<>();
150+
151+
CommunityBoard communityBoard1 = createCommunityBoard("저장 잘 되나요?", writerId);
152+
CommunityBoard savedBoard1 = communityBoardRepository.save(communityBoard1);
153+
CommunityBoard communityBoard2 = createCommunityBoard("잘 되나요?", "저장이요", writerId);
154+
CommunityBoard savedBoard2 = communityBoardRepository.save(communityBoard2);
155+
communityBoards.add(savedBoard1);
156+
communityBoards.add(savedBoard2);
157+
158+
//when
159+
communityBoardRepository.saveDocuments(communityBoards);
160+
161+
//then
162+
Page<CommunityBoardView> findBoard = communityBoardRepository.findByCommunityBoardsContaining("저장", pageable);
163+
164+
assertThat(findBoard).isNotNull();
165+
assertThat(findBoard.getTotalElements()).isEqualTo(2);
166+
167+
communityBoardRepository.deleteDocument(savedBoard1.getId());
168+
communityBoardRepository.deleteDocument(savedBoard2.getId());
169+
}
170+
141171
private Pageable getPageable() {
142172
return PageRequest.of(0, 10);
143173
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.somemore.community.service.board;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.auth.oauth.OAuthProvider;
5+
import com.somemore.community.domain.CommunityBoard;
6+
import com.somemore.community.dto.response.CommunityBoardResponseDto;
7+
import com.somemore.community.repository.board.CommunityBoardRepository;
8+
import com.somemore.volunteer.domain.Volunteer;
9+
import com.somemore.volunteer.repository.VolunteerRepository;
10+
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeEach;
12+
import org.junit.jupiter.api.DisplayName;
13+
import org.junit.jupiter.api.Test;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.data.domain.Page;
16+
17+
import java.util.UUID;
18+
19+
import static com.somemore.common.fixture.CommunityBoardFixture.createCommunityBoard;
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
public class CommunityBoardDocumentServiceTest extends IntegrationTestSupport {
23+
24+
@Autowired
25+
private CommunityBoardDocumentService communityBoardDocumentService;
26+
27+
@Autowired
28+
private CommunityBoardRepository communityBoardRepository;
29+
30+
@Autowired
31+
private VolunteerRepository volunteerRepository;
32+
33+
private UUID writerId;
34+
35+
@BeforeEach
36+
void setUp() {
37+
String oAuthId1 = "example-oauth-id1";
38+
Volunteer volunteer1 = Volunteer.createDefault(OAuthProvider.NAVER, oAuthId1);
39+
volunteerRepository.save(volunteer1);
40+
writerId = volunteer1.getId();
41+
42+
for (int i = 1; i <= 18; i++) {
43+
String title = "제목" + i;
44+
CommunityBoard communityBoard = createCommunityBoard(title, writerId);
45+
communityBoardRepository.save(communityBoard);
46+
}
47+
}
48+
49+
@AfterEach
50+
void tearDown() {
51+
communityBoardRepository.deleteAllInBatch();
52+
}
53+
54+
@DisplayName("검색 키워드가 포함된 게시글을 조회한다. (service)")
55+
@Test
56+
void getCommunityBoardBySearch() {
57+
//given
58+
//when
59+
Page<CommunityBoardResponseDto> dtos = communityBoardDocumentService.getCommunityBoardBySearch("봉사", 0);
60+
61+
//then
62+
assertThat(dtos).isNotNull();
63+
assertThat(dtos.getContent()).isNotNull();
64+
assertThat(dtos.getTotalElements()).isEqualTo(9);
65+
assertThat(dtos.getSize()).isEqualTo(10);
66+
assertThat(dtos.getTotalPages()).isEqualTo(1);
67+
}
68+
}

src/test/java/com/somemore/community/service/board/CommunityBoardQueryServiceTest.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import org.springframework.beans.factory.annotation.Autowired;
2222
import org.springframework.data.domain.Page;
2323

24+
import java.util.ArrayList;
25+
import java.util.List;
2426
import java.util.UUID;
2527

2628
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,6 +45,8 @@ class CommunityBoardQueryServiceTest extends IntegrationTestSupport {
4345
DeleteCommunityBoardUseCase deleteCommunityBoardUseCase;
4446
@Autowired
4547
CommunityBoardQueryService communityBoardQueryService;
48+
@Autowired
49+
private CommunityBoardDocumentService communityBoardDocumentService;
4650

4751
private UUID writerId1;
4852
private Long communityId1;
@@ -146,5 +150,30 @@ void getCommunityBoardDetailWithDeletedId() {
146150
.isThrownBy(callable)
147151
.withMessage(ExceptionMessage.NOT_EXISTS_COMMUNITY_BOARD.getMessage());
148152
}
153+
154+
@DisplayName("게시글을 elastic search index에 저장한다. (service)")
155+
@Test
156+
void saveCommunityBoardDocuments() {
157+
//given
158+
List<CommunityBoard> communityBoards = new ArrayList<>();
159+
160+
CommunityBoard communityBoard = createCommunityBoard("저장 잘 되나요?", "안녕하세요", UUID.randomUUID());
161+
CommunityBoard savedBoard = communityBoardRepository.save(communityBoard);
162+
communityBoards.add(savedBoard);
163+
164+
//when
165+
communityBoardDocumentService.saveCommunityBoardDocuments(communityBoards);
166+
167+
//then
168+
Page<CommunityBoardResponseDto> dtos = communityBoardDocumentService.getCommunityBoardBySearch("저장", 0);
169+
170+
assertThat(dtos).isNotNull();
171+
assertThat(dtos.getContent()).isNotNull();
172+
assertThat(dtos.getTotalElements()).isEqualTo(1);
173+
assertThat(dtos.getSize()).isEqualTo(10);
174+
assertThat(dtos.getTotalPages()).isEqualTo(1);
175+
176+
communityBoardRepository.deleteDocument(savedBoard.getId());
177+
}
149178
}
150179

0 commit comments

Comments
 (0)