|
| 1 | +package com.somemore.recruitboard.controller; |
| 2 | + |
| 3 | +import static com.somemore.recruitboard.domain.VolunteerType.ADMINISTRATIVE_SUPPORT; |
| 4 | +import static org.mockito.Mockito.any; |
| 5 | +import static org.mockito.Mockito.eq; |
| 6 | +import static org.mockito.Mockito.times; |
| 7 | +import static org.mockito.Mockito.verify; |
| 8 | +import static org.mockito.Mockito.when; |
| 9 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 10 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 11 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 12 | + |
| 13 | +import com.somemore.IntegrationTestSupport; |
| 14 | +import com.somemore.recruitboard.dto.condition.RecruitBoardNearByCondition; |
| 15 | +import com.somemore.recruitboard.dto.condition.RecruitBoardSearchCondition; |
| 16 | +import com.somemore.recruitboard.dto.response.RecruitBoardDetailResponseDto; |
| 17 | +import com.somemore.recruitboard.dto.response.RecruitBoardResponseDto; |
| 18 | +import com.somemore.recruitboard.dto.response.RecruitBoardWithCenterResponseDto; |
| 19 | +import com.somemore.recruitboard.dto.response.RecruitBoardWithLocationResponseDto; |
| 20 | +import com.somemore.recruitboard.usecase.query.RecruitBoardQueryUseCase; |
| 21 | +import java.util.Collections; |
| 22 | +import java.util.UUID; |
| 23 | +import org.junit.jupiter.api.DisplayName; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import org.springframework.beans.factory.annotation.Autowired; |
| 26 | +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; |
| 27 | +import org.springframework.boot.test.context.SpringBootTest; |
| 28 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 29 | +import org.springframework.data.domain.Page; |
| 30 | +import org.springframework.data.domain.PageImpl; |
| 31 | +import org.springframework.data.domain.Pageable; |
| 32 | +import org.springframework.http.MediaType; |
| 33 | +import org.springframework.test.web.servlet.MockMvc; |
| 34 | + |
| 35 | +@SpringBootTest |
| 36 | +@AutoConfigureMockMvc |
| 37 | +class RecruitBoardQueryControllerTest extends IntegrationTestSupport { |
| 38 | + |
| 39 | + @Autowired |
| 40 | + private MockMvc mockMvc; |
| 41 | + |
| 42 | + @MockBean |
| 43 | + private RecruitBoardQueryUseCase recruitBoardQueryUseCase; |
| 44 | + |
| 45 | + @Test |
| 46 | + @DisplayName("모집글 ID로 상세 조회할 수 있다.") |
| 47 | + void getById() throws Exception { |
| 48 | + Long recruitBoardId = 1L; |
| 49 | + var responseDto = RecruitBoardWithLocationResponseDto.builder().build(); |
| 50 | + |
| 51 | + when(recruitBoardQueryUseCase.getWithLocationById(recruitBoardId)).thenReturn(responseDto); |
| 52 | + |
| 53 | + mockMvc.perform(get("/api/recruit-board/{id}", recruitBoardId) |
| 54 | + .accept(MediaType.APPLICATION_JSON)) |
| 55 | + .andExpect(status().isOk()) |
| 56 | + .andExpect(jsonPath("$.code").value(200)) |
| 57 | + .andExpect(jsonPath("$.data").exists()) |
| 58 | + .andExpect(jsonPath("$.message").value("봉사 활동 모집 상세 조회 성공")); |
| 59 | + |
| 60 | + verify(recruitBoardQueryUseCase, times(1)).getWithLocationById(recruitBoardId); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @DisplayName("모집글 페이징 처리하여 전체 조회 할 수 있다.") |
| 65 | + void getAll() throws Exception { |
| 66 | + Page<RecruitBoardWithCenterResponseDto> page = new PageImpl<>(Collections.emptyList()); |
| 67 | + |
| 68 | + when(recruitBoardQueryUseCase.getAllWithCenter(any(RecruitBoardSearchCondition.class))) |
| 69 | + .thenReturn(page); |
| 70 | + |
| 71 | + mockMvc.perform(get("/api/recruit-boards") |
| 72 | + .accept(MediaType.APPLICATION_JSON)) |
| 73 | + .andExpect(status().isOk()) |
| 74 | + .andExpect(jsonPath("$.code").value(200)) |
| 75 | + .andExpect(jsonPath("$.data").exists()) |
| 76 | + .andExpect(jsonPath("$.message").value("봉사 활동 모집글 리스트 조회 성공")); |
| 77 | + |
| 78 | + verify(recruitBoardQueryUseCase, times(1)).getAllWithCenter( |
| 79 | + any(RecruitBoardSearchCondition.class)); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + @DisplayName("모집글을 검색 조건으로 페이징 조회할 수 있다.") |
| 84 | + void getAllBySearch() throws Exception { |
| 85 | + Page<RecruitBoardWithCenterResponseDto> page = new PageImpl<>(Collections.emptyList()); |
| 86 | + |
| 87 | + when(recruitBoardQueryUseCase.getAllWithCenter(any(RecruitBoardSearchCondition.class))) |
| 88 | + .thenReturn(page); |
| 89 | + |
| 90 | + mockMvc.perform(get("/api/recruit-boards/search") |
| 91 | + .param("keyword", "volunteer") |
| 92 | + .param("type", ADMINISTRATIVE_SUPPORT.name()) |
| 93 | + .accept(MediaType.APPLICATION_JSON)) |
| 94 | + .andExpect(status().isOk()) |
| 95 | + .andExpect(jsonPath("$.code").value(200)) |
| 96 | + .andExpect(jsonPath("$.data").exists()) |
| 97 | + .andExpect(jsonPath("$.message").value("봉사 활동 모집글 검색 조회 성공")); |
| 98 | + |
| 99 | + verify(recruitBoardQueryUseCase, times(1)).getAllWithCenter( |
| 100 | + any(RecruitBoardSearchCondition.class)); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + @DisplayName("위치 기반으로 근처 있는 모집글 페이징 조회할 수 있다.") |
| 105 | + void getNearby() throws Exception { |
| 106 | + Page<RecruitBoardDetailResponseDto> page = new PageImpl<>(Collections.emptyList()); |
| 107 | + |
| 108 | + when( |
| 109 | + recruitBoardQueryUseCase.getRecruitBoardsNearby(any(RecruitBoardNearByCondition.class))) |
| 110 | + .thenReturn(page); |
| 111 | + |
| 112 | + mockMvc.perform(get("/api/recruit-boards/nearby") |
| 113 | + .param("latitude", "37.5665") |
| 114 | + .param("longitude", "126.9780") |
| 115 | + .param("radius", "10") |
| 116 | + .accept(MediaType.APPLICATION_JSON)) |
| 117 | + .andExpect(status().isOk()) |
| 118 | + .andExpect(jsonPath("$.code").value(200)) |
| 119 | + .andExpect(jsonPath("$.data").exists()) |
| 120 | + .andExpect(jsonPath("$.message").value("근처 봉사 활동 모집글 조회 성공")); |
| 121 | + |
| 122 | + verify(recruitBoardQueryUseCase, times(1)).getRecruitBoardsNearby( |
| 123 | + any(RecruitBoardNearByCondition.class)); |
| 124 | + } |
| 125 | + |
| 126 | + @Test |
| 127 | + @DisplayName("기관 ID로 모집글 페이징 조회할 수 있다.") |
| 128 | + void getRecruitBoardsByCenterId() throws Exception { |
| 129 | + UUID centerId = UUID.randomUUID(); |
| 130 | + Page<RecruitBoardResponseDto> page = new PageImpl<>(Collections.emptyList()); |
| 131 | + |
| 132 | + when(recruitBoardQueryUseCase.getRecruitBoardsByCenterId(eq(centerId), any(Pageable.class))) |
| 133 | + .thenReturn(page); |
| 134 | + |
| 135 | + mockMvc.perform(get("/api/recruit-boards/center/{centerId}", centerId) |
| 136 | + .accept(MediaType.APPLICATION_JSON)) |
| 137 | + .andExpect(status().isOk()) |
| 138 | + .andExpect(jsonPath("$.data").exists()) |
| 139 | + .andExpect(jsonPath("$.message").value("기관 봉사 활동 모집글 조회 성공")); |
| 140 | + |
| 141 | + verify(recruitBoardQueryUseCase, times(1)).getRecruitBoardsByCenterId(eq(centerId), |
| 142 | + any(Pageable.class)); |
| 143 | + } |
| 144 | +} |
0 commit comments