From f05edf50468fca40be695a0519fe962a020d26b1 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Fri, 20 May 2022 12:15:50 +0200 Subject: [PATCH] SONAR-16419 merge rules and rules metadata in single table and DTO --- .../issue/AdHocRuleCreator.java | 19 +- .../rule/ExportAdHocRulesStep.java | 5 +- .../issue/AdHocRuleCreatorTest.java | 46 ++- .../issue/IntegrateIssuesVisitorTest.java | 4 +- .../rule/ExportAdHocRulesStepTest.java | 7 - .../java/org/sonar/db/version/SqTables.java | 1 - .../main/java/org/sonar/db/rule/RuleDao.java | 18 -- .../main/java/org/sonar/db/rule/RuleDto.java | 305 ++++++++++-------- .../org/sonar/db/rule/RuleForIndexingDto.java | 2 +- .../java/org/sonar/db/rule/RuleMapper.java | 10 - .../org/sonar/db/rule/RuleMetadataDto.java | 241 -------------- .../QualityProfileExportMapper.xml | 5 +- .../org/sonar/db/rule/RuleMapper.xml | 163 +++------- .../java/org/sonar/db/issue/IssueDaoTest.java | 2 +- .../org/sonar/db/issue/IssueMapperTest.java | 2 +- .../QualityProfileExportDaoTest.java | 21 +- .../java/org/sonar/db/rule/RuleDaoTest.java | 170 +++------- .../java/org/sonar/db/rule/RuleDtoTest.java | 23 ++ .../java/org/sonar/db/rule/RuleDbTester.java | 42 +-- .../java/org/sonar/db/rule/RuleTesting.java | 25 +- .../server/rule/DefaultRuleFinderTest.java | 13 +- .../sonar/server/rule/index/RuleDocTest.java | 5 +- .../server/rule/index/RuleIndexTest.java | 51 ++- .../issue/index/IssueQueryFactoryTest.java | 6 +- .../org/sonar/server/rule/ws/RuleMapper.java | 66 ++-- .../sonar/server/rule/ws/SearchAction.java | 2 +- .../org/sonar/server/rule/ws/ShowAction.java | 2 +- .../sonar/server/rule/ws/UpdateAction.java | 2 +- .../sonar/server/issue/IssueFinderTest.java | 2 +- .../server/issue/SetSeverityActionTest.java | 2 +- .../sonar/server/issue/SetTypeActionTest.java | 2 +- .../server/issue/ws/ChangelogActionTest.java | 2 +- .../server/issue/ws/SearchActionTest.java | 13 +- .../sonar/server/rule/RuleCreatorTest.java | 1 - .../sonar/server/rule/RuleUpdaterTest.java | 5 - .../server/rule/ws/CreateActionTest.java | 1 - .../server/rule/ws/SearchActionTest.java | 130 ++++---- .../sonar/server/rule/ws/ShowActionTest.java | 102 +++--- .../sonar/server/rule/ws/TagsActionTest.java | 5 +- .../server/rule/ws/UpdateActionTest.java | 26 +- 40 files changed, 538 insertions(+), 1011 deletions(-) delete mode 100644 server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java index 477c2a8bab8..ac1e8a5626e 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java @@ -29,7 +29,6 @@ import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.server.rule.index.RuleIndexer; import static java.util.Objects.requireNonNull; @@ -59,29 +58,27 @@ public class AdHocRuleCreator { */ public RuleDto persistAndIndex(DbSession dbSession, NewAdHocRule adHoc) { RuleDao dao = dbClient.ruleDao(); - RuleMetadataDto metadata; long now = system2.now(); RuleDto ruleDtoToUpdate = findOrCreateRuleDto(dbSession, adHoc, dao, now); - metadata = ruleDtoToUpdate.getMetadata(); if (adHoc.hasDetails()) { boolean changed = false; - if (!Objects.equals(metadata.getAdHocName(), adHoc.getName())) { - metadata.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME)); + if (!Objects.equals(ruleDtoToUpdate.getAdHocName(), adHoc.getName())) { + ruleDtoToUpdate.setAdHocName(substring(adHoc.getName(), 0, MAX_LENGTH_AD_HOC_NAME)); changed = true; } - if (!Objects.equals(metadata.getAdHocDescription(), adHoc.getDescription())) { - metadata.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC)); + if (!Objects.equals(ruleDtoToUpdate.getAdHocDescription(), adHoc.getDescription())) { + ruleDtoToUpdate.setAdHocDescription(substring(adHoc.getDescription(), 0, MAX_LENGTH_AD_HOC_DESC)); changed = true; } - if (!Objects.equals(metadata.getAdHocSeverity(), adHoc.getSeverity())) { - metadata.setAdHocSeverity(adHoc.getSeverity()); + if (!Objects.equals(ruleDtoToUpdate.getAdHocSeverity(), adHoc.getSeverity())) { + ruleDtoToUpdate.setAdHocSeverity(adHoc.getSeverity()); changed = true; } RuleType ruleType = requireNonNull(adHoc.getRuleType(), "Rule type should not be null"); - if (!Objects.equals(metadata.getAdHocType(), ruleType.getDbConstant())) { - metadata.setAdHocType(ruleType); + if (!Objects.equals(ruleDtoToUpdate.getAdHocType(), ruleType.getDbConstant())) { + ruleDtoToUpdate.setAdHocType(ruleType); changed = true; } if (changed) { diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java index 9e7c5b3b5ea..8b1007aac14 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStep.java @@ -41,10 +41,9 @@ public class ExportAdHocRulesStep implements ComputationStep { private static final String ISSUE_STATUS_CLOSED = "CLOSED"; private static final String QUERY = "select" + - " r.uuid, r.plugin_key, r.plugin_rule_key, r.plugin_name, r.name, r.status, r.rule_type, r.scope, rm.rule_uuid, rm.ad_hoc_name," + - " rm.ad_hoc_description,rm.ad_hoc_severity, rm.ad_hoc_type" + + " r.uuid, r.plugin_key, r.plugin_rule_key, r.plugin_name, r.name, r.status, r.rule_type, r.scope, r.ad_hoc_name," + + " r.ad_hoc_description,r.ad_hoc_severity, r.ad_hoc_type" + " from rules r" + - " left join rules_metadata rm on rm.rule_uuid = r.uuid" + " inner join issues i on r.uuid = i.rule_uuid and r.status <> ? and r.is_ad_hoc = ?" + " left join components p on p.uuid = i.project_uuid" + " left join project_branches pb on pb.uuid = p.uuid" + diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java index c992f745732..5dc6fbba25e 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java @@ -28,7 +28,6 @@ import org.sonar.core.util.SequenceUuidFactory; 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.scanner.protocol.Constants; import org.sonar.scanner.protocol.output.ScannerReport; import org.sonar.server.es.EsTester; @@ -63,10 +62,10 @@ public class AdHocRuleCreatorTest { assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty(); assertThat(rule.getSeverity()).isNull(); assertThat(rule.getType()).isZero(); - assertThat(rule.getMetadata().getAdHocName()).isNull(); - assertThat(rule.getMetadata().getAdHocDescription()).isNull(); - assertThat(rule.getMetadata().getAdHocSeverity()).isNull(); - assertThat(rule.getMetadata().getAdHocType()).isNull(); + assertThat(rule.getAdHocName()).isNull(); + assertThat(rule.getAdHocDescription()).isNull(); + assertThat(rule.getAdHocSeverity()).isNull(); + assertThat(rule.getAdHocType()).isNull(); } @Test @@ -91,10 +90,10 @@ public class AdHocRuleCreatorTest { assertThat(rule.getRuleDescriptionSectionDtos()).isEmpty(); assertThat(rule.getSeverity()).isNull(); assertThat(rule.getType()).isZero(); - assertThat(rule.getMetadata().getAdHocName()).isEqualTo("No condition assigned"); - assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo("A description"); - assertThat(rule.getMetadata().getAdHocSeverity()).isEqualTo(Severity.BLOCKER); - assertThat(rule.getMetadata().getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant()); + assertThat(rule.getAdHocName()).isEqualTo("No condition assigned"); + assertThat(rule.getAdHocDescription()).isEqualTo("A description"); + assertThat(rule.getAdHocSeverity()).isEqualTo(Severity.BLOCKER); + assertThat(rule.getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant()); } @Test @@ -110,8 +109,8 @@ public class AdHocRuleCreatorTest { RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule); - assertThat(rule.getMetadata().getAdHocName()).isEqualTo(repeat("a", 200)); - assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo(repeat("a", 16_777_215)); + assertThat(rule.getAdHocName()).isEqualTo(repeat("a", 200)); + assertThat(rule.getAdHocDescription()).isEqualTo(repeat("a", 16_777_215)); } @Test @@ -146,10 +145,10 @@ public class AdHocRuleCreatorTest { assertThat(ruleUpdated.getRuleDescriptionSectionDtos()).isEmpty(); assertThat(ruleUpdated.getSeverity()).isNull(); assertThat(ruleUpdated.getType()).isZero(); - assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo("No condition assigned updated"); - assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo("A description updated"); - assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL); - assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); + assertThat(ruleUpdated.getAdHocName()).isEqualTo("No condition assigned updated"); + assertThat(ruleUpdated.getAdHocDescription()).isEqualTo("A description updated"); + assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(Severity.CRITICAL); + assertThat(ruleUpdated.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); assertThat(ruleUpdated.getCreatedAt()).isEqualTo(creationDate); assertThat(ruleUpdated.getUpdatedAt()).isGreaterThan(creationDate); } @@ -157,15 +156,14 @@ public class AdHocRuleCreatorTest { @Test public void does_not_update_rule_when_no_change() { RuleDto rule = db.rules().insert(r -> r.setRepositoryKey("external_eslint").setIsExternal(true).setIsAdHoc(true)); - RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule); RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId(rule.getKey().rule()) - .setName(ruleMetadata.getAdHocName()) - .setDescription(ruleMetadata.getAdHocDescription()) - .setSeverity(Constants.Severity.valueOf(ruleMetadata.getAdHocSeverity())) - .setType(ScannerReport.IssueType.forNumber(ruleMetadata.getAdHocType())) + .setName(rule.getAdHocName()) + .setDescription(rule.getAdHocDescription()) + .setSeverity(Constants.Severity.valueOf(rule.getAdHocSeverity())) + .setType(ScannerReport.IssueType.forNumber(rule.getAdHocType())) .build())); assertThat(ruleUpdated).isNotNull(); @@ -179,10 +177,10 @@ public class AdHocRuleCreatorTest { assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt()); assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(rule.getUpdatedAt()); - assertThat(ruleUpdated.getMetadata().getAdHocName()).isEqualTo(ruleMetadata.getAdHocName()); - assertThat(ruleUpdated.getMetadata().getAdHocDescription()).isEqualTo(ruleMetadata.getAdHocDescription()); - assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(ruleMetadata.getAdHocSeverity()); - assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(ruleMetadata.getAdHocType()); + assertThat(ruleUpdated.getAdHocName()).isEqualTo(rule.getAdHocName()); + assertThat(ruleUpdated.getAdHocDescription()).isEqualTo(rule.getAdHocDescription()); + assertThat(ruleUpdated.getAdHocSeverity()).isEqualTo(rule.getAdHocSeverity()); + assertThat(ruleUpdated.getAdHocType()).isEqualTo(rule.getAdHocType()); } } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java index 10fd7448c27..2f3ebe623f3 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/IntegrateIssuesVisitorTest.java @@ -323,7 +323,7 @@ public class IntegrateIssuesVisitorTest { dbTester.components().insertComponents(project, file); RuleDto ruleDto = RuleTesting.newDto(ruleKey); - dbTester.rules().insertRule(ruleDto); + dbTester.rules().insert(ruleDto); ruleRepositoryRule.add(ruleKey); IssueDto issue = IssueTesting.newDto(ruleDto, file, project) @@ -340,7 +340,7 @@ public class IntegrateIssuesVisitorTest { dbTester.components().insertComponents(project, file); RuleDto ruleDto = RuleTesting.newDto(ruleKey); - dbTester.rules().insertRule(ruleDto); + dbTester.rules().insert(ruleDto); ruleRepositoryRule.add(ruleKey); IssueDto issue = IssueTesting.newDto(ruleDto, file, project) diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepTest.java index b93ceeb324c..d659a4fe09e 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectexport/rule/ExportAdHocRulesStepTest.java @@ -44,7 +44,6 @@ import org.sonar.db.component.ComponentDto; import org.sonar.db.issue.IssueDto; import org.sonar.db.project.ProjectDto; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -220,10 +219,4 @@ public class ExportAdHocRulesStepTest { .orElseThrow(() -> new RuntimeException("insertAdHocRule failed")); } - private void insertRuleMetadata(String ruleUuid, String adHocName) { - dbTester.rules().insertOrUpdateMetadata(new RuleMetadataDto() - .setRuleUuid(ruleUuid) - .setAdHocName(adHocName)); - dbTester.commit(); - } } diff --git a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java index b46f6a4a1d9..710e707f9bd 100644 --- a/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java +++ b/server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java @@ -91,7 +91,6 @@ public final class SqTables { "saml_message_ids", "rules", "rule_desc_sections", - "rules_metadata", "rules_parameters", "rules_profiles", "rule_repositories", 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 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 ruleExtensionIds, Consumer 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 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 deserializeTagsString(@Nullable String tags) { + return deserializeStringSet(tags); + } + + public static Set deserializeSecurityStandardsString(@Nullable String securityStandards) { + return deserializeStringSet(securityStandards); + } + + private static Set 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 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 deserializeTagsString(@Nullable String tags) { - return deserializeStringSet(tags); + public RuleDto setTags(Set 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 deserializeSecurityStandardsString(@Nullable String securityStandards) { - return deserializeStringSet(securityStandards); + private String getTagsField() { + return tags; } - private static Set 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 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 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 selectByKeys(@Param("ruleKeys") List keys); List selectIndexingRuleExtensionsByIds(@Param("ruleExtensionIds") List 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 selectParamsByRuleUuids(@Param("ruleUuids") List ruleUuids); List 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 getTags() { - return tags == null ? new HashSet<>() : new TreeSet<>(Arrays.asList(StringUtils.split(tags, ','))); - } - - String getTagsAsString() { - return tags; - } - - public RuleMetadataDto setTags(Set 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" @@ -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 where oqp.uuid = #{id, jdbcType=VARCHAR} 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" - - - - 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" @@ -81,7 +75,6 @@ from rules r - where r.status != 'REMOVED' @@ -92,7 +85,6 @@ from rules r - where r.uuid=#{uuid,jdbcType=VARCHAR} @@ -103,7 +95,6 @@ from rules r - where @@ -116,7 +107,6 @@ from rules r - 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 != '' @@ -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 - - - select - count(1) - from - rules_metadata rm - where - rm.rule_uuid=#{ruleUuid,jdbcType=VARCHAR} - - - - 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} - ) - - - - 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} - + delete from - rules_metadata + rule_desc_sections where - rule_uuid=#{uuid,jdbcType=INTEGER} + rule_uuid=#{ruleUuid,jdbcType=VARCHAR} 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 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 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 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 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 accumulator = new Accumulator<>(); @@ -1004,17 +928,15 @@ public class RuleDaoTest { @Test public void scrollIndexingRuleExtensionsByIds() { Accumulator 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; @@ -63,6 +64,28 @@ public class RuleDtoTest { assertThat(dto.getTags()).isEmpty(); } + @Test + public void tags_are_joined_with_comma() { + Set 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 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 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(); 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; @@ -125,26 +124,6 @@ public class RuleDbTester { return rule; } - @SafeVarargs - public final RuleMetadataDto insertOrUpdateMetadata(RuleDto rule, Consumer... 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... 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... populaters) { RuleParamDto param = RuleTesting.newRuleParam(rule); @@ -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... populaters) { RuleDto ruleDto = newRuleDto(); asList(populaters).forEach(populater -> populater.accept(ruleDto)); - return insertRule(ruleDto); + return insert(ruleDto); } public RuleDto insertRule(Consumer 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 setTags(String... tags) { + public static Consumer setTags(String... tags) { return rule -> rule.setTags(copyOf(tags)); } diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java index a4286390138..0d9f7680640 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/DefaultRuleFinderTest.java @@ -19,7 +19,7 @@ */ package org.sonar.server.rule; -import com.google.common.collect.ImmutableSet; +import java.util.Set; import org.junit.Before; import org.junit.Test; import org.sonar.api.rule.RuleKey; @@ -33,6 +33,7 @@ import org.sonar.db.DbTester; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleDto.Scope; +import static java.util.Collections.emptySet; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; @@ -85,10 +86,10 @@ public class DefaultRuleFinderTest { @Before public void setup() { - dbTester.rules().insertRule(rule1); - dbTester.rules().insertRule(rule2); - dbTester.rules().insertRule(rule3); - dbTester.rules().insertRule(rule4); + dbTester.rules().insert(rule1); + dbTester.rules().insert(rule2); + dbTester.rules().insert(rule3); + dbTester.rules().insert(rule4); session.commit(); } @@ -139,7 +140,7 @@ public class DefaultRuleFinderTest { @Test public void findByKey_populates_system_tags_but_not_tags() { RuleDto ruleDto = dbTester.rules() - .insert(t -> t.setSystemTags(ImmutableSet.of(randomAlphanumeric(5), randomAlphanumeric(6)))); + .insert(t -> t.setSystemTags(Set.of(randomAlphanumeric(5), randomAlphanumeric(6))).setTags(emptySet())); dbTester.rules().insertRule(); Rule rule = underTest.findByKey(ruleDto.getKey()); 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 af46148f964..9d5dcf92920 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 @@ -59,7 +59,10 @@ public class RuleDocTest { assertThat(ruleDoc.status()).isEqualTo(ruleForIndexingDto.getStatus()); assertThat(ruleDoc.type().name()).isEqualTo(ruleForIndexingDto.getTypeAsRuleType().name()); assertThat(ruleDoc.createdAt()).isEqualTo(ruleForIndexingDto.getCreatedAt()); - assertThat(ruleDoc.getTags()).isEqualTo(ruleForIndexingDto.getSystemTags()); + assertThat(ruleDoc.getTags()) + .containsAll(ruleForIndexingDto.getSystemTags()) + .containsAll(ruleForIndexingDto.getTags()) + .hasSize(ruleForIndexingDto.getSystemTags().size() + ruleForIndexingDto.getTags().size()); assertThat(ruleDoc.updatedAt()).isEqualTo(ruleForIndexingDto.getUpdatedAt()); assertThat(ruleDoc.templateKey().repository()).isEqualTo(ruleForIndexingDto.getTemplateRepository()); assertThat(ruleDoc.templateKey().rule()).isEqualTo(ruleForIndexingDto.getTemplateRuleKey()); 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 2b300399360..41b786e2751 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 @@ -38,7 +38,6 @@ import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbTester; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.server.es.EsTester; import org.sonar.server.es.Facets; import org.sonar.server.es.SearchIdResult; @@ -295,10 +294,8 @@ public class RuleIndexTest { @Test public void filter_by_tags() { - RuleDto rule1 = createRule(setSystemTags("tag1s")); - createRuleMetadata(rule1, setTags("tag1")); - RuleDto rule2 = createRule(setSystemTags("tag2s")); - createRuleMetadata(rule2, setTags("tag2")); + RuleDto rule1 = createRule(setSystemTags("tag1s"), setTags("tag1")); + RuleDto rule2 = createRule(setSystemTags("tag2s"), setTags("tag2")); index(); assertThat(es.countDocuments(TYPE_RULE)).isEqualTo(2); @@ -321,8 +318,7 @@ public class RuleIndexTest { @Test public void tags_facet_supports_selected_value_with_regexp_special_characters() { - RuleDto rule = createRule(); - createRuleMetadata(rule, setTags("misra++")); + createRule(r -> r.setTags(Set.of("misra++"))); index(); RuleQuery query = new RuleQuery() @@ -571,11 +567,6 @@ public class RuleIndexTest { return createRule(r -> r.setLanguage("java"), consumer); } - @SafeVarargs - private final RuleMetadataDto createRuleMetadata(RuleDto rule, Consumer... populaters) { - return db.rules().insertOrUpdateMetadata(rule, populaters); - } - @Test public void search_by_any_of_severities() { createRule(setSeverity(BLOCKER)); @@ -762,11 +753,8 @@ public class RuleIndexTest { @Test public void listTags_should_return_tags() { - RuleDto rule1 = createRule(setSystemTags("sys1", "sys2")); - createRuleMetadata(rule1, setTags("tag1")); - - RuleDto rule2 = createRule(setSystemTags()); - createRuleMetadata(rule2, setTags("tag2")); + createRule(setSystemTags("sys1", "sys2"), setTags("tag1")); + createRule(setSystemTags(), setTags("tag2")); index(); @@ -800,11 +788,9 @@ public class RuleIndexTest { @Test public void global_facet_on_repositories_and_tags() { - createRule(setRepositoryKey("php"), setSystemTags("sysTag")); - RuleDto rule1 = createRule(setRepositoryKey("php"), setSystemTags()); - createRuleMetadata(rule1, setTags("tag1")); - RuleDto rule2 = createRule(setRepositoryKey("javascript"), setSystemTags()); - createRuleMetadata(rule2, setTags("tag1", "tag2")); + createRule(setRepositoryKey("php"), setSystemTags("sysTag"), setTags()); + createRule(setRepositoryKey("php"), setSystemTags(), setTags("tag1")); + createRule(setRepositoryKey("javascript"), setSystemTags(), setTags("tag1", "tag2")); index(); // should not have any facet! @@ -834,16 +820,16 @@ public class RuleIndexTest { } private void setupStickyFacets() { - createRule(setRepositoryKey("xoo"), setRuleKey("S001"), setLanguage("java"), setSystemTags(), setType(BUG)); - createRule(setRepositoryKey("xoo"), setRuleKey("S002"), setLanguage("java"), setSystemTags(), setType(CODE_SMELL)); - createRule(setRepositoryKey("xoo"), setRuleKey("S003"), setLanguage("java"), setSystemTags("T1", "T2"), setType(CODE_SMELL)); - createRule(setRepositoryKey("xoo"), setRuleKey("S011"), setLanguage("cobol"), setSystemTags(), setType(CODE_SMELL)); - createRule(setRepositoryKey("xoo"), setRuleKey("S012"), setLanguage("cobol"), setSystemTags(), setType(BUG)); - createRule(setRepositoryKey("foo"), setRuleKey("S013"), setLanguage("cobol"), setSystemTags("T3", "T4"), + createRule(setRepositoryKey("xoo"), setRuleKey("S001"), setLanguage("java"), setTags(), setSystemTags(), setType(BUG)); + createRule(setRepositoryKey("xoo"), setRuleKey("S002"), setLanguage("java"), setTags(), setSystemTags(), setType(CODE_SMELL)); + createRule(setRepositoryKey("xoo"), setRuleKey("S003"), setLanguage("java"), setTags(), setSystemTags("T1", "T2"), setType(CODE_SMELL)); + createRule(setRepositoryKey("xoo"), setRuleKey("S011"), setLanguage("cobol"), setTags(), setSystemTags(), setType(CODE_SMELL)); + createRule(setRepositoryKey("xoo"), setRuleKey("S012"), setLanguage("cobol"), setTags(), setSystemTags(), setType(BUG)); + createRule(setRepositoryKey("foo"), setRuleKey("S013"), setLanguage("cobol"), setTags(), setSystemTags("T3", "T4"), setType(VULNERABILITY)); - createRule(setRepositoryKey("foo"), setRuleKey("S111"), setLanguage("cpp"), setSystemTags(), setType(BUG)); - createRule(setRepositoryKey("foo"), setRuleKey("S112"), setLanguage("cpp"), setSystemTags(), setType(CODE_SMELL)); - createRule(setRepositoryKey("foo"), setRuleKey("S113"), setLanguage("cpp"), setSystemTags("T2", "T3"), setType(CODE_SMELL)); + createRule(setRepositoryKey("foo"), setRuleKey("S111"), setLanguage("cpp"), setTags(), setSystemTags(), setType(BUG)); + createRule(setRepositoryKey("foo"), setRuleKey("S112"), setLanguage("cpp"), setTags(), setSystemTags(), setType(CODE_SMELL)); + createRule(setRepositoryKey("foo"), setRuleKey("S113"), setLanguage("cpp"), setTags(), setSystemTags("T2", "T3"), setType(CODE_SMELL)); index(); } @@ -913,8 +899,7 @@ public class RuleIndexTest { @Test public void tags_facet_should_find_tags() { - RuleDto rule = createRule(setSystemTags()); - createRuleMetadata(rule, setTags("bla")); + createRule(setSystemTags(), setTags("bla")); index(); RuleQuery query = new RuleQuery(); diff --git a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java index 50493b8252d..649e3f5abb8 100644 --- a/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java +++ b/server/sonar-webserver-es/src/test/java/org/sonar/server/issue/index/IssueQueryFactoryTest.java @@ -80,10 +80,8 @@ public class IssueQueryFactoryTest { ComponentDto module = db.components().insertComponent(newModuleDto(project)); ComponentDto file = db.components().insertComponent(newFileDto(project)); - RuleDto rule1 = ruleDbTester.insert(); - RuleDto rule2 = ruleDbTester.insert(); - ruleDbTester.insertOrUpdateMetadata(rule1, m -> m.setAdHocName(ruleAdHocName)); - ruleDbTester.insertOrUpdateMetadata(rule2, m -> m.setAdHocName(ruleAdHocName)); + RuleDto rule1 = ruleDbTester.insert(r -> r.setAdHocName(ruleAdHocName)); + RuleDto rule2 = ruleDbTester.insert(r -> r.setAdHocName(ruleAdHocName)); newRule(RuleKey.of("findbugs", "NullReference")); SearchRequest request = new SearchRequest() .setIssues(asList("anIssueKey")) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index 9756754118f..5102d4b3490 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -38,7 +38,6 @@ import org.sonar.db.rule.DeprecatedRuleKeyDto; import org.sonar.db.rule.RuleDescriptionSectionDto; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleDto.Scope; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.user.UserDto; import org.sonar.markdown.Markdown; @@ -49,7 +48,6 @@ import org.sonarqube.ws.Common; import org.sonarqube.ws.Common.RuleScope; import org.sonarqube.ws.Rules; -import static java.util.stream.Collectors.joining; import static org.sonar.api.utils.DateUtils.formatDateTime; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.db.rule.RuleDto.Format.MARKDOWN; @@ -103,12 +101,12 @@ public class RuleMapper { return ruleResponse.build(); } - public Rules.Rule toWsRule(RuleDto ruleDto, SearchResult result, Set fieldsToReturn, RuleMetadataDto metadata, - Map usersByUuid, Map> deprecatedRuleKeysByRuleUuid) { + public Rules.Rule toWsRule(RuleDto ruleDto, SearchResult result, Set fieldsToReturn, Map usersByUuid, + Map> deprecatedRuleKeysByRuleUuid) { Rules.Rule.Builder ruleResponse = Rules.Rule.newBuilder(); applyRuleDefinition(ruleResponse, ruleDto, result, fieldsToReturn, deprecatedRuleKeysByRuleUuid); - applyRuleMetadata(ruleResponse, ruleDto, metadata, usersByUuid, fieldsToReturn); - setDebtRemediationFunctionFields(ruleResponse, ruleDto, metadata, fieldsToReturn); + setDebtRemediationFunctionFields(ruleResponse, ruleDto, fieldsToReturn); + setNotesFields(ruleResponse, ruleDto, usersByUuid, fieldsToReturn); return ruleResponse.build(); } @@ -138,45 +136,41 @@ public class RuleMapper { setEffortToFixDescription(ruleResponse, ruleDto, fieldsToReturn); setScope(ruleResponse, ruleDto, fieldsToReturn); setDeprecatedKeys(ruleResponse, ruleDto, fieldsToReturn, deprecatedRuleKeysByRuleUuid); - return ruleResponse; - } - private void applyRuleMetadata(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, RuleMetadataDto metadata, Map usersByUuid, - Set fieldsToReturn) { - setTags(ruleResponse, metadata, fieldsToReturn); - setNotesFields(ruleResponse, metadata, usersByUuid, fieldsToReturn); - setIsRemediationFunctionOverloaded(ruleResponse, metadata, fieldsToReturn); + setTags(ruleResponse, ruleDto, fieldsToReturn); + setIsRemediationFunctionOverloaded(ruleResponse, ruleDto, fieldsToReturn); if (ruleDto.isAdHoc()) { - setAdHocName(ruleResponse, metadata, fieldsToReturn); - setAdHocDescription(ruleResponse, metadata, fieldsToReturn); - setAdHocSeverity(ruleResponse, metadata, fieldsToReturn); - setAdHocType(ruleResponse, metadata); + setAdHocName(ruleResponse, ruleDto, fieldsToReturn); + setAdHocDescription(ruleResponse, ruleDto, fieldsToReturn); + setAdHocSeverity(ruleResponse, ruleDto, fieldsToReturn); + setAdHocType(ruleResponse, ruleDto); } + return ruleResponse; } - private static void setAdHocName(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set fieldsToReturn) { - String adHocName = metadata.getAdHocName(); + private static void setAdHocName(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { + String adHocName = ruleDto.getAdHocName(); if (adHocName != null && shouldReturnField(fieldsToReturn, FIELD_NAME)) { ruleResponse.setName(adHocName); } } - private void setAdHocDescription(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set fieldsToReturn) { - String adHocDescription = metadata.getAdHocDescription(); + private void setAdHocDescription(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { + String adHocDescription = ruleDto.getAdHocDescription(); if (adHocDescription != null && shouldReturnField(fieldsToReturn, FIELD_HTML_DESCRIPTION)) { ruleResponse.setHtmlDesc(macroInterpreter.interpret(adHocDescription)); } } - private static void setAdHocSeverity(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata, Set fieldsToReturn) { - String severity = metadata.getAdHocSeverity(); + private static void setAdHocSeverity(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { + String severity = ruleDto.getAdHocSeverity(); if (shouldReturnField(fieldsToReturn, FIELD_SEVERITY) && severity != null) { ruleResponse.setSeverity(severity); } } - private static void setAdHocType(Rules.Rule.Builder ruleResponse, RuleMetadataDto metadata) { - Integer ruleType = metadata.getAdHocType(); + private static void setAdHocType(Rules.Rule.Builder ruleResponse, RuleDto ruleDto) { + Integer ruleType = ruleDto.getAdHocType(); if (ruleType != null) { ruleResponse.setType(Common.RuleType.forNumber(ruleType)); } @@ -233,7 +227,7 @@ public class RuleMapper { } } - private static void setIsRemediationFunctionOverloaded(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Set fieldsToReturn) { + private static void setIsRemediationFunctionOverloaded(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { if (shouldReturnField(fieldsToReturn, FIELD_DEBT_OVERLOADED) || shouldReturnField(fieldsToReturn, FIELD_REM_FUNCTION_OVERLOADED)) { ruleResponse.setDebtOverloaded(isRemediationFunctionOverloaded(ruleDto)); ruleResponse.setRemFnOverloaded(isRemediationFunctionOverloaded(ruleDto)); @@ -265,10 +259,10 @@ public class RuleMapper { } } - private static void setDebtRemediationFunctionFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDefinitionDto, RuleMetadataDto ruleMetadataDto, + private static void setDebtRemediationFunctionFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { if (shouldReturnField(fieldsToReturn, FIELD_DEBT_REM_FUNCTION) || shouldReturnField(fieldsToReturn, FIELD_REM_FUNCTION)) { - DebtRemediationFunction debtRemediationFunction = debtRemediationFunction(ruleDefinitionDto, ruleMetadataDto); + DebtRemediationFunction debtRemediationFunction = debtRemediationFunction(ruleDto); if (debtRemediationFunction != null) { if (debtRemediationFunction.type() != null) { ruleResponse.setRemFnType(debtRemediationFunction.type().name()); @@ -303,7 +297,7 @@ public class RuleMapper { } } - private static void setTags(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Set fieldsToReturn) { + private static void setTags(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { if (shouldReturnField(fieldsToReturn, FIELD_TAGS)) { ruleResponse.getTagsBuilder().addAllTags(ruleDto.getTags()); } @@ -362,7 +356,7 @@ public class RuleMapper { sectionDto.getContent(); } - private void setNotesFields(Rules.Rule.Builder ruleResponse, RuleMetadataDto ruleDto, Map usersByUuid, Set fieldsToReturn) { + private void setNotesFields(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Map usersByUuid, Set fieldsToReturn) { String noteData = ruleDto.getNoteData(); if (shouldReturnField(fieldsToReturn, "htmlNote") && noteData != null) { ruleResponse.setHtmlNote(macroInterpreter.interpret(Markdown.convertToHtml(noteData))); @@ -429,7 +423,7 @@ public class RuleMapper { return fieldsToReturn.isEmpty() || fieldsToReturn.contains(fieldName); } - private static boolean isRemediationFunctionOverloaded(RuleMetadataDto rule) { + private static boolean isRemediationFunctionOverloaded(RuleDto rule) { return rule.getRemediationFunction() != null; } @@ -447,15 +441,15 @@ public class RuleMapper { } @CheckForNull - private static DebtRemediationFunction debtRemediationFunction(RuleDto ruleDefinitionDto, RuleMetadataDto ruleMetadataDto) { - final String function = ruleMetadataDto.getRemediationFunction(); + private static DebtRemediationFunction debtRemediationFunction(RuleDto ruleDto) { + final String function = ruleDto.getRemediationFunction(); if (function == null || function.isEmpty()) { - return defaultDebtRemediationFunction(ruleDefinitionDto); + return defaultDebtRemediationFunction(ruleDto); } else { return new DefaultDebtRemediationFunction( DebtRemediationFunction.Type.valueOf(function.toUpperCase(Locale.ENGLISH)), - ruleMetadataDto.getRemediationGapMultiplier(), - ruleMetadataDto.getRemediationBaseEffort()); + ruleDto.getRemediationGapMultiplier(), + ruleDto.getRemediationBaseEffort()); } } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java index 8fa9a6bd490..690af4e2847 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -216,7 +216,7 @@ public class SearchAction implements RulesWsAction { private void writeRules(DbSession dbSession, SearchResponse.Builder response, SearchResult result, SearchOptions context) { Map usersByUuid = ruleWsSupport.getUsersByUuid(dbSession, result.rules); Map> deprecatedRuleKeysByRuleUuid = getDeprecatedRuleKeysByRuleUuid(dbSession, result.rules, context); - result.rules.forEach(rule -> response.addRules(mapper.toWsRule(rule, result, context.getFields(), rule.getMetadata(), usersByUuid, + result.rules.forEach(rule -> response.addRules(mapper.toWsRule(rule, result, context.getFields(), usersByUuid, deprecatedRuleKeysByRuleUuid))); } diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ShowAction.java index a41e6a2b4f2..191df2bf198 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/ShowAction.java @@ -121,7 +121,7 @@ public class ShowAction implements RulesWsAction { private ShowResponse buildResponse(DbSession dbSession, Request request, SearchAction.SearchResult searchResult) { ShowResponse.Builder responseBuilder = ShowResponse.newBuilder(); RuleDto rule = searchResult.getRules().get(0); - responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), rule.getMetadata(), + responseBuilder.setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), ruleWsSupport.getUsersByUuid(dbSession, searchResult.getRules()), emptyMap())); if (request.mandatoryParamAsBoolean(PARAM_ACTIVES)) { activeRuleCompleter.completeShow(dbSession, rule).forEach(responseBuilder::addActives); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/UpdateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/UpdateAction.java index 1b47dbd1bb5..a69c88c05f9 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/UpdateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/rule/ws/UpdateAction.java @@ -249,7 +249,7 @@ public class UpdateAction implements RulesWsAction { .setRuleParameters(ruleParameters) .setTotal(1L); responseBuilder - .setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), rule.getMetadata(), + .setRule(mapper.toWsRule(rule, searchResult, Collections.emptySet(), ruleWsSupport.getUsersByUuid(dbSession, singletonList(rule)), emptyMap())); return responseBuilder.build(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueFinderTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueFinderTest.java index 910ca5d924f..c63244d423c 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueFinderTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/IssueFinderTest.java @@ -87,7 +87,7 @@ public class IssueFinderTest { } private IssueDto insertIssue() { - RuleDto rule = ruleDbTester.insertRule(newRuleDto()); + RuleDto rule = ruleDbTester.insert(newRuleDto()); ComponentDto project = componentDbTester.insertPrivateProject(); ComponentDto file = componentDbTester.insertComponent(newFileDto(project)); return issueDbTester.insert(newDto(rule, file, project)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetSeverityActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetSeverityActionTest.java index 900f01d8895..248d40dbe95 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetSeverityActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetSeverityActionTest.java @@ -128,7 +128,7 @@ public class SetSeverityActionTest { } private IssueDto newIssue() { - RuleDto rule = db.rules().insertRule(newRuleDto()); + RuleDto rule = db.rules().insert(newRuleDto()); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); return newDto(rule, file, project); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetTypeActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetTypeActionTest.java index 5b81a8b8c24..f36045cfa47 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetTypeActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/SetTypeActionTest.java @@ -123,7 +123,7 @@ public class SetTypeActionTest { } private IssueDto newIssue() { - RuleDto rule = db.rules().insertRule(newRuleDto()); + RuleDto rule = db.rules().insert(newRuleDto()); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); return newDto(rule, file, project); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java index 209be35bf0d..0e8a52b1bf1 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/ChangelogActionTest.java @@ -100,7 +100,7 @@ public class ChangelogActionTest { @Test public void changelog_of_file_move_contains_file_names() { - RuleDto rule = db.rules().insertRule(newRuleDto()); + RuleDto rule = db.rules().insert(newRuleDto()); ComponentDto project = db.components().insertPrivateProject(); ComponentDto file1 = db.components().insertComponent(newFileDto(project)); ComponentDto file2 = db.components().insertComponent(newFileDto(project)); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java index b18c7ab60b7..0da9576c31f 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java @@ -55,7 +55,6 @@ import org.sonar.db.permission.GroupPermissionDto; import org.sonar.db.protobuf.DbCommons; import org.sonar.db.protobuf.DbIssues; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsTester; @@ -223,8 +222,12 @@ public class SearchActionTest { ComponentDto project = db.components().insertPublicProject(); indexPermissions(); ComponentDto file = db.components().insertComponent(newFileDto(project)); - RuleDto rule = db.rules().insertIssueRule(RuleTesting.EXTERNAL_XOO, r -> r.setIsExternal(true).setLanguage("xoo").setIsAdHoc(true)); - RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule, m -> m.setAdHocName("different_rule_name")); + RuleDto rule = db.rules().insertIssueRule(RuleTesting.EXTERNAL_XOO, + r -> r + .setIsExternal(true) + .setLanguage("xoo") + .setIsAdHoc(true) + .setAdHocName("different_rule_name")); IssueDto issue = db.issues().insertIssue(rule, project, file); indexIssues(); @@ -238,7 +241,7 @@ public class SearchActionTest { assertThat(response.getRules().getRulesList()) .extracting(Common.Rule::getKey, Common.Rule::getName) - .containsExactlyInAnyOrder(tuple(rule.getKey().toString(), ruleMetadata.getAdHocName())); + .containsExactlyInAnyOrder(tuple(rule.getKey().toString(), rule.getAdHocName())); } @Test @@ -846,7 +849,7 @@ public class SearchActionTest { assertThat(ws.newRequest() .setMultiParam("author", singletonList("unknown")) .executeProtobuf(SearchWsResponse.class).getIssuesList()) - .isEmpty(); + .isEmpty(); } @Test diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleCreatorTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleCreatorTest.java index 46d27094d6d..41cf430db08 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleCreatorTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleCreatorTest.java @@ -579,7 +579,6 @@ public class RuleCreatorTest { .setCreatedAt(new Date().getTime()) .setUpdatedAt(new Date().getTime()); dbTester.rules().insert(templateRule); - dbTester.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleUuid(templateRule.getUuid())); dbTester.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*")); ruleIndexer.commitAndIndex(dbTester.getSession(), templateRule.getUuid()); return templateRule; diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java index 0dfcd2f2dbf..c0a447fe454 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/RuleUpdaterTest.java @@ -111,7 +111,6 @@ public class RuleUpdaterTest { .setRemediationGapMultiplier("1d") .setRemediationBaseEffort("5min"); db.rules().insert(ruleDto); - db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY); @@ -143,7 +142,6 @@ public class RuleUpdaterTest { .setRemediationGapMultiplier("1d") .setRemediationBaseEffort("5min"); db.rules().insert(ruleDto); - db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) @@ -169,7 +167,6 @@ public class RuleUpdaterTest { .setNoteData("my *note*") .setNoteUserUuid("me"); db.rules().insert(ruleDto); - db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) @@ -213,7 +210,6 @@ public class RuleUpdaterTest { .setTags(Sets.newHashSet("security")) .setSystemTags(Sets.newHashSet("java8", "javadoc")); db.rules().insert(ruleDto); - db.rules().insertOrUpdateMetadata(ruleDto.getMetadata()); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) @@ -313,7 +309,6 @@ public class RuleUpdaterTest { .setRemediationGapMultiplier(null) .setRemediationBaseEffort("1min"); db.rules().insert(ruleDto); - db.rules().insertOrUpdateMetadata(ruleDto.getMetadata().setRuleUuid(ruleDto.getUuid())); dbSession.commit(); RuleUpdate update = createForPluginRule(RULE_KEY) diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java index 57f92598018..bcb20ba7d4a 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/CreateActionTest.java @@ -91,7 +91,6 @@ public class CreateActionTest { // Template rule RuleDto templateRule = newTemplateRule(RuleKey.of("java", "S001")).setType(CODE_SMELL); db.rules().insert(templateRule); - db.rules().insertOrUpdateMetadata(templateRule.getMetadata().setRuleUuid(templateRule.getUuid())); db.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*")); String result = ws.newRequest() diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java index 12aa783dcb4..1d071bc6629 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/SearchActionTest.java @@ -21,6 +21,7 @@ package org.sonar.server.rule.ws; import java.util.HashSet; import java.util.List; +import java.util.Set; import java.util.function.Consumer; import java.util.stream.Collectors; import org.assertj.core.api.iterable.Extractor; @@ -41,7 +42,6 @@ import org.sonar.db.DbTester; import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsTester; @@ -71,6 +71,7 @@ import org.sonarqube.ws.Rules.SearchResponse; import static java.util.Arrays.asList; import static java.util.Arrays.stream; +import static java.util.Collections.emptySet; import static java.util.Collections.singleton; import static java.util.Collections.singletonList; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; @@ -156,8 +157,8 @@ public class SearchActionTest { @Test public void return_all_rules() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java")); - RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java")); + RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null)); + RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null)); indexRules(); verify(r -> { @@ -167,11 +168,9 @@ public class SearchActionTest { @Test public void return_note_login() { UserDto user1 = db.users().insertUser(); - RuleDto rule1 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule1, user1); + RuleDto rule1 = db.rules().insert(r -> r.setNoteUserUuid(user1.getUuid())); UserDto disableUser = db.users().insertDisabledUser(); - RuleDto rule2 = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule2, disableUser); + RuleDto rule2 = db.rules().insert(r -> r.setNoteUserUuid(disableUser.getUuid())); indexRules(); SearchResponse result = ws.newRequest() @@ -187,8 +186,8 @@ public class SearchActionTest { @Test public void filter_by_rule_key() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java")); - RuleDto rule2 = db.rules().insert(r1 -> r1.setLanguage("java")); + RuleDto rule1 = db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null)); + db.rules().insert(r1 -> r1.setLanguage("java").setNoteUserUuid(null)); indexRules(); verify(r -> r.setParam(PARAM_RULE_KEY, rule1.getKey().toString()), rule1); @@ -197,8 +196,8 @@ public class SearchActionTest { @Test public void filter_by_rule_name() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever")); - RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff")); + RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null)); + RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null)); indexRules(); verify(r -> r.setParam("q", "Be"), rule1); @@ -216,8 +215,8 @@ public class SearchActionTest { @Test public void filter_by_rule_name_requires_all_words_to_match() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever")); - RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff")); + RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null)); + RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null)); indexRules(); verify(r -> r.setParam("q", "Best other")); @@ -227,8 +226,8 @@ public class SearchActionTest { @Test public void filter_by_rule_name_does_not_interpret_query() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule for-ever")); - RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff")); + RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule for-ever").setNoteUserUuid(null)); + RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null)); indexRules(); // do not interpret "-" as a "not" @@ -238,8 +237,11 @@ public class SearchActionTest { @Test public void filter_by_rule_description() { RuleDto rule1 = db.rules() - .insert(r1 -> r1.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is the best rule now&forever"))); - RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Some other stuff")); + .insert( + r1 -> r1 + .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is the best rule now&forever")) + .setNoteUserUuid(null)); + db.rules().insert(r1 -> r1.setName("Some other stuff").setNoteUserUuid(null)); indexRules(); verify(r -> r.setParam("q", "Best "), rule1); @@ -249,9 +251,9 @@ public class SearchActionTest { @Test public void filter_by_rule_name_or_descriptions_requires_all_words_to_match_anywhere() { - RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever") + RuleDto rule1 = db.rules().insert(r1 -> r1.setName("Best rule ever").setNoteUserUuid(null) .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "This is a good rule"))); - RuleDto rule2 = db.rules().insert(r1 -> r1.setName("Another thing") + db.rules().insert(r1 -> r1.setName("Another thing").setNoteUserUuid(null) .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "Another thing"))); indexRules(); @@ -261,7 +263,7 @@ public class SearchActionTest { @Test public void return_all_rule_fields_by_default() { - RuleDto rule = db.rules().insert(r -> r.setLanguage("java")); + RuleDto rule = db.rules().insert(r -> r.setLanguage("java").setNoteUserUuid(null)); indexRules(); Rules.SearchResponse response = ws.newRequest().executeProtobuf(Rules.SearchResponse.class); @@ -341,28 +343,22 @@ public class SearchActionTest { @Test public void should_filter_on_specific_tags() { - RuleDto rule1 = db.rules().insert(r -> r.setLanguage("java")); - RuleMetadataDto metadata1 = insertMetadata(rule1, setTags("tag1", "tag2")); - RuleDto rule2 = db.rules().insert(r -> r.setLanguage("java")); - RuleMetadataDto metadata2 = insertMetadata(rule2); + RuleDto rule1 = db.rules().insert(r -> r.setLanguage("java").setTags(Set.of("tag1", "tag2"))); + db.rules().insert(r -> r.setLanguage("java")); indexRules(); Consumer request = r -> r .setParam("f", "repo,name") - .setParam("tags", metadata1.getTags().stream().collect(Collectors.joining(","))); + .setParam("tags", rule1.getTags().stream().collect(Collectors.joining(","))); verify(request, rule1); } @Test public void when_searching_for_several_tags_combine_them_with_OR() { - RuleDto bothTagsRule = db.rules().insert(r -> r.setLanguage("java")); - insertMetadata(bothTagsRule, setTags("tag1", "tag2")); - RuleDto oneTagRule = db.rules().insert(r -> r.setLanguage("java")); - insertMetadata(oneTagRule, setTags("tag1")); - RuleDto otherTagRule = db.rules().insert(r -> r.setLanguage("java")); - insertMetadata(otherTagRule, setTags("tag2")); - RuleDto noTagRule = db.rules().insert(r -> r.setLanguage("java")); - insertMetadata(noTagRule, setTags()); + RuleDto bothTagsRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1", "tag2")); + RuleDto oneTagRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1")); + RuleDto otherTagRule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag2")); + db.rules().insert(r -> r.setLanguage("java"), setTags()); indexRules(); Consumer request = r -> r @@ -374,8 +370,7 @@ public class SearchActionTest { @Test public void should_list_tags_in_tags_facet() { String[] tags = get101Tags(); - RuleDto rule = db.rules().insert(setSystemTags("x")); - insertMetadata(rule, setTags(tags)); + db.rules().insert(setSystemTags("x"), setTags(tags)); indexRules(); SearchResponse result = ws.newRequest() @@ -389,9 +384,8 @@ public class SearchActionTest { @Test public void should_list_tags_ordered_by_count_then_by_name_in_tags_facet() { - RuleDto rule = db.rules().insert(setSystemTags("tag7", "tag5", "tag3", "tag1", "tag9")); - insertMetadata(rule, setTags("tag2", "tag4", "tag6", "tag8", "tagA")); - db.rules().insert(setSystemTags("tag2")); + db.rules().insert(setSystemTags("tag7", "tag5", "tag3", "tag1", "tag9"), setTags("tag2", "tag4", "tag6", "tag8", "tagA")); + db.rules().insert(setSystemTags("tag2"), setTags()); indexRules(); SearchResponse result = ws.newRequest() @@ -405,7 +399,9 @@ public class SearchActionTest { @Test public void should_include_selected_matching_tag_in_facet() { - RuleDto rule = db.rules().insert(setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x")); + db.rules().insert( + setSystemTags("tag1", "tag2", "tag3", "tag4", "tag5", "tag6", "tag7", "tag8", "tag9", "tagA", "x"), + r -> r.setNoteUserUuid(null)); indexRules(); SearchResponse result = ws.newRequest() @@ -429,8 +425,7 @@ public class SearchActionTest { @Test public void should_return_specific_tags() { - RuleDto rule = db.rules().insert(r -> r.setLanguage("java")); - RuleMetadataDto metadata = insertMetadata(rule, setTags("tag1", "tag2")); + RuleDto rule = db.rules().insert(r -> r.setLanguage("java"), setTags("tag1", "tag2")); indexRules(); SearchResponse result = ws.newRequest() @@ -439,7 +434,7 @@ public class SearchActionTest { assertThat(result.getRulesList()).extracting(Rule::getKey).containsExactly(rule.getKey().toString()); assertThat(result.getRulesList()) .extracting(Rule::getTags).flatExtracting(Rules.Tags::getTagsList) - .containsExactly(metadata.getTags().toArray(new String[0])); + .containsExactly(rule.getTags().toArray(new String[0])); } @Test @@ -482,15 +477,13 @@ public class SearchActionTest { @Test public void search_debt_rules_with_default_and_overridden_debt_values() { - RuleDto rule = db.rules().insert(r -> r.setLanguage("java") + db.rules().insert(r -> r.setLanguage("java") .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefRemediationGapMultiplier("1h") - .setDefRemediationBaseEffort("15min")); - - RuleMetadataDto metadata = insertMetadata(rule, - r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) - .setRemediationGapMultiplier("2h") - .setRemediationBaseEffort("25min")); + .setDefRemediationBaseEffort("15min") + .setRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) + .setRemediationGapMultiplier("2h") + .setRemediationBaseEffort("25min")); indexRules(); @@ -516,15 +509,13 @@ public class SearchActionTest { @Test public void search_debt_rules_with_default_linear_offset_and_overridden_constant_debt() { - RuleDto rule = db.rules().insert(r -> r.setLanguage("java") + db.rules().insert(r -> r.setLanguage("java") .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefRemediationGapMultiplier("1h") - .setDefRemediationBaseEffort("15min")); - - RuleMetadataDto metadata = insertMetadata(rule, - r -> r.setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) - .setRemediationGapMultiplier(null) - .setRemediationBaseEffort("5min")); + .setDefRemediationBaseEffort("15min") + .setRemediationFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()) + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort("5min")); indexRules(); @@ -550,15 +541,13 @@ public class SearchActionTest { @Test public void search_debt_rules_with_default_linear_offset_and_overridden_linear_debt() { - RuleDto rule = db.rules().insert(r -> r.setLanguage("java") + db.rules().insert(r -> r.setLanguage("java") .setDefRemediationFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setDefRemediationGapMultiplier("1h") - .setDefRemediationBaseEffort("15min")); - - RuleMetadataDto metadata = insertMetadata(rule, - r -> r.setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) - .setRemediationGapMultiplier("1h") - .setRemediationBaseEffort(null)); + .setDefRemediationBaseEffort("15min") + .setRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) + .setRemediationGapMultiplier("1h") + .setRemediationBaseEffort(null)); indexRules(); @@ -640,7 +629,7 @@ public class SearchActionTest { @Test public void search_all_active_rules() { QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage("java")); - RuleDto rule = db.rules().insert(r -> r.setLanguage("java")); + RuleDto rule = db.rules().insert(r -> r.setLanguage("java").setNoteUserUuid(null)); RuleActivation activation = RuleActivation.create(rule.getUuid(), BLOCKER, null); qProfileRules.activateAndCommit(db.getSession(), profile, singleton(activation)); @@ -774,7 +763,9 @@ public class SearchActionTest { .setLanguage(profile.getLanguage()) .setRepositoryKey("repositoryKey1") .setSystemTags(new HashSet<>(singletonList("tag1"))) + .setTags(emptySet()) .setSeverity("CRITICAL") + .setNoteUserUuid(null) .setStatus(RuleStatus.BETA) .setType(RuleType.CODE_SMELL)); @@ -783,7 +774,9 @@ public class SearchActionTest { .setLanguage(profile.getLanguage()) .setRepositoryKey("repositoryKey2") .setSystemTags(new HashSet<>(singletonList("tag2"))) + .setTags(emptySet()) .setSeverity("MAJOR") + .setNoteUserUuid(null) .setStatus(RuleStatus.DEPRECATED) .setType(RuleType.VULNERABILITY)); RuleActivation activation = RuleActivation.create(rule2.getUuid(), null, null); @@ -794,6 +787,8 @@ public class SearchActionTest { .setLanguage("language3") .setRepositoryKey("repositoryKey3") .setSystemTags(new HashSet<>(singletonList("tag3"))) + .setTags(emptySet()) + .setNoteUserUuid(null) .setSeverity("BLOCKER") .setStatus(RuleStatus.READY) .setType(RuleType.BUG)); @@ -921,13 +916,6 @@ public class SearchActionTest { assertThat(result.getRulesList()).extracting(responseExtractor).containsExactly(expected); } - @SafeVarargs - private final RuleMetadataDto insertMetadata(RuleDto rule, Consumer... populaters) { - RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, populaters); - ruleIndexer.commitAndIndex(db.getSession(), rule.getUuid()); - return metadata; - } - private void verifyNoResults(Consumer requestPopulator) { verify(requestPopulator); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java index 6da5721cfd2..a2228b01e00 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/ShowActionTest.java @@ -35,7 +35,6 @@ import org.sonar.db.qualityprofile.ActiveRuleParamDto; import org.sonar.db.qualityprofile.QProfileDto; import org.sonar.db.rule.RuleDescriptionSectionDto; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.rule.RuleParamDto; import org.sonar.db.user.UserDto; import org.sonar.server.rule.RuleDescriptionFormatter; @@ -86,15 +85,17 @@ public class ShowActionTest { new ShowAction(db.getDbClient(), new RuleMapper(languages, macroInterpreter, new RuleDescriptionFormatter()), new ActiveRuleCompleter(db.getDbClient(), languages), new RuleWsSupport(db.getDbClient(), userSession))); + private UserDto userDto; @Before public void before() { + userDto = db.users().insertUser(); doReturn(INTERPRETED).when(macroInterpreter).interpret(anyString()); } @Test public void show_rule_key() { - RuleDto rule = db.rules().insert(); + RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(userDto.getUuid())); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -105,7 +106,7 @@ public class ShowActionTest { @Test public void show_rule_with_basic_info() { - RuleDto rule = db.rules().insert(); + RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(userDto.getUuid())); RuleParamDto ruleParam = db.rules().insertRuleParam(rule); ShowResponse result = ws.newRequest() @@ -128,22 +129,20 @@ public class ShowActionTest { @Test public void show_rule_tags() { - RuleDto rule = db.rules().insert(); - RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(rule, setTags("tag1", "tag2"), m -> m.setNoteData(null).setNoteUserUuid(null)); + RuleDto rule = db.rules().insert(setTags("tag1", "tag2"), r -> r.setNoteData(null).setNoteUserUuid(null)); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) .executeProtobuf(ShowResponse.class); assertThat(result.getRule().getTags().getTagsList()) - .containsExactly(metadata.getTags().toArray(new String[0])); + .containsExactly(rule.getTags().toArray(new String[0])); } @Test public void show_rule_with_note_login() { - RuleDto rule = db.rules().insert(); UserDto user = db.users().insertUser(); - db.rules().insertOrUpdateMetadata(rule, user); + RuleDto rule = db.rules().insert(r -> r.setNoteUserUuid(user.getUuid())); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -158,7 +157,11 @@ public class ShowActionTest { .setDefRemediationFunction("LINEAR_OFFSET") .setDefRemediationGapMultiplier("5d") .setDefRemediationBaseEffort("10h") - .setGapDescription("gap desc")); + .setGapDescription("gap desc") + .setNoteUserUuid(userDto.getUuid()) + .setRemediationFunction(null) + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort(null)); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -181,13 +184,12 @@ public class ShowActionTest { RuleDto rule = db.rules().insert(r -> r .setDefRemediationFunction(null) .setDefRemediationGapMultiplier(null) - .setDefRemediationBaseEffort(null)); - db.rules().insertOrUpdateMetadata(rule, - m -> m.setNoteData(null).setNoteUserUuid(null), - m -> m - .setRemediationFunction("LINEAR_OFFSET") - .setRemediationGapMultiplier("5d") - .setRemediationBaseEffort("10h")); + .setDefRemediationBaseEffort(null) + .setNoteData(null) + .setNoteUserUuid(null) + .setRemediationFunction("LINEAR_OFFSET") + .setRemediationGapMultiplier("5d") + .setRemediationBaseEffort("10h")); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -209,12 +211,12 @@ public class ShowActionTest { RuleDto rule = db.rules().insert(r -> r .setDefRemediationFunction("LINEAR_OFFSET") .setDefRemediationGapMultiplier("5d") - .setDefRemediationBaseEffort("10h")); - db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null), - m -> m - .setRemediationFunction("CONSTANT_ISSUE") - .setRemediationGapMultiplier(null) - .setRemediationBaseEffort("15h")); + .setDefRemediationBaseEffort("10h") + .setNoteData(null) + .setNoteUserUuid(null) + .setRemediationFunction("CONSTANT_ISSUE") + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort("15h")); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -237,12 +239,12 @@ public class ShowActionTest { RuleDto rule = db.rules().insert(r -> r .setDefRemediationFunction(null) .setDefRemediationGapMultiplier(null) - .setDefRemediationBaseEffort(null)); - db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null), - m -> m - .setRemediationFunction(null) - .setRemediationGapMultiplier(null) - .setRemediationBaseEffort(null)); + .setDefRemediationBaseEffort(null) + .setNoteData(null) + .setNoteUserUuid(null) + .setRemediationFunction(null) + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort(null)); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -265,12 +267,12 @@ public class ShowActionTest { .setDefRemediationFunction("LINEAR_OFFSET") .setDefRemediationGapMultiplier("5d") .setDefRemediationBaseEffort("10h") - .setGapDescription("gap desc")); - db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null), - m -> m - .setRemediationFunction("CONSTANT_ISSUE") - .setRemediationGapMultiplier(null) - .setRemediationBaseEffort("15h")); + .setGapDescription("gap desc") + .setNoteData(null) + .setNoteUserUuid(null) + .setRemediationFunction("CONSTANT_ISSUE") + .setRemediationGapMultiplier(null) + .setRemediationBaseEffort("15h")); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, rule.getKey().toString()) @@ -296,7 +298,8 @@ public class ShowActionTest { // Custom rule RuleDto customRule = newCustomRule(templateRule) .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "
line1\nline2
")) - .setDescriptionFormat(MARKDOWN); + .setDescriptionFormat(MARKDOWN) + .setNoteUserUuid(userDto.getUuid()); db.rules().insert(customRule); doReturn("<div>line1
line2</div>").when(macroInterpreter).interpret("
line1\nline2
"); @@ -313,7 +316,8 @@ public class ShowActionTest { public void show_external_rule() { RuleDto externalRule = db.rules().insert(r -> r .setIsExternal(true) - .setName("ext rule name")); + .setName("ext rule name") + .setNoteUserUuid(userDto.getUuid())); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, externalRule.getKey().toString()) @@ -327,15 +331,14 @@ public class ShowActionTest { public void show_adhoc_rule() { RuleDto externalRule = db.rules().insert(r -> r .setIsExternal(true) - .setIsAdHoc(true)); - RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m + .setIsAdHoc(true) .setAdHocName("adhoc name") .setAdHocDescription("
desc
") .setAdHocSeverity(Severity.BLOCKER) .setAdHocType(RuleType.VULNERABILITY) .setNoteData(null) .setNoteUserUuid(null)); - doReturn("<div>desc2</div>").when(macroInterpreter).interpret(metadata.getAdHocDescription()); + doReturn("<div>desc2</div>").when(macroInterpreter).interpret(externalRule.getAdHocDescription()); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, externalRule.getKey().toString()) @@ -357,6 +360,7 @@ public class ShowActionTest { RuleDto rule = createRuleWithDescriptionSections(section1, section2, section3); rule.setType(RuleType.SECURITY_HOTSPOT); + rule.setNoteUserUuid(userDto.getUuid()); db.rules().insert(rule); ShowResponse result = ws.newRequest() @@ -399,6 +403,7 @@ public class ShowActionTest { RuleDto rule = createRuleWithDescriptionSections(section); rule.setDescriptionFormat(MARKDOWN); + rule.setNoteUserUuid(userDto.getUuid()); db.rules().insert(rule); ShowResponse result = ws.newRequest() @@ -423,15 +428,14 @@ public class ShowActionTest { .setName("predefined name") .addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(uuidFactory.create(), "
predefined desc
")) .setSeverity(Severity.BLOCKER) - .setType(RuleType.VULNERABILITY)); - RuleMetadataDto metadata = db.rules().insertOrUpdateMetadata(externalRule, m -> m + .setType(RuleType.VULNERABILITY) .setAdHocName("adhoc name") .setAdHocDescription("
adhoc desc
") .setAdHocSeverity(Severity.MAJOR) .setAdHocType(RuleType.CODE_SMELL) .setNoteData(null) .setNoteUserUuid(null)); - doReturn("<div>adhoc desc</div>").when(macroInterpreter).interpret(metadata.getAdHocDescription()); + doReturn("<div>adhoc desc</div>").when(macroInterpreter).interpret(externalRule.getAdHocDescription()); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, externalRule.getKey().toString()) @@ -451,7 +455,13 @@ public class ShowActionTest { .setName(null) .setDescriptionFormat(null) .setSeverity((String) null) - .setType(0)); + .setNoteData(null) + .setNoteUserUuid(null) + .setAdHocDescription(null) + .setType(0) + .setAdHocSeverity(null) + .setAdHocName(null) + .setAdHocType(0)); ShowResponse result = ws.newRequest() .setParam(PARAM_KEY, externalRule.getKey().toString()) @@ -465,9 +475,8 @@ public class ShowActionTest { @Test public void show_rule_with_activation() { - RuleDto rule = db.rules().insert(); + RuleDto rule = db.rules().insert(r -> r.setNoteData(null).setNoteUserUuid(null)); RuleParamDto ruleParam = db.rules().insertRuleParam(rule, p -> p.setType("STRING").setDescription("Reg *exp*").setDefaultValue(".*")); - db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null)); QProfileDto qProfile = db.qualityProfiles().insert(); ActiveRuleDto activeRule = db.qualityProfiles().activateRule(qProfile, rule); db.getDbClient().activeRuleDao().insertParam(db.getSession(), activeRule, new ActiveRuleParamDto() @@ -492,8 +501,7 @@ public class ShowActionTest { @Test public void show_rule_without_activation() { - RuleDto rule = db.rules().insert(); - db.rules().insertOrUpdateMetadata(rule, m -> m.setNoteData(null).setNoteUserUuid(null)); + RuleDto rule = db.rules().insert(r -> r.setNoteData(null).setNoteUserUuid(null)); QProfileDto qProfile = db.qualityProfiles().insert(); db.qualityProfiles().activateRule(qProfile, rule); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/TagsActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/TagsActionTest.java index 7864d6d1cf1..f731fe5b081 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/TagsActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/TagsActionTest.java @@ -79,7 +79,7 @@ public class TagsActionTest { @Test public void system_tag() { - RuleDto r = db.rules().insert(setSystemTags("tag")); + RuleDto r = db.rules().insert(setSystemTags("tag"), setTags()); ruleIndexer.commitAndIndex(db.getSession(), r.getUuid()); String result = ws.newRequest().execute().getInput(); @@ -88,9 +88,8 @@ public class TagsActionTest { @Test public void tag() { - RuleDto r = db.rules().insert(setSystemTags()); + RuleDto r = db.rules().insert(setSystemTags(), setTags("tag")); ruleIndexer.commitAndIndex(db.getSession(), r.getUuid()); - db.rules().insertOrUpdateMetadata(r, setTags("tag")); ruleIndexer.commitAndIndex(db.getSession(), r.getUuid()); String result = ws.newRequest().execute().getInput(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java index ef3ed9d9032..c5dc656057e 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/rule/ws/UpdateActionTest.java @@ -31,7 +31,6 @@ import org.sonar.db.DbClient; import org.sonar.db.DbTester; import org.sonar.db.rule.RuleDescriptionSectionDto; import org.sonar.db.rule.RuleDto; -import org.sonar.db.rule.RuleMetadataDto; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsClient; import org.sonar.server.es.EsTester; @@ -107,6 +106,7 @@ public class UpdateActionTest { RuleDto templateRule = db.rules().insert( r -> r.setRuleKey(RuleKey.of("java", "S001")), r -> r.setIsTemplate(true), + r -> r.setNoteUserUuid(null), r -> r.setCreatedAt(PAST), r -> r.setUpdatedAt(PAST)); db.rules().insertRuleParam(templateRule, param -> param.setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*")); @@ -118,6 +118,7 @@ public class UpdateActionTest { r -> r.setStatus(RuleStatus.BETA), r -> r.setTemplateUuid(templateRule.getUuid()), r -> r.setLanguage("js"), + r -> r.setNoteUserUuid(null), r -> r.setCreatedAt(PAST), r -> r.setUpdatedAt(PAST)); db.rules().insertRuleParam(customRule, param -> param.setName("regex").setType("a").setDescription("Reg ex")); @@ -156,8 +157,7 @@ public class UpdateActionTest { public void update_tags() { logInAsQProfileAdministrator(); - RuleDto rule = db.rules().insert(setSystemTags("stag1", "stag2")); - db.rules().insertOrUpdateMetadata(rule, setTags("tag1", "tag2"), m -> m.setNoteData(null).setNoteUserUuid(null)); + RuleDto rule = db.rules().insert(setSystemTags("stag1", "stag2"), setTags("tag1", "tag2"), r -> r.setNoteData(null).setNoteUserUuid(null)); Rules.UpdateResponse result = ws.newRequest().setMethod("POST") .setParam(PARAM_KEY, rule.getKey().toString()) @@ -179,7 +179,8 @@ public class UpdateActionTest { RuleDto rule = db.rules().insert( r -> r.setDefRemediationFunction(LINEAR.toString()), r -> r.setDefRemediationGapMultiplier("10d"), - r -> r.setDefRemediationBaseEffort(null)); + r -> r.setDefRemediationBaseEffort(null), + r -> r.setNoteUserUuid(null)); String newOffset = LINEAR_OFFSET.toString(); String newMultiplier = "15d"; @@ -206,18 +207,17 @@ public class UpdateActionTest { assertThat(updatedRule.getRemFnBaseEffort()).isEqualTo(newEffort); // check database - RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey()) + RuleDto updatedRuleDto = db.getDbClient().ruleDao().selectByKey(db.getSession(), rule.getKey()) .orElseThrow(() -> new IllegalStateException("Cannot load metadata")); - assertThat(metadataOfSpecificOrg.getRemediationFunction()).isEqualTo(newOffset); - assertThat(metadataOfSpecificOrg.getRemediationGapMultiplier()).isEqualTo(newMultiplier); - assertThat(metadataOfSpecificOrg.getRemediationBaseEffort()).isEqualTo(newEffort); + assertThat(updatedRuleDto.getRemediationFunction()).isEqualTo(newOffset); + assertThat(updatedRuleDto.getRemediationGapMultiplier()).isEqualTo(newMultiplier); + assertThat(updatedRuleDto.getRemediationBaseEffort()).isEqualTo(newEffort); } @Test public void update_note() { - RuleDto rule = db.rules().insert(); UserDto userHavingUpdatingNote = db.users().insertUser(); - db.rules().insertOrUpdateMetadata(rule, userHavingUpdatingNote, m -> m.setNoteData("old data")); + RuleDto rule = db.rules().insert(m -> m.setNoteData("old data").setNoteUserUuid(userHavingUpdatingNote.getUuid())); UserDto userAuthenticated = db.users().insertUser(); userSession.logIn(userAuthenticated).addPermission(ADMINISTER_QUALITY_PROFILES); @@ -233,9 +233,9 @@ public class UpdateActionTest { assertThat(updatedRule.getNoteLogin()).isEqualTo(userAuthenticated.getLogin()); // check database - RuleMetadataDto metadataOfSpecificOrg = db.getDbClient().ruleDao().selectMetadataByKey(db.getSession(), rule.getKey()).get(); - assertThat(metadataOfSpecificOrg.getNoteData()).isEqualTo("new data"); - assertThat(metadataOfSpecificOrg.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid()); + RuleDto updatedRuleDto = db.getDbClient().ruleDao().selectByKey(db.getSession(), rule.getKey()).get(); + assertThat(updatedRuleDto.getNoteData()).isEqualTo("new data"); + assertThat(updatedRuleDto.getNoteUserUuid()).isEqualTo(userAuthenticated.getUuid()); } @Test -- 2.39.5