diff --git a/src/test/java/cz/muni/fi/pa165/icehockeymanager/facades/LeagueManagerImplTest.java b/src/test/java/cz/muni/fi/pa165/icehockeymanager/facades/LeagueManagerImplTest.java new file mode 100644 index 0000000000000000000000000000000000000000..d7ac34d5e55fac9356c3cdd076ddbe0bd4d98a3d --- /dev/null +++ b/src/test/java/cz/muni/fi/pa165/icehockeymanager/facades/LeagueManagerImplTest.java @@ -0,0 +1,119 @@ +package cz.muni.fi.pa165.icehockeymanager.facades; + +import cz.muni.fi.pa165.icehockeymanager.config.ApplicationConfig; +import cz.muni.fi.pa165.icehockeymanager.dto.GameCreateDTO; +import cz.muni.fi.pa165.icehockeymanager.dto.TeamDTO; +import cz.muni.fi.pa165.icehockeymanager.model.Game; +import cz.muni.fi.pa165.icehockeymanager.model.Player; +import cz.muni.fi.pa165.icehockeymanager.model.Team; +import cz.muni.fi.pa165.icehockeymanager.services.*; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.ApplicationContext; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import java.util.Optional; + +import static org.mockito.Mockito.*; + +@ExtendWith(SpringExtension.class) +@ContextConfiguration(classes = ApplicationConfig.class) +public class LeagueManagerImplTest { + @Autowired + private ApplicationContext context; + private GameService gameService; + private TeamService teamService; + private BeanMappingService beanMappingService; + private LeagueManagerFacade leagueManagerFacade; + + @BeforeEach + public void setUp() { + gameService = mock(GameService.class); + teamService = mock(TeamService.class); + beanMappingService = context.getBean(BeanMappingServiceImpl.class); + leagueManagerFacade = new LeagueManagerFacadeImpl(teamService, beanMappingService, gameService); + } + + private Player buildPlayerMock(Long id, String name) { + var player = mock(Player.class); + when(player.getId()).thenReturn(id); + when(player.getName()).thenReturn(name); + return player; + } + + public Team buildTeamMock(Long id, String name) { + var team = mock(Team.class); + when(team.getId()).thenReturn(id); + when(team.getName()).thenReturn(name); + return team; + } + + public TeamDTO buildTeamDTO(Long id, String name) { + var team = new TeamDTO(); + team.setId(id); + team.setName(name); + return team; + } + + public Game buildGameMock(Long id) { + var game = mock(Game.class); + when(game.getId()).thenReturn(id); + var edmonton = buildTeamMock(1L, "Edmonton"); + var slovan = buildTeamMock(2L, "Slovan"); + when(game.getAwayTeam()).thenReturn(edmonton); + when(game.getHomeTeam()).thenReturn(slovan); + return game; + } + + public GameCreateDTO buildGameCreateDTO(TeamDTO homeTeam, TeamDTO awayTeam) { + GameCreateDTO gameCreateDTO = new GameCreateDTO(); + gameCreateDTO.setAwayTeam(homeTeam); + gameCreateDTO.setHomeTeam(awayTeam); + return gameCreateDTO; + } + + @Test + public void scheduleGame() { + var edmonton = buildTeamDTO(1L, "Edmonton Oilers"); + var washington = buildTeamDTO(2L, "Washington Capitals"); + var gameCreateDTO = buildGameCreateDTO(washington, edmonton); + leagueManagerFacade.scheduleGame(gameCreateDTO); + verify(gameService).createGame(any(Game.class)); + verify(teamService).addAwayGame(any(Team.class), any(Game.class)); + verify(teamService).addHomeGame(any(Team.class), any(Game.class)); + } + + @Test + public void dropGame() { + var game = buildGameMock(1L); + when(gameService.findGame(1L)).thenReturn(Optional.ofNullable(game)); + leagueManagerFacade.dropGame(1L); + verify(gameService).deleteGame(any(Game.class)); + verify(teamService).removeHomeGame(any(Team.class), any(Game.class)); + verify(teamService).removeAwayGame(any(Team.class), any(Game.class)); + } + + @Test + public void updateGameScore() { + var game = buildGameMock(1L); + when(gameService.findGame(1L)).thenReturn(Optional.ofNullable(game)); + leagueManagerFacade.updateGameScore(1L, 1, 2); + verify(gameService).updateGameScore(game, 1, 2); + } + + @Test + public void setGameWinner() { + var game = buildGameMock(1L); + when(gameService.findGame(1L)).thenReturn(Optional.ofNullable(game)); + leagueManagerFacade.updateGameScore(1L, 1, 2); + verify(gameService).updateGameScore(game, 1, 2); + } + + @Test + public void createNewTeam() { + + } +}