From 723f1a0bf7908a98b191c29c61e9f63e3f64c2e2 Mon Sep 17 00:00:00 2001 From: Stephane Gamard Date: Mon, 23 Jun 2014 10:32:15 +0200 Subject: [PATCH] SONAR-5407 - Added debtOverloaded() method to Rule domain object and defaultChar/SubChar --- .../main/java/org/sonar/server/rule/Rule.java | 8 ++++++++ .../org/sonar/server/rule/index/RuleDoc.java | 18 ++++++++++++++++++ .../server/rule/index/RuleNormalizer.java | 19 ++++++++++++++++--- .../org/sonar/server/rule/ws/RuleMapping.java | 2 ++ .../server/rule/RuleBackendMediumTest.java | 10 ++++++++-- 5 files changed, 52 insertions(+), 5 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/rule/Rule.java b/sonar-server/src/main/java/org/sonar/server/rule/Rule.java index 93a238bc12c..d8a61eb9103 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/Rule.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/Rule.java @@ -74,12 +74,20 @@ public interface Rule { @CheckForNull RuleParam param(final String key); + boolean debtOverloaded(); + @CheckForNull String debtCharacteristicKey(); + @CheckForNull + String defaultDebtCharacteristicKey(); + @CheckForNull String debtSubCharacteristicKey(); + @CheckForNull + String defaultDebtSubCharacteristicKey(); + @CheckForNull DebtRemediationFunction debtRemediationFunction(); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java index d5f88efdc78..8b38025382a 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java @@ -181,6 +181,24 @@ public class RuleDoc extends BaseDoc implements Rule { }, null); } + @Override + public boolean debtOverloaded() { + return !defaultDebtSubCharacteristicKey() + .equalsIgnoreCase(debtSubCharacteristicKey()); + } + + @Override + @CheckForNull + public String defaultDebtCharacteristicKey() { + return (String) getNullableField(RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field()); + } + + @Override + @CheckForNull + public String defaultDebtSubCharacteristicKey() { + return (String) getNullableField(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field()); + } + @Override @CheckForNull public String debtCharacteristicKey() { diff --git a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java index d1a3b6fd525..0c6ec3c77d1 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java @@ -99,8 +99,10 @@ public class RuleNormalizer extends BaseNormalizer { public static final IndexField DEBT_FUNCTION_TYPE = add(IndexField.Type.STRING, "debtRemFnType"); public static final IndexField DEBT_FUNCTION_COEFFICIENT = add(IndexField.Type.STRING, "debtRemFnCoefficient"); public static final IndexField DEBT_FUNCTION_OFFSET = add(IndexField.Type.STRING, "debtRemFnOffset"); - public static final IndexField SUB_CHARACTERISTIC = add(IndexField.Type.STRING, "debtSubChar"); + public static final IndexField DEFAULT_CHARACTERISTIC = add(IndexField.Type.STRING, "_debtChar"); + public static final IndexField DEFAULT_SUB_CHARACTERISTIC = add(IndexField.Type.STRING, "_debtSubChar"); public static final IndexField CHARACTERISTIC = add(IndexField.Type.STRING, "debtChar"); + public static final IndexField SUB_CHARACTERISTIC = add(IndexField.Type.STRING, "debtSubChar"); public static final IndexField NOTE = add(IndexField.Type.TEXT, "markdownNote"); public static final IndexField NOTE_LOGIN = add(IndexField.Type.STRING, "noteLogin"); public static final IndexField NOTE_CREATED_AT = add(IndexField.Type.DATE, "noteCreatedAt"); @@ -198,8 +200,11 @@ public class RuleNormalizer extends BaseNormalizer { //TODO Legacy ID in DTO should be Key CharacteristicDto characteristic = null; + CharacteristicDto defaultCharacteristic = null; if (rule.getDefaultSubCharacteristicId() != null) { characteristic = db.debtCharacteristicDao().selectById(rule.getDefaultSubCharacteristicId(), session); + defaultCharacteristic = characteristic; + update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), defaultCharacteristic.getKey()); } if (rule.getSubCharacteristicId() != null) { characteristic = db.debtCharacteristicDao().selectById(rule.getSubCharacteristicId(), session); @@ -207,8 +212,16 @@ public class RuleNormalizer extends BaseNormalizer { if (characteristic != null && characteristic.getId() != -1) { update.put(RuleField.SUB_CHARACTERISTIC.field(), characteristic.getKey()); if (characteristic.getParentId() != null) { - update.put(RuleField.CHARACTERISTIC.field(), - db.debtCharacteristicDao().selectById(characteristic.getParentId(), session).getKey()); + CharacteristicDto parentCharacteristic = + db.debtCharacteristicDao().selectById(characteristic.getParentId(), session); + update.put(RuleField.CHARACTERISTIC.field(), parentCharacteristic.getKey()); + if (characteristic.getId() == defaultCharacteristic.getId()) { + update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), parentCharacteristic.getKey()); + } else { + update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), + db.debtCharacteristicDao().selectById(defaultCharacteristic.getParentId(), session) + .getKey()); + } } } else { update.put(RuleField.CHARACTERISTIC.field(), null); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java index 4c7585a6d4b..65bf05ce14e 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java @@ -48,6 +48,8 @@ public class RuleMapping extends BaseMapping { addIndexStringField("templateKey", RuleNormalizer.RuleField.TEMPLATE_KEY.field()); addIndexArrayField("tags", RuleNormalizer.RuleField.TAGS.field()); addIndexArrayField("sysTags", RuleNormalizer.RuleField.SYSTEM_TAGS.field()); + addIndexStringField("defaultDebtChar", RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field()); + addIndexStringField("defaultDebtSubChar", RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field()); addField("debtChar", new IndexStringField("debtChar", RuleNormalizer.RuleField.CHARACTERISTIC.field())); addField("debtChar", new IndexStringField("debtSubChar", RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field())); addField("debtRemFn", new IndexStringField("debtRemFnType", RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field())); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java index f5709040c7f..3cc5438ba97 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java @@ -331,13 +331,19 @@ public class RuleBackendMediumTest { ruleDto.setSubCharacteristicId(char21.getId()); dao.update(dbSession, ruleDto); - dbSession.commit(); - // 4. Get non-default chars from Rule rule = index.getByKey(ruleKey); + + // 4. Get non-default chars from Rule assertThat(rule.debtCharacteristicKey()).isEqualTo(char2.getKey()); assertThat(rule.debtSubCharacteristicKey()).isEqualTo(char21.getKey()); + + // 5 Assert still get the default one + assertThat(rule.debtOverloaded()).isTrue(); + assertThat(rule.defaultDebtCharacteristicKey()).isEqualTo(char1.getKey()); + assertThat(rule.defaultDebtSubCharacteristicKey()).isEqualTo(char11.getKey()); + } -- 2.39.5