From 476f72f1820d112fe2b8a2317ae66d570046618d Mon Sep 17 00:00:00 2001 From: Anita Stanisz <106669481+anita-stanisz-sonarsource@users.noreply.github.com> Date: Thu, 3 Oct 2024 09:50:00 +0200 Subject: [PATCH] SGB-126 Migrate the test to Junit 5 (#11904) --- .../notification/NewIssuesStatisticsTest.java | 50 ++++++++++--------- 1 file changed, 27 insertions(+), 23 deletions(-) diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewIssuesStatisticsTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewIssuesStatisticsTest.java index f0ea29d45d4..48c45249eed 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewIssuesStatisticsTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewIssuesStatisticsTest.java @@ -28,7 +28,7 @@ import java.util.Random; import java.util.stream.IntStream; import java.util.stream.Stream; import javax.annotation.CheckForNull; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.sonar.api.issue.Issue; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; @@ -40,14 +40,14 @@ import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic; import static org.apache.commons.lang3.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; -public class NewIssuesStatisticsTest { +class NewIssuesStatisticsTest { private final Random random = new Random(); private RuleType randomRuleTypeExceptHotspot = RuleType.values()[random.nextInt(RuleType.values().length - 1)]; private NewIssuesStatistics underTest = new NewIssuesStatistics(Issue::isNew); @Test - public void add_issues_with_correct_global_statistics() { + void add_issues_with_correct_global_statistics() { DefaultIssue issue = new DefaultIssue() .setAssigneeUuid("maynard") .setComponentUuid("file-uuid") @@ -77,7 +77,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issues_on_current_analysis_globally_and_per_assignee() { + void add_counts_issues_on_current_analysis_globally_and_per_assignee() { String assignee = randomAlphanumeric(10); IntStream.range(0, 10) .mapToObj(i -> new DefaultIssue().setAssigneeUuid(assignee).setNew(true)) @@ -92,7 +92,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issues_off_current_analysis_globally_and_per_assignee() { + void add_counts_issues_off_current_analysis_globally_and_per_assignee() { String assignee = randomAlphanumeric(10); IntStream.range(0, 10) .mapToObj(i -> new DefaultIssue().setAssigneeUuid(assignee).setNew(false)) @@ -107,7 +107,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_component_on_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_component_on_current_analysis_globally_and_per_assignee() { List componentUuids = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); componentUuids.stream() @@ -115,13 +115,14 @@ public class NewIssuesStatisticsTest { .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); - DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); + DistributedMetricStatsInt assigneeDistribution = + underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> componentUuids.forEach(componentUuid -> assertStats(distribution, componentUuid, 1, 1))); } @Test - public void add_counts_issue_per_component_off_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_component_off_current_analysis_globally_and_per_assignee() { List componentUuids = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); componentUuids.stream() @@ -136,12 +137,13 @@ public class NewIssuesStatisticsTest { } @Test - public void add_does_not_count_component_if_null_neither_globally_nor_per_assignee() { + void add_does_not_count_component_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setComponentUuid(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.COMPONENT); - DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); + DistributedMetricStatsInt assigneeDistribution = + underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.COMPONENT); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isZero(); @@ -150,7 +152,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_ruleKey_on_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_ruleKey_on_current_analysis_globally_and_per_assignee() { String repository = randomAlphanumeric(3); List ruleKeys = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); @@ -166,7 +168,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_ruleKey_off_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_ruleKey_off_current_analysis_globally_and_per_assignee() { String repository = randomAlphanumeric(3); List ruleKeys = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); @@ -175,18 +177,20 @@ public class NewIssuesStatisticsTest { .forEach(underTest::add); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE); - DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); + DistributedMetricStatsInt assigneeDistribution = + underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> ruleKeys.forEach(ruleKey -> assertStats(distribution, RuleKey.of(repository, ruleKey).toString(), 0, 1))); } @Test - public void add_does_not_count_ruleKey_if_null_neither_globally_nor_per_assignee() { + void add_does_not_count_ruleKey_if_null_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setRuleKey(null).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.RULE); - DistributedMetricStatsInt assigneeDistribution = underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); + DistributedMetricStatsInt assigneeDistribution = + underTest.getAssigneesStatistics().get(assignee).getDistributedMetricStats(Metric.RULE); Stream.of(globalDistribution, assigneeDistribution) .forEach(distribution -> { assertThat(distribution.getTotal()).isZero(); @@ -195,7 +199,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_assignee_on_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_assignee_on_current_analysis_globally_and_per_assignee() { List assignees = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); assignees.stream() .map(assignee -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(assignee).setNew(true)) @@ -223,7 +227,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_assignee_off_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_assignee_off_current_analysis_globally_and_per_assignee() { List assignees = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); assignees.stream() .map(assignee -> new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(assignee).setNew(false)) @@ -251,7 +255,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_does_not_assignee_if_empty_neither_globally_nor_per_assignee() { + void add_does_not_assignee_if_empty_neither_globally_nor_per_assignee() { underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setAssigneeUuid(null).setNew(new Random().nextBoolean())); DistributedMetricStatsInt globalDistribution = underTest.globalStatistics().getDistributedMetricStats(Metric.ASSIGNEE); @@ -261,7 +265,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_tags_on_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_tags_on_current_analysis_globally_and_per_assignee() { List tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(true)); @@ -273,7 +277,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_counts_issue_per_tags_off_current_analysis_globally_and_per_assignee() { + void add_counts_issue_per_tags_off_current_analysis_globally_and_per_assignee() { List tags = IntStream.range(0, 1 + new Random().nextInt(10)).mapToObj(i -> randomAlphabetic(3)).toList(); String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(tags).setAssigneeUuid(assignee).setNew(false)); @@ -285,7 +289,7 @@ public class NewIssuesStatisticsTest { } @Test - public void add_does_not_count_tags_if_empty_neither_globally_nor_per_assignee() { + void add_does_not_count_tags_if_empty_neither_globally_nor_per_assignee() { String assignee = randomAlphanumeric(10); underTest.add(new DefaultIssue().setType(randomRuleTypeExceptHotspot).setTags(Collections.emptyList()).setAssigneeUuid(assignee).setNew(new Random().nextBoolean())); @@ -299,12 +303,12 @@ public class NewIssuesStatisticsTest { } @Test - public void do_not_have_issues_when_no_issue_added() { + void do_not_have_issues_when_no_issue_added() { assertThat(underTest.globalStatistics().hasIssues()).isFalse(); } @Test - public void verify_toString() { + void verify_toString() { String componentUuid = randomAlphanumeric(2); String tag = randomAlphanumeric(3); String assignee = randomAlphanumeric(4); -- 2.39.5