-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseControllerTest.java
More file actions
230 lines (196 loc) Β· 10.3 KB
/
Copy pathCourseControllerTest.java
File metadata and controls
230 lines (196 loc) Β· 10.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package org.runnect.server.course.controller;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.mockito.BDDMockito;
import org.runnect.server.common.constant.TokenStatus;
import org.runnect.server.common.exception.NotFoundException;
import org.runnect.server.common.module.convert.CoordinateDto;
import org.runnect.server.config.jwt.JwtService;
import org.runnect.server.config.slack.SlackApi;
import org.runnect.server.course.dto.request.CourseCreateRequestDto;
import org.runnect.server.course.dto.request.DeleteCoursesRequestDto;
import org.runnect.server.course.dto.response.CourseCreateResponseDto;
import org.runnect.server.course.dto.response.CourseGetByUserResponseDto;
import org.runnect.server.course.dto.response.DeleteCoursesResponseDto;
import org.runnect.server.course.dto.response.UpdateCourseResponseDto;
import org.runnect.server.course.dto.response.UserResponse;
import org.runnect.server.course.service.CourseService;
import org.runnect.server.external.aws.S3Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
/**
* 컨νΈλ‘€λ¬ λ μ΄μ΄(λΌμ°ν
, @Valid κ²μ¦, @UserId μΈμ¦ 리쑸λ², μμΈβHTTP μνμ½λ λ§€ν)λ₯Ό
* μλΉμ€ λ μ΄μ΄ λ¨μ ν
μ€νΈμλ λ³κ°λ‘ κ²μ¦νλ€. CourseService/S3Serviceλ λͺ¨νΉνκ³
* λλ¨Έμ§(WebConfig, UserIdResolver, ControllerExceptionAdvice)λ μ€μ λΉμ κ·Έλλ‘ μ΄λ€.
*/
@WebMvcTest(CourseController.class)
class CourseControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private CourseService courseService;
@MockBean
private S3Service s3Service;
@MockBean
private JwtService jwtService;
@MockBean
private SlackApi slackApi;
@BeforeEach
void setUpAuth() {
BDDMockito.lenient().when(jwtService.verifyToken("valid")).thenReturn(TokenStatus.TOKEN_VALID);
BDDMockito.lenient().when(jwtService.getJwtContents("valid")).thenReturn("1");
}
private <B extends org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder> B withAuth(
B builder) {
builder.header("accessToken", "valid").header("refreshToken", "valid");
return builder;
}
private CourseCreateRequestDto validCreateRequest() {
return new CourseCreateRequestDto(
Arrays.asList(new CoordinateDto(37.5665, 126.9780), new CoordinateDto(37.5651, 126.9895)),
"μ μμ μ½μ€", 5.2f, "μ μμ", "κ²½κΈ° μν₯μ μ μλ");
}
@Nested
@DisplayName("POST /api/course")
class CreateCourse {
@Test
@DisplayName("μ μ μμ²μ΄λ©΄ 201κ³Ό μμ±λ μ½μ€ μ 보λ₯Ό λ°ννλ€")
void μ μ_μμ±() throws Exception {
when(s3Service.uploadImage(any(), eq("course"))).thenReturn("https://image.example/course.png");
when(courseService.createCourse(eq(1L), any(), any()))
.thenReturn(CourseCreateResponseDto.of(100L, LocalDateTime.of(2026, 1, 1, 0, 0)));
MockMultipartFile data = new MockMultipartFile("data", "", "application/json",
objectMapper.writeValueAsBytes(validCreateRequest()));
MockMultipartFile image = new MockMultipartFile("image", "photo.jpg", "image/jpeg", "content".getBytes());
mockMvc.perform(withAuth(multipart("/api/course")).file(data).file(image))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.data.id").value(100));
}
@Test
@DisplayName("μΈμ¦ ν€λκ° μμΌλ©΄ 400κ³Ό ν¨κ» μμ²μ΄ μλΉμ€κΉμ§ λλ¬νμ§ μλλ€")
void μΈμ¦ν€λ_μμ() throws Exception {
MockMultipartFile data = new MockMultipartFile("data", "", "application/json",
objectMapper.writeValueAsBytes(validCreateRequest()));
MockMultipartFile image = new MockMultipartFile("image", "photo.jpg", "image/jpeg", "content".getBytes());
mockMvc.perform(multipart("/api/course").file(data).file(image))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.success").value(false));
BDDMockito.verifyNoInteractions(courseService);
}
@Test
@DisplayName("titleμ΄ λΉμ΄μμΌλ©΄ μ ν¨μ± κ²μ¦μμ 400μ λ°ννλ€")
void μ ν¨μ±κ²μ¦_μ€ν¨() throws Exception {
CourseCreateRequestDto invalid = new CourseCreateRequestDto(
Arrays.asList(new CoordinateDto(37.5665, 126.9780), new CoordinateDto(37.5651, 126.9895)),
"", 5.2f, "μ μμ", "κ²½κΈ° μν₯μ μ μλ");
MockMultipartFile data = new MockMultipartFile("data", "", "application/json",
objectMapper.writeValueAsBytes(invalid));
MockMultipartFile image = new MockMultipartFile("image", "photo.jpg", "image/jpeg", "content".getBytes());
mockMvc.perform(withAuth(multipart("/api/course")).file(data).file(image))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(courseService);
}
}
@Nested
@DisplayName("GET /api/course/user")
class GetCourseByUser {
@Test
@DisplayName("μ μ μμ²μ΄λ©΄ 200κ³Ό μ½μ€ λͺ©λ‘μ λ°ννλ€")
void μ μ_μ‘°ν() throws Exception {
when(courseService.getCourseByUser(1L)).thenReturn(
CourseGetByUserResponseDto.of(UserResponse.of(1L), Collections.emptyList()));
mockMvc.perform(withAuth(get("/api/course/user")))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.user.id").value(1));
}
}
@Nested
@DisplayName("GET /api/course/detail/{courseId}")
class GetCourseDetail {
@Test
@DisplayName("μλΉμ€μμ NotFoundExceptionμ΄ λλ©΄ 400μΌλ‘ λ§€νλλ€")
void μ‘΄μ¬νμ§_μλ_μ½μ€() throws Exception {
when(courseService.getCourseDetail(anyLong(), eq(1L)))
.thenThrow(new NotFoundException(
org.runnect.server.common.constant.ErrorStatus.NOT_FOUND_COURSE_EXCEPTION,
org.runnect.server.common.constant.ErrorStatus.NOT_FOUND_COURSE_EXCEPTION.getMessage()));
mockMvc.perform(withAuth(get("/api/course/detail/999")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.success").value(false));
}
}
@Nested
@DisplayName("PATCH /api/course/{courseId}")
class UpdateCourse {
@Test
@DisplayName("μ μ μμ²μ΄λ©΄ 200κ³Ό μμ λ μ λͺ©μ λ°ννλ€")
void μ μ_μμ () throws Exception {
when(courseService.updateCourse(eq(1L), eq(10L), eq("μ μ λͺ©")))
.thenReturn(UpdateCourseResponseDto.of(courseWithTitle(10L, "μ μ λͺ©")));
mockMvc.perform(withAuth(patch("/api/course/10"))
.contentType("application/json")
.content("{\"title\":\"μ μ λͺ©\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.course.title").value("μ μ λͺ©"));
}
@Test
@DisplayName("titleμ΄ λΉμ΄μμΌλ©΄ 400μ λ°ννλ€")
void λΉ_μ λͺ©() throws Exception {
mockMvc.perform(withAuth(patch("/api/course/10"))
.contentType("application/json")
.content("{\"title\":\"\"}"))
.andExpect(status().isBadRequest());
BDDMockito.verifyNoInteractions(courseService);
}
private org.runnect.server.course.entity.Course courseWithTitle(Long id, String title) {
org.runnect.server.course.entity.Course course = org.runnect.server.course.entity.Course.builder()
.title(title)
.departureRegion("κ²½κΈ°").departureCity("μν₯μ").departureTown("μ μλ")
.distance(5.2f).image("image.png")
.path(org.runnect.server.common.module.convert.CoordinatePathConverter.coorConvertPath(
Arrays.asList(new CoordinateDto(37.5665, 126.9780), new CoordinateDto(37.5651, 126.9895))))
.build();
org.springframework.test.util.ReflectionTestUtils.setField(course, "id", id);
return course;
}
}
@Nested
@DisplayName("PUT /api/course")
class DeleteCourses {
@Test
@DisplayName("μ μ μμ²μ΄λ©΄ 200κ³Ό μμ κ°μλ₯Ό λ°ννλ€")
void μ μ_μμ () throws Exception {
when(courseService.deleteCourses(Collections.singletonList(10L), 1L))
.thenReturn(DeleteCoursesResponseDto.from(1));
mockMvc.perform(withAuth(put("/api/course"))
.contentType("application/json")
.content(objectMapper.writeValueAsString(
new DeleteCoursesRequestDto(Collections.singletonList(10L)))))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data.deletedCourseCount").value(1));
}
}
}