From 7fcc8394a3062fcc887d0fb36561f65fce84da87 Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Thu, 21 Nov 2024 14:12:35 +0100 Subject: [PATCH] SONAR-23531 Change impact representation in IssueChangedEvent to fit convention --- .../issues/IssueChangeEventServiceImplTest.java | 12 ++++++++---- .../main/java/org/sonar/core/util/issue/Issue.java | 14 +++++++++----- .../core/util/issue/IssueChangedEventTest.java | 6 +++--- 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/issues/IssueChangeEventServiceImplTest.java b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/issues/IssueChangeEventServiceImplTest.java index 6d28537363d..a45bdb1d7b9 100644 --- a/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/issues/IssueChangeEventServiceImplTest.java +++ b/server/sonar-webserver-pushapi/src/test/java/org/sonar/server/pushapi/issues/IssueChangeEventServiceImplTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.pushapi.issues; +import com.google.gson.Gson; import java.nio.charset.StandardCharsets; import java.util.Deque; import java.util.HashMap; @@ -34,6 +35,7 @@ import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rules.RuleType; import org.sonar.core.issue.DefaultIssue; import org.sonar.core.issue.FieldDiffs; +import org.sonar.core.util.issue.Issue; import org.sonar.db.DbTester; import org.sonar.db.component.BranchDto; import org.sonar.db.component.BranchType; @@ -63,6 +65,7 @@ import static org.sonarqube.ws.Common.Severity.MAJOR; public class IssueChangeEventServiceImplTest { + private static final Gson GSON = new Gson(); @Rule public DbTester db = DbTester.create(); @@ -203,14 +206,16 @@ public class IssueChangeEventServiceImplTest { .contains("\"userSeverity\":\"" + CRITICAL.name() + "\"", "\"userType\":\"" + CODE_SMELL.name() + "\"", "\"resolved\":" + false) - .contains("\"issues\":[{\"issueKey\":\"%s\",\"branchName\":\"main\",\"impacts\":{\"RELIABILITY\":\"MEDIUM\"}}]".formatted(issue1.getKee())); + .contains(""" + "issues":[{"issueKey":"%s","branchName":"main","impacts":[{"softwareQuality":"RELIABILITY","severity":"MEDIUM"}]}]""".formatted(issue1.getKee())); String secondPayload = new String(project2Event.get().getPayload(), StandardCharsets.UTF_8); assertThat(secondPayload) .contains("\"userSeverity\":\"" + CRITICAL.name() + "\"", "\"userType\":\"" + CODE_SMELL.name() + "\"", "\"resolved\":" + true) - .contains("\"issues\":[{\"issueKey\":\"%s\",\"branchName\":\"main\",\"impacts\":{\"MAINTAINABILITY\":\"LOW\"}}]".formatted(issue2.getKee())); + .contains(""" + "issues":[{"issueKey":"%s","branchName":"main","impacts":[{"softwareQuality":"MAINTAINABILITY","severity":"LOW"}]}]""".formatted(issue2.getKee())); } @@ -278,8 +283,7 @@ public class IssueChangeEventServiceImplTest { } for (Map.Entry impact : impacts.entrySet()) { - assertThat(payload).contains("\"impacts\":{\""); - assertThat(payload).contains(impact.getKey().name() + "\":\"" + impact.getValue().name() + "\"}"); + assertThat(payload).contains(GSON.toJson(new Issue.Impact(impact.getKey(), impact.getValue()))); } } diff --git a/sonar-core/src/main/java/org/sonar/core/util/issue/Issue.java b/sonar-core/src/main/java/org/sonar/core/util/issue/Issue.java index 73c44873ae3..68771066ac4 100644 --- a/sonar-core/src/main/java/org/sonar/core/util/issue/Issue.java +++ b/sonar-core/src/main/java/org/sonar/core/util/issue/Issue.java @@ -20,15 +20,15 @@ package org.sonar.core.util.issue; import java.io.Serializable; -import java.util.EnumMap; -import java.util.Map; +import java.util.ArrayList; +import java.util.List; import org.sonar.api.issue.impact.Severity; import org.sonar.api.issue.impact.SoftwareQuality; public class Issue implements Serializable { private final String issueKey; private final String branchName; - private final Map impacts = new EnumMap<>(SoftwareQuality.class); + private final List impacts = new ArrayList<>(); public Issue(String issueKey, String branchName) { this.issueKey = issueKey; @@ -44,10 +44,14 @@ public class Issue implements Serializable { } public void addImpact(SoftwareQuality quality, Severity severity) { - impacts.put(quality, severity); + impacts.add(new Impact(quality, severity)); } - public Map getImpacts() { + public List getImpacts() { return impacts; } + + public record Impact(SoftwareQuality softwareQuality, Severity severity) { + } + } diff --git a/sonar-core/src/test/java/org/sonar/core/util/issue/IssueChangedEventTest.java b/sonar-core/src/test/java/org/sonar/core/util/issue/IssueChangedEventTest.java index c8c3649c16e..87881193597 100644 --- a/sonar-core/src/test/java/org/sonar/core/util/issue/IssueChangedEventTest.java +++ b/sonar-core/src/test/java/org/sonar/core/util/issue/IssueChangedEventTest.java @@ -22,10 +22,10 @@ package org.sonar.core.util.issue; import java.util.Arrays; import org.junit.Test; import org.sonar.api.issue.impact.Severity; -import org.sonar.api.issue.impact.SoftwareQuality; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; +import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY; public class IssueChangedEventTest { private static final String BRANCH_NAME = "branch-name"; @@ -35,7 +35,7 @@ public class IssueChangedEventTest { @Test public void issueChangedEvent_instantiation_accepts_nulls() { Issue issue = new Issue(ISSUE_KEY, BRANCH_NAME); - issue.addImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); + issue.addImpact(MAINTAINABILITY, Severity.HIGH); IssueChangedEvent event = new IssueChangedEvent(PROJECT_KEY, new Issue[] {issue}, null, null, null); @@ -47,7 +47,7 @@ public class IssueChangedEventTest { assertThat(event.getIssues()).hasSize(1); assertThat(Arrays.stream(event.getIssues()).iterator().next().getImpacts()) .hasSize(1) - .containsEntry(SoftwareQuality.MAINTAINABILITY, Severity.HIGH); + .containsExactlyInAnyOrder(new Issue.Impact(MAINTAINABILITY, Severity.HIGH)); } @Test -- 2.39.5