diff options
Diffstat (limited to 'server/sonar-db-dao/src')
14 files changed, 299 insertions, 730 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java index 9678de4dbfa..e243bca7cdd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java @@ -19,7 +19,6 @@ */ package org.sonar.db.rule; -import com.google.common.annotations.VisibleForTesting; import java.util.Collection; import java.util.Collections; import java.util.List; @@ -56,10 +55,6 @@ public class RuleDao implements Dao { return Optional.ofNullable(mapper(session).selectByKey(key)); } - public Optional<RuleMetadataDto> selectMetadataByKey(DbSession session, RuleKey key) { - return Optional.ofNullable(mapper(session).selectMetadataByKey(key)); - } - public RuleDto selectOrFailByKey(DbSession session, RuleKey key) { return Optional.ofNullable(mapper(session).selectByKey(key)) .orElseThrow(() -> new RowNotFoundException(String.format("Rule with key '%s' does not exist", key))); @@ -103,14 +98,12 @@ public class RuleDao implements Dao { checkNotNull(ruleDto.getUuid(), "RuleDto has no 'uuid'."); RuleMapper mapper = mapper(session); mapper.insertRule(ruleDto); - insertOrUpdateRuleMetadata(session, ruleDto.getMetadata()); updateRuleDescriptionSectionDtos(ruleDto, mapper); } public void update(DbSession session, RuleDto ruleDto) { RuleMapper mapper = mapper(session); mapper.updateRule(ruleDto); - insertOrUpdateRuleMetadata(session, ruleDto.getMetadata()); updateRuleDescriptionSectionDtos(ruleDto, mapper); } @@ -124,17 +117,6 @@ public class RuleDao implements Dao { .forEach(section -> mapper.insertRuleDescriptionSection(ruleDto.getUuid(), section)); } - @VisibleForTesting - void insertOrUpdateRuleMetadata(DbSession session, RuleMetadataDto ruleMetadataDto) { - if (ruleMetadataDto.isUndefined()) { - mapper(session).deleteMetadata(ruleMetadataDto.getRuleUuid()); - } else if (mapper(session).countMetadata(ruleMetadataDto) > 0) { - mapper(session).updateMetadata(ruleMetadataDto); - } else { - mapper(session).insertMetadata(ruleMetadataDto); - } - } - public void scrollIndexingRuleExtensionsByIds(DbSession dbSession, Collection<String> ruleExtensionIds, Consumer<RuleExtensionForIndexingDto> consumer) { RuleMapper mapper = mapper(dbSession); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java index a353fc695a7..eda4dc4a533 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java @@ -21,11 +21,13 @@ package org.sonar.db.rule; import com.google.common.base.Splitter; import com.google.common.collect.ImmutableSet; +import java.util.Arrays; import java.util.Collection; import java.util.HashSet; import java.util.Objects; import java.util.Optional; import java.util.Set; +import java.util.TreeSet; import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.StringUtils; @@ -50,70 +52,85 @@ public class RuleDto { private static final Splitter SPLITTER = Splitter.on(',').trimResults().omitEmptyStrings(); - private String uuid; - private String repositoryKey; - private String ruleKey; + private String uuid = null; + private String repositoryKey = null; + private String ruleKey = null; private Set<RuleDescriptionSectionDto> ruleDescriptionSectionDtos = new HashSet<>(); /** * Description format can be null on external rule, otherwise it should never be null */ - private RuleDto.Format descriptionFormat; - private RuleStatus status; - private String name; - private String configKey; + private RuleDto.Format descriptionFormat = null; + private RuleStatus status = null; + private String name = null; + private String configKey = null; /** * Severity can be null on external rule, otherwise it should never be null */ - private Integer severity; + private Integer severity = null; - private boolean isTemplate; + private boolean isTemplate = false; /** * This flag specify that this is an external rule, meaning that generated issues from this rule will be provided by the analyzer without being activated on a quality profile. */ - private boolean isExternal; + private boolean isExternal = false; /** * When an external rule is defined as ad hoc, it means that it's not defined using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}. * As the opposite, an external rule not being defined as ad hoc is declared by using {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository(String, String)}. * This flag is only used for external rules (it can only be set to true for when {@link #isExternal()} is true) */ - private boolean isAdHoc; + private boolean isAdHoc = false; + + private String language = null; + private String templateUuid = null; + private String defRemediationFunction = null; + private String defRemediationGapMultiplier = null; + private String defRemediationBaseEffort = null; + private String gapDescription = null; + private String systemTagsField = null; + private String securityStandardsField = null; + private int type = 0; + private Scope scope = null; + + private RuleKey key = null; + + private String pluginKey = null; + private String noteData = null; + private String noteUserUuid = null; + private Long noteCreatedAt = null; + private Long noteUpdatedAt = null; + private String remediationFunction = null; + private String remediationGapMultiplier = null; + private String remediationBaseEffort = null; + private String tags = null; - private String language; - private String templateUuid; - private String defRemediationFunction; - private String defRemediationGapMultiplier; - private String defRemediationBaseEffort; - private String gapDescription; - private String systemTagsField; - private String securityStandardsField; - private int type; - private Scope scope; - - private RuleKey key; - - private String pluginKey; - - private long createdAt; - private long updatedAt; - - private final RuleMetadataDto metadata; + /** + * Name of on ad hoc rule. + */ + private String adHocName = null; - public RuleDto() { - this(new RuleMetadataDto()); - } + /** + * Optional description of on ad hoc rule. + */ + private String adHocDescription = null; - public RuleDto(RuleMetadataDto metadata) { - this.metadata = metadata; - } + /** + * Severity of on ad hoc rule. + * When {@link RuleDto#isAdHoc()} is true, this field should always be set + */ + private String adHocSeverity = null; - public RuleMetadataDto getMetadata() { - return metadata; - } + /** + * Type of on ad hoc rule. + * When {@link RuleDto#isAdHoc()} is true, this field should always be set + */ + private Integer adHocType = null; + private long createdAt = 0; + private long updatedAt = 0; public RuleKey getKey() { if (key == null) { @@ -135,7 +152,6 @@ public class RuleDto { public RuleDto setUuid(String uuid) { this.uuid = uuid; - metadata.setRuleUuid(uuid); return this; } @@ -297,51 +313,6 @@ public class RuleDto { return this; } - @CheckForNull - public String getAdHocName() { - return metadata.getAdHocName(); - } - - public RuleDto setAdHocName(@Nullable String adHocName) { - metadata.setAdHocName(adHocName); - return this; - } - - @CheckForNull - public String getAdHocDescription() { - return metadata.getAdHocDescription(); - } - - public RuleDto setAdHocDescription(@Nullable String adHocDescription) { - metadata.setAdHocDescription(adHocDescription); - return this; - } - - @CheckForNull - public String getAdHocSeverity() { - return metadata.getAdHocSeverity(); - } - - public RuleDto setAdHocSeverity(@Nullable String adHocSeverity) { - metadata.setAdHocSeverity(adHocSeverity); - return this; - } - - @CheckForNull - public Integer getAdHocType() { - return metadata.getAdHocType(); - } - - public RuleDto setAdHocType(@Nullable Integer type) { - metadata.setAdHocType(type); - return this; - } - - public RuleDto setAdHocType(@Nullable RuleType adHocType) { - metadata.setAdHocType(adHocType); - return this; - } - public boolean isTemplate() { return isTemplate; } @@ -425,151 +396,203 @@ public class RuleDto { return this; } + @CheckForNull - public String getNoteData() { - return metadata.getNoteData(); + public String getDefRemediationFunction() { + return defRemediationFunction; } - public RuleDto setNoteData(@Nullable String s) { - metadata.setNoteData(s); + public RuleDto setDefRemediationFunction(@Nullable String defaultRemediationFunction) { + this.defRemediationFunction = defaultRemediationFunction; return this; } @CheckForNull - public String getNoteUserUuid() { - return metadata.getNoteUserUuid(); + public String getDefRemediationGapMultiplier() { + return defRemediationGapMultiplier; } - public RuleDto setNoteUserUuid(@Nullable String noteUserUuid) { - metadata.setNoteUserUuid(noteUserUuid); + public RuleDto setDefRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) { + this.defRemediationGapMultiplier = defaultRemediationGapMultiplier; return this; } @CheckForNull - public Long getNoteCreatedAt() { - return metadata.getNoteCreatedAt(); + public String getDefRemediationBaseEffort() { + return defRemediationBaseEffort; } - public RuleDto setNoteCreatedAt(@Nullable Long noteCreatedAt) { - metadata.setNoteCreatedAt(noteCreatedAt); + public RuleDto setDefRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) { + this.defRemediationBaseEffort = defaultRemediationBaseEffort; return this; } @CheckForNull - public Long getNoteUpdatedAt() { - return metadata.getNoteUpdatedAt(); + public String getGapDescription() { + return gapDescription; } - public RuleDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) { - metadata.setNoteUpdatedAt(noteUpdatedAt); + public RuleDto setGapDescription(@Nullable String s) { + this.gapDescription = s; return this; } + public static Set<String> deserializeTagsString(@Nullable String tags) { + return deserializeStringSet(tags); + } + + public static Set<String> deserializeSecurityStandardsString(@Nullable String securityStandards) { + return deserializeStringSet(securityStandards); + } + + private static Set<String> deserializeStringSet(@Nullable String str) { + if (str == null || str.isEmpty()) { + return emptySet(); + } + + return ImmutableSet.copyOf(SPLITTER.split(str)); + } @CheckForNull - public String getDefRemediationFunction() { - return defRemediationFunction; + public String getNoteData() { + return noteData; } - public RuleDto setDefRemediationFunction(@Nullable String defaultRemediationFunction) { - this.defRemediationFunction = defaultRemediationFunction; + public RuleDto setNoteData(@Nullable String s) { + this.noteData = s; return this; } @CheckForNull - public String getDefRemediationGapMultiplier() { - return defRemediationGapMultiplier; + public String getNoteUserUuid() { + return noteUserUuid; } - public RuleDto setDefRemediationGapMultiplier(@Nullable String defaultRemediationGapMultiplier) { - this.defRemediationGapMultiplier = defaultRemediationGapMultiplier; + public RuleDto setNoteUserUuid(@Nullable String noteUserUuid) { + this.noteUserUuid = noteUserUuid; return this; } @CheckForNull - public String getDefRemediationBaseEffort() { - return defRemediationBaseEffort; + public Long getNoteCreatedAt() { + return noteCreatedAt; } - public RuleDto setDefRemediationBaseEffort(@Nullable String defaultRemediationBaseEffort) { - this.defRemediationBaseEffort = defaultRemediationBaseEffort; + public RuleDto setNoteCreatedAt(@Nullable Long noteCreatedAt) { + this.noteCreatedAt = noteCreatedAt; return this; } @CheckForNull - public String getGapDescription() { - return gapDescription; + public Long getNoteUpdatedAt() { + return noteUpdatedAt; } - public RuleDto setGapDescription(@Nullable String s) { - this.gapDescription = s; + public RuleDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) { + this.noteUpdatedAt = noteUpdatedAt; return this; } @CheckForNull public String getRemediationFunction() { - return metadata.getRemediationFunction(); + return remediationFunction; } public RuleDto setRemediationFunction(@Nullable String remediationFunction) { - metadata.setRemediationFunction(remediationFunction); + this.remediationFunction = remediationFunction; return this; } @CheckForNull public String getRemediationGapMultiplier() { - return metadata.getRemediationGapMultiplier(); + return remediationGapMultiplier; } public RuleDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) { - metadata.setRemediationGapMultiplier(remediationGapMultiplier); + this.remediationGapMultiplier = remediationGapMultiplier; return this; } @CheckForNull public String getRemediationBaseEffort() { - return metadata.getRemediationBaseEffort(); + return remediationBaseEffort; } public RuleDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) { - metadata.setRemediationBaseEffort(remediationBaseEffort); + this.remediationBaseEffort = remediationBaseEffort; return this; } public Set<String> getTags() { - return metadata.getTags(); + return tags == null ? new HashSet<>() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ','))); } - /** - * Used in MyBatis mapping. - */ - private void setTagsField(String s) { - metadata.setTagsField(s); + String getTagsAsString() { + return tags; } - public static Set<String> deserializeTagsString(@Nullable String tags) { - return deserializeStringSet(tags); + public RuleDto setTags(Set<String> tags) { + String raw = tags.isEmpty() ? null : String.join(",", tags); + checkArgument(raw == null || raw.length() <= 4000, "Rule tags are too long: %s", raw); + this.tags = raw; + return this; } - public static Set<String> deserializeSecurityStandardsString(@Nullable String securityStandards) { - return deserializeStringSet(securityStandards); + private String getTagsField() { + return tags; } - private static Set<String> deserializeStringSet(@Nullable String str) { - if (str == null || str.isEmpty()) { - return emptySet(); - } + void setTagsField(String s) { + tags = s; + } - return ImmutableSet.copyOf(SPLITTER.split(str)); + @CheckForNull + public String getAdHocName() { + return adHocName; } - public RuleDto setTags(Set<String> tags) { - this.metadata.setTags(tags); + public RuleDto setAdHocName(@Nullable String adHocName) { + this.adHocName = adHocName; + return this; + } + + @CheckForNull + public String getAdHocDescription() { + return adHocDescription; + } + + public RuleDto setAdHocDescription(@Nullable String adHocDescription) { + this.adHocDescription = adHocDescription; + return this; + } + + @CheckForNull + public String getAdHocSeverity() { + return adHocSeverity; + } + + public RuleDto setAdHocSeverity(@Nullable String adHocSeverity) { + this.adHocSeverity = adHocSeverity; + return this; + } + + @CheckForNull + public Integer getAdHocType() { + return adHocType; + } + + public RuleDto setAdHocType(@Nullable Integer adHocType) { + this.adHocType = adHocType; + return this; + } + + public RuleDto setAdHocType(@Nullable RuleType adHocType) { + setAdHocType(adHocType != null ? adHocType.getDbConstant() : null); return this; } private static String serializeStringSet(@Nullable Set<String> strings) { - return strings == null || strings.isEmpty() ? null : StringUtils.join(strings, ','); + return strings == null || strings.isEmpty() ? null : String.join(",", strings); } @Override 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 50b92cd20c1..165be4c2580 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 @@ -71,7 +71,7 @@ public class RuleForIndexingDto { ruleForIndexingDto.status = r.getStatus(); ruleForIndexingDto.isTemplate = r.isTemplate(); ruleForIndexingDto.systemTags = Sets.newHashSet(r.getSystemTags()); - ruleForIndexingDto.tags = r.getMetadata() != null ? Sets.newHashSet(r.getMetadata().getTags()) : Collections.emptySet(); + ruleForIndexingDto.tags = r.getTags() != null ? Sets.newHashSet(r.getTags()) : Collections.emptySet(); ruleForIndexingDto.securityStandards = Sets.newHashSet(r.getSecurityStandards()); ruleForIndexingDto.internalKey = r.getConfigKey(); ruleForIndexingDto.language = r.getLanguage(); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java index 56ce1bbdd41..6c7ca31c2ff 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java @@ -38,8 +38,6 @@ public interface RuleMapper { RuleDto selectByKey(@Param("ruleKey") RuleKey ruleKey); - RuleMetadataDto selectMetadataByKey(@Param("ruleKey") RuleKey ruleKey); - List<RuleDto> selectByKeys(@Param("ruleKeys") List<RuleKey> keys); List<RuleExtensionForIndexingDto> selectIndexingRuleExtensionsByIds(@Param("ruleExtensionIds") List<String> ruleExtensionIds); @@ -56,14 +54,6 @@ public interface RuleMapper { void deleteRuleDescriptionSection(String ruleUuid); - int countMetadata(RuleMetadataDto ruleMetadataDto); - - void insertMetadata(RuleMetadataDto ruleMetadataDto); - - void deleteMetadata(@Param("uuid") String uuid); - - void updateMetadata(RuleMetadataDto ruleMetadataDto); - List<RuleParamDto> selectParamsByRuleUuids(@Param("ruleUuids") List<String> ruleUuids); List<RuleParamDto> selectParamsByRuleKey(RuleKey ruleKey); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java deleted file mode 100644 index cf3c620cf19..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java +++ /dev/null @@ -1,241 +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.db.rule; - -import java.util.Arrays; -import java.util.HashSet; -import java.util.Set; -import java.util.TreeSet; -import javax.annotation.CheckForNull; -import javax.annotation.Nullable; -import org.apache.commons.lang.StringUtils; -import org.sonar.api.rules.RuleType; - -import static com.google.common.base.Preconditions.checkArgument; - -public class RuleMetadataDto { - private String ruleUuid; - private String noteData; - private String noteUserUuid; - private Long noteCreatedAt; - private Long noteUpdatedAt; - private String remediationFunction; - private String remediationGapMultiplier; - private String remediationBaseEffort; - private String tags; - - /** - * Name of on ad hoc rule. - */ - private String adHocName; - - /** - * Optional description of on ad hoc rule. - */ - private String adHocDescription; - - /** - * Severity of on ad hoc rule. - * When {@link RuleDto#isAdHoc()} is true, this field should always be set - */ - private String adHocSeverity; - - /** - * Type of on ad hoc rule. - * When {@link RuleDto#isAdHoc()} is true, this field should always be set - */ - private Integer adHocType; - - public RuleMetadataDto() { - // nothing to do here - } - - public String getRuleUuid() { - return ruleUuid; - } - - public RuleMetadataDto setRuleUuid(String ruleUuid) { - this.ruleUuid = ruleUuid; - return this; - } - - @CheckForNull - public String getNoteData() { - return noteData; - } - - public RuleMetadataDto setNoteData(@Nullable String s) { - this.noteData = s; - return this; - } - - @CheckForNull - public String getNoteUserUuid() { - return noteUserUuid; - } - - public RuleMetadataDto setNoteUserUuid(@Nullable String noteUserUuid) { - this.noteUserUuid = noteUserUuid; - return this; - } - - @CheckForNull - public Long getNoteCreatedAt() { - return noteCreatedAt; - } - - public RuleMetadataDto setNoteCreatedAt(@Nullable Long noteCreatedAt) { - this.noteCreatedAt = noteCreatedAt; - return this; - } - - @CheckForNull - public Long getNoteUpdatedAt() { - return noteUpdatedAt; - } - - public RuleMetadataDto setNoteUpdatedAt(@Nullable Long noteUpdatedAt) { - this.noteUpdatedAt = noteUpdatedAt; - return this; - } - - @CheckForNull - public String getRemediationFunction() { - return remediationFunction; - } - - public RuleMetadataDto setRemediationFunction(@Nullable String remediationFunction) { - this.remediationFunction = remediationFunction; - return this; - } - - @CheckForNull - public String getRemediationGapMultiplier() { - return remediationGapMultiplier; - } - - public RuleMetadataDto setRemediationGapMultiplier(@Nullable String remediationGapMultiplier) { - this.remediationGapMultiplier = remediationGapMultiplier; - return this; - } - - @CheckForNull - public String getRemediationBaseEffort() { - return remediationBaseEffort; - } - - public RuleMetadataDto setRemediationBaseEffort(@Nullable String remediationBaseEffort) { - this.remediationBaseEffort = remediationBaseEffort; - return this; - } - - public Set<String> getTags() { - return tags == null ? new HashSet<>() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ','))); - } - - String getTagsAsString() { - return tags; - } - - public RuleMetadataDto setTags(Set<String> tags) { - String raw = tags.isEmpty() ? null : StringUtils.join(tags, ','); - checkArgument(raw == null || raw.length() <= 4000, "Rule tags are too long: %s", raw); - this.tags = raw; - return this; - } - - private String getTagsField() { - return tags; - } - - void setTagsField(String s) { - tags = s; - } - - @CheckForNull - public String getAdHocName() { - return adHocName; - } - - public RuleMetadataDto setAdHocName(@Nullable String adHocName) { - this.adHocName = adHocName; - return this; - } - - @CheckForNull - public String getAdHocDescription() { - return adHocDescription; - } - - public RuleMetadataDto setAdHocDescription(@Nullable String adHocDescription) { - this.adHocDescription = adHocDescription; - return this; - } - - @CheckForNull - public String getAdHocSeverity() { - return adHocSeverity; - } - - public RuleMetadataDto setAdHocSeverity(@Nullable String adHocSeverity) { - this.adHocSeverity = adHocSeverity; - return this; - } - - @CheckForNull - public Integer getAdHocType() { - return adHocType; - } - - public RuleMetadataDto setAdHocType(@Nullable Integer adHocType) { - this.adHocType = adHocType; - return this; - } - - public RuleMetadataDto setAdHocType(@Nullable RuleType adHocType) { - setAdHocType(adHocType != null ? adHocType.getDbConstant() : null); - return this; - } - - - - - - @Override - public String toString() { - return "RuleMetadataDto{" + - "ruleUuid=" + ruleUuid + - ", noteData='" + noteData + '\'' + - ", noteUserUuid='" + noteUserUuid + '\'' + - ", noteCreatedAt=" + noteCreatedAt + - ", noteUpdatedAt=" + noteUpdatedAt + - ", remediationFunction='" + remediationFunction + '\'' + - ", remediationGapMultiplier='" + remediationGapMultiplier + '\'' + - ", remediationBaseEffort='" + remediationBaseEffort + '\'' + - ", tags='" + tags + '\'' + - '}'; - } - - public boolean isUndefined() { - return StringUtils.isEmpty(noteData) && StringUtils.isEmpty(noteUserUuid) && noteCreatedAt == null && noteUpdatedAt == null && StringUtils.isEmpty(remediationFunction) && - StringUtils.isEmpty(remediationGapMultiplier) && StringUtils.isEmpty(remediationBaseEffort) && StringUtils.isEmpty(tags) - && StringUtils.isEmpty(adHocName) && StringUtils.isEmpty(adHocDescription) && StringUtils.isEmpty(adHocSeverity) && adHocType == null; - } -} 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 57d80943f23..b6934162212 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 @@ -25,9 +25,9 @@ r.name, r.description_format as "descriptionFormat", r.rule_type as "type", + r.note_data as "extendedDescription", + r.tags, rt.plugin_rule_key as "template", - rm.note_data as "extendedDescription", - rm.tags, rds.content as "description" </sql> @@ -60,7 +60,6 @@ inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid 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="leftOuterJoinRulesDefaultDescriptionSection"/> 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 f96f6862310..cf7ed03865c 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 @@ -37,23 +37,18 @@ r.scope, r.created_at as "createdAt", r.updated_at as "updatedAt", - rm.note_data as "noteData", - rm.note_user_uuid as "noteUserUuid", - rm.note_created_at as "noteCreatedAt", - rm.note_updated_at as "noteUpdatedAt", - rm.remediation_function as "remediationFunction", - rm.remediation_gap_mult as "remediationGapMultiplier", - rm.remediation_base_effort as "remediationBaseEffort", - rm.tags as "tagsField", - rm.ad_hoc_name as "adHocName", - rm.ad_hoc_description as "adHocDescription", - rm.ad_hoc_severity as "adHocSeverity", - rm.ad_hoc_type as "adHocType" - </sql> - - <sql id="outerJoinRulesMetadata"> - left outer join rules_metadata rm on - rm.rule_uuid = r.uuid + r.note_data as "noteData", + r.note_user_uuid as "noteUserUuid", + r.note_created_at as "noteCreatedAt", + r.note_updated_at as "noteUpdatedAt", + r.remediation_function as "remediationFunction", + r.remediation_gap_mult as "remediationGapMultiplier", + r.remediation_base_effort as "remediationBaseEffort", + r.tags as "tagsField", + r.ad_hoc_name as "adHocName", + r.ad_hoc_description as "adHocDescription", + r.ad_hoc_severity as "adHocSeverity", + r.ad_hoc_type as "adHocType" </sql> <select id="selectAll" resultMap="ruleResultMap"> @@ -61,7 +56,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> </select> @@ -81,7 +75,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where r.status != 'REMOVED' @@ -92,7 +85,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where r.uuid=#{uuid,jdbcType=VARCHAR} @@ -103,7 +95,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where <foreach collection="uuids" index="index" item="uuid" open="" separator=" or " close=""> @@ -116,7 +107,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR} @@ -136,12 +126,11 @@ r.uuid as "ruleUuid", r.plugin_name as "pluginName", r.plugin_rule_key as "pluginRuleKey", - rm.tags as "tags" + r.tags as "tags" from rules r - inner join rules_metadata rm on rm.rule_uuid = r.uuid where - rm.tags is not null and - rm.tags != '' + r.tags is not null and + r.tags != '' </sql> <sql id="sqlSelectIndexingRuleExtensions" databaseId="oracle"> @@ -149,42 +138,17 @@ r.uuid as "ruleUuid", r.plugin_name as "pluginName", r.plugin_rule_key as "pluginRuleKey", - rm.tags as "tags" + r.tags as "tags" from rules r - inner join rules_metadata rm on rm.rule_uuid = r.uuid where - rm.tags is not null + r.tags is not null </sql> - <select id="selectMetadataByKey" parameterType="map" resultType="org.sonar.db.rule.RuleMetadataDto"> - select - rm.rule_uuid as "ruleUuid", - rm.note_data as "noteData", - rm.note_user_uuid as "noteUserUuid", - rm.note_created_at as "noteCreatedAt", - rm.note_updated_at as "noteUpdatedAt", - rm.remediation_function as "remediationFunction", - rm.remediation_gap_mult as "remediationGapMultiplier", - rm.remediation_base_effort as "remediationBaseEffort", - rm.tags as "tagsField", - rm.ad_hoc_name as "adHocName", - rm.ad_hoc_description as "adHocDescription", - rm.ad_hoc_severity as "adHocSeverity", - rm.ad_hoc_type as "adHocType" - from - rules_metadata rm - inner join rules r on rm.rule_uuid = r.uuid - where - r.plugin_name=#{ruleKey.repository,jdbcType=VARCHAR} - and r.plugin_rule_key=#{ruleKey.rule,jdbcType=VARCHAR} - </select> - <select id="selectByKeys" parameterType="map" resultMap="ruleResultMap"> select <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where <foreach collection="ruleKeys" index="index" item="ruleKey" open="" separator=" or " close=""> @@ -197,7 +161,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where r.status != 'REMOVED' @@ -219,7 +182,6 @@ <include refid="selectJoinedTablesColumns"/> from rules r - <include refid="outerJoinRulesMetadata"/> <include refid="leftOuterJoinRulesDescriptionSections"/> where r.status != 'REMOVED' and r.is_external=${_false} and r.is_template=${_false} @@ -268,6 +230,18 @@ security_standards, rule_type, scope, + note_data, + note_user_uuid, + note_created_at, + note_updated_at, + remediation_function, + remediation_gap_mult, + remediation_base_effort, + tags, + ad_hoc_name, + ad_hoc_description, + ad_hoc_severity, + ad_hoc_type, created_at, updated_at ) @@ -294,6 +268,18 @@ #{securityStandardsField,jdbcType=VARCHAR}, #{type,jdbcType=TINYINT}, #{scope,jdbcType=VARCHAR}, + #{noteData,jdbcType=CLOB}, + #{noteUserUuid,jdbcType=VARCHAR}, + #{noteCreatedAt,jdbcType=BIGINT}, + #{noteUpdatedAt,jdbcType=BIGINT}, + #{remediationFunction,jdbcType=VARCHAR}, + #{remediationGapMultiplier,jdbcType=VARCHAR}, + #{remediationBaseEffort,jdbcType=VARCHAR}, + #{tagsField,jdbcType=VARCHAR}, + #{adHocName,jdbcType=VARCHAR}, + #{adHocDescription,jdbcType=CLOB}, + #{adHocSeverity,jdbcType=VARCHAR}, + #{adHocType,jdbcType=TINYINT}, #{createdAt,jdbcType=BIGINT}, #{updatedAt,jdbcType=BIGINT} ) @@ -322,62 +308,6 @@ security_standards=#{securityStandardsField,jdbcType=VARCHAR}, scope=#{scope,jdbcType=VARCHAR}, rule_type=#{type,jdbcType=TINYINT}, - updated_at=#{updatedAt,jdbcType=BIGINT} - where - uuid=#{uuid,jdbcType=VARCHAR} - </update> - - <delete id="deleteRuleDescriptionSection" parameterType="String"> - delete from - rule_desc_sections - where - rule_uuid=#{ruleUuid,jdbcType=VARCHAR} - </delete> - - <select id="countMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto" resultType="int"> - select - count(1) - from - rules_metadata rm - where - rm.rule_uuid=#{ruleUuid,jdbcType=VARCHAR} - </select> - - <insert id="insertMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto"> - insert into rules_metadata ( - rule_uuid, - note_data, - note_user_uuid, - note_created_at, - note_updated_at, - remediation_function, - remediation_gap_mult, - remediation_base_effort, - tags, - ad_hoc_name, - ad_hoc_description, - ad_hoc_severity, - ad_hoc_type - ) - values ( - #{ruleUuid,jdbcType=VARCHAR}, - #{noteData,jdbcType=CLOB}, - #{noteUserUuid,jdbcType=VARCHAR}, - #{noteCreatedAt,jdbcType=BIGINT}, - #{noteUpdatedAt,jdbcType=BIGINT}, - #{remediationFunction,jdbcType=VARCHAR}, - #{remediationGapMultiplier,jdbcType=VARCHAR}, - #{remediationBaseEffort,jdbcType=VARCHAR}, - #{tagsField,jdbcType=VARCHAR}, - #{adHocName,jdbcType=VARCHAR}, - #{adHocDescription,jdbcType=CLOB}, - #{adHocSeverity,jdbcType=VARCHAR}, - #{adHocType,jdbcType=TINYINT} - ) - </insert> - - <update id="updateMetadata" parameterType="org.sonar.db.rule.RuleMetadataDto"> - update rules_metadata set note_data=#{noteData,jdbcType=CLOB}, note_user_uuid=#{noteUserUuid,jdbcType=VARCHAR}, note_created_at=#{noteCreatedAt,jdbcType=BIGINT}, @@ -389,16 +319,17 @@ ad_hoc_name=#{adHocName,jdbcType=VARCHAR}, ad_hoc_description=#{adHocDescription,jdbcType=CLOB}, ad_hoc_severity=#{adHocSeverity,jdbcType=VARCHAR}, - ad_hoc_type=#{adHocType,jdbcType=TINYINT} + ad_hoc_type=#{adHocType,jdbcType=TINYINT}, + updated_at=#{updatedAt,jdbcType=BIGINT} where - rule_uuid=#{ruleUuid,jdbcType=VARCHAR} + uuid=#{uuid,jdbcType=VARCHAR} </update> - <delete id="deleteMetadata" parameterType="String"> + <delete id="deleteRuleDescriptionSection" parameterType="String"> delete from - rules_metadata + rule_desc_sections where - rule_uuid=#{uuid,jdbcType=INTEGER} + rule_uuid=#{ruleUuid,jdbcType=VARCHAR} </delete> <delete id="deleteParams" parameterType="String"> diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java index f6fe4d569fd..6298eb244bd 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueDaoTest.java @@ -497,7 +497,7 @@ public class IssueDaoTest { } private void prepareIssuesComponent() { - db.rules().insertRule(RULE.setIsExternal(true)); + db.rules().insert(RULE.setIsExternal(true)); ComponentDto projectDto = db.components().insertPrivateProject(t -> t.setUuid(PROJECT_UUID).setDbKey(PROJECT_KEY)); db.components().insertComponent(newFileDto(projectDto).setUuid(FILE_UUID).setDbKey(FILE_KEY)); } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java index 48eec38af1b..f756a75f13e 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/issue/IssueMapperTest.java @@ -77,7 +77,7 @@ public class IssueMapperTest { file2 = ComponentTesting.newFileDto(project, null).setUuid("file2 uuid"); dbTester.getDbClient().componentDao().insert(dbSession, file2); rule = RuleTesting.newXooX1(); - dbTester.rules().insertRule(rule); + dbTester.rules().insert(rule); dbSession.commit(); } 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 0c446f4f76d..873b23ff21f 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.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; import static org.assertj.core.api.Assertions.assertThat; @@ -73,16 +72,14 @@ public class QualityProfileExportDaoTest { String language = "java"; RuleDto ruleTemplate = createRule(language); RuleDto customRule = createRule(language, RuleStatus.READY, ruleTemplate.getUuid()); + customRule.setNoteData("Extended description") + .setTags(Sets.newHashSet("tag1", "tag2", "tag3")); + db.rules().update(customRule); + var customRuleContent = customRule.getDefaultRuleDescriptionSection().getContent(); - RuleMetadataDto customRuleMetadata = createRuleMetadata(new RuleMetadataDto() - .setRuleUuid(customRule.getUuid()) - .setNoteData("Extended description") - .setTags(Sets.newHashSet("tag1", "tag2", "tag3"))); RuleDto rule = createRule(language, RuleStatus.READY, null); var ruleContent = rule.getDefaultRuleDescriptionSection().getContent(); - RuleMetadataDto ruleMetadata = createRuleMetadata(new RuleMetadataDto() - .setRuleUuid(rule.getUuid())); QProfileDto profile = createProfile(language); List<ActiveRuleDto> activeRules = activate(profile, customRule, rule); @@ -97,11 +94,11 @@ public class QualityProfileExportDaoTest { assertThat(exportCustomRuleDto.isCustomRule()).isTrue(); assertThat(exportCustomRuleDto.getParams()).isEmpty(); assertThat(exportCustomRuleDto.getDescriptionOrThrow()).isEqualTo(customRuleContent); - assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRuleMetadata.getNoteData()); + assertThat(exportCustomRuleDto.getExtendedDescription()).isEqualTo(customRule.getNoteData()); assertThat(exportCustomRuleDto.getName()).isEqualTo(customRule.getName()); assertThat(exportCustomRuleDto.getRuleKey()).isEqualTo(customRule.getKey()); assertThat(exportCustomRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(customRule.getType())); - assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRuleMetadata.getTags())); + assertThat(exportCustomRuleDto.getTags()).isEqualTo(String.join(",", customRule.getTags())); assertThat(exportCustomRuleDto.getTemplateRuleKey()).isEqualTo(ruleTemplate.getKey()); ActiveRuleDto activeCustomRule = activeRules.stream().filter(activeRuleDto -> activeRuleDto.getRuleKey().equals(customRule.getKey())).findFirst().get(); @@ -113,7 +110,7 @@ public class QualityProfileExportDaoTest { assertThat(exportRuleDto.isCustomRule()).isFalse(); assertThat(exportRuleDto.getParams()).isEmpty(); assertThat(exportRuleDto.getDescriptionOrThrow()).isEqualTo(ruleContent); - assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(ruleMetadata.getNoteData()); + assertThat(exportRuleDto.getExtendedDescription()).isEqualTo(rule.getNoteData()); assertThat(exportRuleDto.getName()).isEqualTo(rule.getName()); assertThat(exportRuleDto.getRuleKey()).isEqualTo(rule.getKey()); assertThat(exportRuleDto.getRuleType()).isEqualTo(RuleType.valueOf(rule.getType())); @@ -219,10 +216,6 @@ public class QualityProfileExportDaoTest { .setTemplateUuid(templateUuid)); } - private RuleMetadataDto createRuleMetadata(RuleMetadataDto metadataDto) { - return db.rules().insertOrUpdateMetadata(metadataDto); - } - private QProfileDto createProfile(String lanugage) { return db.qualityProfiles().insert(p -> p.setLanguage(lanugage)); } 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 e859952d32f..dc4eb6fc52e 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 @@ -56,7 +56,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.assertj.core.api.Assertions.tuple; import static org.sonar.api.rule.RuleStatus.REMOVED; -import static org.sonar.db.rule.RuleTesting.newRuleMetadata; public class RuleDaoTest { private static final String UNKNOWN_RULE_UUID = "unknown-uuid"; @@ -69,14 +68,11 @@ public class RuleDaoTest { @Test public void selectByKey() { RuleDto ruleDto = db.rules().insert(); - RuleMetadataDto metadata = newRuleMetadata(ruleDto); - db.rules().insertRule(ruleDto, metadata); assertThat(underTest.selectByKey(db.getSession(), RuleKey.of("foo", "bar"))) .isEmpty(); - RuleDto rule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get(); - assertEquals(rule, ruleDto); - verifyMetadata(rule.getMetadata(), metadata); + RuleDto actualRule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get(); + assertEquals(actualRule, ruleDto); } @Test @@ -86,52 +82,19 @@ public class RuleDaoTest { assertThat(underTest.selectByKey(db.getSession(), ruleDto.getKey())).isNotEmpty(); } - @Test - public void selectByKey_returns_metadata() { - RuleDto ruleDto = db.rules().insert(); - RuleMetadataDto ruleMetadata = newRuleMetadata(ruleDto); - db.rules().insertRule(ruleDto, ruleMetadata); - - RuleDto rule = underTest.selectByKey(db.getSession(), ruleDto.getKey()).get(); - verifyMetadata(rule.getMetadata(), ruleMetadata); - } - - @Test - public void selectDefinitionByKey() { - RuleDto rule = db.rules().insert(); - - assertThat(underTest.selectByKey(db.getSession(), RuleKey.of("NOT", "FOUND"))).isEmpty(); - - Optional<RuleDto> reloaded = underTest.selectByKey(db.getSession(), rule.getKey()); - assertThat(reloaded).isPresent(); - } - @Test - public void selectMetadataByKey() { - RuleDto rule1 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule1); - - assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty(); - - Optional<RuleMetadataDto> rulesMetadata = underTest.selectMetadataByKey(db.getSession(), rule1.getKey()); - assertThat(rulesMetadata).isPresent(); - assertThat(rulesMetadata.get().getRuleUuid()).isEqualTo(rule1.getUuid()); - } @Test public void selectByUuid() { RuleDto ruleDto = db.rules().insert(); - RuleMetadataDto metadata = newRuleMetadata(ruleDto); - RuleDto expected = db.rules().insertRule(ruleDto, metadata); - assertThat(underTest.selectByUuid(expected.getUuid() + 500, db.getSession())).isEmpty(); - RuleDto rule = underTest.selectByUuid(expected.getUuid(), db.getSession()).get(); - assertEquals(rule, ruleDto); - verifyMetadata(rule.getMetadata(), metadata); + assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty(); + RuleDto actualRule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get(); + assertEquals(actualRule, ruleDto); } @Test - public void selectByUuidWithDifferentsValuesOfBooleans() { + public void selectByUuidWithDifferentValuesOfBooleans() { for (int i = 0; i < 3; i++) { int indexBoolean = i; RuleDto ruleDto = db.rules().insert((ruleDto1 -> { @@ -139,13 +102,10 @@ public class RuleDaoTest { ruleDto1.setIsExternal(indexBoolean == 1); ruleDto1.setIsAdHoc(indexBoolean == 2); })); - RuleMetadataDto metadata = newRuleMetadata(ruleDto); - RuleDto expected = db.rules().insertRule(ruleDto, metadata); - assertThat(underTest.selectByUuid(expected.getUuid() + 500, db.getSession())).isEmpty(); - RuleDto rule = underTest.selectByUuid(expected.getUuid(), db.getSession()).get(); + assertThat(underTest.selectByUuid(ruleDto.getUuid() + 500, db.getSession())).isEmpty(); + RuleDto rule = underTest.selectByUuid(ruleDto.getUuid(), db.getSession()).get(); assertEquals(rule, ruleDto); - verifyMetadata(rule.getMetadata(), metadata); } } @@ -161,11 +121,8 @@ public class RuleDaoTest { @Test public void selectByUuids() { RuleDto rule1 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule1); RuleDto rule2 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule2); RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED)); - db.rules().insertOrUpdateMetadata(removedRule); assertThat(underTest.selectByUuids(db.getSession(), singletonList(rule1.getUuid()))).hasSize(1); assertThat(underTest.selectByUuids(db.getSession(), asList(rule1.getUuid(), rule2.getUuid()))).hasSize(2); @@ -211,9 +168,7 @@ public class RuleDaoTest { @Test public void selectByKeys() { RuleDto rule1 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule1); - RuleDto rule2 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule2); + db.rules().insert(); assertThat(underTest.selectByKeys(db.getSession(), Collections.emptyList())).isEmpty(); assertThat(underTest.selectByKeys(db.getSession(), singletonList(RuleKey.of("NOT", "FOUND")))).isEmpty(); @@ -246,18 +201,6 @@ public class RuleDaoTest { .containsOnly(rule1.getUuid(), rule2.getUuid(), rule3.getUuid()); } - @Test - public void selectAll_returns_metadata() { - RuleDto ruleDto = db.rules().insert(); - RuleMetadataDto expected = newRuleMetadata(ruleDto); - db.rules().insertRule(ruleDto, expected); - - List<RuleDto> rules = underTest.selectAll(db.getSession()); - assertThat(rules).hasSize(1); - - verifyMetadata(rules.iterator().next().getMetadata(), expected); - } - private void assertEquals(RuleDto actual, RuleDto expected) { assertThat(actual.getUuid()).isEqualTo(expected.getUuid()); assertThat(actual.getRepositoryKey()).isEqualTo(expected.getRepositoryKey()); @@ -281,24 +224,21 @@ public class RuleDaoTest { assertThat(actual.getSecurityStandards()).isEqualTo(expected.getSecurityStandards()); assertThat(actual.getType()).isEqualTo(expected.getType()); assertThat(actual.getDescriptionFormat()).isEqualTo(expected.getDescriptionFormat()); + assertThat(actual.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort()); + assertThat(actual.getRemediationFunction()).isEqualTo(expected.getRemediationFunction()); + assertThat(actual.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier()); + assertThat(actual.getTags()).isEqualTo(expected.getTags()); + assertThat(actual.getNoteData()).isEqualTo(expected.getNoteData()); + assertThat(actual.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt()); + assertThat(actual.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt()); + assertThat(actual.getAdHocName()).isEqualTo(expected.getAdHocName()); + assertThat(actual.getAdHocDescription()).isEqualTo(expected.getAdHocDescription()); + assertThat(actual.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity()); + assertThat(actual.getAdHocType()).isEqualTo(expected.getAdHocType()); assertThat(actual.getRuleDescriptionSectionDtos()).usingRecursiveFieldByFieldElementComparator() .isEqualTo(expected.getRuleDescriptionSectionDtos()); } - private static void verifyMetadata(RuleMetadataDto metadata, RuleMetadataDto expected) { - assertThat(metadata.getRemediationBaseEffort()).isEqualTo(expected.getRemediationBaseEffort()); - assertThat(metadata.getRemediationFunction()).isEqualTo(expected.getRemediationFunction()); - assertThat(metadata.getRemediationGapMultiplier()).isEqualTo(expected.getRemediationGapMultiplier()); - assertThat(metadata.getTags()).isEqualTo(expected.getTags()); - assertThat(metadata.getNoteData()).isEqualTo(expected.getNoteData()); - assertThat(metadata.getNoteCreatedAt()).isEqualTo(expected.getNoteCreatedAt()); - assertThat(metadata.getNoteUpdatedAt()).isEqualTo(expected.getNoteUpdatedAt()); - assertThat(metadata.getAdHocName()).isEqualTo(expected.getAdHocName()); - assertThat(metadata.getAdHocDescription()).isEqualTo(expected.getAdHocDescription()); - assertThat(metadata.getAdHocSeverity()).isEqualTo(expected.getAdHocSeverity()); - assertThat(metadata.getAdHocType()).isEqualTo(expected.getAdHocType()); - } - @Test public void selectAllDefinitions() { RuleDto rule1 = db.rules().insert(); @@ -329,13 +269,11 @@ public class RuleDaoTest { .setConfigKey("S1") .setType(RuleType.VULNERABILITY) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule1); RuleDto rule2 = db.rules().insert( r -> r.setKey(RuleKey.of("js", "S002")) .setType(RuleType.SECURITY_HOTSPOT) .setLanguage("js")); - db.rules().insertOrUpdateMetadata(rule2); assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java"))) .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType) @@ -351,18 +289,16 @@ public class RuleDaoTest { @Test public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() { - RuleDto rule1 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S001")) .setConfigKey("S1") .setType(RuleType.VULNERABILITY) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule1); - RuleDto rule2 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("js", "S002")) .setType(RuleType.VULNERABILITY) .setLanguage("js")); - db.rules().insertOrUpdateMetadata(rule2); assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("cpp"))) .isEmpty(); @@ -370,24 +306,21 @@ public class RuleDaoTest { @Test public void selectByTypeAndLanguages_return_nothing_when_no_rule_with_type() { - RuleDto rule1 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S001")) .setConfigKey("S1") .setType(RuleType.VULNERABILITY) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule1); - RuleDto rule2 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S002")) .setType(RuleType.SECURITY_HOTSPOT) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule2); - RuleDto rule3 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S003")) .setType(RuleType.CODE_SMELL) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule3); assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.BUG.getDbConstant()), singletonList("java"))) .isEmpty(); @@ -395,13 +328,12 @@ public class RuleDaoTest { @Test public void selectByTypeAndLanguages_ignores_external_rules() { - RuleDto rule1 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S001")) .setConfigKey("S1") .setType(RuleType.VULNERABILITY) .setIsExternal(true) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule1); assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java"))) .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType) @@ -410,13 +342,12 @@ public class RuleDaoTest { @Test public void selectByTypeAndLanguages_ignores_template_rules() { - RuleDto rule1 = db.rules().insert( + db.rules().insert( r -> r.setKey(RuleKey.of("java", "S001")) .setConfigKey("S1") .setType(RuleType.VULNERABILITY) .setIsTemplate(true) .setLanguage("java")); - db.rules().insertOrUpdateMetadata(rule1); assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("java"))) .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType) @@ -425,11 +356,9 @@ public class RuleDaoTest { @Test public void select_by_query() { - RuleDto rule1 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1")); - db.rules().insertOrUpdateMetadata(rule1); - RuleDto rule2 = db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002"))); - db.rules().insertOrUpdateMetadata(rule2); - RuleDto removedRule = db.rules().insert(r -> r.setStatus(REMOVED)); + db.rules().insert(r -> r.setKey(RuleKey.of("java", "S001")).setConfigKey("S1")); + db.rules().insert(r -> r.setKey(RuleKey.of("java", "S002"))); + db.rules().insert(r -> r.setStatus(REMOVED)); assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create())).hasSize(2); assertThat(underTest.selectByQuery(db.getSession(), RuleQuery.create().withKey("S001"))).hasSize(1); @@ -663,21 +592,20 @@ public class RuleDaoTest { underTest.update(db.getSession(), rule); db.commit(); - assertThat(db.countRowsOfTable("RULES_METADATA")).isOne(); RuleDto ruleDtoAfterUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey()); - assertThat(ruleDtoAfterUpdate.getNoteData()).isNull(); - assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isNull(); - assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isNull(); - assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isNull(); - assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isNull(); - assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isNull(); - assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isNull(); - assertThat(ruleDtoAfterUpdate.getTags()).isEmpty(); - assertThat(ruleDtoAfterUpdate.getAdHocName()).isNull(); + assertThat(ruleDtoAfterUpdate.getNoteData()).isEqualTo(rule.getNoteData()); + assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isEqualTo(rule.getNoteUserUuid()); + assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isEqualTo(rule.getNoteCreatedAt()); + assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isEqualTo(rule.getNoteUpdatedAt()); + assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isEqualTo(rule.getRemediationFunction()); + assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isEqualTo(rule.getRemediationGapMultiplier()); + assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isEqualTo(rule.getRemediationBaseEffort()); + assertThat(ruleDtoAfterUpdate.getTags()).isEqualTo(rule.getTags()); + assertThat(ruleDtoAfterUpdate.getAdHocName()).isEqualTo(rule.getAdHocName()); assertThat(ruleDtoAfterUpdate.getAdHocDescription()).isEqualTo("ad-hoc-desc"); - assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isNull(); - assertThat(ruleDtoAfterUpdate.getAdHocType()).isNull(); - assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEmpty(); + assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity()); + assertThat(ruleDtoAfterUpdate.getAdHocType()).isEqualTo(rule.getAdHocType()); + assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEqualTo(rule.getSecurityStandards()); assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt()); assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(4_000_000_000_000L); @@ -914,7 +842,7 @@ public class RuleDaoTest { RuleForIndexingDto secondRule = findRuleForIndexingWithUuid(accumulator, r2.getUuid()); assertRuleDefinitionFieldsAreEquals(r1, firstRule); - assertRuleMetadataFieldsAreEquals(r1.getMetadata(), firstRule); + assertThat(r1.getTags()).isEqualTo(firstRule.getTags()); assertThat(firstRule.getTemplateRuleKey()).isNull(); assertThat(firstRule.getTemplateRepository()).isNull(); assertRuleDefinitionFieldsAreEquals(r2, secondRule); @@ -986,10 +914,6 @@ public class RuleDaoTest { assertThat(ruleForIndexing.getUpdatedAt()).isEqualTo(r1.getUpdatedAt()); } - private static void assertRuleMetadataFieldsAreEquals(RuleMetadataDto r1Metadatas, RuleForIndexingDto firstRule) { - assertThat(r1Metadatas.getTags()).isEqualTo(firstRule.getTags()); - } - @Test public void scrollIndexingRulesByKeys_scrolls_nothing_if_key_does_not_exist() { Accumulator<RuleForIndexingDto> accumulator = new Accumulator<>(); @@ -1004,17 +928,15 @@ public class RuleDaoTest { @Test public void scrollIndexingRuleExtensionsByIds() { Accumulator<RuleExtensionForIndexingDto> accumulator = new Accumulator<>(); - RuleDto r1 = db.rules().insert(); - RuleMetadataDto r1Extension = db.rules().insertOrUpdateMetadata(r1, r -> r.setTagsField("t1,t2")); - RuleDto r2 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(r2, r -> r.setTagsField("t1,t3")); + RuleDto r1 = db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t2")); + db.rules().insert(ruleDto -> ruleDto.setTagsField("t1,t3")); underTest.scrollIndexingRuleExtensionsByIds(db.getSession(), singletonList(r1.getUuid()), accumulator); assertThat(accumulator.list) .extracting(RuleExtensionForIndexingDto::getRuleUuid, RuleExtensionForIndexingDto::getRuleKey, RuleExtensionForIndexingDto::getTags) .containsExactlyInAnyOrder( - tuple(r1.getUuid(), r1.getKey(), r1Extension.getTagsAsString())); + tuple(r1.getUuid(), r1.getKey(), r1.getTagsAsString())); } @Test diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java index 6f7ed24fa85..e1d0ee61ee7 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDtoTest.java @@ -22,6 +22,7 @@ package org.sonar.db.rule; import com.google.common.collect.ImmutableSet; import java.util.Collections; import java.util.Set; +import java.util.TreeSet; import org.junit.Test; import org.sonar.core.util.Uuids; @@ -64,6 +65,28 @@ public class RuleDtoTest { } @Test + public void tags_are_joined_with_comma() { + Set<String> tags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag")); + RuleDto dto = new RuleDto().setTags(tags); + assertThat(dto.getTags()).isEqualTo(tags); + assertThat(dto.getTagsAsString()).isEqualTo("first_tag,second_tag,third_tag"); + } + + @Test + public void system_tags_are_joined_with_comma() { + Set<String> systemTags = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag")); + RuleDto dto = new RuleDto().setSystemTags(systemTags); + assertThat(dto.getSystemTags()).isEqualTo(systemTags); + } + + @Test + public void security_standards_are_joined_with_comma() { + Set<String> securityStandards = new TreeSet<>(Set.of("first_tag", "second_tag", "third_tag")); + RuleDto dto = new RuleDto().setSecurityStandards(securityStandards); + assertThat(dto.getSecurityStandards()).isEqualTo(securityStandards); + } + + @Test public void equals_is_based_on_uuid() { String uuid = Uuids.createFast(); RuleDto dto = newRule().setUuid(uuid); diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java index 0c09ee46243..4aadca4f202 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleDbTester.java @@ -26,7 +26,6 @@ import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.RuleType; import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; -import org.sonar.db.user.UserDto; import static java.util.Arrays.asList; import static org.sonar.api.rules.RuleType.SECURITY_HOTSPOT; @@ -126,26 +125,6 @@ public class RuleDbTester { } @SafeVarargs - public final RuleMetadataDto insertOrUpdateMetadata(RuleDto rule, Consumer<RuleMetadataDto>... populaters) { - RuleMetadataDto dto = RuleTesting.newRuleMetadata(rule); - asList(populaters).forEach(populater -> populater.accept(dto)); - return insertOrUpdateMetadata(dto); - } - - @SafeVarargs - public final RuleMetadataDto insertOrUpdateMetadata(RuleDto rule, UserDto noteUser, Consumer<RuleMetadataDto>... populaters) { - RuleMetadataDto dto = RuleTesting.newRuleMetadata(rule, noteUser); - asList(populaters).forEach(populater -> populater.accept(dto)); - return insertOrUpdateMetadata(dto); - } - - public RuleMetadataDto insertOrUpdateMetadata(RuleMetadataDto metadata) { - db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), metadata); - db.commit(); - return db.getDbClient().ruleDao().selectByUuid(metadata.getRuleUuid(), db.getSession()).get().getMetadata(); - } - - @SafeVarargs public final RuleParamDto insertRuleParam(RuleDto rule, Consumer<RuleParamDto>... populaters) { RuleParamDto param = RuleTesting.newRuleParam(rule); asList(populaters).forEach(populater -> populater.accept(param)); @@ -154,18 +133,6 @@ public class RuleDbTester { return param; } - public RuleDto insertRule(RuleDto ruleDto) { - if (ruleDto.getUuid() == null) { - ruleDto.setUuid(Uuids.createFast()); - } - - insert(ruleDto); - RuleMetadataDto metadata = ruleDto.getMetadata(); - db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), metadata.setRuleUuid(ruleDto.getUuid())); - db.commit(); - return ruleDto; - } - /** * Create and persist a rule with random values. */ @@ -178,7 +145,7 @@ public class RuleDbTester { public final RuleDto insertRule(Consumer<RuleDto>... populaters) { RuleDto ruleDto = newRuleDto(); asList(populaters).forEach(populater -> populater.accept(ruleDto)); - return insertRule(ruleDto); + return insert(ruleDto); } public RuleDto insertRule(Consumer<RuleDto> populateRuleDto) { @@ -189,7 +156,7 @@ public class RuleDbTester { ruleDto.setUuid(Uuids.createFast()); } - return insertRule(ruleDto); + return insert(ruleDto); } @SafeVarargs @@ -200,9 +167,4 @@ public class RuleDbTester { return deprecatedRuleKeyDto; } - public RuleDto insertRule(RuleDto ruleDto, RuleMetadataDto ruleMetadata) { - db.getDbClient().ruleDao().insertOrUpdateRuleMetadata(db.getSession(), ruleMetadata.setRuleUuid(ruleDto.getUuid())); - db.commit(); - return db.getDbClient().ruleDao().selectOrFailByKey(db.getSession(), ruleDto.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 982b9e4820a..57bdd634f10 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 @@ -33,7 +33,6 @@ import org.sonar.core.util.UuidFactory; import org.sonar.core.util.UuidFactoryFast; import org.sonar.core.util.Uuids; import org.sonar.db.rule.RuleDto.Scope; -import org.sonar.db.user.UserDto; import static com.google.common.base.Preconditions.checkNotNull; import static com.google.common.collect.ImmutableSet.copyOf; @@ -96,14 +95,6 @@ public class RuleTesting { //voluntarily offset the remediation to be able to detect issues .setDefRemediationGapMultiplier((nextInt(10) + 10) + "h") .setDefRemediationFunction("LINEAR_OFFSET") - .setCreatedAt(currentTimeMillis) - .setUpdatedAt(currentTimeMillis + 5) - .setScope(Scope.MAIN); - } - - public static RuleMetadataDto newRuleMetadata() { - return new RuleMetadataDto() - .setRuleUuid("uuid_" + randomAlphanumeric(5)) .setRemediationBaseEffort(nextInt(10) + "h") .setRemediationGapMultiplier(nextInt(10) + "h") .setRemediationFunction("LINEAR_OFFSET") @@ -115,16 +106,10 @@ public class RuleTesting { .setAdHocName("adHocName_" + randomAlphanumeric(5)) .setAdHocDescription("adHocDescription_" + randomAlphanumeric(5)) .setAdHocSeverity(Severity.ALL.get(nextInt(Severity.ALL.size()))) - .setAdHocType(RuleType.values()[nextInt(RuleType.values().length - 1)]); - } - - public static RuleMetadataDto newRuleMetadata(RuleDto rule) { - return newRuleMetadata() - .setRuleUuid(rule.getUuid()); - } - - public static RuleMetadataDto newRuleMetadata(RuleDto rule, UserDto noteUser) { - return newRuleMetadata(rule).setNoteUserUuid(noteUser.getUuid()); + .setAdHocType(RuleType.values()[nextInt(RuleType.values().length - 1)]) + .setCreatedAt(currentTimeMillis) + .setUpdatedAt(currentTimeMillis + 5) + .setScope(Scope.MAIN); } public static RuleParamDto newRuleParam(RuleDto rule) { @@ -287,7 +272,7 @@ public class RuleTesting { return rule -> rule.setSystemTags(copyOf(tags)); } - public static Consumer<RuleMetadataDto> setTags(String... tags) { + public static Consumer<RuleDto> setTags(String... tags) { return rule -> rule.setTags(copyOf(tags)); } |