aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorZipeng WU <zipeng.wu@sonarsource.com>2022-05-10 15:49:14 +0200
committersonartech <sonartech@sonarsource.com>2022-05-11 20:02:59 +0000
commitb26a95828aa3a52f9855f95e7b987ff72020e7de (patch)
treea64e3a7bf3d2a40be3969ec779db227200bd1606 /server/sonar-server-common
parentbf0c2e6432fc59fee288a3d1a3df19c7edd49943 (diff)
downloadsonarqube-b26a95828aa3a52f9855f95e7b987ff72020e7de.tar.gz
sonarqube-b26a95828aa3a52f9855f95e7b987ff72020e7de.zip
SONAR-16303 Change RuleDescriptionFormatter from a static helper to regular bean
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java12
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/rule/HotspotRuleDescription.java144
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/rule/RuleDescriptionFormatter.java4
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java2
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java2
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java3
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/RuleDescriptionFormatterTest.java11
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java8
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java2
9 files changed, 22 insertions, 166 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
index 84edc168bdb..f813a76572d 100644
--- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/DefaultRuleFinder.java
@@ -40,8 +40,6 @@ import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleParamDto;
-import static org.sonar.server.rule.RuleDescriptionFormatter.getDescriptionAsHtml;
-
/**
* Will be removed in the future.
*/
@@ -49,10 +47,12 @@ public class DefaultRuleFinder implements ServerRuleFinder {
private final DbClient dbClient;
private final RuleDao ruleDao;
+ private final RuleDescriptionFormatter ruleDescriptionFormatter;
- public DefaultRuleFinder(DbClient dbClient) {
+ public DefaultRuleFinder(DbClient dbClient, RuleDescriptionFormatter ruleDescriptionFormatter) {
this.dbClient = dbClient;
this.ruleDao = dbClient.ruleDao();
+ this.ruleDescriptionFormatter = ruleDescriptionFormatter;
}
@Override
@@ -131,7 +131,7 @@ public class DefaultRuleFinder implements ServerRuleFinder {
return rules;
}
- private static org.sonar.api.rules.Rule toRule(RuleDto rule, List<RuleParamDto> params) {
+ private org.sonar.api.rules.Rule toRule(RuleDto rule, List<RuleParamDto> params) {
String severity = rule.getSeverityString();
org.sonar.api.rules.Rule apiRule = new org.sonar.api.rules.Rule();
@@ -147,9 +147,9 @@ public class DefaultRuleFinder implements ServerRuleFinder {
.setSeverity(severity != null ? RulePriority.valueOf(severity) : null)
.setStatus(rule.getStatus().name())
.setSystemTags(rule.getSystemTags().toArray(new String[rule.getSystemTags().size()]))
- .setTags(rule.getTags().toArray(new String[rule.getTags().size()]))
- .setDescription(getDescriptionAsHtml(rule));
+ .setTags(rule.getTags().toArray(new String[rule.getTags().size()]));
+ Optional.ofNullable(ruleDescriptionFormatter.getDescriptionAsHtml(rule)).ifPresent(apiRule::setDescription);
List<org.sonar.api.rules.RuleParam> apiParams = new ArrayList<>();
for (RuleParamDto param : params) {
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/HotspotRuleDescription.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/HotspotRuleDescription.java
deleted file mode 100644
index 82b09178a84..00000000000
--- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/HotspotRuleDescription.java
+++ /dev/null
@@ -1,144 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2022 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
-package org.sonar.server.rule;
-
-import java.util.Optional;
-import javax.annotation.CheckForNull;
-import javax.annotation.Nullable;
-import org.sonar.db.rule.RuleDto;
-
-import static java.util.Optional.ofNullable;
-
-/***
- * Temporary rule split before we have a better
- * solution to specify security hotspot.
- */
-public class HotspotRuleDescription {
- private static final HotspotRuleDescription NO_DESCRIPTION = new HotspotRuleDescription(null, null, null);
-
- @CheckForNull
- private final String risk;
- @CheckForNull
- private final String vulnerable;
- @CheckForNull
- private final String fixIt;
-
- private HotspotRuleDescription(@Nullable String risk, @Nullable String vulnerable, @Nullable String fixIt) {
- this.risk = risk;
- this.vulnerable = vulnerable;
- this.fixIt = fixIt;
- }
-
- public static HotspotRuleDescription from(RuleDto dto) {
- String description = RuleDescriptionFormatter.getDescriptionAsHtml(dto);
- return from(description);
- }
-
- private static HotspotRuleDescription from(@Nullable String description) {
- if (description == null) {
- return NO_DESCRIPTION;
- }
-
- String[] split = extractSection("", description);
- description = split[0];
- String ruleDescriptionSection = split[1];
-
- split = extractSection("<h2>Exceptions</h2>", description);
- description = split[0];
- String exceptions = split[1];
-
- split = extractSection("<h2>Ask Yourself Whether</h2>", description);
- description = split[0];
- String askSection = split[1];
-
- split = extractSection("<h2>Sensitive Code Example</h2>", description);
- description = split[0];
- String sensitiveSection = split[1];
-
- split = extractSection("<h2>Noncompliant Code Example</h2>", description);
- description = split[0];
- String noncompliantSection = split[1];
-
- split = extractSection("<h2>Recommended Secure Coding Practices</h2>", description);
- description = split[0];
- String recommendedSection = split[1];
-
- split = extractSection("<h2>Compliant Solution</h2>", description);
- description = split[0];
- String compliantSection = split[1];
-
- split = extractSection("<h2>See</h2>", description);
- description = split[0];
- String seeSection = split[1];
-
- return new HotspotRuleDescription(
- trimToNull(ruleDescriptionSection + exceptions + description),
- trimToNull(askSection + sensitiveSection + noncompliantSection),
- trimToNull(recommendedSection + compliantSection + seeSection));
- }
-
- private static String trimToNull(String input) {
- return input.isEmpty() ? null : input;
- }
-
- private static String[] extractSection(String beginning, String description) {
- String endSection = "<h2>";
- int beginningIndex = description.indexOf(beginning);
- if (beginningIndex != -1) {
- int endIndex = description.indexOf(endSection, beginningIndex + beginning.length());
- if (endIndex == -1) {
- endIndex = description.length();
- }
- return new String[]{
- description.substring(0, beginningIndex) + description.substring(endIndex),
- description.substring(beginningIndex, endIndex)
- };
- } else {
- return new String[]{description, ""};
- }
-
- }
-
- public Optional<String> getRisk() {
- return ofNullable(risk);
- }
-
- public Optional<String> getVulnerable() {
- return ofNullable(vulnerable);
- }
-
- public Optional<String> getFixIt() {
- return ofNullable(fixIt);
- }
-
- public boolean isComplete() {
- return risk != null && vulnerable != null && fixIt != null;
- }
-
- @Override
- public String toString() {
- return "HotspotRuleDescription{" +
- "risk='" + risk + '\'' +
- ", vulnerable='" + vulnerable + '\'' +
- ", fixIt='" + fixIt + '\'' +
- '}';
- }
-
-}
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 0b78ff94321..39107e39de4 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
@@ -61,10 +61,8 @@ public class RuleDescriptionFormatter {
RESOURCES_SECTION_KEY, "Resources"
);
- private RuleDescriptionFormatter() { /* static helpers */ }
-
@CheckForNull
- public static String getDescriptionAsHtml(RuleDto ruleDto) {
+ public String getDescriptionAsHtml(RuleDto ruleDto) {
if (ruleDto.getDescriptionFormat() == null) {
return null;
}
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java
index e9cc6b74875..0fa670dcfe0 100644
--- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java
@@ -295,7 +295,7 @@ public class RuleDoc extends BaseDoc {
return ReflectionToStringBuilder.toString(this);
}
- public static RuleDoc of(RuleForIndexingDto dto, SecurityStandards securityStandards) {
+ public static RuleDoc createFrom(RuleForIndexingDto dto, SecurityStandards securityStandards) {
return new RuleDoc()
.setUuid(dto.getUuid())
.setKey(dto.getRuleKey().toString())
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
index da9fa447957..9da22bebdaf 100644
--- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleIndexer.java
@@ -160,7 +160,7 @@ public class RuleIndexer implements ResilientIndexer {
.sorted(SQ_CATEGORY_KEYS_ORDERING)
.collect(joining(", ")));
}
- return RuleDoc.of(dto, securityStandards);
+ return RuleDoc.createFrom(dto, securityStandards);
}
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java
index 8b83e2d5811..a4286390138 100644
--- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java
@@ -35,6 +35,7 @@ import org.sonar.db.rule.RuleDto.Scope;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
public class DefaultRuleFinderTest {
@@ -80,7 +81,7 @@ public class DefaultRuleFinderTest {
.setScope(Scope.MAIN)
.setStatus(RuleStatus.READY);
- private final DefaultRuleFinder underTest = new DefaultRuleFinder(dbClient);
+ private final DefaultRuleFinder underTest = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class));
@Before
public void setup() {
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 ab79c311530..b0aaeb8a38f 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
@@ -34,18 +34,19 @@ public class RuleDescriptionFormatterTest {
private static final RuleDescriptionSectionDto HTML_SECTION = createDefaultRuleDescriptionSection("uuid", "<span class=\"example\">*md* ``description``</span>");
private static final RuleDescriptionSectionDto MARKDOWN_SECTION = createDefaultRuleDescriptionSection("uuid", "*md* ``description``");
+ private static final RuleDescriptionFormatter ruleDescriptionFormatter = new RuleDescriptionFormatter();
@Test
public void getMarkdownDescriptionAsHtml() {
RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.MARKDOWN).addRuleDescriptionSectionDto(MARKDOWN_SECTION).setType(RuleType.BUG);
- String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
+ String html = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
assertThat(html).isEqualTo("<strong>md</strong> <code>description</code>");
}
@Test
public void getHtmlDescriptionAsIs() {
RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML).addRuleDescriptionSectionDto(HTML_SECTION).setType(RuleType.BUG);
- String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
+ String html = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
assertThat(html).isEqualTo(HTML_SECTION.getContent());
}
@@ -59,7 +60,7 @@ public class RuleDescriptionFormatterTest {
.addRuleDescriptionSectionDto(section1)
.addRuleDescriptionSectionDto(section2)
.addRuleDescriptionSectionDto(section3);
- String html = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
+ String html = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
assertThat(html)
.contains(
"<h2>What is the risk?</h2>"
@@ -74,7 +75,7 @@ public class RuleDescriptionFormatterTest {
@Test
public void handleEmptyDescription() {
RuleDto rule = new RuleDto().setDescriptionFormat(RuleDto.Format.HTML).setType(RuleType.BUG);
- String result = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
+ String result = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
assertThat(result).isNull();
}
@@ -82,7 +83,7 @@ public class RuleDescriptionFormatterTest {
public void handleNullDescriptionFormat() {
RuleDescriptionSectionDto sectionWithNullFormat = createDefaultRuleDescriptionSection("uuid", "whatever");
RuleDto rule = new RuleDto().addRuleDescriptionSectionDto(sectionWithNullFormat).setType(RuleType.BUG);
- String result = RuleDescriptionFormatter.getDescriptionAsHtml(rule);
+ String result = ruleDescriptionFormatter.getDescriptionAsHtml(rule);
assertThat(result).isNull();
}
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java
index 93d4a1e3530..af46148f964 100644
--- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java
@@ -40,7 +40,7 @@ public class RuleDocTest {
ruleForIndexingDto.setTemplateRepository("repoKey");
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
- RuleDoc ruleDoc = RuleDoc.of(ruleForIndexingDto, securityStandards);
+ RuleDoc ruleDoc = RuleDoc.createFrom(ruleForIndexingDto, securityStandards);
assertThat(ruleDoc.getId()).isEqualTo(ruleDto.getUuid());
assertThat(ruleDoc.key()).isEqualTo(ruleForIndexingDto.getRuleKey());
@@ -75,7 +75,7 @@ public class RuleDocTest {
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
- RuleDoc ruleDoc = RuleDoc.of(ruleForIndexingDto, securityStandards);
+ RuleDoc ruleDoc = RuleDoc.createFrom(ruleForIndexingDto, securityStandards);
assertThat(ruleDoc.htmlDescription()).isEmpty();
}
@@ -92,7 +92,7 @@ public class RuleDocTest {
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
- RuleDoc ruleDoc = RuleDoc.of(ruleForIndexingDto, securityStandards);
+ RuleDoc ruleDoc = RuleDoc.createFrom(ruleForIndexingDto, securityStandards);
assertThat(ruleDoc.htmlDescription())
.contains(section1.getContent())
.contains(section2.getContent())
@@ -112,7 +112,7 @@ public class RuleDocTest {
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
- RuleDoc ruleDoc = RuleDoc.of(ruleForIndexingDto, securityStandards);
+ RuleDoc ruleDoc = RuleDoc.createFrom(ruleForIndexingDto, securityStandards);
assertThat(ruleDoc.htmlDescription())
.contains(convertToHtml(section1.getContent()))
.contains(convertToHtml(section2.getContent()))
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java
index f276fb08843..2b300399360 100644
--- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java
@@ -106,7 +106,7 @@ public class RuleIndexTest {
private RuleIndex underTest = new RuleIndex(es.client(), system2);
private UuidFactory uuidFactory = UuidFactoryFast.getInstance();
-
+
@Test
public void search_all_rules() {
createRule();