-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathProblemControllerTest.java
More file actions
385 lines (361 loc) · 17 KB
/
ProblemControllerTest.java
File metadata and controls
385 lines (361 loc) · 17 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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
package com.gamzabat.algohub.feature.problem.controller;
import static org.hamcrest.Matchers.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.anyLong;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.internal.matchers.Null;
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.context.annotation.Import;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gamzabat.algohub.common.jwt.TokenProvider;
import com.gamzabat.algohub.config.SpringSecurityConfig;
import com.gamzabat.algohub.exception.ProblemValidationException;
import com.gamzabat.algohub.exception.StudyGroupValidationException;
import com.gamzabat.algohub.feature.group.studygroup.repository.GroupMemberRepository;
import com.gamzabat.algohub.feature.group.studygroup.repository.StudyGroupRepository;
import com.gamzabat.algohub.feature.problem.dto.CreateProblemRequest;
import com.gamzabat.algohub.feature.problem.dto.EditProblemRequest;
import com.gamzabat.algohub.feature.problem.dto.GetProblemResponse;
import com.gamzabat.algohub.feature.problem.enums.ProblemListStatus;
import com.gamzabat.algohub.feature.problem.exception.InvalidRequestException;
import com.gamzabat.algohub.feature.problem.exception.SolvedAcApiErrorException;
import com.gamzabat.algohub.feature.problem.repository.ProblemRepository;
import com.gamzabat.algohub.feature.problem.service.ProblemService;
import com.gamzabat.algohub.feature.solution.repository.SolutionRepository;
import com.gamzabat.algohub.feature.user.domain.User;
import com.gamzabat.algohub.feature.user.repository.UserRepository;
@WebMvcTest(ProblemController.class)
@WithMockUser
@Import(SpringSecurityConfig.class)
class ProblemControllerTest {
private final String token = "token";
private final Long groupId = 1L;
private final Long problemId = 10L;
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private ProblemService problemService;
@MockBean
private ProblemRepository problemRepository;
@MockBean
private SolutionRepository solutionRepository;
@MockBean
private StudyGroupRepository studyGroupRepository;
@MockBean
private GroupMemberRepository groupMemberRepository;
@MockBean
private TokenProvider tokenProvider;
@MockBean
private UserRepository userRepository;
private User user;
@BeforeEach
void setUp() {
user = User.builder().email("email").password("password").build();
when(tokenProvider.getUserEmail(token)).thenReturn("email");
when(userRepository.findByEmail("email")).thenReturn(Optional.ofNullable(user));
}
@Test
@DisplayName("문제 생성 성공")
void createProblem() throws Exception {
// given
CreateProblemRequest request = CreateProblemRequest.builder()
.link("link")
.startDate(LocalDate.now().minusDays(7))
.endDate(LocalDate.now())
.build();
doNothing().when(problemService).createProblem(any(User.class), anyLong(), any(CreateProblemRequest.class));
// when, then
mockMvc.perform(post("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk());
verify(problemService, times(1)).createProblem(user, groupId, request);
}
@ParameterizedTest
@CsvSource(value = {
"'','2024-07-10','2024-07-18',link : 문제 링크는 필수 입력 입니다.",
"link, null, '2024-07-18', startDate : 시작 날짜는 필수 입력 입니다.",
"link, '2024-07-18', null, endDate : 마감 날짜는 필수 입력 입니다."
}, nullValues = "null")
@DisplayName("문제 생성 실패 : 잘못된 요청")
void createProblemFailed_1(String link, LocalDate startDate, LocalDate endDate,
String exceptionMessage) throws Exception {
// given
CreateProblemRequest request = CreateProblemRequest.builder()
.link(link)
.startDate(startDate)
.endDate(endDate)
.build();
// when, then
mockMvc.perform(post("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("문제 생성 요청이 올바르지 않습니다."))
.andExpect(jsonPath("$.messages", hasItems(exceptionMessage)));
}
@Test
@DisplayName("문제 생성 실패 : 권한 없음")
void createProblemFailed_2() throws Exception {
// given
CreateProblemRequest request = CreateProblemRequest.builder()
.link("link")
.startDate(LocalDate.now())
.endDate(LocalDate.now())
.build();
doThrow(new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(),
"문제 생성 권한이 없습니다. 방장, 부방장일 경우에만 생성이 가능합니다.")).when(
problemService).createProblem(user, groupId, request);
// when, then
mockMvc.perform(post("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.error").value("문제 생성 권한이 없습니다. 방장, 부방장일 경우에만 생성이 가능합니다."));
verify(problemService, times(1)).createProblem(user, groupId, request);
}
@Test
@DisplayName("문제 생성 실패 : 백준에 유효하지 않은 문제")
void createProblemFailed_3() throws Exception {
// given
CreateProblemRequest request = CreateProblemRequest.builder()
.link("link")
.startDate(LocalDate.now())
.endDate(LocalDate.now())
.build();
doThrow(new SolvedAcApiErrorException(HttpStatus.BAD_REQUEST.value(), "백준에 유효하지 않은 문제입니다.")).when(
problemService).createProblem(user, groupId, request);
// when, then
mockMvc.perform(post("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("백준에 유효하지 않은 문제입니다."));
verify(problemService, times(1)).createProblem(user, groupId, request);
}
@Test
@DisplayName("문제 생성 실패 : solved.ac의 잘못된 response")
void createProblemFailed_4() throws Exception {
// given
CreateProblemRequest request = CreateProblemRequest.builder()
.link("link")
.startDate(LocalDate.now())
.endDate(LocalDate.now())
.build();
doThrow(new SolvedAcApiErrorException(HttpStatus.SERVICE_UNAVAILABLE.value(),
"solved.ac API로부터 예상치 못한 응답을 받았습니다.")).when(
problemService).createProblem(user, groupId, request);
// when, then
mockMvc.perform(post("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isServiceUnavailable())
.andExpect(jsonPath("$.error").value("solved.ac API로부터 예상치 못한 응답을 받았습니다."));
verify(problemService, times(1)).createProblem(user, groupId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 성공")
void editProblemDeadline() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now(), LocalDate.now().plusDays(10));
doNothing().when(problemService).editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk());
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 실패 : 존재하지 않는 문제")
void editProblemDeadlineFailed_2() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now(), LocalDate.now().plusDays(10));
doThrow(new ProblemValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 문제 입니다.")).when(problemService)
.editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.error").value("존재하지 않는 문제 입니다."));
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 실패 : 권한 없음")
void editProblemDeadlineFailed_3() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now(), LocalDate.now().plusDays(10));
doThrow(new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(),
"문제 수정 권한이 없습니다. 방장, 부방장일 경우에만 수정이 가능합니다.")).when(problemService)
.editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.error").value("문제 수정 권한이 없습니다. 방장, 부방장일 경우에만 수정이 가능합니다."));
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 실패 : 이미 진행 중인 문제인데 시작날짜 수정을 요청하는 경우")
void editProblemDeadlineFailed_4() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now(), LocalDate.now().plusDays(10));
doThrow(
new ProblemValidationException(HttpStatus.FORBIDDEN.value(), "문제 시작 날짜 수정이 불가합니다. : 이미 진행 중인 문제입니다.")).when(
problemService)
.editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.error").value("문제 시작 날짜 수정이 불가합니다. : 이미 진행 중인 문제입니다."));
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 실패 : 문제 시작 날짜를 오늘 이전의 날짜로 요청한 경우")
void editProblemDeadlineFailed_5() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now().minusDays(10), LocalDate.now());
doThrow(
new ProblemValidationException(HttpStatus.BAD_REQUEST.value(), "문제 시작 날짜는 오늘 이전의 날짜로 수정할 수 없습니다.")).when(
problemService)
.editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("문제 시작 날짜는 오늘 이전의 날짜로 수정할 수 없습니다."));
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 진행 기간 수정 실패 : 문제 마감 날짜를 오늘 이전의 날짜로 요청한 경우")
void editProblemDeadlineFailed_6() throws Exception {
// given
EditProblemRequest request = new EditProblemRequest(LocalDate.now().minusDays(20),
LocalDate.now().minusDays(10));
doThrow(
new ProblemValidationException(HttpStatus.BAD_REQUEST.value(), "문제 마감 날짜는 오늘 이전의 날짜로 수정할 수 없습니다.")).when(
problemService)
.editProblem(any(User.class), anyLong(), any(EditProblemRequest.class));
// when, then
mockMvc.perform(patch("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("문제 마감 날짜는 오늘 이전의 날짜로 수정할 수 없습니다."));
verify(problemService, times(1)).editProblem(user, problemId, request);
}
@Test
@DisplayName("문제 목록 조회 성공")
void getProblemList() throws Exception {
// given
Pageable pageable = PageRequest.of(0, 20, Sort.by("endDate").descending());
Page<GetProblemResponse> response = new PageImpl<>(new ArrayList<>());
when(problemService.getProblems(any(User.class), anyLong(), eq(ProblemListStatus.IN_PROGRESS) ,eq(false),
any(Pageable.class))).thenReturn(
response);
// when, then
mockMvc.perform(get("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.param("page", "0")
.param("size", "20")
.param("unsolved-only", String.valueOf(false))
.param("status", String.valueOf(ProblemListStatus.IN_PROGRESS)))
.andExpect(status().isOk())
.andExpect(content().string(objectMapper.writeValueAsString(response)));
verify(problemService, times(1)).getProblems(user, groupId,ProblemListStatus.IN_PROGRESS,false, pageable);
}
@Test
@DisplayName("문제 목록 조회 실패 : 올바르지 않은 요청")
void getProblemListFailed_1() throws Exception {
// given
Pageable pageable = PageRequest.of(0, 20, Sort.by("endDate").descending());
// when, then
mockMvc.perform(get("/api/groups/{groupId}/problems", groupId)
.header("Authorization", token)
.param("status", String.valueOf(ProblemListStatus.IN_PROGRESS)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.error").value("IN_PROGRESS 상태에서는 unsolvedOnly 는 필수입니다."));
verifyNoInteractions(problemService);
}
@Test
@DisplayName("문제 삭제 성공")
void deleteProblem() throws Exception {
// given
doNothing().when(problemService).deleteProblem(user, problemId);
// when, then
mockMvc.perform(delete("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.param("problemId", String.valueOf(problemId)))
.andExpect(status().isOk());
verify(problemService, times(1)).deleteProblem(user, problemId);
}
@Test
@DisplayName("문제 삭제 실패 : 존재하지 않는 문제")
void deleteProblemFailed_1() throws Exception {
// given
doThrow(new ProblemValidationException(HttpStatus.NOT_FOUND.value(), "존재하지 않는 문제 입니다.")).when(problemService)
.deleteProblem(user, problemId);
// when, then
mockMvc.perform(delete("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.param("problemId", String.valueOf(problemId)))
.andExpect(status().isNotFound())
.andExpect(jsonPath("$.error").value("존재하지 않는 문제 입니다."));
verify(problemService, times(1)).deleteProblem(user, problemId);
}
@Test
@DisplayName("문제 삭제 실패 : 권한 없음")
void deleteProblemFailed_2() throws Exception {
// given
doThrow(new StudyGroupValidationException(HttpStatus.FORBIDDEN.value(),
"문제 삭제 권한이 없습니다. 방장, 부방장일 경우에만 삭제가 가능합니다.")).when(problemService)
.deleteProblem(user, problemId);
// when, then
mockMvc.perform(delete("/api/problems/{problemId}", problemId)
.header("Authorization", token)
.param("problemId", String.valueOf(problemId)))
.andExpect(status().isForbidden())
.andExpect(jsonPath("$.error").value("문제 삭제 권한이 없습니다. 방장, 부방장일 경우에만 삭제가 가능합니다."));
verify(problemService, times(1)).deleteProblem(user, problemId);
}
}