1+ package com .cf .cfteam .services .codeforces ;
2+
3+ import com .cf .cfteam .exceptions .codeforces .GroupNotFoundException ;
4+ import com .cf .cfteam .exceptions .security .UserAlreadyRegisterException ;
5+ import com .cf .cfteam .exceptions .security .UserNotFoundException ;
6+ import com .cf .cfteam .models .entities .codeforces .Group ;
7+ import com .cf .cfteam .models .entities .security .Role ;
8+ import com .cf .cfteam .models .entities .security .User ;
9+ import com .cf .cfteam .repositories .jpa .codeforces .GroupRepository ;
10+ import com .cf .cfteam .repositories .jpa .security .UserRepository ;
11+ import com .cf .cfteam .transfer .payloads .codeforces .GroupPayload ;
12+ import org .junit .jupiter .api .BeforeEach ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .mockito .InjectMocks ;
15+ import org .mockito .Mock ;
16+ import org .mockito .MockitoAnnotations ;
17+ import org .springframework .test .context .ActiveProfiles ;
18+
19+ import java .util .List ;
20+ import java .util .Optional ;
21+
22+ import static org .assertj .core .api .Assertions .assertThat ;
23+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
24+ import static org .junit .jupiter .api .Assertions .*;
25+ import static org .mockito .ArgumentMatchers .any ;
26+ import static org .mockito .Mockito .*;
27+
28+ @ ActiveProfiles ("test" )
29+ class GroupServiceTest {
30+
31+ @ InjectMocks
32+ private GroupService groupService ;
33+
34+ @ Mock
35+ private GroupRepository groupRepository ;
36+
37+ @ Mock
38+ private UserRepository userRepository ;
39+
40+ private User user ;
41+ private Group group ;
42+ private GroupPayload groupPayload ;
43+
44+ @ BeforeEach
45+ void setUp () {
46+ MockitoAnnotations .openMocks (this );
47+
48+ user = User .builder ()
49+ .name ("User name" )
50+ .login ("User login" )
51+ .hashedPassword ("Password" )
52+ .role (Role .USER )
53+ .build ();
54+
55+ group = Group .builder ()
56+ .name ("Test Group" )
57+ .description ("Test description" )
58+ .user (user )
59+ .build ();
60+
61+ groupPayload = GroupPayload .builder ()
62+ .name ("Test Group" )
63+ .description ("Test description" )
64+ .build ();
65+ }
66+
67+ @ Test
68+ void getAllGroupsByUser_ShouldReturnGroups_WhenUserExists () {
69+ when (userRepository .findById (1L )).thenReturn (Optional .of (user ));
70+ when (groupRepository .findByUser (user )).thenReturn (List .of (group ));
71+
72+ var groups = groupService .getAllGroupsByUser (1L );
73+
74+ assertAll (
75+ () -> assertThat (groups ).hasSize (1 ),
76+ () -> assertThat (groups ).contains (group )
77+ );
78+ }
79+
80+ @ Test
81+ void getAllGroupsByUser_ShouldThrowUserNotFoundException_WhenUserDoesNotExist () {
82+ when (userRepository .findById (1L )).thenReturn (Optional .empty ());
83+
84+ assertThatThrownBy (() -> groupService .getAllGroupsByUser (1L ))
85+ .isInstanceOf (UserNotFoundException .class )
86+ .hasMessageContaining ("id.not_found" );
87+ }
88+
89+ @ Test
90+ void getGroupById_ShouldReturnGroup_WhenGroupExists () {
91+ when (groupRepository .findById (1L )).thenReturn (Optional .of (group ));
92+
93+ var result = groupService .getGroupById (1L );
94+
95+ assertThat (result ).isEqualTo (group );
96+ }
97+
98+ @ Test
99+ void getGroupById_ShouldThrowGroupNotFoundException_WhenGroupDoesNotExist () {
100+ when (groupRepository .findById (1L )).thenReturn (Optional .empty ());
101+
102+ assertThatThrownBy (() -> groupService .getGroupById (1L ))
103+ .isInstanceOf (GroupNotFoundException .class )
104+ .hasMessageContaining ("id.not_found" );
105+ }
106+
107+ @ Test
108+ void addGroupToUser_ShouldAddGroup_WhenUserExists () {
109+ when (userRepository .findById (1L )).thenReturn (Optional .of (user ));
110+ when (groupRepository .save (any (Group .class ))).thenReturn (group );
111+
112+ var result = groupService .addGroupToUser (1L , groupPayload );
113+
114+ assertThat (result ).isEqualTo (group );
115+ verify (groupRepository , times (1 )).save (any (Group .class ));
116+ }
117+
118+ @ Test
119+ void addGroupToUser_ShouldThrowUserNotFoundException_WhenUserDoesNotExist () {
120+ when (userRepository .findById (1L )).thenReturn (Optional .empty ());
121+
122+ assertThatThrownBy (() -> groupService .addGroupToUser (1L , groupPayload ))
123+ .isInstanceOf (UserNotFoundException .class )
124+ .hasMessageContaining ("id.not_found" );
125+ }
126+
127+ @ Test
128+ void updateGroup_ShouldUpdateGroup_WhenGroupExists () {
129+ when (groupRepository .findById (1L )).thenReturn (Optional .of (group ));
130+ when (groupRepository .save (any (Group .class ))).thenReturn (group );
131+
132+ groupPayload = GroupPayload .builder ()
133+ .name ("NEW Test Group" )
134+ .description ("NEW Test description" )
135+ .build ();
136+
137+ var result = groupService .updateGroup (1L , groupPayload );
138+
139+ assertAll (
140+ () -> assertThat (result .getDescription ()).isEqualTo (groupPayload .description ()),
141+ () -> assertThat (result .getName ()).isEqualTo (group .getName ())
142+ );
143+ verify (groupRepository , times (1 )).save (any (Group .class ));
144+ }
145+
146+ @ Test
147+ void updateGroup_ShouldThrowGroupNotFoundException_WhenGroupDoesNotExist () {
148+ when (groupRepository .findById (1L )).thenReturn (Optional .empty ());
149+
150+ assertThatThrownBy (() -> groupService .updateGroup (1L , groupPayload ))
151+ .isInstanceOf (GroupNotFoundException .class )
152+ .hasMessageContaining ("id.not_found" );
153+ }
154+
155+ @ Test
156+ void deleteGroup_ShouldDeleteGroup_WhenGroupExists () {
157+ when (groupRepository .findById (1L )).thenReturn (Optional .of (group ));
158+
159+ groupService .deleteGroup (1L );
160+
161+ verify (groupRepository , times (1 )).deleteById (1L );
162+ }
163+
164+ @ Test
165+ void deleteAllGroupsByUser_ShouldDeleteAllGroups_WhenUserExists () {
166+ when (userRepository .findById (1L )).thenReturn (Optional .of (user ));
167+ when (groupRepository .findByUser (user )).thenReturn (List .of (group ));
168+
169+ groupService .deleteAllGroupsByUser (1L );
170+
171+ verify (groupRepository , times (1 )).deleteAll (anyList ());
172+ }
173+
174+ @ Test
175+ void deleteAllGroupsByUser_ShouldThrowUserNotFoundException_WhenUserDoesNotExist () {
176+ when (userRepository .findById (1L )).thenReturn (Optional .empty ());
177+
178+ assertThatThrownBy (() -> groupService .deleteAllGroupsByUser (1L ))
179+ .isInstanceOf (UserNotFoundException .class )
180+ .hasMessageContaining ("id.not_found" );
181+ }
182+ }
0 commit comments