From b4e39aba5fa94abf8406c59797ffc0a8a3b7891f Mon Sep 17 00:00:00 2001 From: Antoine Vinot Date: Tue, 21 Mar 2023 08:28:37 +0100 Subject: [PATCH] SONAR-18462 - Fix ALM PR decoration not using ad hoc type --- .../issue/RuleRepositoryImpl.java | 2 +- .../issue/RuleRepositoryImplTest.java | 34 +++++++++++++++++++ 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java index 73598cc54a8..a41f4c0a774 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java @@ -182,7 +182,7 @@ public class RuleRepositoryImpl implements RuleRepository { @Override @CheckForNull public RuleType getType() { - return null; + return addHocRule.getRuleType(); } @Override diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java index 18fd1ef0f53..59498fe155a 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java @@ -21,10 +21,14 @@ package org.sonar.ce.task.projectanalysis.issue; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; +import com.tngtech.java.junit.dataprovider.DataProvider; +import com.tngtech.java.junit.dataprovider.DataProviderRunner; +import com.tngtech.java.junit.dataprovider.UseDataProvider; import java.util.Optional; import org.assertj.core.api.ThrowableAssert.ThrowingCallable; import org.junit.Before; import org.junit.Test; +import org.junit.runner.RunWith; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rules.RuleType; @@ -36,6 +40,7 @@ import org.sonar.db.DbTester; import org.sonar.db.rule.DeprecatedRuleKeyDto; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleDto; +import org.sonar.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.server.rule.index.RuleIndexer; @@ -50,6 +55,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions; import static org.mockito.Mockito.when; import static org.mockito.internal.verification.VerificationModeFactory.times; +@RunWith(DataProviderRunner.class) public class RuleRepositoryImplTest { private static final RuleDto AB_RULE = createABRuleDto().setUuid("rule-uuid"); private static final RuleKey AB_RULE_DEPRECATED_KEY_1 = RuleKey.of("old_a", "old_b"); @@ -281,6 +287,34 @@ public class RuleRepositoryImplTest { verify(ruleIndexer).commitAndIndex(db.getSession(), ruleDefinitionDto.get().getUuid()); } + @DataProvider + public static Object[][] typesMapping() { + return new Object[][]{ + {ScannerReport.IssueType.CODE_SMELL, RuleType.CODE_SMELL}, + {ScannerReport.IssueType.BUG, RuleType.BUG}, + {ScannerReport.IssueType.VULNERABILITY, RuleType.VULNERABILITY}, + {ScannerReport.IssueType.SECURITY_HOTSPOT, RuleType.SECURITY_HOTSPOT} + }; + } + + @Test + @UseDataProvider("typesMapping") + public void addOrUpdateAddHocRuleIfNeeded_whenAdHocRule_shouldReturnRuleWithSameType(ScannerReport.IssueType type, RuleType expectedType) { + RuleKey ruleKey = RuleKey.of("any_repo", "any_rule"); + ScannerReport.AdHocRule adHocRule = ScannerReport.AdHocRule.newBuilder() + .setEngineId("ANY_ENGINE") + .setRuleId("any_rule") + .setName("ANY_NAME") + .setSeverity(Constants.Severity.MAJOR) + .setType(type) + .build(); + + underTest.addOrUpdateAddHocRuleIfNeeded(ruleKey, () -> new NewAdHocRule(adHocRule)); + + Rule rule = underTest.getByKey(ruleKey); + assertThat(rule.getType()).isEqualTo(expectedType); + } + private void expectNullRuleKeyNPE(ThrowingCallable callback) { assertThatThrownBy(callback) .isInstanceOf(NullPointerException.class) -- 2.39.5