aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao/src
diff options
context:
space:
mode:
authorLéo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com>2022-04-27 10:53:10 +0200
committersonartech <sonartech@sonarsource.com>2022-05-06 20:02:43 +0000
commit63c72abe86c9b5a9fa15a40406ede811128d075c (patch)
treed611d907ce780e4a36a6c9938f5c4dfbe52e8a81 /server/sonar-db-dao/src
parentc333b3ae6984bf1b402d3398fa71de7f0a75b817 (diff)
downloadsonarqube-63c72abe86c9b5a9fa15a40406ede811128d075c.tar.gz
sonarqube-63c72abe86c9b5a9fa15a40406ede811128d075c.zip
SONAR-16302 add management of indexing for rules description sections
Diffstat (limited to 'server/sonar-db-dao/src')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java18
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java41
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml74
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java19
4 files changed, 121 insertions, 31 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java
index 76dc0684d9e..444f0e64522 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java
@@ -19,6 +19,7 @@
*/
package org.sonar.db.rule;
+import java.util.Objects;
import java.util.StringJoiner;
import static org.sonar.api.utils.Preconditions.checkArgument;
@@ -73,6 +74,23 @@ public class RuleDescriptionSectionDto {
.toString();
}
+ @Override
+ public boolean equals(Object o) {
+ if (this == o) {
+ return true;
+ }
+ if (o == null || getClass() != o.getClass()) {
+ return false;
+ }
+ RuleDescriptionSectionDto that = (RuleDescriptionSectionDto) o;
+ return Objects.equals(uuid, that.uuid) && Objects.equals(key, that.key) && Objects.equals(description, that.description);
+ }
+
+ @Override
+ public int hashCode() {
+ return Objects.hash(uuid, key, description);
+ }
+
public static final class RuleDescriptionSectionDtoBuilder {
private String uuid;
private String key = null;
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java
index afb8a4af448..58b88632d1a 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java
@@ -19,19 +19,23 @@
*/
package org.sonar.db.rule;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Optional;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rules.RuleType;
+import static org.sonar.db.rule.RuleDescriptionSectionDto.DEFAULT_KEY;
+
public class RuleForIndexingDto {
private String uuid;
private String repository;
private String pluginRuleKey;
private String name;
- private String description;
private RuleDto.Format descriptionFormat;
private Integer severity;
private RuleStatus status;
@@ -48,6 +52,8 @@ public class RuleForIndexingDto {
private long createdAt;
private long updatedAt;
+ private Set<RuleDescriptionSectionDto> ruleDescriptionSectionsDtos = new HashSet<>();
+
public RuleForIndexingDto() {
// nothing to do here
}
@@ -60,22 +66,30 @@ public class RuleForIndexingDto {
return repository;
}
+ public void setRepository(String repository) {
+ this.repository = repository;
+ }
+
public String getPluginRuleKey() {
return pluginRuleKey;
}
- public String getName() {
- return name;
+ public void setPluginRuleKey(String pluginRuleKey) {
+ this.pluginRuleKey = pluginRuleKey;
}
- public String getDescription() {
- return description;
+ public String getName() {
+ return name;
}
public RuleDto.Format getDescriptionFormat() {
return descriptionFormat;
}
+ public void setDescriptionFormat(RuleDto.Format descriptionFormat) {
+ this.descriptionFormat = descriptionFormat;
+ }
+
public Integer getSeverity() {
return severity;
}
@@ -144,4 +158,21 @@ public class RuleForIndexingDto {
public RuleKey getRuleKey() {
return RuleKey.of(repository, pluginRuleKey);
}
+
+ public Set<RuleDescriptionSectionDto> getRuleDescriptionSectionsDtos() {
+ return ruleDescriptionSectionsDtos;
+ }
+
+ public void setRuleDescriptionSectionsDtos(Set<RuleDescriptionSectionDto> ruleDescriptionSectionsDtos) {
+ this.ruleDescriptionSectionsDtos = ruleDescriptionSectionsDtos;
+ }
+
+ private Optional<RuleDescriptionSectionDto> findExistingSectionWithSameKey(String ruleDescriptionSectionKey) {
+ return ruleDescriptionSectionsDtos.stream().filter(section -> section.getKey().equals(ruleDescriptionSectionKey)).findAny();
+ }
+
+ @CheckForNull
+ public RuleDescriptionSectionDto getDefaultRuleDescriptionSectionDto() {
+ return findExistingSectionWithSameKey(DEFAULT_KEY).orElse(null);
+ }
}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
index 0188bf511d8..bef6fde79b5 100644
--- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
@@ -83,22 +83,22 @@
<resultMap id="ruleResultMap" type="org.sonar.db.rule.RuleDto">
<id property="uuid" column="r_uuid"/>
- <result property="createdAtFromDefinition" column="createdAtFromDefinition" />
- <result property="updatedAtFromDefinition" column="updatedAtFromDefinition" />
- <result property="noteData" column="noteData" />
- <result property="noteUserUuid" column="noteUserUuid" />
- <result property="noteCreatedAt" column="noteCreatedAt" />
- <result property="noteUpdatedAt" column="noteUpdatedAt" />
- <result property="remediationFunction" column="remediationFunction" />
- <result property="remediationGapMultiplier" column="remediationGapMultiplier" />
- <result property="remediationBaseEffort" column="remediationBaseEffort" />
- <result property="tagsField" column="tagsField" />
- <result property="adHocName" column="adHocName" />
- <result property="adHocDescription" column="adHocDescription" />
- <result property="adHocSeverity" column="adHocSeverity" />
- <result property="adHocType" column="adHocType" />
- <result property="createdAtFromMetadata" column="createdAtFromMetadata" />
- <result property="updatedAtFromMetadata" column="updatedAtFromMetadata" />
+ <result property="createdAtFromDefinition" column="createdAtFromDefinition"/>
+ <result property="updatedAtFromDefinition" column="updatedAtFromDefinition"/>
+ <result property="noteData" column="noteData"/>
+ <result property="noteUserUuid" column="noteUserUuid"/>
+ <result property="noteCreatedAt" column="noteCreatedAt"/>
+ <result property="noteUpdatedAt" column="noteUpdatedAt"/>
+ <result property="remediationFunction" column="remediationFunction"/>
+ <result property="remediationGapMultiplier" column="remediationGapMultiplier"/>
+ <result property="remediationBaseEffort" column="remediationBaseEffort"/>
+ <result property="tagsField" column="tagsField"/>
+ <result property="adHocName" column="adHocName"/>
+ <result property="adHocDescription" column="adHocDescription"/>
+ <result property="adHocSeverity" column="adHocSeverity"/>
+ <result property="adHocType" column="adHocType"/>
+ <result property="createdAtFromMetadata" column="createdAtFromMetadata"/>
+ <result property="updatedAtFromMetadata" column="updatedAtFromMetadata"/>
<result property="ruleKey" column="ruleKey"/>
<result property="repositoryKey" column="repositoryKey"/>
@@ -165,6 +165,33 @@
</resultMap>
+ <resultMap id="ruleForIndexingDtoResultMap" type="org.sonar.db.rule.RuleForIndexingDto">
+ <result property="uuid" column="uuid"/>
+ <result property="repository" column="repository"/>
+ <result property="pluginRuleKey" column="pluginRuleKey"/>
+ <result property="name" column="name"/>
+ <result property="descriptionFormat" column="descriptionFormat"/>
+ <result property="severity" column="severity"/>
+ <result property="status" column="status"/>
+ <result property="isTemplate" column="isTemplate"/>
+ <result property="isExternal" column="isExternal"/>
+ <result property="systemTags" column="systemTags"/>
+ <result property="securityStandards" column="securityStandards"/>
+ <result property="templateRuleKey" column="templateRuleKey"/>
+ <result property="templateRepository" column="templateRepository"/>
+ <result property="internalKey" column="internalKey"/>
+ <result property="language" column="language"/>
+ <result property="type" column="type"/>
+ <result property="createdAt" column="createdAt"/>
+ <result property="updatedAt" column="updatedAt"/>
+ <result property="tags" column="tags"/>
+ <collection property="ruleDescriptionSectionsDtos" ofType="org.sonar.db.rule.RuleDescriptionSectionDto">
+ <id property="uuid" column="rds_uuid"/>
+ <result property="key" column="rds_kee"/>
+ <result property="description" column="rds_description"/>
+ </collection>
+ </resultMap>
+
<select id="selectAllDefinitions" resultMap="ruleDefinitionResultMap">
select
<include refid="selectRuleTableColumns"/>
@@ -361,7 +388,7 @@
</foreach>
</select>
- <select id="selectIndexingRulesByUuids" parameterType="map" resultType="org.sonar.db.rule.RuleForIndexingDto">
+ <select id="selectIndexingRulesByUuids" parameterType="map" resultMap="ruleForIndexingDtoResultMap">
<include refid="sqlSelectIndexingRules"/>
where
<foreach collection="ruleUuids" index="index" item="ruleUuid" open="" separator=" or " close="">
@@ -370,10 +397,10 @@
order by r.created_at asc
</select>
- <select id="scrollIndexingRules" resultType="org.sonar.db.rule.RuleForIndexingDto" fetchSize="${_scrollFetchSize}"
- resultSetType="FORWARD_ONLY">
+ <select id="scrollIndexingRules" resultMap="ruleForIndexingDtoResultMap" fetchSize="${_scrollFetchSize}"
+ resultSetType="FORWARD_ONLY" resultOrdered="true">
<include refid="sqlSelectIndexingRules"/>
- order by r.created_at asc
+ order by r.created_at,r.uuid asc
</select>
<sql id="sqlSelectIndexingRules">
@@ -396,10 +423,15 @@
r.rule_type as "type",
r.created_at as "createdAt",
r.updated_at as "updatedAt",
- rm.tags as "tags"
+ rm.tags as "tags",
+ rds.uuid as "rds_uuid",
+ rds.kee as "rds_kee",
+ rds.description as "rds_description"
from rules r
left outer join rules t on t.uuid = r.template_uuid
left outer join rules_metadata rm on r.uuid = rm.rule_uuid
+ left outer join rule_desc_sections rds on
+ rds.rule_uuid = r.uuid
</sql>
<select id="selectByQuery" parameterType="map" resultMap="ruleResultMap">
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
index c68c59ad0bf..d1b7857156f 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
@@ -20,6 +20,7 @@
package org.sonar.db.rule;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.MoreCollectors;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
@@ -833,7 +834,14 @@ public class RuleDaoTest {
@Test
public void scrollIndexingRules() {
Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>();
- RuleDefinitionDto r1 = db.rules().insert();
+ RuleDescriptionSectionDto ruleDescriptionSectionDto = RuleDescriptionSectionDto.builder()
+ .key("DESC")
+ .uuid("uuid")
+ .description("my description")
+ .build();
+ RuleDefinitionDto r1 = db.rules().insert(r -> {
+ r.addRuleDescriptionSectionDto(ruleDescriptionSectionDto);
+ });
RuleDefinitionDto r2 = db.rules().insert(r -> r.setIsExternal(true));
underTest.scrollIndexingRules(db.getSession(), accumulator);
@@ -847,8 +855,10 @@ public class RuleDaoTest {
assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
assertThat(firstRule.getName()).isEqualTo(r1.getName());
- //FIXME SONAR-16309
- //assertThat(firstRule.getDescription()).isEqualTo(r1.getRuleDescriptionSectionDtos().stream().map(RuleDescriptionSectionDto::getDescription).collect(Collectors.joining()));
+ assertThat(firstRule.getRuleDescriptionSectionsDtos().stream()
+ .filter(s -> s.getKey().equals(ruleDescriptionSectionDto.getKey()))
+ .collect(MoreCollectors.onlyElement()))
+ .isEqualTo(ruleDescriptionSectionDto);
assertThat(firstRule.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
assertThat(firstRule.getSeverity()).isEqualTo(r1.getSeverity());
assertThat(firstRule.getStatus()).isEqualTo(r1.getStatus());
@@ -927,8 +937,7 @@ public class RuleDaoTest {
assertThat(firstRule.getRepository()).isEqualTo(r1.getRepositoryKey());
assertThat(firstRule.getPluginRuleKey()).isEqualTo(r1.getRuleKey());
assertThat(firstRule.getName()).isEqualTo(r1.getName());
- //FIXME SONAR-16309
- //assertThat(firstRule.getDescription()).isEqualTo(r1.getRuleDescriptionSectionDtos().stream().map(RuleDescriptionSectionDto::getDescription).collect(Collectors.joining()));
+ assertThat(firstRule.getRuleDescriptionSectionsDtos()).isEqualTo(r1.getRuleDescriptionSectionDtos());
assertThat(firstRule.getDescriptionFormat()).isEqualTo(r1.getDescriptionFormat());
assertThat(firstRule.getSeverity()).isEqualTo(r1.getSeverity());
assertThat(firstRule.getSeverityAsString()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(r1.getSeverity()));