Skip to content
Snippets Groups Projects
Commit 3a751cba authored by Erik Moravec's avatar Erik Moravec
Browse files

test: unit test for scheduleGame, dropGame, setGameWinner and necessarry...

test: unit test for scheduleGame, dropGame, setGameWinner and necessarry mocking/build methods implemented
parent 945c5133
No related branches found
No related tags found
No related merge requests found
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() {
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment