diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2020-02-07 11:16:05 +0100 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2020-02-11 20:46:12 +0100 |
commit | 3ebf6d2303098f010cff38df15a4ae5fb7a737f4 (patch) | |
tree | baaf7110a0b20fadda8ad23a76c592e14ed3c5a9 /server/sonar-server-common | |
parent | c51fa2468cfe46d85b86d8121de84cd0a06fed89 (diff) | |
download | sonarqube-3ebf6d2303098f010cff38df15a4ae5fb7a737f4.tar.gz sonarqube-3ebf6d2303098f010cff38df15a4ae5fb7a737f4.zip |
SONAR-12960 Make Security Review Rating more intuitive on Portfolios
Diffstat (limited to 'server/sonar-server-common')
2 files changed, 27 insertions, 41 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java b/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java index f96839250fc..d2c038ccc97 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/security/SecurityReviewRating.java @@ -33,28 +33,6 @@ public class SecurityReviewRating { // Only static method } - /** - * This code will be removed when updating computation of Security Review Rating for portfolios - */ - @Deprecated - public static Rating computeForPortfolios(int ncloc, int securityHotspots) { - if (ncloc == 0) { - return A; - } - double ratio = (double) securityHotspots * 1000d / (double) ncloc; - if (ratio <= 3d) { - return A; - } else if (ratio <= 10) { - return B; - } else if (ratio <= 15) { - return C; - } else if (ratio <= 25) { - return D; - } else { - return E; - } - } - public static double computePercent(long hotspotsToReview, long hotspotsReviewed) { long total = hotspotsToReview + hotspotsReviewed; if (total == 0) { @@ -63,7 +41,7 @@ public class SecurityReviewRating { return hotspotsReviewed * 100.0 / total; } - public static Rating computeRating(Double percent) { + public static Rating computeRating(double percent) { if (percent >= 80.0) { return A; } else if (percent >= 70.0) { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java index cdb276aba16..7c4e5d44821 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/security/SecurityReviewRatingTest.java @@ -24,6 +24,7 @@ import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.ArrayList; import java.util.List; +import org.assertj.core.data.Offset; import org.junit.Test; import org.junit.runner.RunWith; import org.sonar.server.measure.Rating; @@ -34,35 +35,42 @@ import static org.sonar.server.measure.Rating.B; import static org.sonar.server.measure.Rating.C; import static org.sonar.server.measure.Rating.D; import static org.sonar.server.measure.Rating.E; +import static org.sonar.server.security.SecurityReviewRating.computePercent; +import static org.sonar.server.security.SecurityReviewRating.computeRating; @RunWith(DataProviderRunner.class) public class SecurityReviewRatingTest { + private static final Offset<Double> DOUBLE_OFFSET = Offset.offset(0.01d); + @DataProvider public static Object[][] values() { List<Object[]> res = new ArrayList<>(); - res.add(new Object[] {1000, 0, A}); - res.add(new Object[] {1000, 3, A}); - res.add(new Object[] {1000, 4, B}); - res.add(new Object[] {1000, 10, B}); - res.add(new Object[] {1000, 11, C}); - res.add(new Object[] {1000, 15, C}); - res.add(new Object[] {1000, 16, D}); - res.add(new Object[] {1000, 25, D}); - res.add(new Object[] {1000, 26, E}); - res.add(new Object[] {1000, 900, E}); - - res.add(new Object[] {0, 2, A}); - res.add(new Object[] {1001, 3, A}); - res.add(new Object[] {999, 3, B}); - res.add(new Object[] {Integer.MAX_VALUE, Integer.MAX_VALUE, E}); - return res.toArray(new Object[res.size()][3]); + res.add(new Object[] {100.0, A}); + res.add(new Object[] {90.0, A}); + res.add(new Object[] {80.0, A}); + res.add(new Object[] {75.0, B}); + res.add(new Object[] {70.0, B}); + res.add(new Object[] {60, C}); + res.add(new Object[] {50.0, C}); + res.add(new Object[] {40.0, D}); + res.add(new Object[] {30.0, D}); + res.add(new Object[] {29.9, E}); + return res.toArray(new Object[res.size()][2]); } @Test @UseDataProvider("values") - public void compute_security_review_rating_on_project(int ncloc, int securityHotspots, Rating expectedRating) { - assertThat(SecurityReviewRating.computeForPortfolios(ncloc, securityHotspots)).isEqualTo(expectedRating); + public void compute_rating(double percent, Rating expectedRating) { + assertThat(computeRating(percent)).isEqualTo(expectedRating); } + @Test + public void compute_percent() { + assertThat(computePercent(0, 10)).isEqualTo(100.0); + assertThat(computePercent(1, 3)).isEqualTo(75.0); + assertThat(computePercent(3, 4)).isEqualTo(57.14, DOUBLE_OFFSET); + assertThat(computePercent(10, 10)).isEqualTo(50.0); + assertThat(computePercent(10, 0)).isEqualTo(0.0); + } } |