]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-18462 - Fix ALM PR decoration not using ad hoc type
authorAntoine Vinot <antoine.vinot@sonarsource.com>
Tue, 21 Mar 2023 07:28:37 +0000 (08:28 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 22 Mar 2023 20:04:05 +0000 (20:04 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImpl.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/RuleRepositoryImplTest.java

index 73598cc54a8ae097f0fa2b68b17876b13a12cf9a..a41f4c0a774d311a0736bfff57bd179ecc329621 100644 (file)
@@ -182,7 +182,7 @@ public class RuleRepositoryImpl implements RuleRepository {
     @Override
     @CheckForNull
     public RuleType getType() {
-      return null;
+      return addHocRule.getRuleType();
     }
 
     @Override
index 18fd1ef0f5303055041a43e00ef82932af3d0420..59498fe155a5c86c37b9ca91d993dce77e556fde 100644 (file)
@@ -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)