diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2023-11-07 16:31:27 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-11-08 20:02:53 +0000 |
commit | 50eddd3eb0ef9c677183698a9aa565d2ae9289ab (patch) | |
tree | 50b87f183752ca5d09a3af0f6e754bc65a069ef7 /sonar-core/src | |
parent | de26dc721fe8a465f054c2f7366838f414531606 (diff) | |
download | sonarqube-50eddd3eb0ef9c677183698a9aa565d2ae9289ab.tar.gz sonarqube-50eddd3eb0ef9c677183698a9aa565d2ae9289ab.zip |
SONAR-20877 Support null values for status
Diffstat (limited to 'sonar-core/src')
4 files changed, 18 insertions, 13 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java index f919ade4c22..3f00898de4b 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/DefaultIssue.java @@ -346,7 +346,6 @@ public class DefaultIssue implements Issue, Trackable, org.sonar.api.ce.measure. @Nullable public IssueStatus getIssueStatus() { - Preconditions.checkArgument(!Strings.isNullOrEmpty(status), "Status must be set"); return IssueStatus.of(status, resolution); } diff --git a/sonar-core/src/main/java/org/sonar/core/issue/status/IssueStatus.java b/sonar-core/src/main/java/org/sonar/core/issue/status/IssueStatus.java index fc615cd266b..8967460de51 100644 --- a/sonar-core/src/main/java/org/sonar/core/issue/status/IssueStatus.java +++ b/sonar-core/src/main/java/org/sonar/core/issue/status/IssueStatus.java @@ -42,7 +42,14 @@ public enum IssueStatus { private static final Logger LOGGER = LoggerFactory.getLogger(IssueStatus.class); @CheckForNull - public static IssueStatus of(String status, @Nullable String resolution) { + public static IssueStatus of(@Nullable String status, @Nullable String resolution) { + + //null status is not supposed to happen, but since it is nullable in database, we want the mapping to be resilient. + if (status == null) { + LOGGER.warn("Missing status, falling back to {}", IssueStatus.OPEN); + return IssueStatus.OPEN; + } + switch (status) { case Issue.STATUS_OPEN: case Issue.STATUS_REOPENED: @@ -68,6 +75,8 @@ public enum IssueStatus { default: } } + + LOGGER.warn("Can't find mapped issue status for status '{}' and resolution '{}'", status, resolution); return null; } diff --git a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java index baae9a88dd6..76e016639b9 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/DefaultIssueTest.java @@ -313,15 +313,6 @@ public class DefaultIssueTest { } @Test - public void getIssueStatus_shouldThrowException_whenStatusNotSet() { - issue.setResolution(Issue.RESOLUTION_FIXED); - - assertThatThrownBy(issue::getIssueStatus) - .hasMessage("Status must be set") - .isInstanceOf(IllegalArgumentException.class); - } - - @Test public void replaceImpacts_shouldReplaceExistingImpacts() { issue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); issue.addImpact(SoftwareQuality.RELIABILITY, Severity.LOW); diff --git a/sonar-core/src/test/java/org/sonar/core/issue/status/IssueStatusTest.java b/sonar-core/src/test/java/org/sonar/core/issue/status/IssueStatusTest.java index c3c8eef6513..606babaae9a 100644 --- a/sonar-core/src/test/java/org/sonar/core/issue/status/IssueStatusTest.java +++ b/sonar-core/src/test/java/org/sonar/core/issue/status/IssueStatusTest.java @@ -54,7 +54,7 @@ public class IssueStatusTest { } @Test - public void of_shouldReturnNull_WhenStatusBelongsToHotspot() { + public void of_shouldReturnNull_whenStatusBelongsToHotspot() { assertThat(IssueStatus.of(Issue.STATUS_TO_REVIEW, null)) .isNull(); @@ -66,7 +66,7 @@ public class IssueStatusTest { } @Test - public void of_shouldThrowExceptionWhenUnknownMapping() { + public void of_shouldLogWarning_whenUnknownMapping() { assertThat(IssueStatus.of(Issue.STATUS_RESOLVED, null)).isNull(); assertThat(logTester.getLogs()).extracting(LogAndArguments::getFormattedMsg).contains("Can't find mapped issue status for status 'RESOLVED' and resolution 'null'"); @@ -74,4 +74,10 @@ public class IssueStatusTest { assertThat(logTester.getLogs()).extracting(LogAndArguments::getFormattedMsg).contains("Can't find mapped issue status for status 'RESOLVED' and resolution 'SAFE'"); } + @Test + public void of_shouldLogWarning_whenStatusIsNull() { + assertThat(IssueStatus.of(null, null)).isEqualTo(IssueStatus.OPEN); + assertThat(logTester.getLogs()).extracting(LogAndArguments::getFormattedMsg).contains("Missing status, falling back to OPEN"); + } + } |