diff --git a/sth-backend/src/test/java/hdm/mi/sthbackend/TournamentControllerTest.java b/sth-backend/src/test/java/hdm/mi/sthbackend/TournamentControllerTest.java new file mode 100644 index 0000000000000000000000000000000000000000..7970142aa0f9cf7c6c4ec0bdba2075a1a7322a0f --- /dev/null +++ b/sth-backend/src/test/java/hdm/mi/sthbackend/TournamentControllerTest.java @@ -0,0 +1,80 @@ +package hdm.mi.sthbackend; + +import com.fasterxml.jackson.databind.ObjectMapper; +import hdm.mi.sthbackend.controller.TournamentController; +import hdm.mi.sthbackend.model.BracketRound; +import hdm.mi.sthbackend.model.Match; +import hdm.mi.sthbackend.model.Tournament; +import hdm.mi.sthbackend.repository.ITournamentRepository; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.junit.runner.RunWith; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.web.servlet.MockMvc; + +import java.util.ArrayList; +import java.util.Optional; +import java.util.UUID; + +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.mockito.Mockito.*; + +@SpringBootTest +@AutoConfigureMockMvc +@ExtendWith(MockitoExtension.class) +public class TournamentControllerTest { + private final Logger log = LogManager.getLogger("TournamentControllerTest"); + + @Mock + ITournamentRepository tournamentRepository; + @InjectMocks + TournamentController tournamentController; + @Autowired + public MockMvc mockMvc; + @Test + public void assignTeamToMatch() throws Exception { + + UUID tournamentId = UUID.randomUUID(); + UUID matchId = UUID.randomUUID(); + UUID teamId = UUID.randomUUID(); + Match match = new Match(matchId); + Tournament tournament = new Tournament("TestTournament", new ArrayList<>()); + tournament.getBracket().add(new BracketRound(0)); + tournament.getBracket().get(0).getMatches().put(matchId, match); + + when(tournamentRepository.findById(tournamentId)).thenReturn(Optional.of(tournament)); + + mockMvc.perform(patch("/tournaments/{tournamentId}/matches/{matchId}/teams/{teamId}/assignTeamToMatch", tournamentId, matchId, teamId) + .contentType(MediaType.APPLICATION_JSON) + .content("{\"bracketRound\": 0, \"teamMatchScore\": 0}")) + .andExpect(status().isOk()); + + + verify(tournamentRepository, times(1)).findById(tournamentId); + verify(tournamentRepository, times(1)).save(tournament); + } + + @Test + public void createTournamentTest() throws Exception { + + UUID tournamentId = UUID.randomUUID(); + Tournament tournament = new Tournament("TestTournament", new ArrayList<>()); + when(tournamentRepository.findById(tournamentId)).thenReturn(Optional.of(tournament)); + + mockMvc.perform(post("/tournaments") + .contentType(MediaType.APPLICATION_JSON) + .content(new ObjectMapper().writeValueAsString(tournament))) + .andExpect(status().isOk()); + } +} diff --git a/sth-backend/src/test/java/hdm/mi/sthbackend/serviceTests/FillBracketRandomTest.java b/sth-backend/src/test/java/hdm/mi/sthbackend/serviceTests/FillBracketRandomTest.java index d97782197bdbb681a0a3a596e05d1f41a0895805..f1fcd6a61264f2d17f617e3a1c159a77c69bc896 100644 --- a/sth-backend/src/test/java/hdm/mi/sthbackend/serviceTests/FillBracketRandomTest.java +++ b/sth-backend/src/test/java/hdm/mi/sthbackend/serviceTests/FillBracketRandomTest.java @@ -8,6 +8,7 @@ import hdm.mi.sthbackend.repository.ITournamentRepository; import hdm.mi.sthbackend.service.TournamentService; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; @@ -19,6 +20,7 @@ import java.util.Optional; import java.util.UUID; import static hdm.mi.sthbackend.dummyObjects.dummyTournaments.*; +import static org.junit.Assert.assertEquals; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -42,11 +44,14 @@ public class FillBracketRandomTest { when(tournamentRepository.findById(dummyTournamentId)).thenReturn(Optional.of(t1withMatches)); Tournament t1Filled = tournamentService.fillBracketRandom(dummyTournamentId); + // summiert die Anzahl der Teams jedes Matches in der ersten BracketRound int teamSum = t1Filled.getBracket() .stream() .flatMap(bracketRound -> bracketRound.getMatches().values().stream()) .map(match -> match.getTeamScores().size()) .reduce(0, Integer::sum); + + Assertions.assertEquals(2, teamSum); } } diff --git a/sth-backend/src/test/java/hdm/mi/sthbackend/t1.java b/sth-backend/src/test/java/hdm/mi/sthbackend/t1.java index a1aaf59116e5c93362e97b763a86cf45bed5cf23..c97e7c712aca73d1ccf7278822e0844b39450737 100644 --- a/sth-backend/src/test/java/hdm/mi/sthbackend/t1.java +++ b/sth-backend/src/test/java/hdm/mi/sthbackend/t1.java @@ -1,88 +1,88 @@ -package hdm.mi.sthbackend; - -import hdm.mi.sthbackend.controller.TournamentController; -import hdm.mi.sthbackend.dto.MatchDTO; -import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; -import hdm.mi.sthbackend.mapper.ModelToDTOMapper; -import hdm.mi.sthbackend.model.Match; -import hdm.mi.sthbackend.repository.IMatchRepository; -import hdm.mi.sthbackend.repository.IPlayerRepository; -import hdm.mi.sthbackend.repository.ITeamRepository; -import hdm.mi.sthbackend.repository.ITournamentRepository; -import hdm.mi.sthbackend.service.TournamentService; -import hdm.mi.sthbackend.types.TeamMatchScore; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.mockito.InjectMocks; -import org.mockito.Mock; -import org.mockito.MockitoAnnotations; -import org.mockito.junit.MockitoJUnitRunner; -import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.ResultActions; -import org.springframework.beans.factory.annotation.Autowired; -import java.util.HashMap; -import java.util.Optional; -import java.util.UUID; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; - - -import static org.hamcrest.Matchers.is; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.Mockito.*; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; -import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; - -@AutoConfigureMockMvc -@RunWith(MockitoJUnitRunner.class) -@SpringBootTest -public class t1 { - - @Mock - private IMatchRepository matchRepository; - - @Mock - private ITeamRepository teamRepository; - - @Mock - private IPlayerRepository playerRepository; - - @Mock - private ITournamentRepository tournamentRepository; - - @Mock - private ModelToDTOMapper mapper; - - @InjectMocks - private TournamentController tournamentController; - - @Autowired - private MockMvc mockMvc; - - @Test - public void assignTeamToMatch() throws Exception { - UUID matchId = UUID.randomUUID(); - - MatchDTO testMatch = new MatchDTO(); - testMatch.setTeamScores(new HashMap<>()); - testMatch.setComment("Sample comment"); - - // Erstelle ein gültiges Match-Objekt für die Mock-Antwort - Match mockMatch = new Match(matchId, new HashMap<>(), UUID.randomUUID(), "Some comment", UUID.randomUUID()); - - when(matchRepository.findById(matchId)).thenReturn(Optional.of(mockMatch)); - when(mapper.mapToMatchDTO(any())).thenReturn(testMatch); - - - verify(matchRepository, times(1)).findById(matchId); - verify(matchRepository, times(1)).save(any()); - verify(mapper, times(1)).mapToMatchDTO(any()); - } - } +//package hdm.mi.sthbackend; +// +//import hdm.mi.sthbackend.controller.TournamentController; +//import hdm.mi.sthbackend.dto.MatchDTO; +//import hdm.mi.sthbackend.exeptions.MatchIdNotFoundException; +//import hdm.mi.sthbackend.mapper.ModelToDTOMapper; +//import hdm.mi.sthbackend.model.Match; +//import hdm.mi.sthbackend.repository.IMatchRepository; +//import hdm.mi.sthbackend.repository.IPlayerRepository; +//import hdm.mi.sthbackend.repository.ITeamRepository; +//import hdm.mi.sthbackend.repository.ITournamentRepository; +//import hdm.mi.sthbackend.service.TournamentService; +//import hdm.mi.sthbackend.types.TeamMatchScore; +//import org.junit.Before; +//import org.junit.Test; +//import org.junit.runner.RunWith; +//import org.mockito.InjectMocks; +//import org.mockito.Mock; +//import org.mockito.MockitoAnnotations; +//import org.mockito.junit.MockitoJUnitRunner; +//import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +//import org.springframework.boot.test.context.SpringBootTest; +//import org.springframework.http.MediaType; +//import org.springframework.test.web.servlet.MockMvc; +//import org.springframework.test.web.servlet.ResultActions; +//import org.springframework.beans.factory.annotation.Autowired; +//import java.util.HashMap; +//import java.util.Optional; +//import java.util.UUID; +//import org.springframework.beans.factory.annotation.Autowired; +//import org.springframework.test.web.servlet.MockMvc; +//import org.springframework.test.web.servlet.setup.MockMvcBuilders; +// +// +//import static org.hamcrest.Matchers.is; +//import static org.mockito.ArgumentMatchers.any; +//import static org.mockito.Mockito.*; +//import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch; +//import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +//import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +// +//@AutoConfigureMockMvc +//@RunWith(MockitoJUnitRunner.class) +//@SpringBootTest +//public class t1 { +// +// @Mock +// private IMatchRepository matchRepository; +// +// @Mock +// private ITeamRepository teamRepository; +// +// @Mock +// private IPlayerRepository playerRepository; +// +// @Mock +// private ITournamentRepository tournamentRepository; +// +// @Mock +// private ModelToDTOMapper mapper; +// +// @InjectMocks +// private TournamentController tournamentController; +// +// @Autowired +// private MockMvc mockMvc; +// +// @Test +// public void assignTeamToMatch() throws Exception { +// UUID matchId = UUID.randomUUID(); +// +// MatchDTO testMatch = new MatchDTO(); +// testMatch.setTeamScores(new HashMap<>()); +// testMatch.setComment("Sample comment"); +// +// // Erstelle ein gültiges Match-Objekt für die Mock-Antwort +// Match mockMatch = new Match(matchId, new HashMap<>(), UUID.randomUUID(), "Some comment", UUID.randomUUID()); +// +// when(matchRepository.findById(matchId)).thenReturn(Optional.of(mockMatch)); +// when(mapper.mapToMatchDTO(any())).thenReturn(testMatch); +// +// +// verify(matchRepository, times(1)).findById(matchId); +// verify(matchRepository, times(1)).save(any()); +// verify(mapper, times(1)).mapToMatchDTO(any()); +// } +// }