Skip to content

Commit 47e49dd

Browse files
committed
test(recruit-board): 생성 수정 삭제 컨트롤러 생성에 따른 테스트
1 parent 0589545 commit 47e49dd

File tree

1 file changed

+261
-0
lines changed

1 file changed

+261
-0
lines changed
Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,261 @@
1+
package com.somemore.recruitboard.controller;
2+
3+
import static com.somemore.common.fixture.LocalDateTimeFixture.createStartDateTime;
4+
import static com.somemore.recruitboard.domain.RecruitStatus.CLOSED;
5+
import static org.mockito.ArgumentMatchers.any;
6+
import static org.mockito.ArgumentMatchers.anyString;
7+
import static org.mockito.BDDMockito.given;
8+
import static org.mockito.BDDMockito.willDoNothing;
9+
import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
10+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
11+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
12+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
13+
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
14+
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
15+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
16+
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
17+
18+
import com.fasterxml.jackson.databind.ObjectMapper;
19+
import com.somemore.ControllerTestSupport;
20+
import com.somemore.imageupload.usecase.ImageUploadUseCase;
21+
import com.somemore.location.dto.request.LocationCreateRequestDto;
22+
import com.somemore.recruitboard.domain.RecruitStatus;
23+
import com.somemore.recruitboard.domain.VolunteerType;
24+
import com.somemore.recruitboard.dto.request.RecruitBoardCreateRequestDto;
25+
import com.somemore.recruitboard.dto.request.RecruitBoardLocationUpdateRequestDto;
26+
import com.somemore.recruitboard.dto.request.RecruitBoardStatusUpdateRequestDto;
27+
import com.somemore.recruitboard.dto.request.RecruitBoardUpdateRequestDto;
28+
import com.somemore.recruitboard.usecase.command.CreateRecruitBoardUseCase;
29+
import com.somemore.recruitboard.usecase.command.DeleteRecruitBoardUseCase;
30+
import com.somemore.recruitboard.usecase.command.UpdateRecruitBoardUseCase;
31+
import java.math.BigDecimal;
32+
import java.time.LocalDateTime;
33+
import java.util.UUID;
34+
import org.junit.jupiter.api.DisplayName;
35+
import org.junit.jupiter.api.Test;
36+
import org.springframework.beans.factory.annotation.Autowired;
37+
import org.springframework.boot.test.mock.mockito.MockBean;
38+
import org.springframework.http.MediaType;
39+
import org.springframework.mock.web.MockHttpServletRequest;
40+
import org.springframework.mock.web.MockMultipartFile;
41+
import org.springframework.security.test.context.support.WithMockUser;
42+
import org.springframework.test.web.servlet.MockMvc;
43+
import org.springframework.test.web.servlet.request.MockMultipartHttpServletRequestBuilder;
44+
import org.springframework.test.web.servlet.request.RequestPostProcessor;
45+
46+
class RecruitBoardCommandApiControllerTest extends ControllerTestSupport {
47+
48+
@Autowired
49+
private MockMvc mockMvc;
50+
51+
@Autowired
52+
private ObjectMapper objectMapper;
53+
54+
@MockBean
55+
private CreateRecruitBoardUseCase createRecruitBoardUseCase;
56+
57+
@MockBean
58+
private UpdateRecruitBoardUseCase updateRecruitBoardUseCase;
59+
60+
@MockBean
61+
private DeleteRecruitBoardUseCase deleteRecruitBoardUseCase;
62+
63+
@MockBean
64+
private ImageUploadUseCase imageUploadUseCase;
65+
66+
@Test
67+
@DisplayName("봉사 활동 모집글 등록 성공 테스트")
68+
@WithMockUser(roles = "CENTER", value = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
69+
void createRecruitBoard_success() throws Exception {
70+
// given
71+
LocalDateTime startDateTime = createStartDateTime();
72+
LocalDateTime endDateTime = startDateTime.plusHours(2);
73+
74+
LocationCreateRequestDto location = LocationCreateRequestDto.builder()
75+
.address("위치위치")
76+
.latitude(BigDecimal.valueOf(37.4845373748015))
77+
.longitude(BigDecimal.valueOf(127.010842267696))
78+
.build();
79+
80+
RecruitBoardCreateRequestDto dto = RecruitBoardCreateRequestDto.builder()
81+
.title("봉사 모집글 작성")
82+
.content("봉사 하실분을 모집합니다. <br>")
83+
.region("지역")
84+
.recruitmentCount(10)
85+
.volunteerStartDateTime(startDateTime)
86+
.volunteerEndDateTime(endDateTime)
87+
.volunteerType(VolunteerType.OTHER)
88+
.admitted(true)
89+
.location(location)
90+
.build();
91+
92+
MockMultipartFile imageFile = new MockMultipartFile(
93+
"img_file",
94+
"test-image.jpg",
95+
MediaType.IMAGE_JPEG_VALUE,
96+
"test image content".getBytes()
97+
);
98+
99+
MockMultipartFile requestData = new MockMultipartFile(
100+
"data",
101+
"",
102+
MediaType.APPLICATION_JSON_VALUE,
103+
objectMapper.writeValueAsBytes(dto)
104+
);
105+
106+
String mockImageUrl = "http://example.com/image/test-image.jpg";
107+
long mockRecruitBoardId = 1L;
108+
109+
given(imageUploadUseCase.uploadImage(any())).willReturn(mockImageUrl);
110+
given(createRecruitBoardUseCase.createRecruitBoard(any(), any(UUID.class),
111+
anyString())).willReturn(mockRecruitBoardId);
112+
113+
// when
114+
mockMvc.perform(multipart("/api/recruit-board")
115+
.file(requestData)
116+
.file(imageFile)
117+
.contentType(MULTIPART_FORM_DATA)
118+
.header("Authorization", "Bearer access-token"))
119+
// then
120+
.andExpect(status().isOk())
121+
.andExpect(jsonPath("$.code").value(201))
122+
.andExpect(jsonPath("$.data").value(mockRecruitBoardId))
123+
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 등록 성공"));
124+
}
125+
126+
@DisplayName("봉사 활동 모집글 수정 성공 테스트")
127+
@Test
128+
@WithMockUser(roles = "CENTER", value = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
129+
void updateRecruitBoard() throws Exception {
130+
// given
131+
LocalDateTime startDateTime = createStartDateTime();
132+
LocalDateTime endDateTime = startDateTime.plusHours(2);
133+
134+
RecruitBoardUpdateRequestDto requestDto = RecruitBoardUpdateRequestDto.builder()
135+
.title("서울 청계천 환경 미화 봉사 모집")
136+
.content("서울 청계천 주변 환경 미화 봉사 모집합니다. <br>")
137+
.recruitmentCount(10)
138+
.volunteerStartDateTime(startDateTime)
139+
.volunteerEndDateTime(endDateTime)
140+
.volunteerType(VolunteerType.OTHER)
141+
.admitted(true)
142+
.build();
143+
144+
MockMultipartFile imageFile = new MockMultipartFile(
145+
"img_file",
146+
"test-image.jpg",
147+
MediaType.IMAGE_JPEG_VALUE,
148+
"test image content".getBytes()
149+
);
150+
151+
MockMultipartFile requestData = new MockMultipartFile(
152+
"data",
153+
"",
154+
MediaType.APPLICATION_JSON_VALUE,
155+
objectMapper.writeValueAsBytes(requestDto)
156+
);
157+
158+
String mockImageUrl = "http://example.com/image/test-image.jpg";
159+
160+
given(imageUploadUseCase.uploadImage(any())).willReturn(mockImageUrl);
161+
willDoNothing().given(updateRecruitBoardUseCase)
162+
.updateRecruitBoard(any(), any(), any(UUID.class), anyString());
163+
164+
MockMultipartHttpServletRequestBuilder builder = multipart("/api/recruit-board/{id}", 1);
165+
builder.with(new RequestPostProcessor() {
166+
@Override
167+
public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
168+
request.setMethod("PUT");
169+
return request;
170+
}
171+
});
172+
173+
// when
174+
mockMvc.perform(builder
175+
.file(requestData)
176+
.file(imageFile)
177+
.contentType(MULTIPART_FORM_DATA)
178+
.header("Authorization", "Bearer access-token"))
179+
//then
180+
.andDo(print())
181+
.andExpect(status().isOk())
182+
.andExpect(jsonPath("$.code").value(200))
183+
.andExpect(jsonPath("$.data").isEmpty())
184+
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 수정 성공"));
185+
}
186+
187+
@DisplayName("봉사 활동 모집글 위치 수정 성공 테스트")
188+
@Test
189+
@WithMockUser(roles = "CENTER", value = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
190+
void updateRecruitBoardLocation() throws Exception {
191+
// given
192+
RecruitBoardLocationUpdateRequestDto requestDto = RecruitBoardLocationUpdateRequestDto.builder()
193+
.region("새로새로지역지역")
194+
.address("새로새로주소주소")
195+
.latitude(BigDecimal.valueOf(37.2222222))
196+
.longitude(BigDecimal.valueOf(127.2222222))
197+
.build();
198+
199+
willDoNothing().given(updateRecruitBoardUseCase)
200+
.updateRecruitBoardLocation(any(), any(), any(UUID.class));
201+
202+
String requestBody = objectMapper.writeValueAsString(requestDto);
203+
204+
// when
205+
mockMvc.perform(put("/api/recruit-board/{id}/location", 1L)
206+
.content(requestBody)
207+
.contentType(MediaType.APPLICATION_JSON)
208+
.header("Authorization", "Bearer access-token"))
209+
// then
210+
.andDo(print())
211+
.andExpect(status().isOk())
212+
.andExpect(jsonPath("$.code").value(200))
213+
.andExpect(jsonPath("$.data").isEmpty())
214+
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 위치 수정 성공"));
215+
}
216+
217+
@DisplayName("봉사 활동 상태 변경 성공")
218+
@Test
219+
@WithMockUser(roles = "CENTER", value = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
220+
void updateRecruitBoardStatus() throws Exception {
221+
// given
222+
RecruitStatus status = CLOSED;
223+
RecruitBoardStatusUpdateRequestDto dto = new RecruitBoardStatusUpdateRequestDto(
224+
status);
225+
String requestBody = objectMapper.writeValueAsString(dto);
226+
willDoNothing().given(updateRecruitBoardUseCase)
227+
.updateRecruitBoardStatus(any(), any(), any(UUID.class), any(LocalDateTime.class));
228+
229+
// when
230+
mockMvc.perform(patch("/api/recruit-board/{id}", 1L)
231+
.content(requestBody)
232+
.contentType(MediaType.APPLICATION_JSON)
233+
.header("Authorization", "Bearer access-token"))
234+
//then
235+
.andDo(print())
236+
.andExpect(status().isOk())
237+
.andExpect(jsonPath("$.code").value(200))
238+
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 상태 수정 성공"))
239+
.andExpect(jsonPath("$.data").isEmpty());
240+
}
241+
242+
@DisplayName("봉사 활동 모집글 삭제 성공")
243+
@Test
244+
@WithMockUser(roles = "CENTER", value = "9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d")
245+
void deleteRecruitBoard() throws Exception {
246+
// given
247+
Long recruitBoardId = 1L;
248+
willDoNothing().given(deleteRecruitBoardUseCase).deleteRecruitBoard(any(UUID.class), any());
249+
250+
// when
251+
mockMvc.perform(delete("/api/recruit-board/{id}", recruitBoardId)
252+
.header("Authorization", "Bearer access-token"))
253+
// then
254+
.andDo(print())
255+
.andExpect(status().isOk())
256+
.andExpect(jsonPath("$.code").value(200))
257+
.andExpect(jsonPath("$.message").value("봉사 활동 모집글 삭제 성공"))
258+
.andExpect(jsonPath("$.data").isEmpty());
259+
}
260+
}
261+

0 commit comments

Comments
 (0)