]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5407 - Added debtOverloaded() method to Rule domain object and defaultChar...
authorStephane Gamard <stephane.gamard@searchbox.com>
Mon, 23 Jun 2014 08:32:15 +0000 (10:32 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Mon, 23 Jun 2014 10:41:26 +0000 (12:41 +0200)
sonar-server/src/main/java/org/sonar/server/rule/Rule.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java
sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
sonar-server/src/test/java/org/sonar/server/rule/RuleBackendMediumTest.java

index 93a238bc12c6062d303823db23e3b11386b611ab..d8a61eb910329aa13b36f8582a583916cbfc65e6 100644 (file)
@@ -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();
 
index d5f88efdc788acefa19cc54cc2de3f2316e6057f..8b38025382ae3d1e0a1782e450b5811cb6df944a 100644 (file)
@@ -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() {
index d1a3b6fd525037a17d2da31284757a2ab2811a33..0c6ec3c77d16008702d51da7f6535e42bda47220 100644 (file)
@@ -99,8 +99,10 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     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<RuleDto, RuleKey> {
 
       //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<RuleDto, RuleKey> {
       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);
index 4c7585a6d4bf025eb8adc5accc6fd8d51d86dba0..65bf05ce14e2b41b431827703ecb704c65198c65 100644 (file)
@@ -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()));
index f5709040c7f13685ef9c935204dee28cbd2d4b33..3cc5438ba97a7f02e3ec1eebf1a8e1890c87da2e 100644 (file)
@@ -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());
+
   }