From b26a95828aa3a52f9855f95e7b987ff72020e7de Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 10 May 2022 15:49:14 +0200 Subject: [PATCH] SONAR-16303 Change RuleDescriptionFormatter from a static helper to regular bean --- .../container/ComputeEngineContainerImpl.java | 2 + .../sonar/server/rule/DefaultRuleFinder.java | 12 +- .../server/rule/HotspotRuleDescription.java | 144 ------------------ .../server/rule/RuleDescriptionFormatter.java | 4 +- .../org/sonar/server/rule/index/RuleDoc.java | 2 +- .../sonar/server/rule/index/RuleIndexer.java | 2 +- .../server/rule/DefaultRuleFinderTest.java | 3 +- .../rule/RuleDescriptionFormatterTest.java | 11 +- .../sonar/server/rule/index/RuleDocTest.java | 8 +- .../server/rule/index/RuleIndexTest.java | 2 +- .../sonar/server/rule/CachingRuleFinder.java | 12 +- .../server/rule/WebServerRuleFinderImpl.java | 8 +- .../server/rule/CachingRuleFinderTest.java | 23 +-- .../rule/WebServerRuleFinderImplTest.java | 2 +- .../sonar/server/rule/RegisterRulesTest.java | 2 - .../sonar/server/es/RecoveryIndexerTest.java | 2 +- .../org/sonar/server/rule/ws/RuleMapper.java | 6 +- .../server/issue/WebIssueStorageTest.java | 3 +- .../server/issue/ws/AddCommentActionTest.java | 3 +- .../server/issue/ws/AssignActionTest.java | 3 +- .../server/issue/ws/BulkChangeActionTest.java | 3 +- .../issue/ws/DoTransitionActionTest.java | 3 +- .../server/issue/ws/IssueUpdaterTest.java | 3 +- .../issue/ws/SetSeverityActionTest.java | 3 +- .../server/issue/ws/SetTagsActionTest.java | 3 +- .../server/issue/ws/SetTypeActionTest.java | 3 +- .../qualityprofile/QProfileExportersTest.java | 3 +- ...gisterQualityProfilesNotificationTest.java | 3 +- .../BuiltInQProfileInsertImplTest.java | 3 +- .../BuiltInQProfileRepositoryImplTest.java | 3 +- .../server/rule/ws/CreateActionTest.java | 4 +- .../server/rule/ws/SearchActionTest.java | 3 +- .../sonar/server/rule/ws/ShowActionTest.java | 3 +- .../server/rule/ws/UpdateActionTest.java | 6 +- .../platformlevel/PlatformLevel4.java | 2 + 35 files changed, 92 insertions(+), 210 deletions(-) delete mode 100644 server/sonar-server-common/src/main/java/org/sonar/server/rule/HotspotRuleDescription.java diff --git a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java index 7a83f3db85b..8193be28b65 100644 --- a/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java +++ b/server/sonar-ce/src/main/java/org/sonar/ce/container/ComputeEngineContainerImpl.java @@ -138,6 +138,7 @@ import org.sonar.server.qualitygate.notification.QGChangeEmailTemplate; import org.sonar.server.qualitygate.notification.QGChangeNotificationHandler; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.setting.DatabaseSettingLoader; @@ -353,6 +354,7 @@ public class ComputeEngineContainerImpl implements ComputeEngineContainer { private static void populateLevel4(Container container, Props props) { container.add( + RuleDescriptionFormatter.class, ResourceTypes.class, DefaultResourceTypes.get(), 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 params) { + private org.sonar.api.rules.Rule toRule(RuleDto rule, List 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 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("

Exceptions

", description); - description = split[0]; - String exceptions = split[1]; - - split = extractSection("

Ask Yourself Whether

", description); - description = split[0]; - String askSection = split[1]; - - split = extractSection("

Sensitive Code Example

", description); - description = split[0]; - String sensitiveSection = split[1]; - - split = extractSection("

Noncompliant Code Example

", description); - description = split[0]; - String noncompliantSection = split[1]; - - split = extractSection("

Recommended Secure Coding Practices

", description); - description = split[0]; - String recommendedSection = split[1]; - - split = extractSection("

Compliant Solution

", description); - description = split[0]; - String compliantSection = split[1]; - - split = extractSection("

See

", 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 = "

"; - 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 getRisk() { - return ofNullable(risk); - } - - public Optional getVulnerable() { - return ofNullable(vulnerable); - } - - public Optional 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", "*md* ``description``"); 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("md description"); } @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( "

What is the risk?

" @@ -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(); diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java index c66e48d1bc3..4e3d57d58dc 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java @@ -59,8 +59,10 @@ public class CachingRuleFinder implements ServerRuleFinder { private final Map ruleDtosByUuid; private final Map ruleByRuleDto; private final Map rulesByKey; + private final RuleDescriptionFormatter ruleDescriptionFormatter; - public CachingRuleFinder(DbClient dbClient) { + public CachingRuleFinder(DbClient dbClient, RuleDescriptionFormatter ruleDescriptionFormatter) { + this.ruleDescriptionFormatter = ruleDescriptionFormatter; try (DbSession dbSession = dbClient.openSession(false)) { List dtos = dbClient.ruleDao().selectAll(dbSession); this.ruleDtosByKey = dtos.stream().collect(Collectors.toMap(RuleDto::getKey, d -> d)); @@ -71,7 +73,7 @@ public class CachingRuleFinder implements ServerRuleFinder { } } - private static Map buildRulesByRuleDefinitionDto(DbClient dbClient, DbSession dbSession, List dtos) { + private Map buildRulesByRuleDefinitionDto(DbClient dbClient, DbSession dbSession, List dtos) { Map> ruleParamsByRuleUuid = retrieveRuleParameters(dbClient, dbSession); Map rulesByDefinition = new HashMap<>(dtos.size()); for (RuleDto definition : dtos) { @@ -143,7 +145,7 @@ public class CachingRuleFinder implements ServerRuleFinder { return configKey == null || configKey.equals(ruleDto.getConfigKey()); } - private static Rule toRule(RuleDto ruleDto, List params) { + private Rule toRule(RuleDto ruleDto, List params) { String severity = ruleDto.getSeverityString(); Rule apiRule = Rule.create() @@ -157,9 +159,9 @@ public class CachingRuleFinder implements ServerRuleFinder { .setSeverity(severity != null ? RulePriority.valueOf(severity) : null) .setStatus(ruleDto.getStatus().name()) .setSystemTags(ruleDto.getSystemTags().toArray(new String[ruleDto.getSystemTags().size()])) - .setTags(new String[0]) - .setDescription(RuleDescriptionFormatter.getDescriptionAsHtml(ruleDto)); + .setTags(new String[0]); + Optional.ofNullable(ruleDescriptionFormatter.getDescriptionAsHtml(ruleDto)).ifPresent(apiRule::setDescription); Optional.ofNullable(ruleDto.getLanguage()).ifPresent(apiRule::setLanguage); List apiParams = new ArrayList<>(); diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/WebServerRuleFinderImpl.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/WebServerRuleFinderImpl.java index 1a79d6528a3..20f023bf7e1 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/WebServerRuleFinderImpl.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/WebServerRuleFinderImpl.java @@ -32,18 +32,20 @@ import org.sonar.db.rule.RuleDto; public class WebServerRuleFinderImpl implements WebServerRuleFinder { private final DbClient dbClient; private final ServerRuleFinder defaultFinder; + private final RuleDescriptionFormatter ruleDescriptionFormatter; @VisibleForTesting ServerRuleFinder delegate; - public WebServerRuleFinderImpl(DbClient dbClient) { + public WebServerRuleFinderImpl(DbClient dbClient, RuleDescriptionFormatter ruleDescriptionFormatter) { this.dbClient = dbClient; - this.defaultFinder = new DefaultRuleFinder(dbClient); + this.ruleDescriptionFormatter = ruleDescriptionFormatter; + this.defaultFinder = new DefaultRuleFinder(dbClient, ruleDescriptionFormatter); this.delegate = this.defaultFinder; } @Override public void startCaching() { - this.delegate = new CachingRuleFinder(dbClient); + this.delegate = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); } @Override diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java index b7b555a8b94..c1f6e2a4f8e 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/CachingRuleFinderTest.java @@ -56,6 +56,7 @@ public class CachingRuleFinderTest { private RuleDto[] ruleDtos; private RuleParamDto[] ruleParams; private CachingRuleFinder underTest; + private RuleDescriptionFormatter ruleDescriptionFormatter = new RuleDescriptionFormatter(); @Before() public void setUp() { @@ -72,7 +73,7 @@ public class CachingRuleFinderTest { .map(rule -> dbTester.rules().insertRuleParam(rule)) .toArray(RuleParamDto[]::new); - underTest = new CachingRuleFinder(dbClient); + underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); // delete all data from DB to ensure tests rely on cache exclusively dbTester.executeUpdateSql("delete from rules"); @@ -89,7 +90,7 @@ public class CachingRuleFinderTest { when(dbClient.openSession(anyBoolean())).thenReturn(dbSession); when(dbClient.ruleDao()).thenReturn(ruleDao); - new CachingRuleFinder(dbClient); + new CachingRuleFinder(dbClient, ruleDescriptionFormatter); verify(dbClient).openSession(anyBoolean()); verify(ruleDao).selectAll(dbSession); @@ -107,7 +108,7 @@ public class CachingRuleFinderTest { List ruleKeys = Arrays.asList(RuleKey.of("A", "B"), RuleKey.of("C", "D"), RuleKey.of("E", "F")); when(ruleDao.selectAll(dbSession)).thenReturn(ruleKeys.stream().map(RuleTesting::newRule).collect(toList())); - new CachingRuleFinder(dbClient); + new CachingRuleFinder(dbClient, ruleDescriptionFormatter); verify(ruleDao).selectAllRuleParams(dbSession); } @@ -166,7 +167,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(toRuleKey(underTest.find(RuleQuery.create().withRepositoryKey(repoKey)))) .isEqualTo(sameRepoKey[1].getKey()); @@ -187,7 +188,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(toRuleKey(underTest.find(RuleQuery.create().withKey(ruleKey)))) .isEqualTo(sameRuleKey[1].getKey()); @@ -208,7 +209,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(toRuleKey(underTest.find(RuleQuery.create().withConfigKey(configKey)))) .isEqualTo(sameConfigKey[1].getKey()); @@ -239,7 +240,7 @@ public class CachingRuleFinderTest { RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey); RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(toRuleKey(underTest.find(allQuery))).isEqualTo(rules[0].getKey()); assertThat(toRuleKey(underTest.find(ruleAndConfigKeyQuery))).isEqualTo(rules[1].getKey()); @@ -272,7 +273,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(currentTimeMillis + system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(underTest.findAll(RuleQuery.create().withRepositoryKey(repoKey))) .extracting(CachingRuleFinderTest::toRuleKey) @@ -295,7 +296,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(underTest.findAll(RuleQuery.create().withKey(ruleKey))) .extracting(CachingRuleFinderTest::toRuleKey) @@ -318,7 +319,7 @@ public class CachingRuleFinderTest { }; RuleDto otherRule = dbTester.rules().insert(rule -> rule.setUpdatedAt(system2.now())); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(underTest.findAll(RuleQuery.create().withConfigKey(configKey))) .extracting(CachingRuleFinderTest::toRuleKey) @@ -351,7 +352,7 @@ public class CachingRuleFinderTest { RuleQuery ruleKeyQuery = RuleQuery.create().withKey(ruleKey); RuleQuery repoKeyQuery = RuleQuery.create().withRepositoryKey(repoKey); - CachingRuleFinder underTest = new CachingRuleFinder(dbClient); + CachingRuleFinder underTest = new CachingRuleFinder(dbClient, ruleDescriptionFormatter); assertThat(underTest.findAll(allQuery)) .extracting(CachingRuleFinderTest::toRuleKey) diff --git a/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/WebServerRuleFinderImplTest.java b/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/WebServerRuleFinderImplTest.java index 28b999c045c..929d72c5c84 100644 --- a/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/WebServerRuleFinderImplTest.java +++ b/server/sonar-webserver-api/src/test/java/org/sonar/server/rule/WebServerRuleFinderImplTest.java @@ -32,7 +32,7 @@ import static org.mockito.Mockito.when; public class WebServerRuleFinderImplTest { private DbClient dbClient = mock(DbClient.class); - private WebServerRuleFinderImpl underTest = new WebServerRuleFinderImpl(dbClient); + private WebServerRuleFinderImpl underTest = new WebServerRuleFinderImpl(dbClient, mock(RuleDescriptionFormatter.class)); @Before public void setUp() { diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index 3aa93a6af1e..39795de074f 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -141,7 +141,6 @@ public class RegisterRulesTest { return Set.of(builder().uuid(UuidFactoryFast.getInstance().create()).key("default").content(description).build()); }); - when(ruleDescriptionSectionsGenerator.isGeneratorForRule(any())).thenReturn(true); } @@ -202,7 +201,6 @@ public class RegisterRulesTest { verifyHotspot(hotspotRule); } - private void verifyRule(RuleDto rule) { assertThat(rule.getName()).isEqualTo("One"); assertThat(rule.getDefaultRuleDescriptionSection().getContent()).isEqualTo("Description of One"); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java index c4c0c81d58c..0a4d57447e2 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/es/RecoveryIndexerTest.java @@ -36,8 +36,8 @@ import org.junit.rules.TestRule; import org.junit.rules.Timeout; import org.sonar.api.config.Configuration; import org.sonar.api.config.internal.MapSettings; -import org.sonar.api.utils.MessageException; import org.sonar.api.impl.utils.TestSystem2; +import org.sonar.api.utils.MessageException; import org.sonar.api.utils.log.LogTester; import org.sonar.api.utils.log.LoggerLevel; import org.sonar.db.DbSession; diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 21202d076db..9756754118f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -89,10 +89,12 @@ public class RuleMapper { private final Languages languages; private final MacroInterpreter macroInterpreter; + private final RuleDescriptionFormatter ruleDescriptionFormatter; - public RuleMapper(final Languages languages, final MacroInterpreter macroInterpreter) { + public RuleMapper(final Languages languages, final MacroInterpreter macroInterpreter, RuleDescriptionFormatter ruleDescriptionFormatter) { this.languages = languages; this.macroInterpreter = macroInterpreter; + this.ruleDescriptionFormatter = ruleDescriptionFormatter; } public Rules.Rule toWsRule(RuleDto ruleDefinitionDto, SearchResult result, Set fieldsToReturn) { @@ -328,7 +330,7 @@ public class RuleMapper { private void setDescriptionFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { if (shouldReturnField(fieldsToReturn, FIELD_HTML_DESCRIPTION)) { - String htmlDescription = RuleDescriptionFormatter.getDescriptionAsHtml(ruleDto); + String htmlDescription = ruleDescriptionFormatter.getDescriptionAsHtml(ruleDto); if (htmlDescription != null) { ruleResponse.setHtmlDesc(macroInterpreter.interpret(htmlDescription)); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/WebIssueStorageTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/WebIssueStorageTest.java index b72c2687c8a..9c1af5b3601 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/WebIssueStorageTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/WebIssueStorageTest.java @@ -40,6 +40,7 @@ import org.sonar.db.issue.IssueDto; import org.sonar.db.rule.RuleDto; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import static java.util.Collections.singletonList; import static org.assertj.core.api.Assertions.assertThat; @@ -58,7 +59,7 @@ public class WebIssueStorageTest { private final DbClient dbClient = db.getDbClient(); private final IssueIndexer issueIndexer = mock(IssueIndexer.class); - private final WebIssueStorage underTest = new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(db.getDbClient()), issueIndexer, + private final WebIssueStorage underTest = new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(db.getDbClient(), mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java index 86143f0b918..9db8356c600 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AddCommentActionTest.java @@ -50,6 +50,7 @@ import org.sonar.server.issue.index.IssueIteratorFactory; import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; @@ -89,7 +90,7 @@ public class AddCommentActionTest { private IssueDbTester issueDbTester = new IssueDbTester(dbTester); private IssueIndexer issueIndexer = new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient), null); - private WebIssueStorage serverIssueStorage = new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, + private WebIssueStorage serverIssueStorage = new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()); private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor(); private IssueUpdater issueUpdater = new IssueUpdater(dbClient, serverIssueStorage, mock(NotificationManager.class), issueChangePostProcessor, diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AssignActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AssignActionTest.java index 3d0c732ff4e..94b5be2ad46 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AssignActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/AssignActionTest.java @@ -45,6 +45,7 @@ import org.sonar.server.issue.notification.IssuesChangesNotification; import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -85,7 +86,7 @@ public class AssignActionTest { private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer(); private AssignAction underTest = new AssignAction(system2, userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(), new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), notificationManager, issueChangePostProcessor, issuesChangesSerializer), responseWriter); private WsActionTester ws = new WsActionTester(underTest); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/BulkChangeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/BulkChangeActionTest.java index 07d0d2af52f..9797f60a958 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/BulkChangeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/BulkChangeActionTest.java @@ -60,6 +60,7 @@ import org.sonar.server.issue.workflow.FunctionExecutor; import org.sonar.server.issue.workflow.IssueWorkflow; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; @@ -115,7 +116,7 @@ public class BulkChangeActionTest { private IssueFieldsSetter issueFieldsSetter = new IssueFieldsSetter(); private IssueWorkflow issueWorkflow = new IssueWorkflow(new FunctionExecutor(issueFieldsSetter), issueFieldsSetter); private WebIssueStorage issueStorage = new WebIssueStorage(system2, dbClient, - new DefaultRuleFinder(dbClient), + new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), new IssueIndexer(es.client(), dbClient, new IssueIteratorFactory(dbClient), null), new SequenceUuidFactory()); private NotificationManager notificationManager = mock(NotificationManager.class); private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/DoTransitionActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/DoTransitionActionTest.java index c9d8180ca8b..8fefd3eaa06 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/DoTransitionActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/DoTransitionActionTest.java @@ -51,6 +51,7 @@ import org.sonar.server.issue.workflow.FunctionExecutor; import org.sonar.server.issue.workflow.IssueWorkflow; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; @@ -96,7 +97,7 @@ public class DoTransitionActionTest { private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor(); private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer(); private IssueUpdater issueUpdater = new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), mock(NotificationManager.class), issueChangePostProcessor, issuesChangesSerializer); private ArgumentCaptor preloadedSearchResponseDataCaptor = ArgumentCaptor.forClass(SearchResponseData.class); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueUpdaterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueUpdaterTest.java index a819ec58b3c..e334d80a7a3 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueUpdaterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/IssueUpdaterTest.java @@ -48,6 +48,7 @@ import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -84,7 +85,7 @@ public class IssueUpdaterTest { private TestIssueChangePostProcessor issueChangePostProcessor = new TestIssueChangePostProcessor(); private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer(); private IssueUpdater underTest = new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), notificationManager, + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), notificationManager, issueChangePostProcessor, issuesChangesSerializer); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetSeverityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetSeverityActionTest.java index 7ba6e06b156..3936efae996 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetSeverityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetSeverityActionTest.java @@ -50,6 +50,7 @@ import org.sonar.server.issue.index.IssueIteratorFactory; import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; @@ -88,7 +89,7 @@ public class SetSeverityActionTest { private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer(); private WsActionTester tester = new WsActionTester(new SetSeverityAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(), new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), mock(NotificationManager.class), issueChangePostProcessor, issuesChangesSerializer), responseWriter)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java index 63d70c9305e..b4585b5b519 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTagsActionTest.java @@ -53,6 +53,7 @@ import org.sonar.server.issue.index.IssueIteratorFactory; import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; @@ -90,7 +91,7 @@ public class SetTagsActionTest { private WsActionTester ws = new WsActionTester(new SetTagsAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(), new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), mock(NotificationManager.class), issueChangePostProcessor, issuesChangesSerializer), responseWriter)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTypeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTypeActionTest.java index 04418f2a760..d4b0cfb5cab 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTypeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SetTypeActionTest.java @@ -58,6 +58,7 @@ import org.sonar.server.issue.index.IssueIteratorFactory; import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer; import org.sonar.server.notification.NotificationManager; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.TestResponse; @@ -101,7 +102,7 @@ public class SetTypeActionTest { private IssuesChangesNotificationSerializer issuesChangesSerializer = new IssuesChangesNotificationSerializer(); private WsActionTester tester = new WsActionTester(new SetTypeAction(userSession, dbClient, new IssueFinder(dbClient, userSession), new IssueFieldsSetter(), new IssueUpdater(dbClient, - new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient), issueIndexer, new SequenceUuidFactory()), + new WebIssueStorage(system2, dbClient, new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)), issueIndexer, new SequenceUuidFactory()), mock(NotificationManager.class), issueChangePostProcessor, issuesChangesSerializer), responseWriter, system2)); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java index 0a188fbfe2e..f1bd898e541 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/QProfileExportersTest.java @@ -44,6 +44,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.NotFoundException; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import static java.nio.charset.StandardCharsets.UTF_8; @@ -65,7 +66,7 @@ public class QProfileExportersTest { @org.junit.Rule public DbTester db = DbTester.create(system2); - private final RuleFinder ruleFinder = new DefaultRuleFinder(db.getDbClient()); + private final RuleFinder ruleFinder = new DefaultRuleFinder(db.getDbClient(), mock(RuleDescriptionFormatter.class)); private final ProfileExporter[] exporters = new ProfileExporter[] { new StandardExporter(), new XooExporter()}; private final ProfileImporter[] importers = new ProfileImporter[] { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java index 729ded73f01..6de9225db6f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/RegisterQualityProfilesNotificationTest.java @@ -57,6 +57,7 @@ import org.sonar.server.qualityprofile.builtin.QProfileName; import org.sonar.server.qualityprofile.builtin.RuleActivator; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.ServerRuleFinder; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.tester.UserSessionRule; @@ -100,7 +101,7 @@ public class RegisterQualityProfilesNotificationTest { private TypeValidations typeValidations = mock(TypeValidations.class); private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class); private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class); - private ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient); + private ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)); private BuiltInQProfileInsert builtInQProfileInsert = new BuiltInQProfileInsertImpl(dbClient, ruleFinder, system2, UuidFactoryFast.getInstance(), typeValidations, activeRuleIndexer); private RuleActivator ruleActivator = new RuleActivator(system2, dbClient, typeValidations, userSessionRule); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileInsertImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileInsertImplTest.java index 300cdd7ba69..b35d37403d6 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileInsertImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileInsertImplTest.java @@ -45,6 +45,7 @@ import org.sonar.db.rule.RuleParamDto; import org.sonar.server.qualityprofile.ActiveRuleChange; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.ServerRuleFinder; import org.sonar.server.util.StringTypeValidation; import org.sonar.server.util.TypeValidations; @@ -65,7 +66,7 @@ public class BuiltInQProfileInsertImplTest { private final TypeValidations typeValidations = new TypeValidations(singletonList(new StringTypeValidation())); private final DbSession dbSession = db.getSession(); private final DbSession batchDbSession = db.getDbClient().openSession(true); - private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(db.getDbClient()); + private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(db.getDbClient(), mock(RuleDescriptionFormatter.class)); private final ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class); private final BuiltInQProfileInsertImpl underTest = new BuiltInQProfileInsertImpl(db.getDbClient(), ruleFinder, system2, uuidFactory, typeValidations, activeRuleIndexer); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImplTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImplTest.java index 4eb4ab9bc5e..41a929b8c57 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImplTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualityprofile/builtin/BuiltInQProfileRepositoryImplTest.java @@ -32,6 +32,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.server.language.LanguageTesting; import org.sonar.server.qualityprofile.builtin.BuiltInQProfile.ActiveRule; import org.sonar.server.rule.DefaultRuleFinder; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.ServerRuleFinder; import static java.util.Arrays.asList; @@ -52,7 +53,7 @@ public class BuiltInQProfileRepositoryImplTest { public DbTester db = DbTester.create(); private final DbClient dbClient = db.getDbClient(); - private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient); + private final ServerRuleFinder ruleFinder = new DefaultRuleFinder(dbClient, mock(RuleDescriptionFormatter.class)); @Test public void create_qprofile_with_rule() { diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java index 9f9d9d7bbb9..57f92598018 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java @@ -34,6 +34,7 @@ import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.rule.RuleCreator; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.text.MacroInterpreter; @@ -60,7 +61,6 @@ public class CreateActionTest { private System2 system2 = mock(System2.class); - @Rule public UserSessionRule userSession = UserSessionRule.standalone(); @@ -74,7 +74,7 @@ public class CreateActionTest { private WsActionTester ws = new WsActionTester(new CreateAction(db.getDbClient(), new RuleCreator(system2, new RuleIndexer(es.client(), db.getDbClient()), db.getDbClient(), newFullTypeValidations(), uuidFactory), - new RuleMapper(new Languages(), createMacroInterpreter()), + new RuleMapper(new Languages(), createMacroInterpreter(), new RuleDescriptionFormatter()), new RuleWsSupport(db.getDbClient(), userSession))); @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index 161e06d44a3..12aa783dcb4 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -54,6 +54,7 @@ import org.sonar.server.qualityprofile.QProfileRulesImpl; import org.sonar.server.qualityprofile.RuleActivation; import org.sonar.server.qualityprofile.builtin.RuleActivator; import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.tester.UserSessionRule; @@ -110,7 +111,7 @@ public class SearchActionTest { private final RuleQueryFactory ruleQueryFactory = new RuleQueryFactory(db.getDbClient()); private final MacroInterpreter macroInterpreter = mock(MacroInterpreter.class); private final QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class); - private final RuleMapper ruleMapper = new RuleMapper(languages, macroInterpreter); + private final RuleMapper ruleMapper = new RuleMapper(languages, macroInterpreter, new RuleDescriptionFormatter()); private final SearchAction underTest = new SearchAction(ruleIndex, activeRuleCompleter, ruleQueryFactory, db.getDbClient(), ruleMapper, new RuleWsSupport(db.getDbClient(), userSession)); private final TypeValidations typeValidations = new TypeValidations(asList(new StringTypeValidation(), new IntegerTypeValidation())); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java index 5d5949263aa..6da5721cfd2 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java @@ -38,6 +38,7 @@ import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.user.UserDto; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.text.MacroInterpreter; import org.sonar.server.ws.WsActionTester; @@ -82,7 +83,7 @@ public class ShowActionTest { private final MacroInterpreter macroInterpreter = mock(MacroInterpreter.class); private final Languages languages = new Languages(newLanguage("xoo", "Xoo")); private final WsActionTester ws = new WsActionTester( - new ShowAction(db.getDbClient(), new RuleMapper(languages, macroInterpreter), + new ShowAction(db.getDbClient(), new RuleMapper(languages, macroInterpreter, new RuleDescriptionFormatter()), new ActiveRuleCompleter(db.getDbClient(), languages), new RuleWsSupport(db.getDbClient(), userSession))); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java index 4e1818d62f6..ef3ed9d9032 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java @@ -29,14 +29,15 @@ import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbTester; -import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleDescriptionSectionDto; +import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.RuleUpdater; import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.tester.UserSessionRule; @@ -81,9 +82,10 @@ public class UpdateActionTest { private DbClient dbClient = db.getDbClient(); private EsClient esClient = es.client(); + private RuleDescriptionFormatter ruleDescriptionFormatter = new RuleDescriptionFormatter(); private Languages languages = new Languages(); - private RuleMapper mapper = new RuleMapper(languages, createMacroInterpreter()); + private RuleMapper mapper = new RuleMapper(languages, createMacroInterpreter(), ruleDescriptionFormatter); private RuleIndexer ruleIndexer = new RuleIndexer(esClient, dbClient); private UuidFactoryFast uuidFactory = UuidFactoryFast.getInstance(); diff --git a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java index 467040683db..6ccdff45a30 100644 --- a/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java +++ b/server/sonar-webserver/src/main/java/org/sonar/server/platform/platformlevel/PlatformLevel4.java @@ -210,6 +210,7 @@ import org.sonar.server.root.ws.RootWsModule; import org.sonar.server.rule.CommonRuleDefinitionsImpl; import org.sonar.server.rule.RuleCreator; import org.sonar.server.rule.RuleDefinitionsLoader; +import org.sonar.server.rule.RuleDescriptionFormatter; import org.sonar.server.rule.RuleUpdater; import org.sonar.server.rule.WebServerRuleFinderImpl; import org.sonar.server.rule.index.RuleIndexDefinition; @@ -285,6 +286,7 @@ public class PlatformLevel4 extends PlatformLevel { addIfStandalone(StandaloneRuleActivatorEventsDistributor.class); add( + RuleDescriptionFormatter.class, ClusterVerification.class, LogServerId.class, LogOAuthWarning.class, -- 2.39.5