Skip to content
This repository was archived by the owner on Dec 27, 2024. It is now read-only.

Commit 061b7f8

Browse files
dev/codeforces/test Добавил unit тесты для GroupController
1 parent dcae5c8 commit 061b7f8

File tree

1 file changed

+112
-0
lines changed

1 file changed

+112
-0
lines changed
Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
package com.cf.cfteam.controllers.codeforces;
2+
3+
import com.cf.cfteam.models.entities.codeforces.Group;
4+
import com.cf.cfteam.services.codeforces.GroupService;
5+
import com.cf.cfteam.transfer.payloads.codeforces.GroupPayload;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.mockito.InjectMocks;
9+
import org.mockito.Mock;
10+
import org.mockito.MockitoAnnotations;
11+
import org.springframework.test.context.ActiveProfiles;
12+
13+
import java.util.List;
14+
15+
import static org.assertj.core.api.Assertions.assertThat;
16+
import static org.junit.jupiter.api.Assertions.assertAll;
17+
import static org.mockito.Mockito.*;
18+
19+
20+
@ActiveProfiles("test")
21+
class GroupControllerTest {
22+
23+
@InjectMocks
24+
private GroupController groupController;
25+
26+
@Mock
27+
private GroupService groupService;
28+
29+
private GroupPayload groupPayload;
30+
private Group group;
31+
32+
@BeforeEach
33+
void setUp() {
34+
MockitoAnnotations.openMocks(this);
35+
36+
group = Group.builder()
37+
.name("Test Group")
38+
.description("Test description")
39+
.user(null)
40+
.build();
41+
42+
groupPayload = GroupPayload.builder()
43+
.name("Test Group")
44+
.description("Test description")
45+
.build();
46+
}
47+
48+
@Test
49+
void shouldReturnGroupsByUserId() {
50+
when(groupService.getAllGroupsByUser(1L)).thenReturn(List.of(group));
51+
52+
List<Group> groups = groupController.getAllGroupsByUser(1L, null).getBody();
53+
54+
verify(groupService, times(1)).getAllGroupsByUser(1L);
55+
56+
assertThat(groups)
57+
.isNotNull()
58+
.hasSize(1)
59+
.contains(group);
60+
}
61+
62+
@Test
63+
void shouldReturnGroupById() {
64+
when(groupService.getGroupById(1L)).thenReturn(group);
65+
66+
Group result = groupController.getGroupById(1L, null).getBody();
67+
68+
verify(groupService, times(1)).getGroupById(1L);
69+
assertThat(result).isEqualTo(group);
70+
}
71+
72+
@Test
73+
void shouldAddGroupToUser() {
74+
when(groupService.addGroupToUser(1L, groupPayload)).thenReturn(group);
75+
76+
77+
Group result = groupController.addGroupToUser(1L, groupPayload, null).getBody();
78+
79+
verify(groupService, times(1)).addGroupToUser(1L, groupPayload);
80+
81+
assertThat(result).isEqualTo(group);
82+
}
83+
84+
@Test
85+
void shouldUpdateGroup() {
86+
when(groupService.updateGroup(1L, groupPayload)).thenReturn(group);
87+
88+
Group result = groupController.updateGroup(1L, groupPayload, null).getBody();
89+
90+
verify(groupService, times(1)).updateGroup(1L, groupPayload);
91+
assertAll(
92+
() -> assertThat(result).isNotNull(),
93+
() -> assertThat(result.getName()).isEqualTo(groupPayload.name()),
94+
() -> assertThat(result.getDescription()).isEqualTo(groupPayload.description())
95+
);
96+
}
97+
98+
@Test
99+
void shouldDeleteGroup() {
100+
groupController.deleteGroup(1L, null);
101+
102+
verify(groupService, times(1)).deleteGroup(1L);
103+
}
104+
105+
@Test
106+
void shouldDeleteAllGroupsByUser() {
107+
groupController.deleteAllGroupsByUser(1L, null);
108+
109+
verify(groupService, times(1)).deleteAllGroupsByUser(1L);
110+
}
111+
}
112+

0 commit comments

Comments
 (0)