From 25c379d3e6620bd94d77c8990d14640941baed00 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 26 Feb 2016 15:44:39 +0100 Subject: [PATCH] SONAR-7330 Remove Dto interface from RuleDto and ActiveRuleDto --- .../org/sonar/server/rule/RegisterRules.java | 4 ---- .../java/org/sonar/server/rule/RuleUpdater.java | 1 - .../org/sonar/server/rule/ws/RuleMapper.java | 4 ++-- .../org/sonar/server/rule/RegisterRulesTest.java | 16 ++++++++-------- .../startup/ClearRulesOverloadedDebtTest.java | 9 ++++----- .../sonar/db/qualityprofile/ActiveRuleDto.java | 9 +++------ .../src/main/java/org/sonar/db/rule/RuleDto.java | 5 +---- .../sonar/db/qualityprofile/ActiveRuleMapper.xml | 9 ++------- .../resources/org/sonar/db/rule/RuleMapper.xml | 8 ++------ 9 files changed, 22 insertions(+), 43 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index b53ffdd268a..03f4b64f31b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -28,7 +28,6 @@ import com.google.common.collect.Sets; import java.util.ArrayList; import java.util.Collection; import java.util.Collections; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.Map; @@ -174,8 +173,6 @@ public class RegisterRules implements Startable { .setSystemTags(ruleDef.tags()) .setCreatedAtInMs(system2.now()) .setUpdatedAtInMs(system2.now()); - ruleDto.setCreatedAt(new Date(system2.now())); - ruleDto.setUpdatedAt(new Date(system2.now())); if (ruleDef.htmlDescription() != null) { ruleDto.setDescription(ruleDef.htmlDescription()); ruleDto.setDescriptionFormat(Format.HTML); @@ -457,7 +454,6 @@ public class RegisterRules implements Startable { private void update(DbSession session, RuleDto rule){ rule.setUpdatedAtInMs(system2.now()); - rule.setUpdatedAt(new Date(system2.now())); dbClient.ruleDao().update(session, rule); } } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java index 0acc2497660..0ee0dd0d4a5 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java @@ -307,7 +307,6 @@ public class RuleUpdater { private void update(DbSession session, RuleDto rule) { rule.setUpdatedAtInMs(system.now()); - rule.setUpdatedAt(new Date(system.now())); dbClient.ruleDao().update(session, rule); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java index ae4ea2e5343..9b01137e134 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapper.java @@ -170,8 +170,8 @@ public class RuleMapper { } private static void setCreatedAt(Rules.Rule.Builder ruleResponse, RuleDto ruleDto, Set fieldsToReturn) { - if (shouldReturnField(fieldsToReturn, RuleNormalizer.RuleField.CREATED_AT) && ruleDto.getCreatedAt() != null) { - ruleResponse.setCreatedAt(formatDateTime(ruleDto.getCreatedAt())); + if (shouldReturnField(fieldsToReturn, RuleNormalizer.RuleField.CREATED_AT)) { + ruleResponse.setCreatedAt(formatDateTime(ruleDto.getCreatedAtInMs())); } } diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index f6be4c25a73..ae72e14747f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -104,8 +104,8 @@ public class RegisterRulesTest { assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3"); assertThat(rule1.getConfigKey()).isEqualTo("config1"); assertThat(rule1.getStatus()).isEqualTo(RuleStatus.BETA); - assertThat(rule1.getCreatedAt()).isEqualTo(DATE1); - assertThat(rule1.getUpdatedAt()).isEqualTo(DATE1); + assertThat(rule1.getCreatedAtInMs()).isEqualTo(DATE1.getTime()); + assertThat(rule1.getUpdatedAtInMs()).isEqualTo(DATE1.getTime()); assertThat(rule1.getDefaultRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name()); assertThat(rule1.getDefaultRemediationCoefficient()).isEqualTo("5d"); assertThat(rule1.getDefaultRemediationOffset()).isEqualTo("10h"); @@ -148,8 +148,8 @@ public class RegisterRulesTest { assertThat(rule1.getNoteData()).isEqualTo("user *note*"); assertThat(rule1.getNoteUserLogin()).isEqualTo("marius"); assertThat(rule1.getStatus()).isEqualTo(RuleStatus.READY); - assertThat(rule1.getCreatedAt()).isEqualTo(DATE1); - assertThat(rule1.getUpdatedAt()).isEqualTo(DATE2); + assertThat(rule1.getCreatedAtInMs()).isEqualTo(DATE1.getTime()); + assertThat(rule1.getUpdatedAtInMs()).isEqualTo(DATE2.getTime()); // TODO check remediation function List params = dbClient.ruleDao().selectRuleParamsByRuleKey(dbTester.getSession(), RULE_KEY1); @@ -161,7 +161,7 @@ public class RegisterRulesTest { // rule2 has been removed -> status set to REMOVED but db row is not deleted RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2); assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED); - assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2); + assertThat(rule2.getUpdatedAtInMs()).isEqualTo(DATE2.getTime()); // rule3 has been created RuleDto rule3 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY3); @@ -273,8 +273,8 @@ public class RegisterRulesTest { execute(new FakeRepositoryV1()); RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY1); - assertThat(rule1.getCreatedAt()).isEqualTo(DATE1); - assertThat(rule1.getUpdatedAt()).isEqualTo(DATE1); + assertThat(rule1.getCreatedAtInMs()).isEqualTo(DATE1.getTime()); + assertThat(rule1.getUpdatedAtInMs()).isEqualTo(DATE1.getTime()); } @Test @@ -306,7 +306,7 @@ public class RegisterRulesTest { // -> rule2 is still removed, but not update at DATE3 rule2 = dbClient.ruleDao().selectOrFailByKey(dbTester.getSession(), RULE_KEY2); assertThat(rule2.getStatus()).isEqualTo(RuleStatus.REMOVED); - assertThat(rule2.getUpdatedAt()).isEqualTo(DATE2); + assertThat(rule2.getUpdatedAtInMs()).isEqualTo(DATE2.getTime()); assertThat(ruleIndex.search(new RuleQuery(), new SearchOptions()).getIds()).containsOnly(RULE_KEY1, RULE_KEY3); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java b/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java index 6c8f2d5be46..684c0fd06c1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/startup/ClearRulesOverloadedDebtTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.startup; -import java.util.Date; import javax.annotation.Nullable; import org.junit.ClassRule; import org.junit.Rule; @@ -91,12 +90,12 @@ public class ClearRulesOverloadedDebtTest { @Test public void not_update_rule_debt_not_overridden() throws Exception { RuleDto rule = insertRuleDto(RULE_KEY_1, null, null, null, null); - Date updateAt = rule.getUpdatedAt(); + long updateAt = rule.getUpdatedAtInMs(); underTest.start(); RuleDto reloaded = ruleDao.selectOrFailByKey(dbSession, RULE_KEY_1); - assertThat(reloaded.getUpdatedAt()).isEqualTo(updateAt); + assertThat(reloaded.getUpdatedAtInMs()).isEqualTo(updateAt); verifyRuleHasNotOverriddenDebt(RULE_KEY_1); verifyTaskRegistered(); @@ -107,12 +106,12 @@ public class ClearRulesOverloadedDebtTest { public void not_update_rule_debt_when_sqale_is_installed() throws Exception { insertSqaleProperty(); RuleDto rule = insertRuleDto(RULE_KEY_1, SUB_CHARACTERISTIC_ID, "LINEAR", null, "1d"); - Date updateAt = rule.getUpdatedAt(); + long updateAt = rule.getUpdatedAtInMs(); underTest.start(); RuleDto reloaded = ruleDao.selectOrFailByKey(dbSession, RULE_KEY_1); - assertThat(reloaded.getUpdatedAt()).isEqualTo(updateAt); + assertThat(reloaded.getUpdatedAtInMs()).isEqualTo(updateAt); verifyTaskRegistered(); verifyEmptyLog(); diff --git a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index 2f6328e413d..0112420c6c7 100644 --- a/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/sonar-db/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -27,11 +27,10 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.rule.RuleKey; import org.sonar.api.rules.ActiveRule; -import org.sonar.db.rule.SeverityUtil; -import org.sonar.db.Dto; import org.sonar.db.rule.RuleDto; +import org.sonar.db.rule.SeverityUtil; -public class ActiveRuleDto extends Dto { +public class ActiveRuleDto { public static final String INHERITED = ActiveRule.INHERITED; public static final String OVERRIDES = ActiveRule.OVERRIDES; @@ -45,7 +44,7 @@ public class ActiveRuleDto extends Dto { private long createdAtInMs; private long updatedAtInMs; - //These fields do not exists in db, it's only retrieve by joins + // These fields do not exists in db, it's only retrieve by joins private Integer parentId; private String repository; private String ruleField; @@ -54,7 +53,6 @@ public class ActiveRuleDto extends Dto { /** * @deprecated for internal use, should be private */ - @Deprecated public ActiveRuleDto setKey(ActiveRuleKey key) { this.repository = key.ruleKey().repository(); this.ruleField = key.ruleKey().rule(); @@ -62,7 +60,6 @@ public class ActiveRuleDto extends Dto { return this; } - @Override public ActiveRuleKey getKey() { return ActiveRuleKey.of(profileKey, RuleKey.of(repository, ruleField)); } diff --git a/sonar-db/src/main/java/org/sonar/db/rule/RuleDto.java b/sonar-db/src/main/java/org/sonar/db/rule/RuleDto.java index 11e38b067c9..3bf350b777c 100644 --- a/sonar-db/src/main/java/org/sonar/db/rule/RuleDto.java +++ b/sonar-db/src/main/java/org/sonar/db/rule/RuleDto.java @@ -33,12 +33,10 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; -import org.sonar.db.Dto; import static com.google.common.base.Preconditions.checkArgument; -// TODO remove extends Dto -public class RuleDto extends Dto { +public class RuleDto { public static final int DISABLED_CHARACTERISTIC_ID = -1; @@ -79,7 +77,6 @@ public class RuleDto extends Dto { private long createdAtInMs; private long updatedAtInMs; - @Override public RuleKey getKey() { if (key == null) { key = RuleKey.of(getRepositoryKey(), getRuleKey()); diff --git a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index ebeecc1030d..a79f5e4103e 100644 --- a/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -12,8 +12,6 @@ r.plugin_rule_key as "rulefield", r.plugin_name as "repository", qp.kee as "profileKey", - a.created_at as "createdAt", - a.updated_at as "updatedAt", a.created_at_ms as "createdAtInMs", a.updated_at_ms as "updatedAtInMs" @@ -31,8 +29,6 @@ a.failure_level as severity, a.inheritance as inheritance, active_rule_parent.id as parentId, - a.created_at as "createdAt", - a.updated_at as "updatedAt", a.created_at_ms as "createdAtInMs", a.updated_at_ms as "updatedAtInMs" @@ -58,8 +54,8 @@ - INSERT INTO active_rules (profile_id, rule_id, failure_level, inheritance, created_at, updated_at, created_at_ms, updated_at_ms) - VALUES (#{profileId}, #{ruleId}, #{severity}, #{inheritance}, #{createdAt}, #{updatedAt}, #{createdAtInMs}, #{updatedAtInMs}) + INSERT INTO active_rules (profile_id, rule_id, failure_level, inheritance, created_at_ms, updated_at_ms) + VALUES (#{profileId}, #{ruleId}, #{severity}, #{inheritance}, #{createdAtInMs}, #{updatedAtInMs}) @@ -68,7 +64,6 @@ rule_id=#{ruleId}, failure_level=#{severity}, inheritance=#{inheritance}, - updated_at=#{updatedAt}, updated_at_ms=#{updatedAtInMs} WHERE id=#{id} diff --git a/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml index 3673dc55619..4d98081a923 100644 --- a/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml +++ b/sonar-db/src/main/resources/org/sonar/db/rule/RuleMapper.xml @@ -31,8 +31,6 @@ r.effort_to_fix_description as "effortToFixDescription", r.tags as "tagsField", r.system_tags as "systemTagsField", - r.created_at as "createdAt", - r.updated_at as "updatedAt", r.created_at_ms as "createdAtInMs", r.updated_at_ms as "updatedAtInMs" @@ -160,7 +158,6 @@ effort_to_fix_description=#{effortToFixDescription}, tags=#{tagsField}, system_tags=#{systemTagsField}, - updated_at=#{updatedAt}, updated_at_ms=#{updatedAtInMs} WHERE id=#{id} @@ -171,7 +168,7 @@ characteristic_id, default_characteristic_id, remediation_function, default_remediation_function, remediation_coeff, default_remediation_coeff, remediation_offset, default_remediation_offset, effort_to_fix_description, tags, system_tags, note_data, note_user_login, note_created_at, note_updated_at, - created_at, updated_at, created_at_ms, updated_at_ms) + created_at_ms, updated_at_ms) @@ -182,7 +179,7 @@ #{subCharacteristicId}, #{defaultSubCharacteristicId}, #{remediationFunction}, #{defaultRemediationFunction}, #{remediationCoefficient}, #{defaultRemediationCoefficient}, #{remediationOffset}, #{defaultRemediationOffset}, #{effortToFixDescription}, #{tagsField}, #{systemTagsField}, #{noteData}, #{noteUserLogin}, #{noteCreatedAt}, - #{noteUpdatedAt}, #{createdAt}, #{updatedAt}, #{createdAtInMs}, #{updatedAtInMs}) + #{noteUpdatedAt}, #{createdAtInMs}, #{updatedAtInMs}) @@ -199,7 +196,6 @@ #{remediationOffset,jdbcType=VARCHAR}, #{defaultRemediationOffset,jdbcType=VARCHAR}, #{effortToFixDescription}, #{tagsField}, #{systemTagsField}, #{noteData}, #{noteUserLogin}, #{noteCreatedAt}, #{noteUpdatedAt,jdbcType=TIMESTAMP}, - #{createdAt,jdbcType=TIMESTAMP}, #{updatedAt,jdbcType=TIMESTAMP}, #{createdAtInMs,jdbcType=BIGINT}, #{updatedAtInMs,jdbcType=BIGINT} ) -- 2.39.5