From 07495e3b383c91a39543a802ac95378cbd8e4c7c Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Wed, 11 May 2022 11:31:42 +0200 Subject: [PATCH] SONAR-16364 fix rule desciption section with no mapping title --- .../org/sonar/server/rule/RuleDescriptionFormatter.java | 7 ++----- .../sonar/server/rule/RuleDescriptionFormatterTest.java | 8 ++++++-- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java index 39107e39de4..f44a1108f2a 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java @@ -90,11 +90,8 @@ public class RuleDescriptionFormatter { var builder = new StringBuilder(); for (String sectionKey : SECTION_KEYS) { if (sectionKeyToHtml.containsKey(sectionKey)) { - builder.append("

") - .append(titleMapping.get(sectionKey)) - .append("

") - .append(sectionKeyToHtml.get(sectionKey)) - .append("
"); + Optional.ofNullable(titleMapping.get(sectionKey)).ifPresent(title -> builder.append("

").append(title).append("

")); + builder.append(sectionKeyToHtml.get(sectionKey)).append("
"); } } return builder.toString(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java index b0aaeb8a38f..0a14950681e 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java @@ -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, "
Root is Root
"); var section2 = createRuleDescriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY, "
This is not a problem
"); var section3 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY, "
I don't want to fix
"); + var section4 = createRuleDescriptionSection(INTRODUCTION_SECTION_KEY, "
Introduction with no title
"); 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( - "

What is the risk?

" + "
Introduction with no title

" + + "

What is the risk?

" + "
Root is Root

" + "

Assess the risk

" + "
This is not a problem

" -- 2.39.5