]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6019 Refactor tech debt related attributes on rules
authorJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 14 Jan 2015 13:51:30 +0000 (14:51 +0100)
committerJean-Baptiste Lievremont <jean-baptiste.lievremont@sonarsource.com>
Wed, 14 Jan 2015 15:52:21 +0000 (16:52 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleDoc.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleNormalizer.java
server/sonar-server/src/main/java/org/sonar/server/rule/ws/RuleMapping.java
server/sonar-server/src/test/java/org/sonar/server/rule/ws/RuleMappingTest.java

index 05211a508112f3114dc14cd3fb25b4bb2ba23e81..59399af577c5f58acc7a97b419748e6a651d39ea 100644 (file)
@@ -21,6 +21,7 @@ package org.sonar.server.rule.index;
 
 import com.google.common.base.Predicate;
 import com.google.common.collect.Iterables;
+import org.apache.commons.lang.BooleanUtils;
 import org.apache.commons.lang.builder.ReflectionToStringBuilder;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
@@ -183,9 +184,9 @@ public class RuleDoc extends BaseDoc implements Rule {
 
   @Override
   public boolean debtOverloaded() {
-    return getNullableField(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()) != null ||
-      getNullableField(RuleNormalizer.RuleField.CHARACTERISTIC.field()) != null ||
-      getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field()) != null;
+    return BooleanUtils.isTrue(isDebtCharacteristicOverridden()) ||
+      BooleanUtils.isTrue(isDebtSubCharacteristicOverridden()) ||
+      BooleanUtils.isTrue(isDebtRemediationFunctionOverridden());
   }
 
   @Override
@@ -203,33 +204,23 @@ public class RuleDoc extends BaseDoc implements Rule {
   @Override
   @CheckForNull
   public String debtCharacteristicKey() {
-    String field = RuleNormalizer.RuleField.CHARACTERISTIC.field();
-    String value = (String) getNullableField(field);
-    if (value != null) {
-      if (value.isEmpty()) {
-        return null;
-      } else {
-        return value;
-      }
-    } else {
-      return defaultDebtCharacteristicKey();
-    }
+    return (String) getNullableField(RuleNormalizer.RuleField.CHARACTERISTIC.field());
   }
 
   @Override
   @CheckForNull
   public String debtSubCharacteristicKey() {
-    String field = RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field();
-    String value = (String) getNullableField(field);
-    if (value != null) {
-      if (value.isEmpty()) {
-        return null;
-      } else {
-        return value;
-      }
-    } else {
-      return defaultDebtSubCharacteristicKey();
-    }
+    return (String) getNullableField(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field());
+  }
+
+  @CheckForNull
+  public Boolean isDebtCharacteristicOverridden() {
+    return (Boolean) getNullableField(RuleNormalizer.RuleField.CHARACTERISTIC_OVERLOADED.field());
+  }
+
+  @CheckForNull
+  public Boolean isDebtSubCharacteristicOverridden() {
+    return (Boolean) getNullableField(RuleNormalizer.RuleField.SUB_CHARACTERISTIC_OVERLOADED.field());
   }
 
   @Override
@@ -240,7 +231,7 @@ public class RuleDoc extends BaseDoc implements Rule {
     }
     final String function = getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field());
     if (function == null || function.isEmpty()) {
-      return defaultDebtRemediationFunction();
+      return null;
     } else {
       return new DebtRemediationFunction() {
         @Override
@@ -287,6 +278,11 @@ public class RuleDoc extends BaseDoc implements Rule {
     }
   }
 
+  @CheckForNull
+  public Boolean isDebtRemediationFunctionOverridden() {
+    return (Boolean) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
+  }
+
   @Override
   @CheckForNull
   public String markdownNote() {
index dbf4dd85987721873bfb364539d4eeba5fadddee..45e40be5d32741728d4d55b28be8330f07fb94a7 100644 (file)
@@ -268,17 +268,7 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
               FilterBuilders.orFilter(
                 FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), debtCharacteristics),
                 FilterBuilders.termsFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field(), debtCharacteristics))
-              ),
-
-            // Match only when NOT NONE (not overridden)
-            FilterBuilders.andFilter(
-              FilterBuilders.orFilter(
-                FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), ""),
-                FilterBuilders.notFilter(FilterBuilders.existsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()))),
-              FilterBuilders.orFilter(
-                FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), debtCharacteristics),
-                FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field(), debtCharacteristics)))
-            )
+              ))
           ));
     }
 
index e6e94fa6cbfb583d367c58f641a7ac3ffed832ce..7ce70f0757ce453e16720716df500984efffb7c8 100644 (file)
@@ -38,7 +38,11 @@ import org.sonar.server.search.BaseNormalizer;
 import org.sonar.server.search.IndexField;
 import org.sonar.server.search.Indexable;
 
-import java.util.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
 
 public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
 
@@ -80,16 +84,23 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
     public static final IndexField INTERNAL_KEY = add(IndexField.Type.STRING, "internalKey");
     public static final IndexField IS_TEMPLATE = add(IndexField.Type.BOOLEAN, "isTemplate");
     public static final IndexField TEMPLATE_KEY = add(IndexField.Type.STRING, "templateKey");
+
+    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 DEFAULT_DEBT_FUNCTION_TYPE = add(IndexField.Type.STRING, "_debtRemFnType");
     public static final IndexField DEFAULT_DEBT_FUNCTION_COEFFICIENT = add(IndexField.Type.STRING, "_debtRemFnCoefficient");
     public static final IndexField DEFAULT_DEBT_FUNCTION_OFFSET = add(IndexField.Type.STRING, "_debtRemFnOffset");
+
+    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 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 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 CHARACTERISTIC_OVERLOADED = add(IndexField.Type.BOOLEAN, "debtCharOverloaded");
+    public static final IndexField SUB_CHARACTERISTIC_OVERLOADED = add(IndexField.Type.BOOLEAN, "debtSubCharOverloaded");
+    public static final IndexField DEBT_FUNCTION_TYPE_OVERLOADED = add(IndexField.Type.BOOLEAN, "debtRemFnTypeOverloaded");
+
     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");
@@ -102,6 +113,7 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
       LANGUAGE, TAGS, SYSTEM_TAGS, INTERNAL_KEY, IS_TEMPLATE, TEMPLATE_KEY, DEFAULT_DEBT_FUNCTION_TYPE,
       DEFAULT_DEBT_FUNCTION_COEFFICIENT, DEFAULT_DEBT_FUNCTION_OFFSET, DEBT_FUNCTION_TYPE, DEBT_FUNCTION_COEFFICIENT,
       DEBT_FUNCTION_OFFSET, DEFAULT_CHARACTERISTIC, DEFAULT_SUB_CHARACTERISTIC, CHARACTERISTIC, SUB_CHARACTERISTIC,
+      DEBT_FUNCTION_TYPE_OVERLOADED, CHARACTERISTIC_OVERLOADED, SUB_CHARACTERISTIC_OVERLOADED,
       NOTE, NOTE_LOGIN, NOTE_CREATED_AT, NOTE_UPDATED_AT, ALL_TAGS, PARAMS);
 
     /**
@@ -187,6 +199,9 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
       update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), null);
       update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), null);
 
+      String defaultCharacteristicKey = null;
+      String defaultSubCharacteristicKey = null;
+
       Integer defaultSubCharacteristicId = rule.getDefaultSubCharacteristicId();
       if (defaultSubCharacteristicId != null) {
         CharacteristicDto subCharacteristic = db.debtCharacteristicDao().selectById(defaultSubCharacteristicId, session);
@@ -195,8 +210,10 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
           if (characteristicId != null) {
             CharacteristicDto characteristic = db.debtCharacteristicDao().selectById(characteristicId);
             if (characteristic != null) {
-              update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), characteristic.getKey());
-              update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), subCharacteristic.getKey());
+              defaultCharacteristicKey = characteristic.getKey();
+              update.put(RuleField.DEFAULT_CHARACTERISTIC.field(), defaultCharacteristicKey);
+              defaultSubCharacteristicKey = subCharacteristic.getKey();
+              update.put(RuleField.DEFAULT_SUB_CHARACTERISTIC.field(), defaultSubCharacteristicKey);
             }
           }
         }
@@ -220,9 +237,13 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
             }
           }
         }
+        update.put(RuleField.CHARACTERISTIC_OVERLOADED.field(), true);
+        update.put(RuleField.SUB_CHARACTERISTIC_OVERLOADED.field(), true);
       } else {
-        update.put(RuleField.CHARACTERISTIC.field(), null);
-        update.put(RuleField.SUB_CHARACTERISTIC.field(), null);
+        update.put(RuleField.CHARACTERISTIC.field(), defaultCharacteristicKey);
+        update.put(RuleField.SUB_CHARACTERISTIC.field(), defaultSubCharacteristicKey);
+        update.put(RuleField.CHARACTERISTIC_OVERLOADED.field(), false);
+        update.put(RuleField.SUB_CHARACTERISTIC_OVERLOADED.field(), false);
       }
 
       if (rule.getDefaultRemediationFunction() != null) {
@@ -239,10 +260,12 @@ public class RuleNormalizer extends BaseNormalizer<RuleDto, RuleKey> {
         update.put(RuleField.DEBT_FUNCTION_TYPE.field(), rule.getRemediationFunction());
         update.put(RuleField.DEBT_FUNCTION_COEFFICIENT.field(), rule.getRemediationCoefficient());
         update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), rule.getRemediationOffset());
+        update.put(RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field(), true);
       } else {
-        update.put(RuleField.DEBT_FUNCTION_TYPE.field(), null);
-        update.put(RuleField.DEBT_FUNCTION_COEFFICIENT.field(), null);
-        update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), null);
+        update.put(RuleField.DEBT_FUNCTION_TYPE.field(), rule.getDefaultRemediationFunction());
+        update.put(RuleField.DEBT_FUNCTION_COEFFICIENT.field(), rule.getDefaultRemediationCoefficient());
+        update.put(RuleField.DEBT_FUNCTION_OFFSET.field(), rule.getDefaultRemediationOffset());
+        update.put(RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field(), false);
       }
 
       update.put(RuleField.TAGS.field(), rule.getTags());
index 71244828bb75d4527ac88e6ed42e89877594446d..b32dcbe9281b1f66015c8fb1db243803a7192cd7 100644 (file)
@@ -106,10 +106,8 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
     map("defaultDebtChar", new IndexStringMapper("defaultDebtChar", RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field()));
     map("defaultDebtSubChar", new IndexStringMapper("defaultDebtSubChar", RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field()));
 
-    map("debtChar", new IndexStringMapper("debtChar", RuleNormalizer.RuleField.CHARACTERISTIC.field(),
-      RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field()));
-    map("debtSubChar", new IndexStringMapper("debtSubChar", RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(),
-      RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field()));
+    map("debtChar", new IndexStringMapper("debtChar", RuleNormalizer.RuleField.CHARACTERISTIC.field()));
+    map("debtSubChar", new IndexStringMapper("debtSubChar", RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()));
 
     map("debtCharName", new CharacteristicNameMapper());
     map("debtSubCharName", new SubCharacteristicNameMapper());
@@ -120,12 +118,9 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
     map("effortToFixDescription", RuleNormalizer.RuleField.FIX_DESCRIPTION.field());
     map("debtOverloaded", new OverriddenMapper());
 
-    map("debtRemFn", new EffectiveDebtRemFn("debtRemFnType", RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field()));
-    map("debtRemFn", new EffectiveDebtRemFn("debtRemFnCoeff", RuleNormalizer.RuleField.DEBT_FUNCTION_COEFFICIENT.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field()));
-    map("debtRemFn", new EffectiveDebtRemFn("debtRemFnOffset", RuleNormalizer.RuleField.DEBT_FUNCTION_OFFSET.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_OFFSET.field()));
+    map("debtRemFn", new IndexStringMapper("debtRemFnType", RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field()));
+    map("debtRemFn", new IndexStringMapper("debtRemFnCoeff", RuleNormalizer.RuleField.DEBT_FUNCTION_COEFFICIENT.field()));
+    map("debtRemFn", new IndexStringMapper("debtRemFnOffset", RuleNormalizer.RuleField.DEBT_FUNCTION_OFFSET.field()));
   }
 
   public static class EffectiveDebtRemFn extends IndexStringMapper<RuleDoc,RuleMappingContext> {
@@ -205,7 +200,7 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
 
   private static class CharacteristicNameMapper extends IndexMapper<RuleDoc, RuleMappingContext> {
     private CharacteristicNameMapper() {
-      super(RuleNormalizer.RuleField.CHARACTERISTIC.field(), RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field());
+      super(RuleNormalizer.RuleField.CHARACTERISTIC.field());
     }
 
     @Override
@@ -216,7 +211,7 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
 
   private static class SubCharacteristicNameMapper extends IndexMapper<RuleDoc, RuleMappingContext> {
     private SubCharacteristicNameMapper() {
-      super(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field());
+      super(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field());
     }
 
     @Override
@@ -225,7 +220,12 @@ public class RuleMapping extends BaseMapping<RuleDoc, RuleMappingContext> {
     }
   }
 
-  private static class OverriddenMapper implements Mapper<RuleDoc, RuleMappingContext> {
+  private static class OverriddenMapper extends IndexMapper<RuleDoc, RuleMappingContext> {
+    private OverriddenMapper() {
+      super(RuleNormalizer.RuleField.CHARACTERISTIC_OVERLOADED.field(),
+        RuleNormalizer.RuleField.SUB_CHARACTERISTIC_OVERLOADED.field(),
+        RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
+    }
     @Override
     public void write(JsonWriter json, RuleDoc rule, RuleMappingContext context) {
       json.prop("debtOverloaded", rule.debtOverloaded());
index 7b6228f4189c495c4432d1c463df4233f1f3b36e..a67998be0a4ce179fc5d1110f7a5d9cf170c7870 100644 (file)
@@ -87,10 +87,7 @@ public class RuleMappingTest {
     assertThat(queryContext.getFieldsToReturn()).containsOnly(
       RuleNormalizer.RuleField.DEBT_FUNCTION_COEFFICIENT.field(),
       RuleNormalizer.RuleField.DEBT_FUNCTION_OFFSET.field(),
-      RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_COEFFICIENT.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_OFFSET.field(),
-      RuleNormalizer.RuleField.DEFAULT_DEBT_FUNCTION_TYPE.field());
+      RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field());
   }
 
   @Test
@@ -115,7 +112,21 @@ public class RuleMappingTest {
     QueryContext queryContext = mapping.newQueryOptions(SearchOptions.create(request));
 
     assertThat(queryContext.getFieldsToReturn()).containsOnly(
-      RuleNormalizer.RuleField.CHARACTERISTIC.field(),
-      RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field());
+      RuleNormalizer.RuleField.CHARACTERISTIC.field());
+  }
+
+  @Test
+  public void toQueryOptions_debt_overloaded() throws Exception {
+    RuleMapping mapping = new RuleMapping(languages, macroInterpreter, debtModel);
+    SimpleGetRequest request = new SimpleGetRequest();
+    request.setParam("p", "1");
+    request.setParam("ps", "10");
+    request.setParam("f", "debtOverloaded");
+    QueryContext queryContext = mapping.newQueryOptions(SearchOptions.create(request));
+
+    assertThat(queryContext.getFieldsToReturn()).containsOnly(
+      RuleNormalizer.RuleField.CHARACTERISTIC_OVERLOADED.field(),
+      RuleNormalizer.RuleField.SUB_CHARACTERISTIC_OVERLOADED.field(),
+      RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
   }
 }