From: Léo Geoffroy Date: Thu, 5 May 2022 09:01:43 +0000 (+0200) Subject: SONAR-16302 Remove usage of field createdAt and updatedAt on RuleMetadataDto X-Git-Tag: 9.5.0.56709~163 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=618432bd5de5443257e4cb610d26e5763646ef57;p=sonarqube.git SONAR-16302 Remove usage of field createdAt and updatedAt on RuleMetadataDto --- 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 c03ddef9fbf..477c2a8bab8 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 @@ -85,8 +85,7 @@ public class AdHocRuleCreator { changed = true; } if (changed) { - metadata.setUpdatedAt(now); - metadata.setCreatedAt(ruleDtoToUpdate.getCreatedAt()); + ruleDtoToUpdate.setUpdatedAt(now); dao.update(dbSession, ruleDtoToUpdate); } 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 680a1e675b1..c537404f637 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 @@ -151,8 +151,7 @@ public class AdHocRuleCreatorTest { assertThat(ruleUpdated.getMetadata().getAdHocSeverity()).isEqualTo(Severity.CRITICAL); assertThat(ruleUpdated.getMetadata().getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); assertThat(ruleUpdated.getCreatedAt()).isEqualTo(creationDate); - assertThat(ruleUpdated.getMetadata().getCreatedAt()).isEqualTo(creationDate); - assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isGreaterThan(creationDate); + assertThat(ruleUpdated.getUpdatedAt()).isGreaterThan(creationDate); } @Test @@ -178,14 +177,12 @@ public class AdHocRuleCreatorTest { assertThat(ruleUpdated.getSeverity()).isEqualTo(rule.getSeverity()); assertThat(ruleUpdated.getType()).isEqualTo(rule.getType()); assertThat(ruleUpdated.getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleUpdated.getUpdatedAt()).isEqualTo(ruleMetadata.getUpdatedAt()); + 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.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleUpdated.getMetadata().getUpdatedAt()).isEqualTo(ruleMetadata.getUpdatedAt()); } } 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 61f95d0d481..0530604d302 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 @@ -110,7 +110,6 @@ public class RuleDto { this.metadata = metadata; } - public RuleMetadataDto getMetadata() { return metadata; } @@ -212,7 +211,6 @@ public class RuleDto { return ruleDescriptionSectionDtos.stream().filter(section -> section.getKey().equals(ruleDescriptionSectionKey)).findAny(); } - @CheckForNull public Format getDescriptionFormat() { return descriptionFormat; @@ -407,16 +405,12 @@ public class RuleDto { return this; } - public long getCreatedAt() { return createdAt; } - public RuleDto setCreatedAt(@Nullable Long createdAt) { - if ((this.createdAt == 0 && createdAt != null) || (createdAt != null && createdAt < this.createdAt)) { - this.createdAt = createdAt; - metadata.setCreatedAt(createdAt); - } + public RuleDto setCreatedAt(long createdAt) { + this.createdAt = createdAt; return this; } @@ -424,11 +418,8 @@ public class RuleDto { return updatedAt; } - public RuleDto setUpdatedAt(@Nullable Long updatedAt) { - if (updatedAt != null && updatedAt > this.updatedAt) { - this.updatedAt = updatedAt; - metadata.setUpdatedAt(updatedAt); - } + public RuleDto setUpdatedAt(long updatedAt) { + this.updatedAt = updatedAt; return this; } @@ -513,7 +504,6 @@ public class RuleDto { return this; } - @CheckForNull public String getRemediationFunction() { return metadata.getRemediationFunction(); @@ -555,7 +545,6 @@ public class RuleDto { metadata.setTagsField(s); } - public static Set deserializeTagsString(@Nullable String tags) { return deserializeStringSet(tags); } 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 index 2ed35d03f6f..cf3c620cf19 100644 --- 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 @@ -63,9 +63,6 @@ public class RuleMetadataDto { */ private Integer adHocType; - private long createdAt; - private long updatedAt; - public RuleMetadataDto() { // nothing to do here } @@ -217,23 +214,9 @@ public class RuleMetadataDto { return this; } - public long getCreatedAt() { - return createdAt; - } - public RuleMetadataDto setCreatedAt(long createdAt) { - this.createdAt = createdAt; - return this; - } - public long getUpdatedAt() { - return updatedAt; - } - public RuleMetadataDto setUpdatedAt(long updatedAt) { - this.updatedAt = updatedAt; - return this; - } @Override public String toString() { @@ -247,8 +230,6 @@ public class RuleMetadataDto { ", remediationGapMultiplier='" + remediationGapMultiplier + '\'' + ", remediationBaseEffort='" + remediationBaseEffort + '\'' + ", tags='" + tags + '\'' + - ", createdAt=" + createdAt + - ", updatedAt=" + updatedAt + '}'; } 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 02ada782137..261298c4cd3 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 @@ -34,6 +34,8 @@ r.rule_type as "type", r.plugin_key as "pluginKey", 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", @@ -45,19 +47,7 @@ rm.ad_hoc_name as "adHocName", rm.ad_hoc_description as "adHocDescription", rm.ad_hoc_severity as "adHocSeverity", - rm.ad_hoc_type as "adHocType", - case - when rm.created_at is null then r.created_at - else case - when r.created_at < rm.created_at then r.created_at - else rm.created_at - end end as "createdAt", - case - when rm.updated_at is null then r.updated_at - else case - when r.updated_at < rm.updated_at then rm.updated_at - else r.updated_at - end end as "updatedAt" + rm.ad_hoc_type as "adHocType" @@ -217,9 +207,7 @@ rm.ad_hoc_name as "adHocName", rm.ad_hoc_description as "adHocDescription", rm.ad_hoc_severity as "adHocSeverity", - rm.ad_hoc_type as "adHocType", - rm.created_at as "createdAt", - rm.updated_at as "updatedAt" + rm.ad_hoc_type as "adHocType" from rules_metadata rm inner join rules r on rm.rule_uuid = r.uuid @@ -406,9 +394,7 @@ ad_hoc_name, ad_hoc_description, ad_hoc_severity, - ad_hoc_type, - created_at, - updated_at + ad_hoc_type ) values ( #{ruleUuid,jdbcType=VARCHAR}, @@ -423,9 +409,7 @@ #{adHocName,jdbcType=VARCHAR}, #{adHocDescription,jdbcType=CLOB}, #{adHocSeverity,jdbcType=VARCHAR}, - #{adHocType,jdbcType=TINYINT}, - #{createdAt,jdbcType=BIGINT}, - #{updatedAt,jdbcType=BIGINT} + #{adHocType,jdbcType=TINYINT} ) @@ -442,8 +426,7 @@ ad_hoc_name=#{adHocName,jdbcType=VARCHAR}, ad_hoc_description=#{adHocDescription,jdbcType=CLOB}, ad_hoc_severity=#{adHocSeverity,jdbcType=VARCHAR}, - ad_hoc_type=#{adHocType,jdbcType=TINYINT}, - updated_at=#{updatedAt,jdbcType=BIGINT} + ad_hoc_type=#{adHocType,jdbcType=TINYINT} where rule_uuid=#{ruleUuid,jdbcType=VARCHAR} 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 21a26e8ee8f..15b4249eace 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 @@ -586,6 +586,8 @@ public class RuleDaoTest { public void update_RuleMetadataDto_inserts_row_in_RULE_METADATA_if_not_exists_yet() { RuleDto rule = db.rules().insert(); + long createdAtBeforeUpdate = rule.getCreatedAt(); + rule.setNoteData("My note"); rule.setNoteUserUuid("admin"); rule.setNoteCreatedAt(DateUtils.parseDate("2013-12-19").getTime()); @@ -618,7 +620,7 @@ public class RuleDaoTest { assertThat(ruleDto.getAdHocSeverity()).isEqualTo(Severity.BLOCKER); assertThat(ruleDto.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); assertThat(ruleDto.getSecurityStandards()).isEmpty(); - assertThat(ruleDto.getCreatedAt()).isEqualTo(rule.getCreatedAt()); + assertThat(ruleDto.getCreatedAt()).isEqualTo(createdAtBeforeUpdate); assertThat(ruleDto.getUpdatedAt()).isEqualTo(4_000_000_000_000L); // Info from rule definition assertThat(ruleDto.getDefRemediationFunction()).isEqualTo(rule.getDefRemediationFunction()); @@ -636,29 +638,28 @@ public class RuleDaoTest { rule.setCreatedAt(3_500_000_000_000L); rule.setUpdatedAt(4_000_000_000_000L); + RuleDto ruleDtoBeforeUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey()); underTest.update(db.getSession(), rule); db.commit(); assertThat(db.countRowsOfTable("RULES_METADATA")).isOne(); - RuleDto ruleDto = underTest.selectOrFailByKey(db.getSession(), rule.getKey()); - assertThat(ruleDto.getNoteData()).isNull(); - assertThat(ruleDto.getNoteUserUuid()).isNull(); - assertThat(ruleDto.getNoteCreatedAt()).isNull(); - assertThat(ruleDto.getNoteUpdatedAt()).isNull(); - assertThat(ruleDto.getRemediationFunction()).isNull(); - assertThat(ruleDto.getRemediationGapMultiplier()).isNull(); - assertThat(ruleDto.getRemediationBaseEffort()).isNull(); - assertThat(ruleDto.getTags()).isEmpty(); - assertThat(ruleDto.getAdHocName()).isNull(); - assertThat(ruleDto.getAdHocDescription()).isEqualTo("ad-hoc-desc"); - assertThat(ruleDto.getAdHocSeverity()).isNull(); - assertThat(ruleDto.getAdHocType()).isNull(); - assertThat(ruleDto.getSecurityStandards()).isEmpty(); - assertThat(ruleDto.getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleDto.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleDto.getUpdatedAt()).isEqualTo(4_000_000_000_000L); - assertThat(ruleDto.getMetadata().getUpdatedAt()).isEqualTo(4_000_000_000_000L); + 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.getAdHocDescription()).isEqualTo("ad-hoc-desc"); + assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isNull(); + assertThat(ruleDtoAfterUpdate.getAdHocType()).isNull(); + assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEmpty(); + assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt()); + assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(4_000_000_000_000L); rule.setNoteData("My note"); rule.setNoteUserUuid("admin"); @@ -677,24 +678,22 @@ public class RuleDaoTest { underTest.update(db.getSession(), rule); db.commit(); - ruleDto = underTest.selectOrFailByKey(db.getSession(), rule.getKey()); - assertThat(ruleDto.getNoteData()).isEqualTo("My note"); - assertThat(ruleDto.getNoteUserUuid()).isEqualTo("admin"); - assertThat(ruleDto.getNoteCreatedAt()).isNotNull(); - assertThat(ruleDto.getNoteUpdatedAt()).isNotNull(); - assertThat(ruleDto.getRemediationFunction()).isEqualTo("LINEAR"); - assertThat(ruleDto.getRemediationGapMultiplier()).isEqualTo("1h"); - assertThat(ruleDto.getRemediationBaseEffort()).isEqualTo("5min"); - assertThat(ruleDto.getTags()).containsOnly("tag1", "tag2"); - assertThat(ruleDto.getAdHocName()).isEqualTo("ad hoc name"); - assertThat(ruleDto.getAdHocDescription()).isEqualTo("ad hoc desc"); - assertThat(ruleDto.getAdHocSeverity()).isEqualTo(Severity.BLOCKER); - assertThat(ruleDto.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); - assertThat(ruleDto.getSecurityStandards()).isEmpty(); - assertThat(ruleDto.getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleDto.getMetadata().getCreatedAt()).isEqualTo(rule.getCreatedAt()); - assertThat(ruleDto.getUpdatedAt()).isEqualTo(7_000_000_000_000L); - assertThat(ruleDto.getMetadata().getUpdatedAt()).isEqualTo(7_000_000_000_000L); + ruleDtoAfterUpdate = underTest.selectOrFailByKey(db.getSession(), rule.getKey()); + assertThat(ruleDtoAfterUpdate.getNoteData()).isEqualTo("My note"); + assertThat(ruleDtoAfterUpdate.getNoteUserUuid()).isEqualTo("admin"); + assertThat(ruleDtoAfterUpdate.getNoteCreatedAt()).isNotNull(); + assertThat(ruleDtoAfterUpdate.getNoteUpdatedAt()).isNotNull(); + assertThat(ruleDtoAfterUpdate.getRemediationFunction()).isEqualTo("LINEAR"); + assertThat(ruleDtoAfterUpdate.getRemediationGapMultiplier()).isEqualTo("1h"); + assertThat(ruleDtoAfterUpdate.getRemediationBaseEffort()).isEqualTo("5min"); + assertThat(ruleDtoAfterUpdate.getTags()).containsOnly("tag1", "tag2"); + assertThat(ruleDtoAfterUpdate.getAdHocName()).isEqualTo("ad hoc name"); + assertThat(ruleDtoAfterUpdate.getAdHocDescription()).isEqualTo("ad hoc desc"); + assertThat(ruleDtoAfterUpdate.getAdHocSeverity()).isEqualTo(Severity.BLOCKER); + assertThat(ruleDtoAfterUpdate.getAdHocType()).isEqualTo(RuleType.CODE_SMELL.getDbConstant()); + assertThat(ruleDtoAfterUpdate.getSecurityStandards()).isEmpty(); + assertThat(ruleDtoAfterUpdate.getCreatedAt()).isEqualTo(ruleDtoBeforeUpdate.getCreatedAt()); + assertThat(ruleDtoAfterUpdate.getUpdatedAt()).isEqualTo(7_000_000_000_000L); } @Test @@ -895,11 +894,11 @@ public class RuleDaoTest { RuleForIndexingDto firstRule = findRuleForIndexingWithUuid(accumulator, r1.getUuid()); RuleForIndexingDto secondRule = findRuleForIndexingWithUuid(accumulator, r2.getUuid()); - assertRuleDefinitionFieldsAreEquals(r1, r1.getMetadata().getUpdatedAt(), firstRule); + assertRuleDefinitionFieldsAreEquals(r1, firstRule); assertRuleMetadataFieldsAreEquals(r1.getMetadata(), firstRule); assertThat(firstRule.getTemplateRuleKey()).isNull(); assertThat(firstRule.getTemplateRepository()).isNull(); - assertRuleDefinitionFieldsAreEquals(r2, r2.getUpdatedAt(), secondRule); + assertRuleDefinitionFieldsAreEquals(r2, secondRule); assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey()); assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey()); } @@ -937,15 +936,15 @@ public class RuleDaoTest { RuleForIndexingDto firstRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r1.getUuid())).findFirst().get(); RuleForIndexingDto secondRule = accumulator.list.stream().filter(t -> t.getUuid().equals(r2.getUuid())).findFirst().get(); - assertRuleDefinitionFieldsAreEquals(r1, r1.getMetadata().getUpdatedAt(), firstRule); + assertRuleDefinitionFieldsAreEquals(r1, firstRule); assertThat(firstRule.getTemplateRuleKey()).isNull(); assertThat(firstRule.getTemplateRepository()).isNull(); - assertRuleDefinitionFieldsAreEquals(r2, r2.getUpdatedAt(), secondRule); + assertRuleDefinitionFieldsAreEquals(r2, secondRule); assertThat(secondRule.getTemplateRuleKey()).isEqualTo(r1.getRuleKey()); assertThat(secondRule.getTemplateRepository()).isEqualTo(r1.getRepositoryKey()); } - private void assertRuleDefinitionFieldsAreEquals(RuleDto r1, long updatedAt, RuleForIndexingDto ruleForIndexing) { + private void assertRuleDefinitionFieldsAreEquals(RuleDto r1, RuleForIndexingDto ruleForIndexing) { assertThat(ruleForIndexing.getUuid()).isEqualTo(r1.getUuid()); assertThat(ruleForIndexing.getRuleKey()).isEqualTo(r1.getKey()); assertThat(ruleForIndexing.getRepository()).isEqualTo(r1.getRepositoryKey()); @@ -966,7 +965,7 @@ public class RuleDaoTest { assertThat(ruleForIndexing.getType()).isEqualTo(r1.getType()); assertThat(ruleForIndexing.getTypeAsRuleType()).isEqualTo(RuleType.valueOf(r1.getType())); assertThat(ruleForIndexing.getCreatedAt()).isEqualTo(r1.getCreatedAt()); - assertThat(ruleForIndexing.getUpdatedAt()).isEqualTo(updatedAt); + assertThat(ruleForIndexing.getUpdatedAt()).isEqualTo(r1.getUpdatedAt()); } private static void assertRuleMetadataFieldsAreEquals(RuleMetadataDto r1Metadatas, RuleForIndexingDto firstRule) { 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 c511d78af71..982b9e4820a 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 @@ -115,9 +115,7 @@ 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)]) - .setCreatedAt(System.currentTimeMillis()) - .setUpdatedAt(System.currentTimeMillis() + 50); + .setAdHocType(RuleType.values()[nextInt(RuleType.values().length - 1)]); } public static RuleMetadataDto newRuleMetadata(RuleDto rule) { diff --git a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java index 313914a6f96..c66e48d1bc3 100644 --- a/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java +++ b/server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java @@ -160,7 +160,7 @@ public class CachingRuleFinder implements ServerRuleFinder { .setTags(new String[0]) .setDescription(RuleDescriptionFormatter.getDescriptionAsHtml(ruleDto)); - Optional.ofNullable(ruleDto.getLanguage()).map(apiRule::setLanguage); + Optional.ofNullable(ruleDto.getLanguage()).ifPresent(apiRule::setLanguage); List apiParams = new ArrayList<>(); for (RuleParamDto param : params) {