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();
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;
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/>"