From 059f8b2d71c09821f33faeb2db23668a8dc2e1bf Mon Sep 17 00:00:00 2001 From: Sven Strittmatter Date: Tue, 22 Nov 2022 12:42:41 +0100 Subject: [PATCH] #151 First draft implementation for team score claulation in backend Signed-off-by: Sven Strittmatter --- .../custom/CustomTeamScoreService.java | 20 ----- .../service/dto/custom/TeamScoreDTO.java | 23 +++++- .../service/impl/custom/CompletionCheck.java | 33 --------- .../custom/CustomTeamScoreServiceImpl.java | 73 +++++-------------- .../service/impl/custom/Progress.java | 23 ------ .../CustomTeamScoreServiceImplTest.java | 51 +++++++++++++ 6 files changed, 91 insertions(+), 132 deletions(-) delete mode 100644 src/main/java/com/iteratec/teamdojo/service/impl/custom/CompletionCheck.java delete mode 100644 src/main/java/com/iteratec/teamdojo/service/impl/custom/Progress.java create mode 100644 src/test/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImplTest.java diff --git a/src/main/java/com/iteratec/teamdojo/service/custom/CustomTeamScoreService.java b/src/main/java/com/iteratec/teamdojo/service/custom/CustomTeamScoreService.java index e85a0c59..c5f1d0ae 100644 --- a/src/main/java/com/iteratec/teamdojo/service/custom/CustomTeamScoreService.java +++ b/src/main/java/com/iteratec/teamdojo/service/custom/CustomTeamScoreService.java @@ -7,7 +7,6 @@ import com.iteratec.teamdojo.service.dto.*; import com.iteratec.teamdojo.service.dto.custom.TeamScoreDTO; import java.util.List; -import lombok.NonNull; /** * This service calculates the team-score for given teams @@ -20,23 +19,4 @@ public interface CustomTeamScoreService { * @return never {@code null} */ TeamScoreDTO calculateTeamScore(TeamDTO t); - - /** - * Calculates the score for given team - * - * @param t must not be {@code null} - * @param teamSkills skills already achieved by the team - * @param skills all available skills - * @param badges all available badges - * @return never {@code null} - */ - TeamScoreDTO calculateTeamScore( - TeamDTO t, - List teamSkills, - List skills, - List badges, - List badgeSkills, - List levels, - List levelSkills - ); } diff --git a/src/main/java/com/iteratec/teamdojo/service/dto/custom/TeamScoreDTO.java b/src/main/java/com/iteratec/teamdojo/service/dto/custom/TeamScoreDTO.java index 522d5a74..f4a8c5df 100644 --- a/src/main/java/com/iteratec/teamdojo/service/dto/custom/TeamScoreDTO.java +++ b/src/main/java/com/iteratec/teamdojo/service/dto/custom/TeamScoreDTO.java @@ -17,6 +17,25 @@ public class TeamScoreDTO { public static final TeamScoreDTO NULL = new TeamScoreDTO(); - private TeamDTO team; - private Long score; + // TODO Define API based on UI requirements. Seems that we need the properties from Progress and irrelevancy. + + /** + * TODO: Please refine. + * Number of achieved skills? + * Not negative? + */ + private int achieved; + /** + * TODO: Please refine. + * How many skills are required? + * Not negative? + */ + private int required; + /** + * TODO: Please refine. + * Not negative? + */ + private int totalScore; + private float progressInPercent; + private boolean completed; } diff --git a/src/main/java/com/iteratec/teamdojo/service/impl/custom/CompletionCheck.java b/src/main/java/com/iteratec/teamdojo/service/impl/custom/CompletionCheck.java deleted file mode 100644 index ea9a8149..00000000 --- a/src/main/java/com/iteratec/teamdojo/service/impl/custom/CompletionCheck.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.iteratec.teamdojo.service.impl.custom; - -import com.iteratec.teamdojo.service.dto.*; -import java.util.List; - -/** - * This is a mirror of the CompletionCheck class found in the frontend: - * src/main/webapp/app/custom/helper/completion-check.ts - */ -public final class CompletionCheck { - - private final TeamDTO team; - private final List skills; - - CompletionCheck(TeamDTO team, List skills) { - this.team = team; - this.skills = skills; - } - - public Progress getProgress(LevelDTO level, List levelSkills) { - return null; - } - - public Progress getProgress(BadgeDTO badge, List badgeSkills) { - return null; - } - - //public Long getIrrelevancy(List levelSkills) { return 0l; } - - public Long getIrrelevancy(List badgeSkills) { - return 0l; - } -} diff --git a/src/main/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImpl.java b/src/main/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImpl.java index 2dfd8afe..cc3536ce 100644 --- a/src/main/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImpl.java +++ b/src/main/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImpl.java @@ -5,11 +5,9 @@ package com.iteratec.teamdojo.service.impl.custom; import com.iteratec.teamdojo.service.custom.CustomTeamScoreService; -import com.iteratec.teamdojo.service.dto.*; +import com.iteratec.teamdojo.service.dto.TeamDTO; import com.iteratec.teamdojo.service.dto.custom.TeamScoreDTO; -import java.util.List; -import java.util.Map; -import java.util.stream.Collectors; +import lombok.NonNull; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; @@ -18,65 +16,32 @@ public class CustomTeamScoreServiceImpl implements CustomTeamScoreService { @Override - public TeamScoreDTO calculateTeamScore(TeamDTO t) { - return TeamScoreDTO.NULL; + public TeamScoreDTO calculateTeamScore(@NonNull final TeamDTO t) { + final var achieved = calculateAchieved(); + final var required = calculateRequired(); + final var totalScore = calculateTotalScore(); + final var progressInPercent = calculateProgressInPercent(); + final var completed = calculateCompleted(achieved, required); + return new TeamScoreDTO(achieved, required, totalScore, progressInPercent, completed); } - @Override - public TeamScoreDTO calculateTeamScore( - TeamDTO t, - List teamSkills, - List skills, - List badges, - List badgeSkills, - List levels, - List levelSkills - ) { - final var groupedBadgeSkills = this.groupBadgeSkillsByBadgeId(badgeSkills); - final var groupedLevelskills = this.groupLevelSkillsByLevelId(levelSkills); - - final var skillScore = this.calcSkillScore(t, teamSkills, skills); - final var levelBonus = this.calcLevelBonus(t, teamSkills, skills, groupedLevelskills); - final var badgeBonus = this.calcBadgeBonus(t, teamSkills, skills, badges, groupedBadgeSkills); - final var totalScore = skillScore + levelBonus + badgeBonus; - return new TeamScoreDTO(t, totalScore); - } - - private Long calcSkillScore(TeamDTO t, List teamSkills, List skills) { - var score = skills.stream().filter(skill -> this.isSkillCompleted(teamSkills, skill)).mapToLong(SkillDTO::getScore).sum(); - return score; - } - - private Long calcLevelBonus( - TeamDTO t, - List teamSkills, - List skills, - Map> groupedLevelSkills - ) { - var score = t.getParticipations(); - - return 0L; + int calculateTotalScore() { + return 0; } - private Long calcBadgeBonus( - TeamDTO t, - List teamSkills, - List skills, - List badges, - Map> groupedBadgeSkills - ) { - return 0L; + int calculateRequired() { + return 0; } - private boolean isSkillCompleted(List teamSkills, SkillDTO skill) { - return false; + int calculateAchieved() { + return 0; } - private Map> groupLevelSkillsByLevelId(List levelSkills) { - return levelSkills.stream().collect(Collectors.groupingBy(levelSkill -> levelSkill.getLevel().getId())); + float calculateProgressInPercent() { + return 0.0f; } - private Map> groupBadgeSkillsByBadgeId(List badgeSkills) { - return badgeSkills.stream().collect(Collectors.groupingBy(badgeSkill -> badgeSkill.getBadge().getId())); + boolean calculateCompleted(final int achieved, final int required) { + return achieved >= required; } } diff --git a/src/main/java/com/iteratec/teamdojo/service/impl/custom/Progress.java b/src/main/java/com/iteratec/teamdojo/service/impl/custom/Progress.java deleted file mode 100644 index 678d99f2..00000000 --- a/src/main/java/com/iteratec/teamdojo/service/impl/custom/Progress.java +++ /dev/null @@ -1,23 +0,0 @@ -package com.iteratec.teamdojo.service.impl.custom; - -import lombok.AllArgsConstructor; - -@AllArgsConstructor -public class Progress { - - private Integer achieved; - private Integer required; - private Integer totalScore; - - public Double getPercentage() { - if (this.totalScore == 0) { - return 0.0; - } - - return (this.achieved / this.totalScore) * 100.0; - } - - public boolean isCompleted() { - return this.achieved >= this.required; - } -} diff --git a/src/test/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImplTest.java b/src/test/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImplTest.java new file mode 100644 index 00000000..f182f1f3 --- /dev/null +++ b/src/test/java/com/iteratec/teamdojo/service/impl/custom/CustomTeamScoreServiceImplTest.java @@ -0,0 +1,51 @@ +package com.iteratec.teamdojo.service.impl.custom; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import org.junit.jupiter.api.Test; + +/** + * + */ +class CustomTeamScoreServiceImplTest { + + private final CustomTeamScoreServiceImpl sut = new CustomTeamScoreServiceImpl(); + + @Test + void calculateProgress() {} + + @Test + void calculateTotalScore() {} + + @Test + void calculateRequired() {} + + @Test + void calculateAchieved() {} + + @Test + void calculateProgressInPercent() {} + + @Test + void calculateCompleted() { + assertAll( + () -> assertThat(sut.calculateCompleted(-1, 0)).isFalse(), + () -> assertThat(sut.calculateCompleted(0, -1)).isTrue(), + () -> assertThat(sut.calculateCompleted(-1, -1)).isTrue(), + () -> assertThat(sut.calculateCompleted(0, 0)).isTrue(), + () -> assertThat(sut.calculateCompleted(1, 0)).isTrue(), + () -> assertThat(sut.calculateCompleted(2, 0)).isTrue(), + () -> assertThat(sut.calculateCompleted(3, 0)).isTrue(), + () -> assertThat(sut.calculateCompleted(3, 1)).isTrue(), + () -> assertThat(sut.calculateCompleted(3, 2)).isTrue(), + () -> assertThat(sut.calculateCompleted(3, 3)).isTrue(), + () -> assertThat(sut.calculateCompleted(3, 4)).isFalse(), + () -> assertThat(sut.calculateCompleted(3, 5)).isFalse(), + () -> assertThat(sut.calculateCompleted(Integer.MAX_VALUE, Integer.MAX_VALUE)).isTrue(), + () -> assertThat(sut.calculateCompleted(Integer.MAX_VALUE, Integer.MIN_VALUE)).isTrue(), + () -> assertThat(sut.calculateCompleted(Integer.MIN_VALUE, Integer.MAX_VALUE)).isFalse(), + () -> assertThat(sut.calculateCompleted(Integer.MIN_VALUE, Integer.MIN_VALUE)).isTrue() + ); + } +}