Skip to content

Commit

Permalink
Added a test on the ScheduledService and adjusted Team Entity
Browse files Browse the repository at this point in the history
  • Loading branch information
Felle33 committed Jan 18, 2024
1 parent 75bd897 commit de4db68
Show file tree
Hide file tree
Showing 14 changed files with 232 additions and 41 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
package ckb.BattleManager.controller;

import ckb.BattleManager.dto.output.BattleFinishedMessage;
import ckb.BattleManager.model.Battle;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

import java.util.List;

@RestController
@Slf4j
public class SendTeamsPointsFinishedBattleController {
private final WebClient.Builder webClientBuilder;

public SendTeamsPointsFinishedBattleController() {
this.webClientBuilder = WebClient.builder();
}

public void sendTeamsPointsFinishedBattle(Long idTournament, List<Pair<Long, Long>> pairsIdUserPoints) {
webClientBuilder.build()
public void sendIdUsersPointsFinishedBattle(Battle battle, List<Pair<Long, Integer>> pairsIdUserPoints) {
ResponseEntity<Object> response = webClientBuilder.build()
.post()
.uri("http://tournament-service:port/api/send-teams-points-finished-battle")
.body(
.uri("http://tournament-manager:8084/api/" +
"tournament/UpdateScore")
.bodyValue(
new BattleFinishedMessage(
idTournament,
battle.getTournamentId(),
pairsIdUserPoints
),
BattleFinishedMessage.class
);
)
)
.retrieve()
.toEntity(Object.class)
.block();

if (response == null) {
log.error("Error sending idUsers and points of the finished battle with id: {}. The response is null", battle.getBattleId());
return;
}

if (!response.getStatusCode().is2xxSuccessful()) {
log.error("Error sending idUsers and points of the finished battle with id: {}. Error {}", battle.getBattleId(), response.getStatusCode());
return;
}

log.info("Successfully sent IdUsers and points of the finished battle with id: {}", battle.getBattleId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import ckb.BattleManager.model.Battle;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.reactive.function.client.WebClient;

Expand All @@ -15,13 +16,24 @@ public StartBattleController() {
}

public void startBattle(Battle battleToStart) {
webClientBuilder.build()
ResponseEntity<Object> response = webClientBuilder.build()
.post()
.uri("http://github-service:port/api/start-battle")
.body(battleToStart, Battle.class);
//.retrieve()
//.bodyToMono(String.class)
//.block();
.uri("http://github-manager:8083/api/github/create-repo")
.bodyValue(battleToStart)
.retrieve()
.toEntity(Object.class)
.block();

if (response == null) {
log.error("Error starting battle with id: {}. The response is null", battleToStart.getBattleId());
return;
}

if (!response.getStatusCode().is2xxSuccessful()) {
log.error("Error starting battle with id: {}. Error {}", battleToStart.getBattleId(), response.getStatusCode());
return;
}

log.info("Battle started with id: {}", battleToStart.getBattleId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
@AllArgsConstructor
public class BattleFinishedMessage {
private Long idTournament;
private List<Pair<Long, Long>> pairsIdUserPoints;
private List<Pair<Long, Integer>> pairsIdUserPoints;
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;
import java.util.Collection;

@Entity(name = "Battle")
@Table(name = "Battles")
Expand All @@ -22,6 +23,9 @@ public class Battle {
@Column(unique = true)
private String repositoryLink;

@OneToMany(mappedBy = "battle")
private Collection<Team> teamsRegistered;

private int minStudents;

private int maxStudents;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ckb.BattleManager.model;

import jakarta.persistence.Column;
import jakarta.persistence.EmbeddedId;
import jakarta.persistence.Entity;
import jakarta.persistence.Table;
Expand All @@ -16,6 +15,6 @@
public class Participation {

@EmbeddedId
@Column(nullable = false, updatable = false)
private ParticipationId participationId;

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ckb.BattleManager.model;

import jakarta.persistence.Embeddable;
import jakarta.persistence.JoinColumn;
import jakarta.persistence.ManyToOne;
import lombok.AllArgsConstructor;
import lombok.Data;
Expand All @@ -17,6 +16,5 @@ public class ParticipationId implements Serializable {
private Long studentId;

@ManyToOne
@JoinColumn(name = "teamId")
private Team team;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import lombok.Data;
import lombok.NoArgsConstructor;

import java.util.Collection;

@Entity(name = "Team")
@Table(name = "Teams")
@Data
Expand All @@ -16,9 +18,11 @@ public class Team {
private Long teamId;

@ManyToOne
@JoinColumn(name = "battleId")
private Battle battle;

@OneToMany(mappedBy = "participationId.team")
private Collection<Participation> participation;

private String repositoryLink;

private Integer score;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import ckb.BattleManager.model.Battle;
import ckb.BattleManager.model.Team;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
Expand All @@ -28,6 +27,6 @@ Optional<Team> findTeamByBattleAndParticipationId_TeamId(@Param("battle") Battle
@Query("select p.participationId.studentId, t.score " +
"from Team t join Participation p on t.teamId = p.participationId.team.teamId " +
"where t.battle = :battle")
List<Pair<Long, Long>> findPairsIdUserPointsByBattleId(@Param("battle") Battle battle);
List<Object[]> findPairsIdUserPointsByBattleId(@Param("battle") Battle battle);

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ public BattleScheduledService(BattleRepository battleRepository, TeamService tea
this.sendTeamsPointsFinishedBattleController = sendTeamsPointsFinishedBattleController;
}

@Scheduled(fixedRate = 60000) // 1 Minute
@Scheduled(fixedRate = 3000) // 1 Minute
public void startBattles() {
List<Battle> battlesToStart = battleRepository.findBattlesByHasStartedIsFalse();
//log.info("Found {} battles to start", battlesToStart.size());
battlesToStart.forEach(battle -> {
if (battle.getRegDeadline().isBefore(LocalDateTime.now())) {
battle.setHasStarted(true);
Expand All @@ -42,7 +43,7 @@ public void startBattles() {
});
}

@Scheduled(fixedRate = 60000) // 1 Minute
@Scheduled(fixedRate = 3000) // 1 Minute
public void closeBattles() {
List<Battle> battlesToStart = battleRepository.
findBattlesByHasEndedIsFalseAndSubDeadlineBefore(LocalDateTime.now());
Expand All @@ -53,8 +54,8 @@ public void closeBattles() {
// Get the teams and the points of each battle
// send the a class containing tournament_id, List<Team> and List<Integer>
// to the Tournament manager
sendTeamsPointsFinishedBattleController.sendTeamsPointsFinishedBattle(
battle.getTournamentId(),
sendTeamsPointsFinishedBattleController.sendIdUsersPointsFinishedBattle(
battle,
teamService.getListPairIdUserPoints(battle)
);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,13 @@ public Optional<Battle> findBattleById(Long id) {
}

public void joinBattle(Long idStudent, Long idBattle) throws Exception {
teamService.createTeam(idStudent, idBattle);
Optional<Battle> optionalBattle = battleRepository.findById(idBattle);
if (optionalBattle.isPresent()) {
teamService.createTeam(idStudent, optionalBattle.get());
} else {
log.info("Battle not found with id: {}", idBattle);
throw new Exception();
}
}

public void leaveBattle(Long idStudent, Long idBattle) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,14 @@ public List<Team> getListTeam(Long idBattle) throws Exception {
);
}

public void createTeam(Long studentId, Long battleId) throws Exception {
Team team = new Team(studentId,
battleService.findBattleById(battleId).orElseThrow(() -> {
log.info("Battle not found with id: {}", battleId);
return new Exception("");
}),
"" ,
0,
false
);
public void createTeam(Long studentId, Battle battle) {
Team team = new Team();
team.setBattle(battle);
team.setParticipation(null);
// TODO: how to set the repository link?
team.setRepositoryLink("");
team.setEduEvaluated(false);
team.setScore(0);

teamRepository.save(team);
participationService.createParticipation(studentId, team);
Expand Down Expand Up @@ -121,7 +119,10 @@ public void registerStudentToTeam(Long idStudent, Long idNewTeam) throws Excepti
log.info("Student {} registered to team {}", idStudent, idNewTeam);
}

public List<Pair<Long, Long>> getListPairIdUserPoints(Battle battle) {
return teamRepository.findPairsIdUserPointsByBattleId(battle);
public List<Pair<Long, Integer>> getListPairIdUserPoints(Battle battle) {
List<Object[]> listArrayObjects = teamRepository.findPairsIdUserPointsByBattleId(battle);
return listArrayObjects.stream()
.map(arrayObject -> Pair.of((Long) arrayObject[0], (Integer) arrayObject[1]))
.toList();
}
}
Loading

0 comments on commit de4db68

Please sign in to comment.