Browse Source

SONAR-16364 fix rule desciption section with no mapping title

tags/9.5.0.56709
Zipeng WU 2 years ago
parent
commit
07495e3b38

+ 2
- 5
server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java View 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();

+ 6
- 2
server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java View 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/>"

Loading…
Cancel
Save