diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2022-04-26 10:49:06 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-05-06 20:02:43 +0000 |
commit | 6bb200a692f7a200c0ca27900c24c8c89982e521 (patch) | |
tree | 6a4a5c604c60dcbfc555c541e29f8f5d1cab97a2 /server/sonar-db-dao | |
parent | 838c48d8e94d0d780582cdfb4429e5cb57615f6f (diff) | |
download | sonarqube-6bb200a692f7a200c0ca27900c24c8c89982e521.tar.gz sonarqube-6bb200a692f7a200c0ca27900c24c8c89982e521.zip |
SONAR-16302 Adapt ExportRuleDto to handle new structure
Diffstat (limited to 'server/sonar-db-dao')
6 files changed, 69 insertions, 22 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java index fcd08f0cd98..3795c115eb7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ExportRuleDto.java @@ -21,23 +21,30 @@ package org.sonar.db.qualityprofile; import java.util.LinkedList; import java.util.List; +import java.util.Optional; +import java.util.Set; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; +import org.sonar.db.rule.RuleDescriptionSectionDto; import org.sonar.db.rule.SeverityUtil; +import static java.util.Optional.ofNullable; +import static org.sonar.db.rule.RuleDescriptionSectionDto.DEFAULT_KEY; + public class ExportRuleDto { private String activeRuleUuid = null; private String repository = null; private String rule = null; private String name = null; - private String description = null; private String extendedDescription = null; private String template = null; private Integer severity = null; private Integer type = null; private String tags = null; - private List<ExportRuleParamDto> params; + private List<ExportRuleParamDto> params = null; + + private Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = null; public boolean isCustomRule() { return template != null; @@ -71,10 +78,6 @@ public class ExportRuleDto { return tags; } - public String getDescription() { - return description; - } - public String getName() { return name; } @@ -89,4 +92,17 @@ public class ExportRuleDto { void setParams(List<ExportRuleParamDto> params) { this.params = params; } + + public Set<RuleDescriptionSectionDto> getRuleDescriptionSections() { + return ruleDescriptionSectionDtos; + } + + public Optional<RuleDescriptionSectionDto> getDefaultRuleDescriptionSectionDto() { + return findExistingSectionWithSameKey(DEFAULT_KEY); + } + + private Optional<RuleDescriptionSectionDto> findExistingSectionWithSameKey(String ruleDescriptionSectionKey) { + return ofNullable(ruleDescriptionSectionDtos).flatMap(sections -> + sections.stream().filter(section -> section.getKey().equals(ruleDescriptionSectionKey)).findAny()); + } } 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 444f0e64522..208987e3f75 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 @@ -25,7 +25,7 @@ import java.util.StringJoiner; import static org.sonar.api.utils.Preconditions.checkArgument; public class RuleDescriptionSectionDto { - static final String DEFAULT_KEY = "default"; + public static final String DEFAULT_KEY = "default"; private final String uuid; private final String key; diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml index 5b1a7825a80..4cea9e1e39b 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileExportMapper.xml @@ -3,14 +3,25 @@ <mapper namespace="org.sonar.db.qualityprofile.QualityProfileExportMapper"> + <sql id="selectRuleDescriptionSectionColumns"> + rds.uuid as "rds_uuid", + rds.kee as "rds_kee", + rds.description as "rds_description", + </sql> + + <sql id="leftOuterJoinRulesDescriptionSections"> + left outer join rule_desc_sections rds on + rds.rule_uuid = r.uuid + </sql> + <sql id="exportRuleColumns"> + <include refid="selectRuleDescriptionSectionColumns"/> a.uuid as "activeRuleUuid", a.failure_level as "severity", r.plugin_rule_key as "rule", r.plugin_name as "repository", r.priority as "defaultSeverity", r.name, - r.description, r.description_format as "descriptionFormat", r.rule_type as "type", rt.plugin_rule_key as "template", @@ -24,7 +35,27 @@ p.value as value </sql> - <select id="selectByProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.ExportRuleDto"> + <resultMap id="ruleDefinitionResultMap" type="org.sonar.db.qualityprofile.ExportRuleDto"> + <id property="activeRuleUuid" column="activeRuleUuid"/> + <result property="ruleKey" column="ruleKey"/> + <result property="severity" column="severity"/> + <result property="rule" column="rule"/> + <result property="repository" column="repository"/> + <result property="name" column="name"/> + <result property="type" column="type"/> + <result property="template" column="template"/> + <result property="extendedDescription" column="extendedDescription"/> + <result property="tags" column="tags"/> + + <collection property="ruleDescriptionSectionDtos" ofType="org.sonar.db.rule.RuleDescriptionSectionDto"> + <id property="uuid" column="rds_uuid"/> + <result property="key" column="rds_kee"/> + <result property="description" column="rds_description" typeHandler="org.apache.ibatis.type.StringTypeHandler"/> + </collection> + + </resultMap> + + <select id="selectByProfileUuid" parameterType="string" resultMap="ruleDefinitionResultMap"> select <include refid="exportRuleColumns"/> from active_rules a @@ -33,6 +64,7 @@ inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED' left join rules rt on rt.uuid = r.template_uuid left join rules_metadata rm on rm.rule_uuid = r.uuid + <include refid="leftOuterJoinRulesDescriptionSections"/> where oqp.uuid = #{id, jdbcType=VARCHAR} </select> 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 bb07883486c..685b9be1a72 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 @@ -85,7 +85,7 @@ <result property="createdAtFromDefinition" column="createdAtFromDefinition"/> <result property="updatedAtFromDefinition" column="updatedAtFromDefinition"/> - <result property="noteData" column="noteData"/> + <result property="noteData" column="noteData" typeHandler="org.apache.ibatis.type.StringTypeHandler"/> <result property="noteUserUuid" column="noteUserUuid"/> <result property="noteCreatedAt" column="noteCreatedAt"/> <result property="noteUpdatedAt" column="noteUpdatedAt"/> @@ -94,7 +94,7 @@ <result property="remediationBaseEffort" column="remediationBaseEffort"/> <result property="tagsField" column="tagsField"/> <result property="adHocName" column="adHocName"/> - <result property="adHocDescription" column="adHocDescription"/> + <result property="adHocDescription" column="adHocDescription" typeHandler="org.apache.ibatis.type.StringTypeHandler"/> <result property="adHocSeverity" column="adHocSeverity"/> <result property="adHocType" column="adHocType"/> <result property="createdAtFromMetadata" column="createdAtFromMetadata"/> @@ -127,7 +127,7 @@ <collection property="ruleDescriptionSectionDtos" ofType="org.sonar.db.rule.RuleDescriptionSectionDto"> <id property="uuid" column="rds_uuid"/> <result property="key" column="rds_kee"/> - <result property="description" column="rds_description"/> + <result property="description" column="rds_description" typeHandler="org.apache.ibatis.type.StringTypeHandler"/> </collection> </resultMap> @@ -160,7 +160,7 @@ <collection property="ruleDescriptionSectionDtos" ofType="org.sonar.db.rule.RuleDescriptionSectionDto"> <id property="uuid" column="rds_uuid"/> <result property="key" column="rds_kee"/> - <result property="description" column="rds_description"/> + <result property="description" column="rds_description" typeHandler="org.apache.ibatis.type.StringTypeHandler"/> </collection> </resultMap> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java index fb9efff61ed..5e4912e4e64 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileExportDaoTest.java @@ -36,7 +36,6 @@ import org.sonar.api.rules.RuleType; import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.rule.RuleDefinitionDto; -import org.sonar.db.rule.RuleDescriptionSectionDto; import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; @@ -74,12 +73,14 @@ public class QualityProfileExportDaoTest { String language = "java"; RuleDefinitionDto ruleTemplate = createRule(language); RuleDefinitionDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid()); + var customRuleDescription = customRule.getDefaultRuleDescriptionSectionDto().getDescription(); RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto() .setRuleUuid(customRule.getUuid()) .setNoteData("Extended description") .setTags(Sets.newHashSet("tag1", "tag2", "tag3"))); RuleDefinitionDto rule = createRule(language, RuleStatus.READY, null); + var ruleDescription = rule.getDefaultRuleDescriptionSectionDto().getDescription(); RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto() .setRuleUuid(rule.getUuid())); QProfileDto profile = createProfile(language); @@ -95,8 +96,7 @@ public class QualityProfileExportDaoTest { assertThat(exportCustomRuleDto).isNotNull(); assertThat(exportCustomRuleDto.isCustomRule()).isTrue(); assertThat(exportCustomRuleDto.getParams()).isEmpty(); - //FIXME SONAR-16314 - assertThat(exportCustomRuleDto.getDescription()).isEqualTo(customRule.getRuleDescriptionSectionDtos().stream().map(RuleDescriptionSectionDto::getDescription).collect(Collectors.joining())); + assertThat(exportCustomRuleDto.getRuleDescriptionSections().iterator().next().getDescription()).isEqualTo(customRuleDescription); assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRuleMetadata.getNoteData()); assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName()); assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey()); @@ -112,8 +112,7 @@ public class QualityProfileExportDaoTest { assertThat(exportRuleDto).isNotNull(); assertThat(exportRuleDto.isCustomRule()).isFalse(); assertThat(exportRuleDto.getParams()).isEmpty(); - //FIXME SONAR-16314 - assertThat(exportRuleDto.getDescription()).isEqualTo(rule.getRuleDescriptionSectionDtos().stream().map(RuleDescriptionSectionDto::getDescription).collect(Collectors.joining())); + assertThat(exportRuleDto.getRuleDescriptionSections().iterator().next().getDescription()).isEqualTo(ruleDescription); assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData()); assertThat(exportRuleDto.getName()).isEqualTo(rule.getName()); assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey()); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java index 28f8fc3d219..833f932a8b2 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java @@ -65,16 +65,16 @@ public class RuleTesting { } public static RuleDefinitionDto newRule(RuleKey key) { - RuleDefinitionDto ruleDefinitionDto = newRuleWithoutSection(key); + RuleDefinitionDto ruleDefinitionDto = newRuleWithoutDescriptionSection(key); ruleDefinitionDto.addRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "description_" + randomAlphabetic(5))); return ruleDefinitionDto; } - public static RuleDefinitionDto newRuleWithoutSection() { - return newRuleWithoutSection(randomRuleKey()); + public static RuleDefinitionDto newRuleWithoutDescriptionSection() { + return newRuleWithoutDescriptionSection(randomRuleKey()); } - public static RuleDefinitionDto newRuleWithoutSection(RuleKey ruleKey) { + public static RuleDefinitionDto newRuleWithoutDescriptionSection(RuleKey ruleKey) { return new RuleDefinitionDto() .setRepositoryKey(ruleKey.repository()) .setRuleKey(ruleKey.rule()) |