]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16364 fix rule desciption section with no mapping title
authorZipeng WU <zipeng.wu@sonarsource.com>
Wed, 11 May 2022 09:31:42 +0000 (11:31 +0200)
committersonartech <sonartech@sonarsource.com>
Wed, 11 May 2022 20:03:00 +0000 (20:03 +0000)
server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java
server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java

index 39107e39de476ccacfbb4c8aeff27205c3d266cd..f44a1108f2ad924ac197eaa2a22f9f06d28e9621 100644 (file)
@@ -90,11 +90,8 @@ public class RuleDescriptionFormatter {
     var builder = new StringBuilder();
     for (String sectionKey : SECTION_KEYS) {
       if (sectionKeyToHtml.containsKey(sectionKey)) {
-        builder.append("<h2>")
-          .append(titleMapping.get(sectionKey))
-          .append("</h2>")
-          .append(sectionKeyToHtml.get(sectionKey))
-          .append("<br/>");
+        Optional.ofNullable(titleMapping.get(sectionKey)).ifPresent(title -> builder.append("<h2>").append(title).append("</h2>"));
+        builder.append(sectionKeyToHtml.get(sectionKey)).append("<br/>");
       }
     }
     return builder.toString();
index b0aaeb8a38ff7e51109356b1dab782ee9126f202..0a14950681e9ee900d4829646805dacbb2b11383 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.db.rule.RuleDto;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ASSESS_THE_PROBLEM_SECTION_KEY;
 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
+import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.INTRODUCTION_SECTION_KEY;
 import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ROOT_CAUSE_SECTION_KEY;
 import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
 
@@ -55,15 +56,18 @@ public class RuleDescriptionFormatterTest {
     var section1 = createRuleDescriptionSection(ROOT_CAUSE_SECTION_KEY, "<div>Root is Root</div>");
     var section2 = createRuleDescriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY, "<div>This is not a problem</div>");
     var section3 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY, "<div>I don't want to fix</div>");
+    var section4 = createRuleDescriptionSection(INTRODUCTION_SECTION_KEY, "<div>Introduction with no title</div>");
     RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML)
       .setType(RuleType.SECURITY_HOTSPOT)
       .addRuleDescriptionSectionDto(section1)
       .addRuleDescriptionSectionDto(section2)
-      .addRuleDescriptionSectionDto(section3);
+      .addRuleDescriptionSectionDto(section3)
+      .addRuleDescriptionSectionDto(section4);
     String html = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
     assertThat(html)
       .contains(
-        "<h2>What is the risk?</h2>"
+        "<div>Introduction with no title</div><br/>"
+          + "<h2>What is the risk?</h2>"
           + "<div>Root is Root</div><br/>"
           + "<h2>Assess the risk</h2>"
           + "<div>This is not a problem</div><br/>"