]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16302 Remove usage of field createdAt and updatedAt on RuleMetadataDto
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Thu, 5 May 2022 09:01:43 +0000 (11:01 +0200)
committersonartech <sonartech@sonarsource.com>
Fri, 6 May 2022 20:02:44 +0000 (20:02 +0000)
server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreator.java
server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/issue/AdHocRuleCreatorTest.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMetadataDto.java
server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java
server/sonar-db-dao/src/testFixtures/java/org/sonar/db/rule/RuleTesting.java
server/sonar-webserver-api/src/main/java/org/sonar/server/rule/CachingRuleFinder.java

index c03ddef9fbf658cfd61c7badebd6628a5f9beb2d..477c2a8bab8774aa28a30cbfa52a6442a3b5ef1b 100644 (file)
@@ -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);
       }
 
index 680a1e675b18382e728b7f4c7b867cf8dc164582..c537404f6370ce2ab211620ad35959ab1fa19702 100644 (file)
@@ -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());
   }
 
 }
index 61f95d0d4811170df69095c30050eb5396a6a1aa..0530604d30288d9340c4a41e155b1458191b76d4 100644 (file)
@@ -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<String> deserializeTagsString(@Nullable String tags) {
     return deserializeStringSet(tags);
   }
index 2ed35d03f6fb75a61fe58bd18f51a24b0c34a1fd..cf3c620cf1994cc8874791635aea5c781dd31da2 100644 (file)
@@ -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 +
       '}';
   }
 
index 02ada78213780803a36e303dd67c5523605604d2..261298c4cd3b5da0908eb426ea85bf5492e83111 100644 (file)
@@ -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",
     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 &lt; 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 &lt; rm.updated_at then rm.updated_at
-                                    else r.updated_at
-                                    end end               as "updatedAt"
+    rm.ad_hoc_type as "adHocType"
   </sql>
 
   <sql id="outerJoinRulesMetadata">
       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
       ad_hoc_name,
       ad_hoc_description,
       ad_hoc_severity,
-      ad_hoc_type,
-      created_at,
-      updated_at
+      ad_hoc_type
     )
     values (
       #{ruleUuid,jdbcType=VARCHAR},
       #{adHocName,jdbcType=VARCHAR},
       #{adHocDescription,jdbcType=CLOB},
       #{adHocSeverity,jdbcType=VARCHAR},
-      #{adHocType,jdbcType=TINYINT},
-      #{createdAt,jdbcType=BIGINT},
-      #{updatedAt,jdbcType=BIGINT}
+      #{adHocType,jdbcType=TINYINT}
     )
   </insert>
 
       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}
   </update>
index 21a26e8ee8f9b147873d79ce69f618c89a040c69..15b4249eace243996e7babf441f028c1203f5b9f 100644 (file)
@@ -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) {
index c511d78af71b3621336ee09cd6ba179f827d5dde..982b9e4820a037c295e4ce36a71d0cc7c358a865 100644 (file)
@@ -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) {
index 313914a6f96ecb1359bfe2e9fd0f71f20bd4db37..c66e48d1bc35a1a0fe32708ca002d727b7bdafa9 100644 (file)
@@ -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<org.sonar.api.rules.RuleParam> apiParams = new ArrayList<>();
     for (RuleParamDto param : params) {