]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5407 - Fixed filtering logic for effective debt including NONE and null
authorStephane Gamard <stephane.gamard@searchbox.com>
Fri, 27 Jun 2014 19:24:28 +0000 (21:24 +0200)
committerStephane Gamard <stephane.gamard@searchbox.com>
Fri, 27 Jun 2014 19:24:37 +0000 (21:24 +0200)
sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java

index 6e6364ccf3d6b8770d18b417f71650dd4fbe2009..c40326a63ea440e59bf63f12c14c433fd5288a5a 100644 (file)
@@ -42,6 +42,7 @@ import org.elasticsearch.search.sort.SortBuilders;
 import org.elasticsearch.search.sort.SortOrder;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
+import org.sonar.api.server.debt.DebtCharacteristic;
 import org.sonar.core.cluster.WorkQueue;
 import org.sonar.core.profiling.Profiling;
 import org.sonar.core.profiling.StopWatch;
@@ -219,15 +220,36 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
       .termFilter(RuleNormalizer.RuleField.STATUS.field(),
         RuleStatus.REMOVED.toString()));
 
-    this.addMultiFieldTermFilter(fb, query.getDebtCharacteristics(),
-      RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(),
-      RuleNormalizer.RuleField.CHARACTERISTIC.field());
     this.addTermFilter(fb, RuleNormalizer.RuleField.LANGUAGE.field(), query.getLanguages());
     this.addTermFilter(fb, RuleNormalizer.RuleField.REPOSITORY.field(), query.getRepositories());
     this.addTermFilter(fb, RuleNormalizer.RuleField.SEVERITY.field(), query.getSeverities());
     this.addTermFilter(fb, RuleNormalizer.RuleField.KEY.field(), query.getKey());
     this.addTermFilter(fb, RuleNormalizer.RuleField._TAGS.field(), query.getTags());
 
+    // Construct the debt filter on effective char and subChar
+    if (query.getDebtCharacteristics() != null && !query.getDebtCharacteristics().isEmpty()) {
+      fb.must(
+        FilterBuilders.orFilter(
+          // Match only when NOT NONE overriden
+          FilterBuilders.andFilter(
+            FilterBuilders.notFilter(
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE)),
+            FilterBuilders.orFilter(
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), query.getDebtCharacteristics()),
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.CHARACTERISTIC.field(), query.getDebtCharacteristics()))
+          ),
+
+          // Match only when NOT NONE overriden
+          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(), query.getDebtCharacteristics()),
+              FilterBuilders.termsFilter(RuleNormalizer.RuleField.DEFAULT_CHARACTERISTIC.field(), query.getDebtCharacteristics())))
+        )
+      );
+    }
 
     //Debt char exist filter
     if (query.getHasDebtCharacteristic() != null && query.getHasDebtCharacteristic()) {
index e43f4813674d97a0d7d70847ee342cd7e633176c..dbc8525868a0b194d684e1323194fd5b48a2900c 100644 (file)
@@ -24,7 +24,10 @@ import com.google.common.collect.ImmutableSet;
 import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
 import org.apache.commons.lang.time.DateUtils;
-import org.junit.*;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.ClassRule;
+import org.junit.Test;
 import org.sonar.api.rule.RuleKey;
 import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
@@ -47,8 +50,12 @@ import org.sonar.server.search.Result;
 import org.sonar.server.tester.ServerTester;
 
 import javax.annotation.Nullable;
-
-import java.util.*;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.fest.assertions.Assertions.assertThat;
@@ -412,7 +419,6 @@ public class RuleIndexMediumTest {
   }
 
   @Test
-  @Ignore("To be fixed")
   public void search_by_characteristics_with_default_and_overridden_char() throws InterruptedException {
     CharacteristicDto char1 = DebtTesting.newCharacteristicDto("RELIABILITY");
     db.debtCharacteristicDao().insert(char1, dbSession);
@@ -462,21 +468,21 @@ public class RuleIndexMediumTest {
 
     // filter by subChar
     query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char11.getKey()));
-    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S004"); // FIXME S002 is missing
+    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S004");
 
     query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char21.getKey()));
     assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S003");
 
     // filter by Char
     query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char1.getKey()));
-    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S004"); // FIXME S002 is missing
+    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S004");
 
     query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char2.getKey()));
     assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S003");
 
     // filter by Char and SubChar
     query = new RuleQuery().setDebtCharacteristics(ImmutableSet.of(char11.getKey(), char1.getKey(), char2.getKey(), char21.getKey()));
-    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S003", "S004"); // FIXME S002 is missing
+    assertThat(ruleKeys(index.search(query, new QueryOptions()).getHits())).containsOnly("S001", "S002", "S003", "S004");
   }
 
   @Test