Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tests #9

Merged
merged 4 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/com/luka/levi9/player/Player.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class Player {
private int losses;
private double elo;
private int hoursPlayed;
private int ratingAdjustment;
private int ratingAdjustment = 50;
@ManyToOne
@JsonIgnore
private Team team;
Expand Down
63 changes: 63 additions & 0 deletions src/test/java/com/luka/levi9/controller/PlayerControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.luka.levi9.controller;

import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.util.List;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;

import com.luka.levi9.player.Player;
import com.luka.levi9.player.PlayerController;
import com.luka.levi9.player.PlayerService;

@WebMvcTest(PlayerController.class)
class PlayerControllerTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private PlayerService playerService;

private Player player;

@BeforeEach
void setUp() {
player = new Player();
player.setId("123");
player.setNickname("testPlayer");
}

@Test
void testGetPlayer_Success() throws Exception {
when(playerService.getPlayer("123")).thenReturn(player);

mockMvc.perform(MockMvcRequestBuilders.get("/players/123")).andExpect(status().isOk())
.andExpect(jsonPath("$.id").value("123")).andExpect(jsonPath("$.nickname").value("testPlayer"));

verify(playerService).getPlayer("123");
}

@Test
void testGetPlayers_Success() throws Exception {
Player player2 = new Player();
player2.setId("456");
player2.setNickname("testPlayer2");
when(playerService.getPlayers()).thenReturn(List.of(player, player2));

mockMvc.perform(MockMvcRequestBuilders.get("/players")).andExpect(status().isOk())
.andExpect(jsonPath("$[0].nickname").value("testPlayer"))
.andExpect(jsonPath("$[1].nickname").value("testPlayer2"));

verify(playerService).getPlayers();
}
}
98 changes: 98 additions & 0 deletions src/test/java/com/luka/levi9/model/PlayerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package com.luka.levi9.model;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.luka.levi9.match.Match;
import com.luka.levi9.player.Player;
import com.luka.levi9.team.Team;

class PlayerTest {

private Player player;
private Match matchMock;
private Team teamMock;
private Team opponentMock;

@BeforeEach
void setUp() {
player = new Player();
player.setNickname("TestPlayer");
player.setElo(1500);
player.setHoursPlayed(0);
player.setWins(0);
player.setLosses(0);

matchMock = mock(Match.class);
teamMock = mock(Team.class);
opponentMock = mock(Team.class);

when(matchMock.getDuration()).thenReturn(60);
when(matchMock.getWinningTeam()).thenReturn(teamMock);
when(matchMock.getOpponentTeam(teamMock)).thenReturn(opponentMock);

when(opponentMock.getAverageElo()).thenReturn(1400.0);

player.setTeam(teamMock);
}

@Test
void testUpdateStats_WinningTeam() {
player.setTeam(teamMock);

player.updateStats(matchMock);

assertEquals(1, player.getWins());
assertEquals(0, player.getLosses());
assertEquals(60, player.getHoursPlayed());
assertEquals(50, player.getRatingAdjustment());
assertEquals(1500 + 50 * (1 - 0.64), player.getElo(), 0.01);
}

@Test
void testUpdateStats_LosingTeam() {
when(matchMock.getWinningTeam()).thenReturn(opponentMock);
when(matchMock.getDuration()).thenReturn(60);
when(matchMock.getOpponentTeam(teamMock)).thenReturn(opponentMock);
when(opponentMock.getAverageElo()).thenReturn(1400.0);

player.updateStats(matchMock);

assertEquals(1, player.getLosses());
assertEquals(0, player.getWins());
assertEquals(60, player.getHoursPlayed());
verify(matchMock, times(2)).getWinningTeam();
}

@Test
void testUpdateElo_IndirectValidation() {
player.setTeam(teamMock);

player.updateStats(matchMock);

double expectedEloIncrease = 50 * (1 - 0.64);
assertEquals(1500 + expectedEloIncrease, player.getElo(), 0.01);
}

@Test
void testHoursPlayedAdjustment() {
player.setTeam(teamMock);

player.updateStats(matchMock);
assertEquals(50, player.getRatingAdjustment());

player.setHoursPlayed(600);
player.updateStats(matchMock);
assertEquals(40, player.getRatingAdjustment());

player.setHoursPlayed(2000);
player.updateStats(matchMock);
assertEquals(30, player.getRatingAdjustment());
}
}
72 changes: 72 additions & 0 deletions src/test/java/com/luka/levi9/service/MatchServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package com.luka.levi9.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;

import com.luka.levi9.common.exception.NotFoundException;
import com.luka.levi9.common.exception.ValidationException;
import com.luka.levi9.match.MatchRepository;
import com.luka.levi9.match.MatchRequest;
import com.luka.levi9.match.MatchService;
import com.luka.levi9.player.PlayerRepository;
import com.luka.levi9.team.TeamRepository;

import jakarta.persistence.EntityManager;

@SpringJUnitConfig
public class MatchServiceTest {

@Mock
private MatchRepository matchRepository;

@Mock
private TeamRepository teamRepository;

@Mock
private PlayerRepository playerRepository;

@Mock
private EntityManager entityManager;

@InjectMocks
private MatchService matchService;

public MatchServiceTest() {
MockitoAnnotations.openMocks(this);
}

@Test
void testCreateMatch_SameTeams() {
MatchRequest request = new MatchRequest();
request.setTeam1Id("team1Id");
request.setTeam2Id("team1Id");

ValidationException exception = assertThrows(ValidationException.class, () -> {
matchService.createMatch(request);
});

assertEquals("Team 1 and Team 2 must be different", exception.getMessage());
}

@Test
void testCreateMatch_InvalidWinningTeam() {
MatchRequest request = new MatchRequest();
request.setTeam1Id("team1Id");
request.setTeam2Id("team2Id");
request.setDuration(60);
request.setWinningTeamId("team3Id");

ValidationException exception = assertThrows(ValidationException.class, () -> {
matchService.createMatch(request);
});

assertEquals("Winning team must be one of the teams in the match", exception.getMessage());
}

}
102 changes: 102 additions & 0 deletions src/test/java/com/luka/levi9/service/PlayerServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.luka.levi9.service;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import com.luka.levi9.common.exception.AlreadyExistsException;
import com.luka.levi9.common.exception.NotFoundException;
import com.luka.levi9.player.Player;
import com.luka.levi9.player.PlayerRepository;
import com.luka.levi9.player.PlayerService;

class PlayerServiceTest {

@Mock
private PlayerRepository playerRepository;

@InjectMocks
private PlayerService playerService;

private Player player;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
player = new Player();
player.setId("123");
player.setNickname("testPlayer");
}

@Test
void testCreatePlayer_Success() {
when(playerRepository.findByNickname("testPlayer")).thenReturn(Optional.empty());
when(playerRepository.save(player)).thenReturn(player);

Player result = playerService.createPlayer(player);

assertNotNull(result);
assertEquals("testPlayer", result.getNickname());
verify(playerRepository).findByNickname("testPlayer");
verify(playerRepository).save(player);
}

@Test
void testCreatePlayer_AlreadyExists() {
when(playerRepository.findByNickname("testPlayer")).thenReturn(Optional.of(player));

assertThrows(AlreadyExistsException.class, () -> playerService.createPlayer(player));
verify(playerRepository).findByNickname("testPlayer");
verify(playerRepository, never()).save(any());
}

@Test
void testGetPlayer_Success() {
when(playerRepository.findById("123")).thenReturn(Optional.of(player));

Player result = playerService.getPlayer("123");

assertNotNull(result);
assertEquals("123", result.getId());
verify(playerRepository).findById("123");
}

@Test
void testGetPlayer_NotFound() {
when(playerRepository.findById("123")).thenReturn(Optional.empty());

assertThrows(NotFoundException.class, () -> playerService.getPlayer("123"));
verify(playerRepository).findById("123");
}

@Test
void testGetPlayers_Success() {
Player player2 = new Player();
player2.setId("456");
player2.setNickname("testPlayer2");

when(playerRepository.findAll()).thenReturn(List.of(player, player2));

List<Player> result = playerService.getPlayers();

assertNotNull(result);
assertEquals(2, result.size());
assertTrue(result.contains(player));
assertTrue(result.contains(player2));
verify(playerRepository).findAll();
}
}
Loading