-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathUpdateTask.java
More file actions
114 lines (96 loc) · 3.85 KB
/
UpdateTask.java
File metadata and controls
114 lines (96 loc) · 3.85 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
package me.eddypbr.todolist;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Optional;
import java.util.UUID;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.mockito.*;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import jakarta.servlet.http.HttpServletRequest;
import me.eddypbr.todolist.task.*;
import me.eddypbr.todolist.user.UserModel;
public class UpdateTask {
@InjectMocks
private TaskController controller;
@Mock
private ITaskRepository repo;
@Mock
private HttpServletRequest request;
private UUID userId;
private UUID taskId;
@BeforeEach
public void setUp() {
MockitoAnnotations.openMocks(this);
userId = UUID.randomUUID();
taskId = UUID.randomUUID();
}
private UserModel mockUser() {
UserModel user = new UserModel();
user.setId(userId);
return user;
}
private TaskModel mockTask() {
TaskModel task = new TaskModel();
task.setId(taskId);
task.setIdUser(userId);
return task;
}
@ParameterizedTest
@CsvSource({"Updated Title 1, Updated Desc 1"})
public void testUpdateTaskSuccess(String title, String description) throws Exception {
when(request.getAttribute("user")).thenReturn(mockUser());
when(repo.findById(taskId)).thenReturn(Optional.of(mockTask()));
when(repo.save(any())).thenReturn(mockTask());
TaskModel update = new TaskModel();
update.setTitle(title);
update.setDescription(description);
ResponseEntity response = controller.update(update, request, taskId);
assertEquals(HttpStatus.OK, response.getStatusCode());
verify(repo, times(1)).save(any());
}
@ParameterizedTest
@CsvSource({"00000000-0000-0000-0000-000000000001"})
public void testUpdateTaskNotFound(String idStr) throws Exception {
UUID id = UUID.fromString(idStr);
when(repo.findById(id)).thenReturn(Optional.empty());
ResponseEntity response = controller.update(new TaskModel(), request, id);
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
}
@Test
void testUpdateTaskEmptyTitle() throws Exception {
when(request.getAttribute("user")).thenReturn(mockUser());
when(repo.findById(taskId)).thenReturn(Optional.of(mockTask()));
TaskModel update = new TaskModel();
update.setTitle("");
update.setDescription("desc");
ResponseEntity response = controller.update(update, request, taskId);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
@Test
void testUpdateTaskNullDescription() throws Exception {
when(request.getAttribute("user")).thenReturn(mockUser());
when(repo.findById(taskId)).thenReturn(Optional.of(mockTask()));
TaskModel update = new TaskModel();
update.setTitle("Valid Title");
update.setDescription(null);
ResponseEntity response = controller.update(update, request, taskId);
assertEquals(HttpStatus.OK, response.getStatusCode());
}
@Test
public void testUpdateTaskUnauthorizedUser() throws Exception {
UUID otherUserId = UUID.randomUUID();
TaskModel existing = new TaskModel();
existing.setId(taskId);
existing.setIdUser(otherUserId);
when(request.getAttribute("user")).thenReturn(mockUser());
when(repo.findById(taskId)).thenReturn(Optional.of(existing));
TaskModel update = new TaskModel();
update.setTitle("Test");
ResponseEntity response = controller.update(update, request, taskId);
assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode());
}
}