aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorAntoine Vinot <antoine.vinot@sonarsource.com>2022-06-27 20:14:29 +0200
committersonartech <sonartech@sonarsource.com>2022-06-30 20:03:10 +0000
commit4eb7b91c56a57096ae44f64b34311cdeaac9b501 (patch)
tree026d6c460be40c0e17188ab25e68ec827c584e22 /server/sonar-server-common
parentbcbc1e6729920d62d198d0683dbd9d92d5f65a04 (diff)
downloadsonarqube-4eb7b91c56a57096ae44f64b34311cdeaac9b501.tar.gz
sonarqube-4eb7b91c56a57096ae44f64b34311cdeaac9b501.zip
SONAR-16518 Remove replaceRuleDescriptionSectionDtos method
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java25
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexTest.java50
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java43
3 files changed, 48 insertions, 70 deletions
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 26fa5c76cd4..2c706afb7eb 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
@@ -24,10 +24,11 @@ import org.sonar.db.rule.RuleDescriptionSectionContextDto;
import org.sonar.db.rule.RuleDescriptionSectionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleForIndexingDto;
-import org.sonar.db.rule.RuleTesting;
import org.sonar.server.security.SecurityStandards;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.rule.RuleTesting.newRule;
+import static org.sonar.db.rule.RuleTesting.newRuleWithoutDescriptionSection;
import static org.sonar.markdown.Markdown.convertToHtml;
import static org.sonar.server.security.SecurityStandards.fromSecurityStandards;
@@ -35,7 +36,7 @@ public class RuleDocTest {
@Test
public void ruleDocOf_mapsFieldCorrectly() {
- RuleDto ruleDto = RuleTesting.newRule();
+ RuleDto ruleDto = newRule();
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
ruleForIndexingDto.setTemplateRuleKey("templateKey");
ruleForIndexingDto.setTemplateRepository("repoKey");
@@ -72,9 +73,8 @@ public class RuleDocTest {
@Test
public void ruleDocOf_whenGivenNoHtmlSections_hasEmptyStringInHtmlDescription() {
- RuleDto ruleDto = RuleTesting.newRule();
+ RuleDto ruleDto = newRuleWithoutDescriptionSection();
ruleDto.setDescriptionFormat(RuleDto.Format.HTML);
- ruleDto.getRuleDescriptionSectionDtos().clear();
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
@@ -85,17 +85,12 @@ public class RuleDocTest {
@Test
public void ruleDocOf_whenGivenMultipleHtmlSections_hasConcatenationInHtmlDescription() {
- RuleDto ruleDto = RuleTesting.newRule();
- ruleDto.setDescriptionFormat(RuleDto.Format.HTML);
- ruleDto.getRuleDescriptionSectionDtos().clear();
RuleDescriptionSectionDto section1 = buildRuleDescriptionSectionDto("section1", "<p>html content 1</p>");
RuleDescriptionSectionDto section2 = buildRuleDescriptionSectionDto("section2", "<p>html content 2</p>");
RuleDescriptionSectionDto section3ctx1 = buildRuleDescriptionSectionDtoWithContext("section3", "<p>html content 3.1</p>", "ctx1");
RuleDescriptionSectionDto section3ctx2 = buildRuleDescriptionSectionDtoWithContext("section3", "<p>html content 3.2</p>", "ctx2");
- ruleDto.addRuleDescriptionSectionDto(section1);
- ruleDto.addRuleDescriptionSectionDto(section2);
- ruleDto.addRuleDescriptionSectionDto(section3ctx1);
- ruleDto.addRuleDescriptionSectionDto(section3ctx2);
+ RuleDto ruleDto = newRule(section1, section2, section3ctx1, section3ctx2);
+ ruleDto.setDescriptionFormat(RuleDto.Format.HTML);
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
@@ -111,13 +106,11 @@ public class RuleDocTest {
@Test
public void ruleDocOf_whenGivenMultipleMarkdownSections_transformToHtmlAndConcatenatesInHtmlDescription() {
- RuleDto ruleDto = RuleTesting.newRule();
- ruleDto.setDescriptionFormat(RuleDto.Format.MARKDOWN);
- ruleDto.getRuleDescriptionSectionDtos().clear();
RuleDescriptionSectionDto section1 = buildRuleDescriptionSectionDto("section1", "*html content 1*");
RuleDescriptionSectionDto section2 = buildRuleDescriptionSectionDto("section2", "*html content 2*");
- ruleDto.addRuleDescriptionSectionDto(section1);
- ruleDto.addRuleDescriptionSectionDto(section2);
+
+ RuleDto ruleDto = newRule(section1, section2);
+ ruleDto.setDescriptionFormat(RuleDto.Format.MARKDOWN);
RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto);
SecurityStandards securityStandards = fromSecurityStandards(ruleDto.getSecurityStandards());
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 94f38180b32..7ee6a96160a 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
@@ -65,6 +65,7 @@ import static org.sonar.api.rules.RuleType.CODE_SMELL;
import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT;
import static org.sonar.api.rules.RuleType.VULNERABILITY;
import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
+import static org.sonar.db.rule.RuleTesting.newRule;
import static org.sonar.db.rule.RuleTesting.setCreatedAt;
import static org.sonar.db.rule.RuleTesting.setIsExternal;
import static org.sonar.db.rule.RuleTesting.setIsTemplate;
@@ -93,18 +94,18 @@ import static org.sonar.server.security.SecurityStandards.SANS_TOP_25_RISKY_RESO
public class RuleIndexTest {
- private System2 system2 = new AlwaysIncreasingSystem2();
+ private final System2 system2 = new AlwaysIncreasingSystem2();
@Rule
public EsTester es = EsTester.create();
@Rule
public DbTester db = DbTester.create(system2);
- private RuleIndexer ruleIndexer = new RuleIndexer(es.client(), db.getDbClient());
- private ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db.getDbClient(), es.client());
+ private final RuleIndexer ruleIndexer = new RuleIndexer(es.client(), db.getDbClient());
+ private final ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db.getDbClient(), es.client());
- private RuleIndex underTest = new RuleIndex(es.client(), system2);
- private UuidFactory uuidFactory = UuidFactoryFast.getInstance();
+ private final RuleIndex underTest = new RuleIndex(es.client(), system2);
+ private final UuidFactory uuidFactory = UuidFactoryFast.getInstance();
@Test
public void search_all_rules() {
@@ -214,22 +215,11 @@ public class RuleIndexTest {
public void search_content_by_query() {
// it's important to set all the fields being used by the search (name, desc, key, lang, ...),
// otherwise the generated random values may raise false-positives
- RuleDto rule1 = createJavaRule(rule -> rule.setRuleKey("123")
- .setName("rule 123")
- .replaceRuleDescriptionSectionDtos(createDefaultRuleDescriptionSection(uuidFactory.create(), "My great rule CWE-123 which makes your code 1000 times better!")));
- RuleDto rule2 = createJavaRule(rule -> rule.setRuleKey("124")
- .setName("rule 124")
- .replaceRuleDescriptionSectionDtos(createDefaultRuleDescriptionSection(uuidFactory.create(), "Another great and shiny rule CWE-124")));
- RuleDto rule3 = createJavaRule(rule -> rule.setRuleKey("1000")
- .setName("rule 1000")
- .replaceRuleDescriptionSectionDtos(createDefaultRuleDescriptionSection(uuidFactory.create(), "Another great rule CWE-1000")));
- RuleDto rule4 = createJavaRule(rule -> rule.setRuleKey("404")
- .setName("rule 404")
- .replaceRuleDescriptionSectionDtos(createDefaultRuleDescriptionSection(uuidFactory.create(),
- "<h1>HTML-Geeks</h1><p style=\"color:blue\">special formatting!</p><table><tr><td>inside</td><td>tables</td></tr></table>")));
- RuleDto rule5 = createJavaRule(rule -> rule.setRuleKey("405")
- .setName("rule 405")
- .replaceRuleDescriptionSectionDtos(createDefaultRuleDescriptionSection(uuidFactory.create(), "internationalization missunderstandings alsdkjfnadklsjfnadkdfnsksdjfn")));
+ RuleDto rule1 = insertJavaRule("My great rule CWE-123 which makes your code 1000 times better!", "123", "rule 123");
+ RuleDto rule2 = insertJavaRule("Another great and shiny rule CWE-124", "124", "rule 124");
+ RuleDto rule3 = insertJavaRule("Another great rule CWE-1000", "1000", "rule 1000");
+ RuleDto rule4 = insertJavaRule("<h1>HTML-Geeks</h1><p style=\"color:blue\">special formatting!</p><table><tr><td>inside</td><td>tables</td></tr></table>", "404", "rule 404");
+ RuleDto rule5 = insertJavaRule("internationalization missunderstandings alsdkjfnadklsjfnadkdfnsksdjfn", "405", "rule 405");
index();
// partial match at word boundary
@@ -269,6 +259,14 @@ public class RuleIndexTest {
assertThat(underTest.search(new RuleQuery().setQueryText("internationalizationBlaBla"), new SearchOptions()).getUuids()).isEmpty();
}
+ private RuleDto insertJavaRule(String description, String ruleKey, String name) {
+ RuleDto javaRule = newRule(createDefaultRuleDescriptionSection(uuidFactory.create(), description))
+ .setLanguage("java")
+ .setRuleKey(ruleKey)
+ .setName(name);
+ return db.rules().insert(javaRule);
+ }
+
@Test
public void search_by_any_of_repositories() {
RuleDto findbugs = createRule(
@@ -555,7 +553,7 @@ public class RuleIndexTest {
}
@SafeVarargs
- private final RuleDto createRule(Consumer<RuleDto>... consumers) {
+ private RuleDto createRule(Consumer<RuleDto>... consumers) {
return db.rules().insert(consumers);
}
@@ -563,10 +561,6 @@ public class RuleIndexTest {
return createRule(r -> r.setLanguage("java"));
}
- private RuleDto createJavaRule(Consumer<RuleDto> consumer) {
- return createRule(r -> r.setLanguage("java"), consumer);
- }
-
@Test
public void search_by_any_of_severities() {
createRule(setSeverity(BLOCKER));
@@ -592,7 +586,7 @@ public class RuleIndexTest {
@Test
public void search_by_any_of_statuses() {
- RuleDto beta = createRule(setStatus(RuleStatus.BETA));
+ createRule(setStatus(RuleStatus.BETA));
RuleDto ready = createRule(setStatus(RuleStatus.READY));
index();
@@ -710,7 +704,7 @@ public class RuleIndexTest {
public void search_by_activation_and_severity() {
RuleDto major = createRule(setSeverity(MAJOR));
RuleDto minor = createRule(setSeverity(MINOR));
- RuleDto info = createRule(setSeverity(INFO));
+ createRule(setSeverity(INFO));
QProfileDto profile1 = createJavaProfile();
QProfileDto profile2 = createJavaProfile();
db.qualityProfiles().activateRule(profile1, major, ar -> ar.setSeverity(BLOCKER));
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java
index 348a1f4c3d3..1bb3603be73 100644
--- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleIndexerTest.java
@@ -23,6 +23,11 @@ import com.google.common.collect.ImmutableSet;
import com.tngtech.java.junit.dataprovider.DataProvider;
import com.tngtech.java.junit.dataprovider.DataProviderRunner;
import com.tngtech.java.junit.dataprovider.UseDataProvider;
+import java.util.EnumSet;
+import java.util.List;
+import java.util.Random;
+import java.util.Set;
+import java.util.stream.Stream;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -38,25 +43,19 @@ import org.sonar.db.DbTester;
import org.sonar.db.rule.RuleDescriptionSectionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleDto.Scope;
-import org.sonar.db.rule.RuleTesting;
import org.sonar.server.es.EsTester;
import org.sonar.server.security.SecurityStandards;
import org.sonar.server.security.SecurityStandards.SQCategory;
-import java.util.EnumSet;
-import java.util.List;
-import java.util.Random;
-import java.util.Set;
-import java.util.stream.IntStream;
-import java.util.stream.Stream;
-
import static com.google.common.collect.Sets.newHashSet;
import static java.lang.String.format;
import static java.util.Collections.emptyList;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toSet;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection;
+import static org.sonar.db.rule.RuleTesting.newRule;
import static org.sonar.server.rule.index.RuleIndexDefinition.TYPE_RULE;
import static org.sonar.server.security.SecurityStandards.CWES_BY_SQ_CATEGORY;
import static org.sonar.server.security.SecurityStandards.SQ_CATEGORY_KEYS_ORDERING;
@@ -85,10 +84,10 @@ public class RuleIndexerTest {
@Rule
public LogTester logTester = new LogTester();
- private DbClient dbClient = dbTester.getDbClient();
+ private final DbClient dbClient = dbTester.getDbClient();
private final RuleIndexer underTest = new RuleIndexer(es.client(), dbClient);
- private DbSession dbSession = dbTester.getSession();
- private RuleDto rule = new RuleDto()
+ private final DbSession dbSession = dbTester.getSession();
+ private final RuleDto rule = new RuleDto()
.setUuid("rule-uuid")
.setRuleKey("S001")
.setRepositoryKey("xoo")
@@ -137,10 +136,9 @@ public class RuleIndexerTest {
@Test
public void index_long_rule_description() {
- String description = IntStream.range(0, 100000).map(i -> i % 100).mapToObj(Integer::toString).collect(joining(" "));
- RuleDescriptionSectionDto ruleDescriptionSectionDto = createDefaultRuleDescriptionSection(uuidFactory.create(), description);
+ RuleDescriptionSectionDto ruleDescriptionSectionDto = createDefaultRuleDescriptionSection(uuidFactory.create(), randomAlphanumeric(100000));
+ RuleDto rule = dbTester.rules().insert(newRule(ruleDescriptionSectionDto));
- RuleDto rule = dbTester.rules().insert(r -> r.replaceRuleDescriptionSectionDtos(ruleDescriptionSectionDto));
underTest.commitAndIndex(dbTester.getSession(), rule.getUuid());
assertThat(es.countDocuments(TYPE_RULE)).isOne();
@@ -148,10 +146,7 @@ public class RuleIndexerTest {
@Test
public void index_long_rule_with_several_sections() {
- RuleDto rule = dbTester.rules().insert(r -> {
- r.replaceRuleDescriptionSectionDtos(RULE_DESCRIPTION_SECTION_DTO);
- r.addRuleDescriptionSectionDto(RULE_DESCRIPTION_SECTION_DTO2);
- });
+ RuleDto rule = dbTester.rules().insert(newRule(RULE_DESCRIPTION_SECTION_DTO, RULE_DESCRIPTION_SECTION_DTO2));
underTest.commitAndIndex(dbTester.getSession(), rule.getUuid());
@@ -165,10 +160,7 @@ public class RuleIndexerTest {
@Test
public void index_long_rule_with_section_in_markdown() {
- RuleDto rule = dbTester.rules().insert(r -> {
- r.setDescriptionFormat(RuleDto.Format.MARKDOWN);
- r.replaceRuleDescriptionSectionDtos(RULE_DESCRIPTION_SECTION_DTO);
- });
+ RuleDto rule = dbTester.rules().insert(newRule(RULE_DESCRIPTION_SECTION_DTO).setDescriptionFormat(RuleDto.Format.MARKDOWN));
underTest.commitAndIndex(dbTester.getSession(), rule.getUuid());
@@ -186,10 +178,9 @@ public class RuleIndexerTest {
.flatMap(t -> CWES_BY_SQ_CATEGORY.get(t).stream().map(e -> "cwe:" + e))
.collect(toSet());
SecurityStandards securityStandards = SecurityStandards.fromSecurityStandards(standards);
- RuleDto rule = dbTester.rules().insert(RuleTesting.newRuleWithoutDescriptionSection()
+ RuleDto rule = dbTester.rules().insert(newRule(RULE_DESCRIPTION_SECTION_DTO)
.setType(RuleType.SECURITY_HOTSPOT)
- .setSecurityStandards(standards)
- .addRuleDescriptionSectionDto(RULE_DESCRIPTION_SECTION_DTO));
+ .setSecurityStandards(standards));
underTest.commitAndIndex(dbTester.getSession(), rule.getUuid());
assertThat(logTester.getLogs()).hasSize(1);
@@ -214,7 +205,7 @@ public class RuleIndexerTest {
SQCategory sqCategory1 = sqCategories.toArray(new SQCategory[0])[random.nextInt(sqCategories.size())];
sqCategories.remove(sqCategory1);
SQCategory sqCategory2 = sqCategories.toArray(new SQCategory[0])[random.nextInt(sqCategories.size())];
- return new Object[][] {
+ return new Object[][]{
{sqCategory1, sqCategory2}
};
}