Skip to content

Commit 4d59d93

Browse files
committed
test(search): recruit 모집글 검색 테스트 추가
1 parent aa4c16d commit 4d59d93

File tree

5 files changed

+249
-3
lines changed

5 files changed

+249
-3
lines changed

src/test/java/com/somemore/recruitboard/controller/RecruitBoardQueryApiControllerTest.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import com.somemore.recruitboard.dto.response.RecruitBoardResponseDto;
1818
import com.somemore.recruitboard.dto.response.RecruitBoardWithCenterResponseDto;
1919
import com.somemore.recruitboard.dto.response.RecruitBoardWithLocationResponseDto;
20+
import com.somemore.recruitboard.usecase.query.RecruitBoardDocumentUseCase;
2021
import com.somemore.recruitboard.usecase.query.RecruitBoardQueryUseCase;
2122
import java.util.Collections;
2223
import java.util.UUID;
@@ -37,6 +38,9 @@ class RecruitBoardQueryApiControllerTest extends ControllerTestSupport {
3738
@MockBean
3839
private RecruitBoardQueryUseCase recruitBoardQueryUseCase;
3940

41+
@MockBean
42+
private RecruitBoardDocumentUseCase documentUseCase;
43+
4044
@Test
4145
@DisplayName("모집글 ID로 상세 조회할 수 있다.")
4246
void getById() throws Exception {
@@ -85,7 +89,7 @@ void getAllBySearch() throws Exception {
8589
// given
8690
Page<RecruitBoardWithCenterResponseDto> page = new PageImpl<>(Collections.emptyList());
8791

88-
given(recruitBoardQueryUseCase.getAllWithCenter(any(RecruitBoardSearchCondition.class)))
92+
given(documentUseCase.getRecruitBoardBySearch(any(), any(RecruitBoardSearchCondition.class)))
8993
.willReturn(page);
9094

9195
// when
@@ -99,8 +103,8 @@ void getAllBySearch() throws Exception {
99103
.andExpect(jsonPath("$.data").exists())
100104
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 검색 조회 성공"));
101105

102-
verify(recruitBoardQueryUseCase, times(1)).getAllWithCenter(
103-
any(RecruitBoardSearchCondition.class));
106+
verify(documentUseCase, times(1)).getRecruitBoardBySearch(
107+
any(), any(RecruitBoardSearchCondition.class));
104108
}
105109

106110
@Test
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.somemore.recruitboard.repository;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.Center;
5+
import com.somemore.center.repository.CenterRepository;
6+
import com.somemore.location.domain.Location;
7+
import com.somemore.location.repository.LocationRepository;
8+
import com.somemore.recruitboard.domain.RecruitBoard;
9+
import com.somemore.recruitboard.dto.condition.RecruitBoardSearchCondition;
10+
import com.somemore.recruitboard.repository.mapper.RecruitBoardWithCenter;
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+
import org.springframework.data.domain.PageRequest;
17+
import org.springframework.data.domain.Pageable;
18+
import org.springframework.data.domain.Sort;
19+
import org.springframework.transaction.annotation.Transactional;
20+
21+
import java.util.ArrayList;
22+
import java.util.List;
23+
24+
import static com.somemore.common.fixture.CenterFixture.createCenter;
25+
import static com.somemore.common.fixture.LocationFixture.createLocation;
26+
import static com.somemore.common.fixture.RecruitBoardFixture.createRecruitBoard;
27+
import static org.assertj.core.api.Assertions.assertThat;
28+
29+
@Transactional
30+
public class RecruitBoardDocumentRepositoryTest extends IntegrationTestSupport {
31+
32+
@Autowired
33+
private RecruitBoardRepository recruitBoardRepository;
34+
35+
@Autowired
36+
private LocationRepository locationRepository;
37+
38+
@Autowired
39+
private CenterRepository centerRepository;
40+
41+
private final List<RecruitBoard> boards = new ArrayList<>();
42+
43+
@BeforeEach
44+
void setUp() {
45+
Location location = createLocation();
46+
locationRepository.save(location);
47+
48+
Center center = createCenter();
49+
centerRepository.save(center);
50+
51+
for (int i = 1; i <= 30; i++) {
52+
String title = "제목" + i;
53+
RecruitBoard board = createRecruitBoard(title, center.getId(), location.getId());
54+
boards.add(board);
55+
}
56+
recruitBoardRepository.saveAll(boards);
57+
}
58+
59+
@DisplayName("검색 키워드가 포함된 모집글을 조회할 수 있다.")
60+
@Test
61+
void findByRecruitBoardsContaining() {
62+
//given
63+
Pageable pageable = getPageable();
64+
RecruitBoardSearchCondition condition = RecruitBoardSearchCondition.builder()
65+
.pageable(pageable)
66+
.build();
67+
68+
//when
69+
Page<RecruitBoardWithCenter> findBoards = recruitBoardRepository.findByRecruitBoardsContaining("없음", condition);
70+
71+
//then
72+
assertThat(findBoards).isNotNull();
73+
assertThat(findBoards.getTotalElements()).isEqualTo(4);
74+
assertThat(findBoards.getSize()).isEqualTo(5);
75+
assertThat(findBoards.getTotalPages()).isEqualTo(1);
76+
}
77+
78+
private Pageable getPageable() {
79+
Sort sort = Sort.by(Sort.Order.desc("created_at"));
80+
return PageRequest.of(0, 5, sort);
81+
}
82+
}

src/test/java/com/somemore/recruitboard/repository/RecruitBoardRepositoryImplTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,16 @@ class RecruitBoardRepositoryImplTest extends IntegrationTestSupport {
5151

5252
private final List<RecruitBoard> boards = new ArrayList<>();
5353

54+
private UUID centerId;
55+
5456
@BeforeEach
5557
void setUp() {
5658
Location location = createLocation();
5759
locationRepository.save(location);
5860

5961
Center center = createCenter();
6062
centerRepository.save(center);
63+
centerId = center.getId();
6164

6265
for (int i = 1; i <= 100; i++) {
6366
String title = "제목" + i;
@@ -426,6 +429,37 @@ void findAllByIds() {
426429
assertThat(all).hasSize(3);
427430
}
428431

432+
@DisplayName("모집글을 elastic search index에 저장할 수 있다. (repository)")
433+
@Test
434+
void saveDocuments() {
435+
//given
436+
Pageable pageable = getPageable();
437+
RecruitBoardSearchCondition condition = RecruitBoardSearchCondition.builder()
438+
.pageable(pageable)
439+
.build();
440+
441+
List<RecruitBoard> recruitBoards = new ArrayList<>();
442+
443+
RecruitBoard board1 = createRecruitBoard("저장 잘 되나요?", centerId);
444+
RecruitBoard savedBoard1 = recruitBoardRepository.save(board1);
445+
RecruitBoard board2 = createRecruitBoard("저장해줘", centerId);
446+
RecruitBoard savedBoard2 = recruitBoardRepository.save(board2);
447+
recruitBoards.add(savedBoard1);
448+
recruitBoards.add(savedBoard2);
449+
450+
//when
451+
recruitBoardRepository.saveDocuments(recruitBoards);
452+
453+
//then
454+
Page<RecruitBoardWithCenter> findBoard = recruitBoardRepository.findByRecruitBoardsContaining("저장", condition);
455+
456+
assertThat(findBoard).isNotNull();
457+
assertThat(findBoard.getTotalElements()).isEqualTo(2);
458+
459+
recruitBoardRepository.deleteDocument(savedBoard1.getId());
460+
recruitBoardRepository.deleteDocument(savedBoard2.getId());
461+
}
462+
429463
private Pageable getPageable() {
430464
Sort sort = Sort.by(Sort.Order.desc("created_at"));
431465
return PageRequest.of(0, 5, sort);
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
package com.somemore.recruitboard.service.query;
2+
3+
import com.somemore.IntegrationTestSupport;
4+
import com.somemore.center.domain.Center;
5+
import com.somemore.center.repository.CenterRepository;
6+
import com.somemore.location.domain.Location;
7+
import com.somemore.location.repository.LocationRepository;
8+
import com.somemore.recruitboard.domain.RecruitBoard;
9+
import com.somemore.recruitboard.dto.condition.RecruitBoardSearchCondition;
10+
import com.somemore.recruitboard.dto.response.RecruitBoardWithCenterResponseDto;
11+
import com.somemore.recruitboard.repository.RecruitBoardRepository;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.DisplayName;
14+
import org.junit.jupiter.api.Test;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.data.domain.Page;
17+
import org.springframework.data.domain.PageRequest;
18+
import org.springframework.data.domain.Pageable;
19+
import org.springframework.data.domain.Sort;
20+
import org.springframework.transaction.annotation.Transactional;
21+
22+
import java.util.ArrayList;
23+
import java.util.List;
24+
25+
import static com.somemore.common.fixture.CenterFixture.createCenter;
26+
import static com.somemore.common.fixture.LocationFixture.createLocation;
27+
import static com.somemore.common.fixture.RecruitBoardFixture.createRecruitBoard;
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
@Transactional
31+
public class RecruitBoardDocumentServiceTest extends IntegrationTestSupport {
32+
33+
@Autowired
34+
private RecruitBoardDocumentService recruitBoardDocumentService;
35+
36+
@Autowired
37+
private RecruitBoardRepository recruitBoardRepository;
38+
39+
@Autowired
40+
private LocationRepository locationRepository;
41+
42+
@Autowired
43+
private CenterRepository centerRepository;
44+
45+
private final List<RecruitBoard> boards = new ArrayList<>();
46+
47+
@BeforeEach
48+
public void setUp() {
49+
Location location = createLocation();
50+
locationRepository.save(location);
51+
52+
Center center = createCenter();
53+
centerRepository.save(center);
54+
55+
for (int i = 1; i <= 30; i++) {
56+
String title = "제목" + i;
57+
RecruitBoard board = createRecruitBoard(title, center.getId(), location.getId());
58+
boards.add(board);
59+
}
60+
recruitBoardRepository.saveAll(boards);
61+
}
62+
63+
@DisplayName("검색 키워드가 포함된 모집글을 조회한다. (service)")
64+
@Test
65+
void getRecruitBoardBySearch() {
66+
//given
67+
Pageable pageable = getPageable();
68+
RecruitBoardSearchCondition condition = RecruitBoardSearchCondition.builder()
69+
.pageable(pageable)
70+
.build();
71+
72+
//when
73+
Page<RecruitBoardWithCenterResponseDto> dtos = recruitBoardDocumentService.getRecruitBoardBySearch("노인", condition);
74+
75+
//then
76+
assertThat(dtos).isNotNull();
77+
assertThat(dtos.getContent()).isNotNull();
78+
assertThat(dtos.getTotalElements()).isEqualTo(4);
79+
assertThat(dtos.getSize()).isEqualTo(5);
80+
assertThat(dtos.getTotalPages()).isEqualTo(1);
81+
}
82+
83+
private Pageable getPageable() {
84+
Sort sort = Sort.by(Sort.Order.desc("created_at"));
85+
return PageRequest.of(0, 5, sort);
86+
}
87+
}

src/test/java/com/somemore/recruitboard/service/query/RecruitBoardQueryServiceTest.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import com.somemore.recruitboard.dto.response.RecruitBoardWithCenterResponseDto;
2626
import com.somemore.recruitboard.dto.response.RecruitBoardWithLocationResponseDto;
2727
import com.somemore.recruitboard.repository.RecruitBoardRepository;
28+
29+
import java.util.ArrayList;
2830
import java.util.List;
2931
import java.util.UUID;
3032
import org.junit.jupiter.api.BeforeEach;
@@ -43,6 +45,9 @@ class RecruitBoardQueryServiceTest extends IntegrationTestSupport {
4345
@Autowired
4446
private RecruitBoardQueryService recruitBoardQueryService;
4547

48+
@Autowired
49+
private RecruitBoardDocumentService recruitBoardDocumentService;
50+
4651
@Autowired
4752
private RecruitBoardRepository recruitBoardRepository;
4853

@@ -280,6 +285,40 @@ void getAllByIds() {
280285
assertThat(all).hasSize(3);
281286
}
282287

288+
@DisplayName("모집글을 elastic search index에 저장한다. (service)")
289+
@Test
290+
void saveRecruitBoardDocuments() {
291+
//given
292+
Center center = createCenter("특별한 기관");
293+
centerRepository.save(center);
294+
295+
Pageable pageable = getPageable();
296+
RecruitBoardSearchCondition condition = RecruitBoardSearchCondition.builder()
297+
.pageable(pageable)
298+
.build();
299+
300+
List<RecruitBoard> recruitBoards = new ArrayList<>();
301+
302+
RecruitBoard board1 = createRecruitBoard("저장 잘 되나요?", center.getId());
303+
RecruitBoard savedBoard1 = recruitBoardRepository.save(board1);
304+
RecruitBoard board2 = createRecruitBoard("저장해줘", center.getId());
305+
RecruitBoard savedBoard2 = recruitBoardRepository.save(board2);
306+
recruitBoards.add(savedBoard1);
307+
recruitBoards.add(savedBoard2);
308+
309+
//when
310+
recruitBoardDocumentService.saveRecruitBoardDocuments(recruitBoards);
311+
312+
//then
313+
Page<RecruitBoardWithCenterResponseDto> findBoard = recruitBoardDocumentService.getRecruitBoardBySearch("저장", condition);
314+
315+
assertThat(findBoard).isNotNull();
316+
assertThat(findBoard.getTotalElements()).isEqualTo(2);
317+
318+
recruitBoardRepository.deleteDocument(savedBoard1.getId());
319+
recruitBoardRepository.deleteDocument(savedBoard2.getId());
320+
}
321+
283322
private Pageable getPageable() {
284323
Sort sort = Sort.by(Sort.Order.desc("created_at"));
285324
return PageRequest.of(0, 5, sort);

0 commit comments

Comments
 (0)