From 15d518496c28f5d782b0f9db53b61ff346b18542 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Wed, 25 Oct 2023 17:56:20 +0200 Subject: [PATCH] SONAR-20877 Add new status mapping --- .../java/org/sonar/db/issue/IssueDto.java | 8 ++- .../java/org/sonar/db/issue/IssueDtoTest.java | 25 ++++++++ .../sonar/core/issue/status/SimpleStatus.java | 56 +++++++++++++++++ .../core/issue/status/SimpleStatusTest.java | 62 +++++++++++++++++++ 4 files changed, 150 insertions(+), 1 deletion(-) create mode 100644 sonar-core/src/main/java/org/sonar/core/issue/status/SimpleStatus.java create mode 100644 sonar-core/src/test/java/org/sonar/core/issue/status/SimpleStatusTest.java diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java index 00b1342dbe3..99d68dc57d3 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/issue/IssueDto.java @@ -44,6 +44,7 @@ import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.utils.Duration; import org.sonar.core.issue.DefaultIssue; +import org.sonar.core.issue.status.SimpleStatus; import org.sonar.core.util.Uuids; import org.sonar.db.component.ComponentDto; import org.sonar.db.protobuf.DbIssues; @@ -362,6 +363,11 @@ public final class IssueDto implements Serializable { return status; } + public SimpleStatus getSimpleStatus() { + checkArgument(status != null, "Status must be initialized to retrieve simple status"); + return SimpleStatus.of(status, resolution); + } + public IssueDto setStatus(@Nullable String s) { checkArgument(s == null || s.length() <= 20, "Value is too long for issue status: %s", s); this.status = s; @@ -828,7 +834,7 @@ public final class IssueDto implements Serializable { this.ruleDefaultImpacts = new HashSet<>(ruleDefaultImpacts); return this; } - + public IssueDto replaceAllImpacts(Collection newImpacts) { Set newSoftwareQuality = newImpacts.stream().map(ImpactDto::getSoftwareQuality).collect(Collectors.toSet()); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java index d7e64ea110f..abfcd39f3e5 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDtoTest.java @@ -27,6 +27,7 @@ import java.util.Date; import java.util.List; import java.util.Set; import org.apache.commons.lang.time.DateUtils; +import org.assertj.core.api.Assertions; import org.junit.Test; import org.sonar.api.issue.Issue; import org.sonar.api.issue.impact.Severity; @@ -36,6 +37,7 @@ import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.RuleType; import org.sonar.api.utils.Duration; import org.sonar.core.issue.DefaultIssue; +import org.sonar.core.issue.status.SimpleStatus; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.protobuf.DbIssues; import org.sonar.db.rule.RuleDto; @@ -342,6 +344,29 @@ public class IssueDtoTest { .containsExactlyInAnyOrder(tuple(MAINTAINABILITY, HIGH), tuple(RELIABILITY, LOW)); } + @Test + public void getSimpleStatus_shouldReturnExpectedValueFromStatusAndResolution(){ + IssueDto dto = new IssueDto(); + dto.setStatus(Issue.STATUS_CLOSED); + assertThat(dto.getSimpleStatus()).isEqualTo(SimpleStatus.FIXED); + + dto.setStatus(Issue.STATUS_RESOLVED); + dto.setResolution(Issue.RESOLUTION_FALSE_POSITIVE); + assertThat(dto.getSimpleStatus()).isEqualTo(SimpleStatus.FALSE_POSITIVE); + + dto.setStatus(Issue.STATUS_RESOLVED); + dto.setResolution(Issue.RESOLUTION_WONT_FIX); + assertThat(dto.getSimpleStatus()).isEqualTo(SimpleStatus.ACCEPTED); + } + + @Test + public void getSimpleStatus_shouldThrowException_whenStatusIsNotInitialized(){ + IssueDto dto = new IssueDto(); + assertThatThrownBy(dto::getSimpleStatus) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Status must be initialized to retrieve simple status"); + } + private DefaultIssue createExampleDefaultIssue(Date dateNow) { DefaultIssue defaultIssue = new DefaultIssue(); defaultIssue.setKey("key") diff --git a/sonar-core/src/main/java/org/sonar/core/issue/status/SimpleStatus.java b/sonar-core/src/main/java/org/sonar/core/issue/status/SimpleStatus.java new file mode 100644 index 00000000000..e910d4d8161 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/issue/status/SimpleStatus.java @@ -0,0 +1,56 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.issue.status; + +import javax.annotation.Nullable; +import org.sonar.api.issue.Issue; + +public enum SimpleStatus { + OPEN, + CONFIRMED, + FALSE_POSITIVE, + ACCEPTED, + FIXED; + + public static SimpleStatus of(String status, @Nullable String resolution) { + switch (status) { + case Issue.STATUS_OPEN: + case Issue.STATUS_REOPENED: + return SimpleStatus.OPEN; + case Issue.STATUS_CONFIRMED: + return SimpleStatus.CONFIRMED; + case Issue.STATUS_CLOSED: + return SimpleStatus.FIXED; + default: + } + if (resolution != null) { + switch (resolution) { + case Issue.RESOLUTION_FALSE_POSITIVE: + return SimpleStatus.FALSE_POSITIVE; + case Issue.RESOLUTION_WONT_FIX: + return SimpleStatus.ACCEPTED; + case Issue.RESOLUTION_FIXED: + return SimpleStatus.FIXED; + default: + } + } + throw new IllegalArgumentException(String.format("Can't find mapped simple status for status '%s' and resolution '%s'", status, resolution)); + } +} diff --git a/sonar-core/src/test/java/org/sonar/core/issue/status/SimpleStatusTest.java b/sonar-core/src/test/java/org/sonar/core/issue/status/SimpleStatusTest.java new file mode 100644 index 00000000000..b8cd9fd70de --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/issue/status/SimpleStatusTest.java @@ -0,0 +1,62 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.core.issue.status; + +import org.junit.Test; +import org.sonar.api.issue.Issue; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +public class SimpleStatusTest { + + @Test + public void of_shouldMapToCorrectSimpleStatus() { + assertThat(SimpleStatus.of(Issue.STATUS_RESOLVED, Issue.RESOLUTION_FIXED)) + .isEqualTo(SimpleStatus.FIXED); + + assertThat(SimpleStatus.of(Issue.STATUS_CONFIRMED, null)) + .isEqualTo(SimpleStatus.CONFIRMED); + + assertThat(SimpleStatus.of(Issue.STATUS_RESOLVED, Issue.RESOLUTION_FALSE_POSITIVE)) + .isEqualTo(SimpleStatus.FALSE_POSITIVE); + + assertThat(SimpleStatus.of(Issue.STATUS_RESOLVED, Issue.RESOLUTION_WONT_FIX)) + .isEqualTo(SimpleStatus.ACCEPTED); + + assertThat(SimpleStatus.of(Issue.STATUS_REOPENED, null)) + .isEqualTo(SimpleStatus.OPEN); + + assertThat(SimpleStatus.of(Issue.STATUS_CLOSED, null)) + .isEqualTo(SimpleStatus.FIXED); + } + + @Test + public void of_shouldThrowExceptionWhenUnknownMapping() { + assertThatThrownBy(() -> SimpleStatus.of(Issue.STATUS_RESOLVED, null)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Can't find mapped simple status for status 'RESOLVED' and resolution 'null'"); + + assertThatThrownBy(() -> SimpleStatus.of(Issue.STATUS_RESOLVED, Issue.RESOLUTION_SAFE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Can't find mapped simple status for status 'RESOLVED' and resolution 'SAFE'"); + } + +} -- 2.39.5