]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21139 fixed the issue where creating custom rule for security hotspot would...
authorlukasz-jarocki-sonarsource <lukasz.jarocki@sonarsource.com>
Thu, 30 Nov 2023 11:16:56 +0000 (12:16 +0100)
committersonartech <sonartech@sonarsource.com>
Wed, 6 Dec 2023 20:02:46 +0000 (20:02 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/rule/RuleCreatorIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/RuleCreator.java

index 99f7a64bbd9c6abf428df4e7c8904e032c574015..3a9984641e69b111844e7ce437c981134dcc84b2 100644 (file)
  */
 package org.sonar.server.rule;
 
-import com.google.common.collect.ImmutableMap;
 import com.google.common.collect.Sets;
 import java.time.Instant;
 import java.util.Arrays;
 import java.util.Date;
 import java.util.List;
+import java.util.Map;
 import java.util.concurrent.atomic.AtomicReference;
 import org.assertj.core.api.Fail;
 import org.assertj.core.groups.Tuple;
@@ -90,7 +90,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("Some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
     RuleKey customRuleKey = underTest.create(dbSession, newRule);
 
     RuleDto rule = dbTester.getDbClient().ruleDao().selectOrFailByKey(dbSession, customRuleKey);
@@ -149,7 +149,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", ""));
+      .setParameters(Map.of("regex", ""));
 
     RuleKey customRuleKey = underTest.create(dbSession, newRule);
 
@@ -172,7 +172,7 @@ public class RuleCreatorIT {
       .setSeverity(Severity.MAJOR)
       .setType(RuleType.SECURITY_HOTSPOT)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", ""));
+      .setParameters(Map.of("regex", ""));
 
     RuleKey customRuleKey = underTest.create(dbSession, newRule);
 
@@ -210,7 +210,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("myIntegers", "1,3"));
+      .setParameters(Map.of("myIntegers", "1,3"));
 
     RuleKey customRuleKey = underTest.create(dbSession, newRule);
 
@@ -262,7 +262,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, singletonList(newRule)))
       .isInstanceOf(IllegalArgumentException.class)
@@ -302,7 +302,7 @@ public class RuleCreatorIT {
         .setMarkdownDescription("Some description")
         .setSeverity(Severity.MAJOR)
         .setStatus(RuleStatus.READY)
-        .setParameters(ImmutableMap.of("myIntegers", "1,polop,2"));
+        .setParameters(Map.of("myIntegers", "1,polop,2"));
       underTest.create(dbSession, newRule);
     })
       .isInstanceOf(BadRequestException.class)
@@ -320,7 +320,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("Some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("first", "polop", "second", "palap"));
+      .setParameters(Map.of("first", "polop", "second", "palap"));
     try {
       underTest.create(dbSession, newRule);
       Fail.failBecauseExceptionWasNotThrown(BadRequestException.class);
@@ -368,7 +368,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("New description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "c.*"));
+      .setParameters(Map.of("regex", "c.*"));
     RuleKey customRuleKey = underTest.create(dbSession, newRule);
 
     RuleDto result = dbTester.getDbClient().ruleDao().selectOrFailByKey(dbSession, customRuleKey);
@@ -406,7 +406,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "c.*"))
+      .setParameters(Map.of("regex", "c.*"))
       .setPreventReactivation(true);
 
     try {
@@ -429,7 +429,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(BadRequestException.class)
@@ -446,7 +446,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*")));
+      .setParameters(Map.of("regex", "a.*")));
     underTest.create(dbSession, newRule.get());
 
     // Create another custom rule having same key
@@ -455,7 +455,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*")));
+      .setParameters(Map.of("regex", "a.*")));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule.get()))
       .isInstanceOf(IllegalArgumentException.class)
@@ -471,7 +471,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(BadRequestException.class)
@@ -488,7 +488,7 @@ public class RuleCreatorIT {
         .setName("My custom")
         .setSeverity(Severity.MAJOR)
         .setStatus(RuleStatus.READY)
-        .setParameters(ImmutableMap.of("regex", "a.*"));
+        .setParameters(Map.of("regex", "a.*"));
       underTest.create(dbSession, newRule);
     })
       .isInstanceOf(BadRequestException.class)
@@ -504,7 +504,7 @@ public class RuleCreatorIT {
       .setName("My custom")
       .setMarkdownDescription("some description")
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(BadRequestException.class)
@@ -521,7 +521,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity("INVALID")
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(BadRequestException.class)
@@ -537,7 +537,7 @@ public class RuleCreatorIT {
       .setName("My custom")
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(BadRequestException.class)
@@ -557,7 +557,7 @@ public class RuleCreatorIT {
       .setMarkdownDescription("some description")
       .setSeverity(Severity.MAJOR)
       .setStatus(RuleStatus.READY)
-      .setParameters(ImmutableMap.of("regex", "a.*"));
+      .setParameters(Map.of("regex", "a.*"));
 
     assertThatThrownBy(() -> underTest.create(dbSession, newRule))
       .isInstanceOf(IllegalArgumentException.class)
@@ -594,6 +594,24 @@ public class RuleCreatorIT {
       .hasMessage("The template key doesn't exist: java:S001");
   }
 
+  @Test
+  public void create_givenSecurityHotspotRule_doNotSetCleanCodeAttribute() {
+    RuleDto templateRule = createTemplateRule();
+
+    NewCustomRule newRule = NewCustomRule.createForCustomRule("security_hotspots_rule", templateRule.getKey())
+      .setName("My custom")
+      .setMarkdownDescription("some description")
+      .setSeverity(Severity.MAJOR)
+      .setStatus(RuleStatus.READY)
+      .setType(RuleType.SECURITY_HOTSPOT);
+
+    RuleKey customRuleKey = underTest.create(dbSession, newRule);
+
+    RuleDto result = dbTester.getDbClient().ruleDao().selectOrFailByKey(dbSession, customRuleKey);
+
+    assertThat(result.getCleanCodeAttribute()).isNull();
+  }
+
   private RuleDto createTemplateRule() {
     RuleDto templateRule = RuleTesting.newRule(RuleKey.of("java", "S001"))
       .setIsTemplate(true)
index 9674b9df9dd515749a98bbcdd2d6d65cdd7e9759..c17f60d341b7a7b386d0458f9594cc367441bf62 100644 (file)
@@ -50,6 +50,7 @@ import org.sonar.db.rule.RuleParamDto;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.rule.index.RuleIndexer;
 import org.sonar.server.util.TypeValidations;
+import org.sonarqube.ws.Common;
 
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.collect.Lists.newArrayList;
@@ -201,7 +202,6 @@ public class RuleCreator {
       .setSeverity(severity)
       .setStatus(newRule.status())
       .setType(type)
-      .setCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL)
       .setLanguage(templateRuleDto.getLanguage())
       .setDefRemediationFunction(templateRuleDto.getDefRemediationFunction())
       .setDefRemediationGapMultiplier(templateRuleDto.getDefRemediationGapMultiplier())
@@ -220,7 +220,9 @@ public class RuleCreator {
     if (type != RuleType.SECURITY_HOTSPOT.getDbConstant()) {
       SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(RuleType.valueOf(type));
       org.sonar.api.issue.impact.Severity impactSeverity = ImpactMapper.convertToImpactSeverity(severity);
-      ruleDto = ruleDto.addDefaultImpact(new ImpactDto().setUuid(uuidFactory.create()).setSoftwareQuality(softwareQuality).setSeverity(impactSeverity));
+      ruleDto = ruleDto.addDefaultImpact(new ImpactDto().setUuid(uuidFactory.create()).setSoftwareQuality(softwareQuality)
+        .setSeverity(impactSeverity))
+        .setCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL);
     }
 
     Set<String> tags = templateRuleDto.getTags();