Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

RuleDescriptionFormatterTest.java 4.3KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2022 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.rule;
  21. import org.junit.Test;
  22. import org.sonar.api.rules.RuleType;
  23. import org.sonar.db.rule.RuleDescriptionSectionDto;
  24. import org.sonar.db.rule.RuleDto;
  25. import static org.assertj.core.api.Assertions.assertThat;
  26. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ASSESS_THE_PROBLEM_SECTION_KEY;
  27. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.HOW_TO_FIX_SECTION_KEY;
  28. import static org.sonar.api.server.rule.RuleDescriptionSection.RuleDescriptionSectionKeys.ROOT_CAUSE_SECTION_KEY;
  29. import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
  30. public class RuleDescriptionFormatterTest {
  31. private static final RuleDescriptionSectionDto HTML_SECTION = createDefaultRuleDescriptionSection("uuid", "<span class=\"example\">*md* ``description``</span>");
  32. private static final RuleDescriptionSectionDto MARKDOWN_SECTION = createDefaultRuleDescriptionSection("uuid", "*md* ``description``");
  33. @Test
  34. public void getMarkdownDescriptionAsHtml() {
  35. RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.MARKDOWN).addRuleDescriptionSectionDto(MARKDOWN_SECTION).setType(RuleType.BUG);
  36. String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
  37. assertThat(html).isEqualTo("<strong>md</strong> <code>description</code>");
  38. }
  39. @Test
  40. public void getHtmlDescriptionAsIs() {
  41. RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML).addRuleDescriptionSectionDto(HTML_SECTION).setType(RuleType.BUG);
  42. String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
  43. assertThat(html).isEqualTo(HTML_SECTION.getContent());
  44. }
  45. @Test
  46. public void concatHtmlDescriptionSections() {
  47. var section1 = createRuleDescriptionSection(ROOT_CAUSE_SECTION_KEY, "<div>Root is Root</div>");
  48. var section2 = createRuleDescriptionSection(ASSESS_THE_PROBLEM_SECTION_KEY, "<div>This is not a problem</div>");
  49. var section3 = createRuleDescriptionSection(HOW_TO_FIX_SECTION_KEY, "<div>I don't want to fix</div>");
  50. RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML)
  51. .setType(RuleType.SECURITY_HOTSPOT)
  52. .addRuleDescriptionSectionDto(section1)
  53. .addRuleDescriptionSectionDto(section2)
  54. .addRuleDescriptionSectionDto(section3);
  55. String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
  56. assertThat(html)
  57. .contains(
  58. "<h2>What is the risk?</h2>"
  59. + "<div>Root is Root</div><br/>"
  60. + "<h2>Assess the risk</h2>"
  61. + "<div>This is not a problem</div><br/>"
  62. + "<h2>How can you fix it?</h2>"
  63. + "<div>I don't want to fix</div><br/>"
  64. );
  65. }
  66. @Test
  67. public void handleEmptyDescription() {
  68. RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML).setType(RuleType.BUG);
  69. String result = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
  70. assertThat(result).isNull();
  71. }
  72. @Test
  73. public void handleNullDescriptionFormat() {
  74. RuleDescriptionSectionDto sectionWithNullFormat = createDefaultRuleDescriptionSection("uuid", "whatever");
  75. RuleDto rule = new RuleDto().addRuleDescriptionSectionDto(sectionWithNullFormat).setType(RuleType.BUG);
  76. String result = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
  77. assertThat(result).isNull();
  78. }
  79. private static RuleDescriptionSectionDto createRuleDescriptionSection(String key, String content) {
  80. return RuleDescriptionSectionDto.builder().key(key).content(content).build();
  81. }
  82. }