1+ package com .cf .cfteam .services .codeforces ;
2+
3+
4+ import com .cf .cfteam .exceptions .codeforces .*;
5+ import com .cf .cfteam .models .entities .codeforces .Player ;
6+ import com .cf .cfteam .models .entities .codeforces .Team ;
7+ import com .cf .cfteam .repositories .jpa .codeforces .PlayerRepository ;
8+ import com .cf .cfteam .repositories .jpa .codeforces .TeamRepository ;
9+ import com .cf .cfteam .transfer .payloads .codeforces .PlayerPayload ;
10+ import com .cf .cfteam .transfer .responses .codeforces .PlayerResponse ;
11+ import com .cf .cfteam .utils .codeforces .mappers .PlayerMapper ;
12+ import org .junit .jupiter .api .BeforeEach ;
13+ import org .junit .jupiter .api .Test ;
14+ import org .junit .jupiter .api .extension .ExtendWith ;
15+ import org .mockito .InjectMocks ;
16+ import org .mockito .Mock ;
17+ import org .mockito .junit .jupiter .MockitoExtension ;
18+ import org .springframework .test .context .ActiveProfiles ;
19+
20+ import java .util .ArrayList ;
21+ import java .util .List ;
22+ import java .util .Optional ;
23+
24+ import static org .assertj .core .api .Assertions .assertThat ;
25+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
26+ import static org .mockito .Mockito .*;
27+
28+ @ ActiveProfiles ("test" )
29+ @ ExtendWith (MockitoExtension .class )
30+ class PlayerServiceTest {
31+
32+ @ InjectMocks
33+ private PlayerService playerService ;
34+
35+ @ Mock
36+ private PlayerRepository playerRepository ;
37+
38+ @ Mock
39+ private TeamRepository teamRepository ;
40+
41+ @ Mock
42+ private PlayerMapper playerMapper ;
43+
44+ @ Mock
45+ private TeamPlayerLinker teamPlayerLinker ;
46+
47+ private Team team ;
48+ private Player player ;
49+ private PlayerPayload playerPayload ;
50+ private PlayerResponse playerResponse ;
51+
52+ @ BeforeEach
53+ void setUp () {
54+ team = Team .builder ()
55+ .id (1L )
56+ .name ("team" )
57+ .description ("team description" )
58+ .build ();
59+
60+ player = Player .builder ()
61+ .id (1L )
62+ .login ("login" )
63+ .teams (List .of ())
64+ .build ();
65+
66+
67+ playerPayload = PlayerPayload .builder ()
68+ .login ("login" )
69+ .build ();
70+
71+ playerResponse = PlayerResponse .builder ()
72+ .login ("login" )
73+ .rating (1000. )
74+ .id (1L )
75+ .build ();
76+ }
77+
78+
79+ @ Test
80+ void getAllPlayersByTeam_ShouldReturnPlayers_WhenTeamExists () {
81+ team .setPlayers (List .of (player ));
82+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
83+
84+ when (playerMapper .fromEntityToResponse (player )).thenReturn (playerResponse );
85+
86+ var players = playerService .getAllPlayersByTeam (1L );
87+
88+ assertThat (players ).hasSize (1 )
89+ .contains (playerResponse );
90+ }
91+
92+ @ Test
93+ void getAllPlayersByTeam_ShouldThrowTeamNotFoundException_WhenTeamDoesNotExist () {
94+ when (teamRepository .findById (1L )).thenReturn (Optional .empty ());
95+
96+ assertThatThrownBy (() -> playerService .getAllPlayersByTeam (1L ))
97+ .isInstanceOf (TeamNotFoundException .class )
98+ .hasMessageContaining ("id.not_found" );
99+ }
100+
101+ @ Test
102+ void getPlayerById_ShouldReturnPlayer_WhenPlayerExists () {
103+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
104+ when (playerMapper .fromEntityToResponse (player )).thenReturn (playerResponse );
105+
106+ var result = playerService .getPlayerById (1L );
107+
108+ assertThat (result ).isEqualTo (playerResponse );
109+ }
110+
111+ @ Test
112+ void getPlayerById_ShouldThrowPlayerNotFoundException_WhenPlayerDoesNotExist () {
113+ when (playerRepository .findById (1L )).thenReturn (Optional .empty ());
114+
115+ assertThatThrownBy (() -> playerService .getPlayerById (1L ))
116+ .isInstanceOf (PlayerNotFoundException .class )
117+ .hasMessageContaining ("id.not_found" );
118+ }
119+
120+ @ Test
121+ void addPlayerToTeam_ShouldAddPlayer_WhenTeamExists () {
122+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
123+
124+ when (playerRepository .findByLogin ("login" )).thenReturn (Optional .of (player ));
125+
126+ doNothing ().when (teamPlayerLinker ).linkTeamAndPlayer (team , player );
127+
128+ doReturn (playerResponse ).when (playerMapper ).fromEntityToResponse (player );
129+
130+ var result = playerService .addPlayerToTeam (1L , playerPayload );
131+
132+ assertThat (result ).isEqualTo (playerResponse );
133+ verify (teamPlayerLinker , times (1 )).linkTeamAndPlayer (team , player );
134+ }
135+
136+ @ Test
137+ void addPlayerToTeam_ShouldThrowTeamNotFoundException_WhenTeamDoesNotExist () {
138+ when (teamRepository .findById (1L )).thenReturn (Optional .empty ());
139+
140+ assertThatThrownBy (() -> playerService .addPlayerToTeam (1L , playerPayload ))
141+ .isInstanceOf (TeamNotFoundException .class )
142+ .hasMessageContaining ("id.not_found" );
143+ }
144+
145+ @ Test
146+ void addPlayerToTeam_ShouldThrowPlayerAlreadyInTeamException_WhenPlayerAlreadyInTeamException () {
147+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
148+ when (playerRepository .findByLogin ("login" )).thenReturn (Optional .of (player ));
149+
150+ team .setPlayers (List .of (player ));
151+
152+ assertThatThrownBy (() -> playerService .addPlayerToTeam (1L , playerPayload ))
153+ .isInstanceOf (PlayerAlreadyInTeamException .class )
154+ .hasMessageContaining ("player.already_in_team" );
155+ }
156+
157+ @ Test
158+ void updatePlayerInTeam_ShouldUpdatePlayer_WhenTeamAndPlayerExistsAndFromOneTeam () {
159+ team .setPlayers (List .of (player ));
160+
161+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
162+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
163+
164+ doNothing ().when (teamPlayerLinker ).unlinkTeamAndPlayer (team , player );
165+ when (playerRepository .findByLogin ("login" )).thenReturn (Optional .of (player ));
166+ doNothing ().when (teamPlayerLinker ).linkTeamAndPlayer (team , player );
167+
168+ when (playerRepository .findByLogin ("login" )).thenReturn (Optional .of (player ));
169+
170+ doReturn (playerResponse ).when (playerMapper ).fromEntityToResponse (player );
171+
172+ var result = playerService .updatePlayerInTeam (1L , 1L , playerPayload );
173+
174+ assertThat (result ).isEqualTo (playerResponse );
175+ verify (teamPlayerLinker , times (1 )).unlinkTeamAndPlayer (team , player );
176+ verify (teamPlayerLinker , times (1 )).linkTeamAndPlayer (team , player );
177+ }
178+
179+ @ Test
180+ void updatePlayerInTeam_ShouldThrowPlayerNotFoundException_WhenPlayerDoesNotExist () {
181+ when (playerRepository .findById (1L )).thenReturn (Optional .empty ());
182+
183+ assertThatThrownBy (() -> playerService .updatePlayerInTeam (1L , 1L , playerPayload ))
184+ .isInstanceOf (PlayerNotFoundException .class )
185+ .hasMessageContaining ("id.not_found" );
186+ }
187+
188+ @ Test
189+ void updatePlayerInTeam_ShouldThrowTeamNotFoundException_WhenTeamDoesNotExist () {
190+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
191+ when (teamRepository .findById (1L )).thenReturn (Optional .empty ());
192+
193+ assertThatThrownBy (() -> playerService .updatePlayerInTeam (1L , 1L , playerPayload ))
194+ .isInstanceOf (TeamNotFoundException .class )
195+ .hasMessageContaining ("id.not_found" );
196+ }
197+
198+ @ Test
199+ void updatePlayerInTeam_ShouldThrowPlayerNotFromTeamException_WhenPlayerNotFromTeam () {
200+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
201+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
202+
203+ assertThatThrownBy (() -> playerService .updatePlayerInTeam (1L , 1L , playerPayload ))
204+ .isInstanceOf (PlayerNotFromTeamException .class )
205+ .hasMessageContaining ("player.not_from_team" );
206+ }
207+
208+ @ Test
209+ void deletePlayerFromTeam_ShouldDeletePlayerFromTeam_WhenTeamNadPlayerExists () {
210+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
211+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
212+
213+ doNothing ().when (teamPlayerLinker ).unlinkTeamAndPlayer (team , player );
214+ playerService .deletePlayerFromTeam (1L , 1L );
215+ verify (teamPlayerLinker , times (1 )).unlinkTeamAndPlayer (team , player );
216+ }
217+
218+ @ Test
219+ void deletePlayerFromTeam_ShouldThrowPlayerNotFoundException_WhenPlayerNotFound () {
220+ when (playerRepository .findById (1L )).thenReturn (Optional .empty ());
221+
222+ assertThatThrownBy (() -> playerService .deletePlayerFromTeam (1L , 1L ))
223+ .isInstanceOf (PlayerNotFoundException .class )
224+ .hasMessageContaining ("id.not_found" );
225+ }
226+
227+ @ Test
228+ void deletePlayerFromTeam_ShouldThrowTeamNotFoundException_WhenTeamNotFound () {
229+ when (playerRepository .findById (1L )).thenReturn (Optional .of (player ));
230+ when (teamRepository .findById (1L )).thenReturn (Optional .empty ());
231+
232+ assertThatThrownBy (() -> playerService .deletePlayerFromTeam (1L , 1L ))
233+ .isInstanceOf (TeamNotFoundException .class )
234+ .hasMessageContaining ("id.not_found" );
235+ }
236+
237+ @ Test
238+ void deleteAllPlayersFromTeam_ShouldDeleteAllPlayersFromTeam_WhenTeamPlayerExists () {
239+ when (teamRepository .findById (1L )).thenReturn (Optional .of (team ));
240+ team .setPlayers (new ArrayList <>(List .of (player )));
241+ player .setTeams (new ArrayList <>(List .of (team )));
242+
243+ doReturn (team ).when (teamRepository ).save (team );
244+ doReturn (player ).when (playerRepository ).save (player );
245+
246+ playerService .deleteAllPlayersFromTeam (1L );
247+
248+ verify (teamRepository , times (1 )).save (team );
249+ verify (playerRepository , times (1 )).save (player );
250+ }
251+
252+ @ Test
253+ void deleteAllPlayersFromTeam_ShouldThrowTeamNotFoundException_WhenTeamNotFound () {
254+ when (teamRepository .findById (1L )).thenReturn (Optional .empty ());
255+
256+ assertThatThrownBy (() -> playerService .deleteAllPlayersFromTeam (1L ))
257+ .isInstanceOf (TeamNotFoundException .class )
258+ .hasMessageContaining ("id.not_found" );
259+ }
260+ }
0 commit comments