1
0
Fork 0
mirror of https://codeberg.org/beerbrawl/beerbrawl.git synced 2024-09-23 05:40:51 +02:00

test(#29): backend: move qualification match tests to separate file

Signed-off-by: Christoph Heiss <e11907069@student.tuwien.ac.at>
This commit is contained in:
Christoph Heiss 2024-05-30 21:42:37 +02:00 committed by Moritz Kepplinger
parent 8acfe916da
commit 1ab497ab21
3 changed files with 251 additions and 191 deletions

View file

@ -1,9 +1,15 @@
package at.ac.tuwien.sepr.groupphase.backend.basetest;
import at.ac.tuwien.sepr.groupphase.backend.datagenerator.TestDataGenerator;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.dto.TournamentUpdateQualificationMatchDto;
import at.ac.tuwien.sepr.groupphase.backend.entity.Tournament;
import at.ac.tuwien.sepr.groupphase.backend.repository.QualificationMatchRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.QualificationParticipationRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.TeamRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.TournamentRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.UserRepository;
import at.ac.tuwien.sepr.groupphase.backend.service.TournamentQualificationService;
import at.ac.tuwien.sepr.groupphase.backend.service.TournamentService;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
@ -16,6 +22,9 @@ import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.IntStream;
import static org.junit.jupiter.api.Assertions.assertEquals;
@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ -27,7 +36,13 @@ public class TestData {
@Autowired
private TeamRepository teamRepository;
@Autowired
private QualificationMatchRepository qualificationMatchRepository;
@Autowired
private PasswordEncoder passwordEncoder;
@Autowired
private TournamentService tournamentService;
@Autowired
private TournamentQualificationService qualificationService;
protected String BASE_URI = "/api/v1";
protected String TOURNAMENT_BASE_URI = BASE_URI + "/tournaments";
@ -44,6 +59,60 @@ public class TestData {
return LocalDateTime.now(ZoneOffset.UTC);
}
protected Tournament generateTournamentWithQualificationMatches() {
var tournament = new Tournament(
"testname", currentUtcTime().plusDays(1), 64L, "testdescription",
userRepository.findByUsername(TEST_USER));
tournamentService.create(tournament, TEST_USER);
IntStream.rangeClosed(1, 32).forEach(i -> {
assertEquals(
Tournament.SignupTeamResult.SUCCESS,
tournamentService.signupTeamForTournament(
tournament.getId(), tournament.getSelfRegistrationToken(), "team" + i
)
);
});
qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER);
return tournament;
}
protected Tournament generateTournamentWithFinishedQualiPhase() {
var tournament = this.generateTournamentWithQualificationMatches();
// mark all teams ready
for (final var team : teamRepository.findAllByTournamentId(tournament.getId())) {
tournamentService.markTeamAsReady(tournament.getId(), team.getId());
}
// finish qualification matches, don't care about the results
final var qMatches = qualificationMatchRepository.findAllByTournamentId(tournament.getId());
for (var qm : qMatches) {
// mark all match participants as drinks collected
qm.getParticipations()
.stream().map(p -> p.getTeam().getId())
.forEach(p -> qualificationService.updateQualificationMatch(
tournament.getId(), qm.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(p)
)
));
qualificationService.updateQualificationMatch(
tournament.getId(), qm.getId(),
new TournamentUpdateQualificationMatchDto(
new TournamentUpdateQualificationMatchDto.ScoreUpdateDto(
qm.getParticipations().get(0).getTeam().getId(), 1L
),
null
)
);
}
return tournament;
}
@BeforeEach
public void generateTestUser() {
var dataGenerator = new TestDataGenerator(

View file

@ -0,0 +1,182 @@
package at.ac.tuwien.sepr.groupphase.backend.unittests;
import at.ac.tuwien.sepr.groupphase.backend.basetest.TestData;
import at.ac.tuwien.sepr.groupphase.backend.endpoint.dto.TournamentUpdateQualificationMatchDto;
import at.ac.tuwien.sepr.groupphase.backend.entity.Team;
import at.ac.tuwien.sepr.groupphase.backend.entity.Tournament;
import at.ac.tuwien.sepr.groupphase.backend.exception.NotFoundException;
import at.ac.tuwien.sepr.groupphase.backend.exception.PreconditionFailedException;
import at.ac.tuwien.sepr.groupphase.backend.repository.QualificationMatchRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.QualificationParticipationRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.TeamRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.TournamentRepository;
import at.ac.tuwien.sepr.groupphase.backend.repository.UserRepository;
import at.ac.tuwien.sepr.groupphase.backend.service.TournamentQualificationService;
import at.ac.tuwien.sepr.groupphase.backend.service.TournamentService;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.AccessDeniedException;
import java.util.HashMap;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TournamentQualificationServiceTest extends TestData {
@Autowired
UserRepository userRepository;
@Autowired
TournamentRepository tournamentRepository;
@Autowired
TeamRepository teamRepository;
@Autowired
QualificationMatchRepository qualificationMatchRepository;
@Autowired
QualificationParticipationRepository qualificationParticipationRepository;
@Autowired
TournamentQualificationService qualificationService;
@Autowired
TournamentService tournamentService;
@Test
public void generateQualificationMatchesForTournamentWithEnoughTeams() throws Exception {
var tournament = new Tournament("TOURNAMENT_WITH_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
var numberOfTeams = 16;
for (int i = 0; i < numberOfTeams; i++) {
var team = new Team("Team" + Integer.toString(i));
tournament.signupTeam(team);
teamRepository.save(team);
}
teamRepository.flush();
var matches = qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER);
assertEquals(matches.size(), numberOfTeams);
var qualificationParticipations = matches.stream()
.flatMap(m -> qualificationParticipationRepository.findAllByQualificationMatchId(m.getId()).stream())
.toList();
var numberOfMatchesForTeam = new HashMap<Long, Integer>();
for (var participation : qualificationParticipations) {
if (!numberOfMatchesForTeam.containsKey(participation.getTeam().getId())) {
numberOfMatchesForTeam.put(participation.getTeam().getId(), 0);
}
numberOfMatchesForTeam.put(participation.getTeam().getId(),
numberOfMatchesForTeam.get(participation.getTeam().getId()) + 1);
}
for (var matchesCount : numberOfMatchesForTeam.values()) {
assertEquals(matchesCount, 2);
}
}
@Test
public void generateQualificationMatchesForTournamentWithoutEnoughTeams() throws Exception {
// setup
var tournament = new Tournament("TOURNAMENT_WITHOUT_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
assertThrows(PreconditionFailedException.class,
() -> qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER));
}
@Test
public void generateQualificationMatchesForTournamentFromAnotherOrganizerWhenItIsntAllowed() throws Exception {
// setup
var tournament = new Tournament("TOURNAMENT_WITHOUT_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
assertThrows(AccessDeniedException.class,
() -> qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER + "_"));
}
@Test
public void generateQualificationMatchesForTournamentThatDoesntExist() throws Exception {
assertThrows(NotFoundException.class,
() -> qualificationService.generateQualificationMatchesForTournament(-1l, TEST_USER + "_"));
}
@Test
public void teamCannotCollectDrinksForQualificationMatchIsNotReadyYet() {
var tournament = this.generateTournamentWithQualificationMatches();
this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.forEach(m -> assertAll(
() -> assertNull(m.getStartTime()),
() -> assertNull(m.getWinner()),
() -> assertNull(m.getEndTime()),
() -> assertNull(m.getWinnerPoints()))
);
var match = this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.getFirst();
final var teamIds = match.getParticipations()
.stream().map(p -> p.getTeam().getId())
.toList();
assertThrows(
PreconditionFailedException.class,
() -> this.qualificationService.updateQualificationMatch(
tournament.getId(), match.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(teamIds.get(0))
)
),
"Team is not marked as ready yet"
);
}
@Test
public void qualificationMatchStartsWhenBothTeamsHaveDrinksCollected() {
var tournament = this.generateTournamentWithQualificationMatches();
this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.forEach(m -> assertAll(
() -> assertNull(m.getStartTime()),
() -> assertNull(m.getWinner()),
() -> assertNull(m.getEndTime()),
() -> assertNull(m.getWinnerPoints()))
);
var match = this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.getFirst();
final var teamIds = match.getParticipations()
.stream().map(p -> p.getTeam().getId())
.toList();
for (final var id : teamIds) {
tournamentService.markTeamAsReady(tournament.getId(), id);
}
for (final var id : teamIds) {
qualificationService.updateQualificationMatch(
tournament.getId(), match.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(id)
)
);
}
final var m = this.qualificationMatchRepository.findById(match.getId());
assertTrue(m.isPresent());
assertNotNull(m.get().getStartTime());
assertTrue(m.get().getStartTime().isBefore(currentUtcTime()));
}
}

View file

@ -73,74 +73,6 @@ public class TournamentServiceTest extends TestData {
userRepository.deleteAll();
}
@Test
public void generateQualificationMatchesForTournamentWithEnoughTeams() throws Exception {
var tournament = new Tournament("TOURNAMENT_WITH_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
var numberOfTeams = 16;
for (int i = 0; i < numberOfTeams; i++) {
var team = new Team("Team" + Integer.toString(i));
tournament.signupTeam(team);
teamRepository.save(team);
}
teamRepository.flush();
var matches = qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER);
assertEquals(matches.size(), numberOfTeams);
var qualificationParticipations = matches.stream()
.flatMap(m -> qualificationParticipationRepository.findAllByQualificationMatchId(m.getId()).stream())
.toList();
var numberOfMatchesForTeam = new HashMap<Long, Integer>();
for (var participation : qualificationParticipations) {
if (!numberOfMatchesForTeam.containsKey(participation.getTeam().getId())) {
numberOfMatchesForTeam.put(participation.getTeam().getId(), 0);
}
numberOfMatchesForTeam.put(participation.getTeam().getId(),
numberOfMatchesForTeam.get(participation.getTeam().getId()) + 1);
}
for (var matchesCount : numberOfMatchesForTeam.values()) {
assertEquals(matchesCount, 2);
}
}
@Test
public void generateQualificationMatchesForTournamentWithoutEnoughTeams() throws Exception {
// setup
var tournament = new Tournament("TOURNAMENT_WITHOUT_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
assertThrows(PreconditionFailedException.class,
() -> qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER));
}
@Test
public void generateQualificationMatchesForTournamentFromAnotherOrganizerWhenItIsntAllowed() throws Exception {
// setup
var tournament = new Tournament("TOURNAMENT_WITHOUT_TEAMS", currentUtcTime().plusDays(1), 64L,
"testdescription",
userRepository.findByUsername(TEST_USER));
tournamentRepository.saveAndFlush(tournament);
assertThrows(AccessDeniedException.class,
() -> qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER + "_"));
}
@Test
public void generateQualificationMatchesForTournamentThatDoesntExist() throws Exception {
assertThrows(NotFoundException.class,
() -> qualificationService.generateQualificationMatchesForTournament(-1l, TEST_USER + "_"));
}
@Test
public void signupTeam_givenDuplicateTeamName_fails() {
@ -168,129 +100,6 @@ public class TournamentServiceTest extends TestData {
assertEquals(nodeCount, koMatches.size());
}
@Test
public void teamCannotCollectDrinksForQualificationMatchIsNotReadyYet() {
var tournament = this.generateTournamentWithQualificationMatches();
this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.forEach(m -> assertAll(
() -> assertNull(m.getStartTime()),
() -> assertNull(m.getWinner()),
() -> assertNull(m.getEndTime()),
() -> assertNull(m.getWinnerPoints()))
);
var match = this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.getFirst();
final var teamIds = match.getParticipations()
.stream().map(p -> p.getTeam().getId())
.toList();
assertThrows(
PreconditionFailedException.class,
() -> this.qualificationService.updateQualificationMatch(
tournament.getId(), match.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(teamIds.get(0))
)
),
"Team is not marked as ready yet"
);
}
@Test
public void qualificationMatchStartsWhenBothTeamsHaveDrinksCollected() {
var tournament = this.generateTournamentWithQualificationMatches();
this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.forEach(m -> assertAll(
() -> assertNull(m.getStartTime()),
() -> assertNull(m.getWinner()),
() -> assertNull(m.getEndTime()),
() -> assertNull(m.getWinnerPoints()))
);
var match = this.qualificationMatchRepository.findAllByTournamentId(tournament.getId())
.getFirst();
final var teamIds = match.getParticipations()
.stream().map(p -> p.getTeam().getId())
.toList();
for (final var id : teamIds) {
tournamentService.markTeamAsReady(tournament.getId(), id);
}
for (final var id : teamIds) {
qualificationService.updateQualificationMatch(
tournament.getId(), match.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(id)
)
);
}
final var m = this.qualificationMatchRepository.findById(match.getId());
assertTrue(m.isPresent());
assertNotNull(m.get().getStartTime());
assertTrue(m.get().getStartTime().isBefore(currentUtcTime()));
}
private Tournament generateTournamentWithQualificationMatches() {
var tournament = new Tournament(
"testname", currentUtcTime().plusDays(1), 64L, "testdescription",
userRepository.findByUsername(TEST_USER));
tournamentService.create(tournament, TEST_USER);
IntStream.rangeClosed(1, 32).forEach(i -> {
assertEquals(
SignupTeamResult.SUCCESS,
tournamentService.signupTeamForTournament(
tournament.getId(), tournament.getSelfRegistrationToken(), "team" + i
)
);
});
qualificationService.generateQualificationMatchesForTournament(tournament.getId(), TEST_USER);
return tournament;
}
private Tournament generateTournamentWithFinishedQualiPhase() {
var tournament = this.generateTournamentWithQualificationMatches();
// mark all teams ready
for (final var team : teamRepository.findAllByTournamentId(tournament.getId())) {
tournamentService.markTeamAsReady(tournament.getId(), team.getId());
}
// finish qualification matches, don't care about the results
final var qMatches = qualificationMatchRepository.findAllByTournamentId(tournament.getId());
for (var qm : qMatches) {
// mark all match participants as drinks collected
qm.getParticipations()
.stream().map(p -> p.getTeam().getId())
.forEach(p -> qualificationService.updateQualificationMatch(
tournament.getId(), qm.getId(),
new TournamentUpdateQualificationMatchDto(
null, new TournamentUpdateQualificationMatchDto.DrinksPickupDto(p)
)
));
qualificationService.updateQualificationMatch(
tournament.getId(), qm.getId(),
new TournamentUpdateQualificationMatchDto(
new TournamentUpdateQualificationMatchDto.ScoreUpdateDto(
qm.getParticipations().get(0).getTeam().getId(), 1L
),
null
)
);
}
return tournament;
}
@Test
public void editTournament_ValidUpdate() {
var tournament = new Tournament(