-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignmentControllerTest.java
More file actions
399 lines (319 loc) · 18.4 KB
/
Copy pathAssignmentControllerTest.java
File metadata and controls
399 lines (319 loc) · 18.4 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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
package com.itasocialacademy.oitassist.taskassignment.controller;
import com.itasocialacademy.oitassist.ControllerUnitTest;
import com.itasocialacademy.oitassist.competition.dao.enums.ExecutionStatus;
import com.itasocialacademy.oitassist.competition.exceptions.CompetitionHierarchyValidationException;
import com.itasocialacademy.oitassist.competition.exceptions.TourNotFoundException;
import com.itasocialacademy.oitassist.filemanager.api.dto.FileDetailsDTO;
import com.itasocialacademy.oitassist.task.exceptions.TaskNotFoundException;
import com.itasocialacademy.oitassist.taskassignment.dao.enums.AssignmentVisibility;
import com.itasocialacademy.oitassist.taskassignment.dao.model.TaskRequirements;
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateAndAssignTaskRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.request.CreateTaskAssignmentRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.request.TaskRequirementsRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.request.UpdateTaskAssignmentRequestDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.DetailedTaskAssignmentResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.LinkedToursResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.dto.response.TaskAssignmentResponseDTO;
import com.itasocialacademy.oitassist.taskassignment.exceptions.TaskAlreadyAssignedException;
import com.itasocialacademy.oitassist.taskassignment.exceptions.TaskAssignmentNotFoundException;
import com.itasocialacademy.oitassist.taskassignment.service.interfaces.AssignmentService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.http.MediaType;
import java.util.List;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
class AssignmentControllerTest extends ControllerUnitTest<AssignmentController> {
@Mock
private AssignmentService assignmentService;
@InjectMocks
private AssignmentController assignmentController;
private TaskAssignmentResponseDTO mockAssignmentResponse;
private DetailedTaskAssignmentResponseDTO mockDetailedResponse;
private TaskRequirementsRequestDTO validRequirements;
@Override
protected AssignmentController getController() {
return assignmentController;
}
@BeforeEach
void setUp() {
validRequirements = new TaskRequirementsRequestDTO(List.of(
new TaskRequirementsRequestDTO.RequiredFileRequest("Файл розв'язку", "PowerPoint_РіздвянаЗірка",
List.of(".pptx"), 50)));
TaskRequirements requirements = new TaskRequirements(List.of(
new TaskRequirements.RequiredFile("Файл розв'язку", "PowerPoint_РіздвянаЗірка",
List.of(".pptx"), 50)));
List<FileDetailsDTO> testFiles = List.of(
new FileDetailsDTO(1L, "problem.pdf", "application/pdf", 2048L, "PROBLEM",
"/uploads/task/problem.pdf"));
mockAssignmentResponse = TaskAssignmentResponseDTO.builder()
.id(1L)
.taskBodyId(3L)
.taskTitle("PowerPoint Різдвяна зірка")
.tourId(10L)
.maxPoints(25)
.createdBy(100L)
.build();
mockDetailedResponse = new DetailedTaskAssignmentResponseDTO(
1L, 3L, "PowerPoint Різдвяна зірка", "Створити у файлі-розв'язку",
10L, AssignmentVisibility.VISIBLE, 25, requirements, testFiles, 100L);
}
// POST /api/v1/tours/{tourId}/task-assignments — assignTask
@Test
void assignTask_validRequest_shouldReturn201() throws Exception {
CreateTaskAssignmentRequestDTO request = new CreateTaskAssignmentRequestDTO(
3L, AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.assignTask(eq(10L), any(CreateTaskAssignmentRequestDTO.class)))
.thenReturn(mockDetailedResponse);
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(mockDetailedResponse.id()))
.andExpect(jsonPath("$.taskBodyId").value(mockDetailedResponse.taskBodyId()))
.andExpect(jsonPath("$.tourId").value(mockDetailedResponse.tourId()))
.andExpect(jsonPath("$.taskTitle").value(mockDetailedResponse.taskTitle()))
.andExpect(jsonPath("$.taskDescription").value(mockDetailedResponse.taskDescription()))
.andExpect(jsonPath("$.visibility").value(mockDetailedResponse.visibility().name()))
.andExpect(jsonPath("$.files").isArray());
}
@Test
void assignTask_tourNotFound_shouldReturn404() throws Exception {
CreateTaskAssignmentRequestDTO request = new CreateTaskAssignmentRequestDTO(
3L, AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.assignTask(eq(99L), any(CreateTaskAssignmentRequestDTO.class)))
.thenThrow(new TourNotFoundException(99L));
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments", 99L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNotFound());
}
@Test
void assignTask_tourNotScheduled_shouldReturn400() throws Exception {
CreateTaskAssignmentRequestDTO request = new CreateTaskAssignmentRequestDTO(
3L, AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.assignTask(eq(10L), any(CreateTaskAssignmentRequestDTO.class)))
.thenThrow(new CompetitionHierarchyValidationException("Cannot assign task. Tour has already started"));
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
}
@Test
void assignTask_duplicateAssignment_shouldReturn409() throws Exception {
CreateTaskAssignmentRequestDTO request = new CreateTaskAssignmentRequestDTO(
3L, AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.assignTask(eq(10L), any(CreateTaskAssignmentRequestDTO.class)))
.thenThrow(new TaskAlreadyAssignedException(3L, 10L));
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isConflict());
}
@Test
void assignTask_nullTaskBodyId_shouldReturn400() throws Exception {
CreateTaskAssignmentRequestDTO request = new CreateTaskAssignmentRequestDTO(
null, AssignmentVisibility.VISIBLE, null, null);
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
verifyNoInteractions(assignmentService);
}
// POST /api/v1/tours/{tourId}/task-assignments/new — createAndAssignTask
@Test
void createAndAssignTask_validRequest_shouldReturn201() throws Exception {
CreateAndAssignTaskRequestDTO request = new CreateAndAssignTaskRequestDTO(
"Task Title", "Task Description", List.of(1L, 2L), AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.createAndAssignTask(eq(10L), any(CreateAndAssignTaskRequestDTO.class)))
.thenReturn(mockDetailedResponse);
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments/new", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isCreated())
.andExpect(jsonPath("$.id").value(mockDetailedResponse.id()))
.andExpect(jsonPath("$.taskBodyId").value(mockDetailedResponse.taskBodyId()))
.andExpect(jsonPath("$.tourId").value(mockDetailedResponse.tourId()))
.andExpect(jsonPath("$.taskTitle").value(mockDetailedResponse.taskTitle()));
}
@Test
void createAndAssignTask_tourNotFound_shouldReturn404() throws Exception {
CreateAndAssignTaskRequestDTO request = new CreateAndAssignTaskRequestDTO(
"Task Title", "Task Description", List.of(1L, 2L), AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.createAndAssignTask(eq(99L), any(CreateAndAssignTaskRequestDTO.class)))
.thenThrow(new TourNotFoundException(99L));
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments/new", 99L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNotFound());
}
@Test
void createAndAssignTask_tourNotScheduled_shouldReturn400() throws Exception {
CreateAndAssignTaskRequestDTO request = new CreateAndAssignTaskRequestDTO(
"Task Title", "Task Description", List.of(1L, 2L), AssignmentVisibility.VISIBLE, 25, validRequirements);
when(assignmentService.createAndAssignTask(eq(10L), any(CreateAndAssignTaskRequestDTO.class)))
.thenThrow(
new CompetitionHierarchyValidationException("Cannot create task assignment. Tour has already started"));
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments/new", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
}
@Test
void createAndAssignTask_blankTitle_shouldReturn400() throws Exception {
CreateAndAssignTaskRequestDTO request = new CreateAndAssignTaskRequestDTO(
" ", "Task Description", List.of(1L, 2L), AssignmentVisibility.VISIBLE, 25, validRequirements);
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments/new", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
verifyNoInteractions(assignmentService);
}
@Test
void createAndAssignTask_nullMaxPoints_shouldReturn400() throws Exception {
CreateAndAssignTaskRequestDTO request = new CreateAndAssignTaskRequestDTO(
"Task Title", "Task Description", List.of(1L, 2L), AssignmentVisibility.VISIBLE, null, validRequirements);
mockMvc.perform(post("/api/v1/tours/{tourId}/task-assignments/new", 10L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
verifyNoInteractions(assignmentService);
}
// GET /api/v1/task-assignments/{assignmentId} — getById
@Test
void getById_existingId_shouldReturn200() throws Exception {
when(assignmentService.getTaskAssignmentById(1L)).thenReturn(mockDetailedResponse);
mockMvc.perform(get("/api/v1/task-assignments/{assignmentId}", 1L))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(mockDetailedResponse.id()))
.andExpect(jsonPath("$.taskTitle").value(mockDetailedResponse.taskTitle()))
.andExpect(jsonPath("$.taskDescription").value(mockDetailedResponse.taskDescription()))
.andExpect(jsonPath("$.visibility").value(mockDetailedResponse.visibility().name()))
.andExpect(jsonPath("$.files").isArray())
.andExpect(jsonPath("$.files[0].originalFilename").value("problem.pdf"));
}
@Test
void getById_nonExistingId_shouldReturn404() throws Exception {
when(assignmentService.getTaskAssignmentById(99L)).thenThrow(new TaskAssignmentNotFoundException(99L));
mockMvc.perform(get("/api/v1/task-assignments/{assignmentId}", 99L))
.andExpect(status().isNotFound());
}
// GET /api/v1/tours/{tourId}/task-assignments — getByTour
@Test
void getByTour_shouldReturnPageResponseAnd200() throws Exception {
when(assignmentService.getAssignmentsByTourId(any(Pageable.class), eq(10L)))
.thenReturn(new PageImpl<>(List.of(mockAssignmentResponse)));
mockMvc.perform(get("/api/v1/tours/{tourId}/task-assignments", 10L))
.andExpect(status().isOk())
.andExpect(jsonPath("$.content[0].id").value(mockAssignmentResponse.id()))
.andExpect(jsonPath("$.pageNumber").value(0))
.andExpect(jsonPath("$.totalElements").value(1));
}
@Test
void getByTour_tourNotFound_shouldReturn404() throws Exception {
when(assignmentService.getAssignmentsByTourId(any(Pageable.class), eq(99L)))
.thenThrow(new TourNotFoundException(99L));
mockMvc.perform(get("/api/v1/tours/{tourId}/task-assignments", 99L))
.andExpect(status().isNotFound());
}
// GET /api/v1/tasks/{taskId}/linked-tours — getLinkedTours
@Test
void getLinkedTours_shouldReturnListAnd200() throws Exception {
LinkedToursResponseDTO mockTourResponse = LinkedToursResponseDTO.builder()
.tourId(10L)
.title("Tour 1")
.description("Description")
.location("Location")
.executionStatus(ExecutionStatus.SCHEDULED)
.build();
when(assignmentService.getLinkedToursByTaskId(3L))
.thenReturn(List.of(mockTourResponse));
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 3L))
.andExpect(status().isOk())
.andExpect(jsonPath("$").isArray())
.andExpect(jsonPath("$[0].tourId").value(10L))
.andExpect(jsonPath("$[0].title").value("Tour 1"))
.andExpect(jsonPath("$[0].description").value("Description"))
.andExpect(jsonPath("$[0].location").value("Location"))
.andExpect(jsonPath("$[0].executionStatus").value("SCHEDULED"));
}
@Test
void getLinkedTours_taskNotFound_shouldReturn404() throws Exception {
when(assignmentService.getLinkedToursByTaskId(99L))
.thenThrow(new TaskNotFoundException(99L));
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 99L))
.andExpect(status().isNotFound());
}
@Test
void getLinkedTours_tourNotFound_shouldReturn404() throws Exception {
when(assignmentService.getLinkedToursByTaskId(3L))
.thenThrow(new TourNotFoundException(10L));
mockMvc.perform(get("/api/v1/tasks/{taskId}/linked-tours", 3L))
.andExpect(status().isNotFound());
}
// PATCH /api/v1/task-assignments/{assignmentId} — update
@Test
void update_validRequest_shouldReturn200() throws Exception {
UpdateTaskAssignmentRequestDTO request = new UpdateTaskAssignmentRequestDTO(
AssignmentVisibility.HIDDEN, 30, validRequirements);
when(assignmentService.updateTaskAssignment(eq(1L), any(UpdateTaskAssignmentRequestDTO.class)))
.thenReturn(mockDetailedResponse);
mockMvc.perform(patch("/api/v1/task-assignments/{assignmentId}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(mockDetailedResponse.id()))
.andExpect(jsonPath("$.taskDescription").value(mockDetailedResponse.taskDescription()))
.andExpect(jsonPath("$.files").isArray());
}
@Test
void update_notFound_shouldReturn404() throws Exception {
UpdateTaskAssignmentRequestDTO request = new UpdateTaskAssignmentRequestDTO(
AssignmentVisibility.HIDDEN, 30, validRequirements);
when(assignmentService.updateTaskAssignment(eq(99L), any(UpdateTaskAssignmentRequestDTO.class)))
.thenThrow(new TaskAssignmentNotFoundException(99L));
mockMvc.perform(patch("/api/v1/task-assignments/{assignmentId}", 99L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isNotFound());
}
@Test
void update_tourNotScheduled_shouldReturn400() throws Exception {
UpdateTaskAssignmentRequestDTO request = new UpdateTaskAssignmentRequestDTO(
AssignmentVisibility.HIDDEN, 30, validRequirements);
when(assignmentService.updateTaskAssignment(eq(1L), any(UpdateTaskAssignmentRequestDTO.class)))
.thenThrow(
new CompetitionHierarchyValidationException("Cannot update task assignment. Tour has already started"));
mockMvc.perform(patch("/api/v1/task-assignments/{assignmentId}", 1L)
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(request)))
.andExpect(status().isBadRequest());
}
// DELETE /api/v1/task-assignments/{assignmentId} — delete
@Test
void delete_shouldReturn204() throws Exception {
doNothing().when(assignmentService).deleteTaskAssignment(1L);
mockMvc.perform(delete("/api/v1/task-assignments/{assignmentId}", 1L))
.andExpect(status().isNoContent());
verify(assignmentService).deleteTaskAssignment(1L);
}
@Test
void delete_notFound_shouldReturn404() throws Exception {
doThrow(new TaskAssignmentNotFoundException(99L)).when(assignmentService).deleteTaskAssignment(99L);
mockMvc.perform(delete("/api/v1/task-assignments/{assignmentId}", 99L))
.andExpect(status().isNotFound());
}
@Test
void delete_tourNotScheduled_shouldReturn400() throws Exception {
doThrow(new CompetitionHierarchyValidationException("Cannot delete task assignment. Tour has already started"))
.when(assignmentService).deleteTaskAssignment(1L);
mockMvc.perform(delete("/api/v1/task-assignments/{assignmentId}", 1L))
.andExpect(status().isBadRequest());
}
}