diff options
Diffstat (limited to 'sonar-scanner-engine')
12 files changed, 139 insertions, 97 deletions
diff --git a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/issues/IssuesMediumIT.java b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/issues/IssuesMediumIT.java index 541a690a63c..2ae6379db66 100644 --- a/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/issues/IssuesMediumIT.java +++ b/sonar-scanner-engine/src/it/java/org/sonar/scanner/mediumtest/issues/IssuesMediumIT.java @@ -142,7 +142,7 @@ public class IssuesMediumIT { .execute(); List<Issue> issues = result.issuesFor(result.inputFile("xources/hello/HelloJava.xoo")); - assertThat(issues.get(0).getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); + assertThat(issues.get(0).getOverriddenSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); } @Test diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java index ddb665c7965..d070a464cb4 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/DefaultFilterableIssue.java @@ -34,12 +34,14 @@ import org.sonar.scanner.protocol.output.ScannerReport.Issue; @ThreadSafe public class DefaultFilterableIssue implements FilterableIssue { private final Issue rawIssue; + private final String severity; private final InputComponent component; private DefaultInputProject project; - public DefaultFilterableIssue(DefaultInputProject project, Issue rawIssue, InputComponent component) { + public DefaultFilterableIssue(DefaultInputProject project, Issue rawIssue, String severity, InputComponent component) { this.project = project; this.rawIssue = rawIssue; + this.severity = severity; this.component = component; } @@ -55,7 +57,7 @@ public class DefaultFilterableIssue implements FilterableIssue { @Override public String severity() { - return rawIssue.getSeverity().name(); + return severity; } @Override diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java new file mode 100644 index 00000000000..76111b56055 --- /dev/null +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/ImpactMapper.java @@ -0,0 +1,38 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.scanner.issue; + +import org.sonar.api.issue.impact.Severity; +import org.sonar.scanner.protocol.output.ScannerReport; + +public class ImpactMapper { + private ImpactMapper() { + } + + public static ScannerReport.ImpactSeverity mapImpactSeverity(Severity severity) { + return switch (severity) { + case BLOCKER -> ScannerReport.ImpactSeverity.ImpactSeverity_BLOCKER; + case HIGH -> ScannerReport.ImpactSeverity.ImpactSeverity_HIGH; + case MEDIUM -> ScannerReport.ImpactSeverity.ImpactSeverity_MEDIUM; + case LOW -> ScannerReport.ImpactSeverity.ImpactSeverity_LOW; + case INFO -> ScannerReport.ImpactSeverity.ImpactSeverity_INFO; + }; + } +} diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java index cce20df21d8..948e9552370 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssueFilters.java @@ -41,11 +41,11 @@ public class IssueFilters { this.project = project; } - public boolean accept(InputComponent component, ScannerReport.Issue rawIssue) { + public boolean accept(InputComponent component, ScannerReport.Issue rawIssue, String severity) { if (filterChain == null) { throw new IllegalStateException("Issue filters must be registered before this class can be used"); } - FilterableIssue fIssue = new DefaultFilterableIssue(project, rawIssue, component); + FilterableIssue fIssue = new DefaultFilterableIssue(project, rawIssue, severity, component); return filterChain.accept(fIssue); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java index 6de30db7829..da79ae46bd0 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/issue/IssuePublisher.java @@ -33,7 +33,6 @@ import org.sonar.api.batch.fs.internal.DefaultInputComponent; import org.sonar.api.batch.fs.internal.DefaultInputFile; import org.sonar.api.batch.rule.ActiveRule; import org.sonar.api.batch.rule.ActiveRules; -import org.sonar.api.batch.rule.internal.DefaultActiveRule; import org.sonar.api.batch.sensor.issue.ExternalIssue; import org.sonar.api.batch.sensor.issue.Issue; import org.sonar.api.batch.sensor.issue.Issue.Flow; @@ -78,9 +77,9 @@ public class IssuePublisher { return false; } - ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId(), activeRule.severity(), ((DefaultActiveRule) activeRule).impacts()); + ScannerReport.Issue rawIssue = createReportIssue(issue, inputComponent.scannerId()); - if (filters.accept(inputComponent, rawIssue)) { + if (filters.accept(inputComponent, rawIssue, activeRule.severity())) { write(inputComponent.scannerId(), rawIssue); return true; } @@ -108,29 +107,28 @@ public class IssuePublisher { return str; } - private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef, String activeRuleSeverity, - Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> activeRuleImpacts) { + private static ScannerReport.Issue createReportIssue(Issue issue, int componentRef) { String primaryMessage = nullToEmpty(issue.primaryLocation().message()); - org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); - Severity severity = overriddenSeverity != null ? Severity.valueOf(overriddenSeverity.name()) : Severity.valueOf(activeRuleSeverity); ScannerReport.Issue.Builder builder = ScannerReport.Issue.newBuilder(); ScannerReport.IssueLocation.Builder locationBuilder = IssueLocation.newBuilder(); ScannerReport.TextRange.Builder textRangeBuilder = ScannerReport.TextRange.newBuilder(); // non-null fields - builder.setSeverity(severity); builder.setRuleRepository(issue.ruleKey().repository()); builder.setRuleKey(issue.ruleKey().rule()); builder.setMsg(primaryMessage); builder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overriddenImpacts = new EnumMap<>(issue.overridenImpacts()); - activeRuleImpacts.entrySet().forEach(e -> overriddenImpacts.putIfAbsent(e.getKey(), e.getValue())); - builder.addAllOverridenImpacts(toProtobufImpacts(overriddenImpacts)); + builder.addAllOverriddenImpacts(toProtobufImpacts(overriddenImpacts)); locationBuilder.setMsg(primaryMessage); locationBuilder.addAllMsgFormatting(toProtobufMessageFormattings(issue.primaryLocation().messageFormattings())); locationBuilder.setComponentRef(componentRef); + org.sonar.api.batch.rule.Severity overriddenSeverity = issue.overriddenSeverity(); + if (overriddenSeverity != null) { + builder.setOverriddenSeverity(Severity.valueOf(overriddenSeverity.name())); + } TextRange primaryTextRange = issue.primaryLocation().textRange(); if (primaryTextRange != null) { builder.setTextRange(toProtobufTextRange(textRangeBuilder, primaryTextRange)); @@ -151,7 +149,9 @@ public class IssuePublisher { private static List<ScannerReport.Impact> toProtobufImpacts(Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> softwareQualitySeverityMap) { List<ScannerReport.Impact> impacts = new ArrayList<>(); - softwareQualitySeverityMap.forEach((q, s) -> impacts.add(ScannerReport.Impact.newBuilder().setSoftwareQuality(q.name()).setSeverity(s.name()).build())); + softwareQualitySeverityMap.forEach((q, s) -> impacts.add(ScannerReport.Impact.newBuilder() + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(q.name())) + .setSeverity(ImpactMapper.mapImpactSeverity(s)).build())); return impacts; } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java index 63672225c9e..dcad7da095b 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/report/ActiveRulesPublisher.java @@ -21,6 +21,7 @@ package org.sonar.scanner.report; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.DefaultActiveRule; +import org.sonar.scanner.issue.ImpactMapper; import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.scanner.protocol.output.ScannerReportWriter; @@ -46,7 +47,12 @@ public class ActiveRulesPublisher implements ReportPublisherStep { builder.setCreatedAt(input.createdAt()); builder.setUpdatedAt(input.updatedAt()); builder.setQProfileKey(input.qpKey()); - builder.getMutableParamsByKey().putAll(input.params()); + builder.putAllParamsByKey(input.params()); + builder.addAllImpacts(input.impacts().entrySet().stream() + .map(entry -> ScannerReport.Impact.newBuilder() + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(entry.getKey().name())) + .setSeverity(ImpactMapper.mapImpactSeverity(entry.getValue())).build()) + .toList()); return builder.build(); }).toList()); } diff --git a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java index e0930fc5372..9ee364cf916 100644 --- a/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java +++ b/sonar-scanner-engine/src/main/java/org/sonar/scanner/sensor/DefaultSensorStorage.java @@ -68,6 +68,7 @@ import org.sonar.core.util.CloseableIterator; import org.sonar.duplications.block.Block; import org.sonar.duplications.internal.pmd.PmdBlockChunker; import org.sonar.scanner.cpd.index.SonarCpdBlockIndex; +import org.sonar.scanner.issue.ImpactMapper; import org.sonar.scanner.issue.IssuePublisher; import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.FileStructure; @@ -263,7 +264,6 @@ public class DefaultSensorStorage implements SensorStorage { builder.setDescription(description); } - org.sonar.api.batch.rule.Severity severity = adHocRule.severity(); if (severity != null) { builder.setSeverity(Constants.Severity.valueOf(severity.name())); @@ -285,8 +285,8 @@ public class DefaultSensorStorage implements SensorStorage { private static List<ScannerReport.Impact> mapImpacts(Map<SoftwareQuality, Severity> impactsMap) { return impactsMap.entrySet().stream() .map(e -> ScannerReport.Impact.newBuilder() - .setSoftwareQuality(e.getKey().name()) - .setSeverity(e.getValue().name()).build()) + .setSoftwareQuality(ScannerReport.SoftwareQuality.valueOf(e.getKey().name())) + .setSeverity(ImpactMapper.mapImpactSeverity(e.getValue())).build()) .toList(); } @@ -379,8 +379,7 @@ public class DefaultSensorStorage implements SensorStorage { private SortedMap<Integer, ScannerReport.LineCoverage.Builder> reloadExistingCoverage(DefaultInputFile inputFile) { SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine = new TreeMap<>(); - try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = - reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) { + try (CloseableIterator<ScannerReport.LineCoverage> lineCoverageCloseableIterator = reportPublisher.getReader().readComponentCoverage(inputFile.scannerId())) { while (lineCoverageCloseableIterator.hasNext()) { final ScannerReport.LineCoverage lineCoverage = lineCoverageCloseableIterator.next(); coveragePerLine.put(lineCoverage.getLine(), ScannerReport.LineCoverage.newBuilder(lineCoverage)); @@ -393,8 +392,8 @@ public class DefaultSensorStorage implements SensorStorage { void apply(Integer value, ScannerReport.LineCoverage.Builder builder); } - private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, - ScannerReport.LineCoverage.Builder> coveragePerLine, LineCoverageOperation op) { + private static void mergeLineCoverageValues(int lineCount, SortedMap<Integer, Integer> valueByLine, SortedMap<Integer, ScannerReport.LineCoverage.Builder> coveragePerLine, + LineCoverageOperation op) { for (Map.Entry<Integer, Integer> lineMeasure : valueByLine.entrySet()) { int lineIdx = lineMeasure.getKey(); if (lineIdx <= lineCount) { diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java index 1f8c28a2768..6145aec1c11 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/DefaultFilterableIssueTest.java @@ -53,20 +53,18 @@ public class DefaultFilterableIssueTest { .setStartOffset(10) .setEndLine(31) .setEndOffset(3)); - builder.setSeverity(Severity.MAJOR); return builder.build(); } private Issue createIssueWithoutFields() { Issue.Builder builder = Issue.newBuilder(); - builder.setSeverity(Severity.MAJOR); return builder.build(); } @Test public void testRoundTrip() { rawIssue = createIssue(); - issue = new DefaultFilterableIssue(mockedProject, rawIssue, component); + issue = new DefaultFilterableIssue(mockedProject, rawIssue, "MAJOR", component); when(mockedProject.key()).thenReturn("projectKey"); @@ -84,7 +82,7 @@ public class DefaultFilterableIssueTest { @Test public void nullValues() { rawIssue = createIssueWithoutFields(); - issue = new DefaultFilterableIssue(mockedProject, rawIssue, component); + issue = new DefaultFilterableIssue(mockedProject, rawIssue, "MAJOR", component); assertThat(issue.line()).isNull(); assertThat(issue.gap()).isNull(); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ImpactMapperTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ImpactMapperTest.java new file mode 100644 index 00000000000..f543b023c75 --- /dev/null +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/ImpactMapperTest.java @@ -0,0 +1,39 @@ +/* + * SonarQube + * Copyright (C) 2009-2024 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.scanner.issue; + +import org.junit.jupiter.api.Test; +import org.sonar.api.issue.impact.Severity; +import org.sonar.scanner.protocol.output.ScannerReport; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ImpactMapperTest { + + @Test + void mapImpactSeverity_shouldReturnExpectedValue() { + assertEquals(ScannerReport.ImpactSeverity.ImpactSeverity_BLOCKER, ImpactMapper.mapImpactSeverity(Severity.BLOCKER)); + assertEquals(ScannerReport.ImpactSeverity.ImpactSeverity_HIGH, ImpactMapper.mapImpactSeverity(Severity.HIGH)); + assertEquals(ScannerReport.ImpactSeverity.ImpactSeverity_MEDIUM, ImpactMapper.mapImpactSeverity(Severity.MEDIUM)); + assertEquals(ScannerReport.ImpactSeverity.ImpactSeverity_LOW, ImpactMapper.mapImpactSeverity(Severity.LOW)); + assertEquals(ScannerReport.ImpactSeverity.ImpactSeverity_INFO, ImpactMapper.mapImpactSeverity(Severity.INFO)); + } + +} diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java index aa59812f63c..0132e1d7ecc 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/issue/IssuePublisherTest.java @@ -53,6 +53,7 @@ import org.sonar.scanner.report.ReportPublisher; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.RETURNS_DEEP_STUBS; import static org.mockito.Mockito.mock; @@ -62,7 +63,6 @@ import static org.mockito.Mockito.when; import static org.sonar.api.batch.sensor.issue.MessageFormatting.Type.CODE; import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY; import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY; -import static org.sonar.api.issue.impact.SoftwareQuality.SECURITY; @RunWith(MockitoJUnitRunner.class) public class IssuePublisherTest { @@ -136,21 +136,23 @@ public class IssuePublisherTest { .overrideImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH) .overrideImpact(RELIABILITY, org.sonar.api.issue.impact.Severity.LOW); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); + when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class), anyString())).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); assertThat(added).isTrue(); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); - assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); + assertThat(argument.getValue().getOverriddenSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.CRITICAL); assertThat(argument.getValue().getQuickFixAvailable()).isTrue(); assertThat(argument.getValue().getRuleDescriptionContextKey()).isEqualTo(ruleDescriptionContextKey); assertThat(argument.getValue().getCodeVariantsList()).containsExactly("variant1", "variant2"); - ScannerReport.Impact impact1 = ScannerReport.Impact.newBuilder().setSoftwareQuality(MAINTAINABILITY.name()).setSeverity("HIGH").build(); - ScannerReport.Impact impact2 = ScannerReport.Impact.newBuilder().setSoftwareQuality(RELIABILITY.name()).setSeverity("LOW").build(); - assertThat(argument.getValue().getOverridenImpactsList()).containsExactlyInAnyOrder(impact1, impact2); + ScannerReport.Impact impact1 = ScannerReport.Impact.newBuilder().setSoftwareQuality(ScannerReport.SoftwareQuality.MAINTAINABILITY) + .setSeverity(ScannerReport.ImpactSeverity.ImpactSeverity_HIGH).build(); + ScannerReport.Impact impact2 = ScannerReport.Impact.newBuilder().setSoftwareQuality(ScannerReport.SoftwareQuality.RELIABILITY) + .setSeverity(ScannerReport.ImpactSeverity.ImpactSeverity_LOW).build(); + assertThat(argument.getValue().getOverriddenImpactsList()).containsExactlyInAnyOrder(impact1, impact2); } @Test @@ -169,7 +171,7 @@ public class IssuePublisherTest { .addFlow(List.of(new DefaultIssueLocation().on(file)), NewIssue.FlowType.EXECUTION, null) .forRule(JAVA_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); + when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class), anyString())).thenReturn(true); moduleIssues.initAndAddIssue(issue); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); @@ -227,75 +229,24 @@ public class IssuePublisherTest { ArgumentCaptor<ScannerReport.ExternalIssue> argument = ArgumentCaptor.forClass(ScannerReport.ExternalIssue.class); verify(reportPublisher.getWriter()).appendComponentExternalIssue(eq(file.scannerId()), argument.capture()); assertThat(argument.getValue().getImpactsList()).extracting(ScannerReport.Impact::getSoftwareQuality, ScannerReport.Impact::getSeverity) - .containsExactly(tuple(MAINTAINABILITY.name(), org.sonar.api.issue.impact.Severity.LOW.name())); + .containsExactly(tuple(ScannerReport.SoftwareQuality.MAINTAINABILITY, ScannerReport.ImpactSeverity.ImpactSeverity_LOW)); assertThat(argument.getValue().getCleanCodeAttribute()).isEqualTo(CleanCodeAttribute.CLEAR.name()); } @Test - public void use_severity_from_active_rule_if_no_severity_on_issue() { + public void dont_store_severity_if_no_severity_override_on_issue() { initModuleIssues(); DefaultIssue issue = new DefaultIssue(project) .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) .forRule(JAVA_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); - moduleIssues.initAndAddIssue(issue); - - ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); - verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); - assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.INFO); - assertThat(argument.getValue().getOverridenImpactsList()).isEmpty(); - } - - @Test - public void initAndAddIssue_whenImpactsOverriddenOnActiveRule_shouldOverrideIssue() { - activeRulesBuilder.addRule(new NewActiveRule.Builder() - .setRuleKey(NOSONAR_RULE_KEY) - .setSeverity(Severity.INFO) - .setImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH) - .setQProfileKey("qp-1") - .build()); - initModuleIssues(); - - DefaultIssue issue = new DefaultIssue(project) - .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) - .forRule(NOSONAR_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); - moduleIssues.initAndAddIssue(issue); - - ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); - verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); - assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.INFO); - assertThat(argument.getValue().getOverridenImpactsList()).extracting(ScannerReport.Impact::getSoftwareQuality, ScannerReport.Impact::getSeverity) - .containsExactly(tuple(MAINTAINABILITY.name(), org.sonar.api.issue.impact.Severity.HIGH.name())); - } - - @Test - public void initAndAddIssue_whenImpactsOverriddenOnActiveRuleAndInIssue_shouldCombineOverriddenImpacts() { - activeRulesBuilder.addRule(new NewActiveRule.Builder() - .setRuleKey(NOSONAR_RULE_KEY) - .setSeverity(Severity.INFO) - .setImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.HIGH) - .setImpact(SECURITY, org.sonar.api.issue.impact.Severity.INFO) - .setQProfileKey("qp-1") - .build()); - initModuleIssues(); - - DefaultIssue issue = new DefaultIssue(project) - .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("Foo")) - .overrideImpact(RELIABILITY, org.sonar.api.issue.impact.Severity.MEDIUM) - .overrideImpact(MAINTAINABILITY, org.sonar.api.issue.impact.Severity.LOW) - .forRule(NOSONAR_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); + when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class), eq("INFO"))).thenReturn(true); moduleIssues.initAndAddIssue(issue); ArgumentCaptor<ScannerReport.Issue> argument = ArgumentCaptor.forClass(ScannerReport.Issue.class); verify(reportPublisher.getWriter()).appendComponentIssue(eq(file.scannerId()), argument.capture()); - assertThat(argument.getValue().getSeverity()).isEqualTo(org.sonar.scanner.protocol.Constants.Severity.INFO); - assertThat(argument.getValue().getOverridenImpactsList()).extracting(ScannerReport.Impact::getSoftwareQuality, ScannerReport.Impact::getSeverity) - .containsExactlyInAnyOrder(tuple(MAINTAINABILITY.name(), org.sonar.api.issue.impact.Severity.LOW.name()), - tuple(RELIABILITY.name(), org.sonar.api.issue.impact.Severity.MEDIUM.name()), - tuple(SECURITY.name(), org.sonar.api.issue.impact.Severity.INFO.name())); + assertThat(argument.getValue().hasOverriddenSeverity()).isFalse(); + assertThat(argument.getValue().getOverriddenImpactsList()).isEmpty(); } @Test @@ -304,7 +255,7 @@ public class IssuePublisherTest { .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(JAVA_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(false); + when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class), anyString())).thenReturn(false); boolean added = moduleIssues.initAndAddIssue(issue); @@ -344,7 +295,7 @@ public class IssuePublisherTest { .at(new DefaultIssueLocation().on(file).at(file.selectLine(3)).message("")) .forRule(NOSONAR_RULE_KEY); - when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class))).thenReturn(true); + when(filters.accept(any(InputComponent.class), any(ScannerReport.Issue.class), anyString())).thenReturn(true); boolean added = moduleIssues.initAndAddIssue(issue); diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ActiveRulesPublisherTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ActiveRulesPublisherTest.java index 772040e1759..8c4db588240 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ActiveRulesPublisherTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/report/ActiveRulesPublisherTest.java @@ -26,6 +26,8 @@ import org.junit.rules.TemporaryFolder; import org.sonar.api.batch.rule.ActiveRules; import org.sonar.api.batch.rule.internal.DefaultActiveRules; import org.sonar.api.batch.rule.internal.NewActiveRule; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; import org.sonar.core.util.CloseableIterator; import org.sonar.scanner.protocol.Constants; @@ -36,6 +38,7 @@ import org.sonar.scanner.protocol.output.ScannerReportWriter; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.groups.Tuple.tuple; public class ActiveRulesPublisherTest { @@ -55,6 +58,8 @@ public class ActiveRulesPublisherTest { .setCreatedAt(1_000L) .setUpdatedAt(2_000L) .setQProfileKey("qp1") + .setImpact(SoftwareQuality.MAINTAINABILITY, Severity.BLOCKER) + .setImpact(SoftwareQuality.RELIABILITY, Severity.LOW) .build(); ActiveRules activeRules = new DefaultActiveRules(singletonList(ar)); @@ -73,6 +78,10 @@ public class ActiveRulesPublisherTest { assertThat(reportAr.getParamsByKeyMap()).hasSize(1); assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getKey()).isEqualTo("p1"); assertThat(reportAr.getParamsByKeyMap().entrySet().iterator().next().getValue()).isEqualTo("v1"); + assertThat(reportAr.getImpactsList()).extracting(ScannerReport.Impact::getSoftwareQuality, ScannerReport.Impact::getSeverity) + .containsExactlyInAnyOrder( + tuple(ScannerReport.SoftwareQuality.MAINTAINABILITY, ScannerReport.ImpactSeverity.ImpactSeverity_BLOCKER), + tuple(ScannerReport.SoftwareQuality.RELIABILITY, ScannerReport.ImpactSeverity.ImpactSeverity_LOW)); assertThat(readIt.hasNext()).isFalse(); } diff --git a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java index a0d785bd163..5a6f8f66a1f 100644 --- a/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java +++ b/sonar-scanner-engine/src/test/java/org/sonar/scanner/sensor/DefaultSensorStorageTest.java @@ -151,8 +151,8 @@ class DefaultSensorStorageTest { .forMetric(CoreMetrics.LINES) .withValue(10); assertThatThrownBy(() -> underTest.store(defaultMeasure)) - .isInstanceOf(UnsupportedOperationException.class) - .hasMessage("Unknown metric: lines"); + .isInstanceOf(UnsupportedOperationException.class) + .hasMessage("Unknown metric: lines"); } @Test @@ -392,8 +392,8 @@ class DefaultSensorStorageTest { .containsExactlyInAnyOrder("ruleId", "name", Constants.Severity.MAJOR, ScannerReport.IssueType.CODE_SMELL, "description"); assertThat(adhocRule.getDefaultImpactsList()).hasSize(2).extracting(ScannerReport.Impact::getSoftwareQuality, ScannerReport.Impact::getSeverity) .containsExactlyInAnyOrder( - Tuple.tuple(SoftwareQuality.MAINTAINABILITY.name(), Severity.HIGH.name()), - Tuple.tuple(SoftwareQuality.RELIABILITY.name(), Severity.MEDIUM.name())); + Tuple.tuple(ScannerReport.SoftwareQuality.MAINTAINABILITY, ScannerReport.ImpactSeverity.ImpactSeverity_HIGH), + Tuple.tuple(ScannerReport.SoftwareQuality.RELIABILITY, ScannerReport.ImpactSeverity.ImpactSeverity_MEDIUM)); assertThat(adhocRule.getCleanCodeAttribute()) .isEqualTo(CleanCodeAttribute.CLEAR.name()); } |