]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6233 Searching for rules without debt characteristic is not working
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 2 Mar 2015 12:53:07 +0000 (13:53 +0100)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 2 Mar 2015 13:32:14 +0000 (14:32 +0100)
server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java
server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java

index d3432183f19c6819baf19b4ab8d72da885bf722d..81df870ca2a2895dddab75ba7c37140905b94abd 100644 (file)
@@ -61,6 +61,7 @@ import static com.google.common.collect.Lists.newArrayList;
 public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
 
   private static final String FILTER_DEBT_CHARACTERISTICS = "debtCharacteristics";
+  private static final String FILTER_HAS_DEBT_CHARACTERISTICS = "hasDebtCharacteristic";
   public static final String FACET_LANGUAGES = "languages";
   public static final String FACET_TAGS = "tags";
   public static final String FACET_REPOSITORIES = "repositories";
@@ -270,14 +271,25 @@ public class RuleIndex extends BaseIndex<Rule, RuleDto, RuleKey> {
 
     // Debt char exist filter
     Boolean hasDebtCharacteristic = query.getHasDebtCharacteristic();
-    if (hasDebtCharacteristic != null && hasDebtCharacteristic) {
-      filters.put("hasDebtCharacteristic",
-        FilterBuilders.boolFilter().mustNot(
-          FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE))
-          .should(
-            FilterBuilders.existsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()))
-          .should(
-            FilterBuilders.existsFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field())));
+    if (hasDebtCharacteristic != null) {
+      if (hasDebtCharacteristic) {
+        filters.put(FILTER_HAS_DEBT_CHARACTERISTICS,
+          // Match either characteristic is not disabled, either characteristic or default characteristic is defined on the rule
+          FilterBuilders.boolFilter()
+            .mustNot(FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE))
+            .should(FilterBuilders.existsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()))
+            .should(FilterBuilders.existsFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field())));
+      } else {
+        filters.put(FILTER_HAS_DEBT_CHARACTERISTICS,
+          // Match either characteristic is disabled, either no characteristic or default characteristic is defined on the rule
+          FilterBuilders.orFilter(
+            FilterBuilders.termsFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field(), DebtCharacteristic.NONE),
+            FilterBuilders.andFilter(
+              FilterBuilders.missingFilter(RuleNormalizer.RuleField.SUB_CHARACTERISTIC.field()),
+              FilterBuilders.missingFilter(RuleNormalizer.RuleField.DEFAULT_SUB_CHARACTERISTIC.field())
+              )
+            ));
+      }
     }
 
     if (query.getAvailableSince() != null) {
index 50c8bf802ba9bee77cba1e09858e235af2b280c5..97b360332089ea37c67fb66a76d64e2c46825658 100644 (file)
@@ -52,15 +52,7 @@ import org.sonar.server.tester.ServerTester;
 
 import javax.annotation.Nullable;
 
-import java.util.Arrays;
-import java.util.Collection;
-import java.util.Collections;
-import java.util.Date;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-import java.util.Set;
+import java.util.*;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static org.assertj.core.api.Assertions.assertThat;
@@ -329,15 +321,23 @@ public class RuleIndexMediumTest {
       .setRemediationFunction("LINEAR").setRemediationCoefficient("2h")
       .setDefaultSubCharacteristicId(null)
       .setDefaultRemediationFunction(null));
+    // Rule without debt characteristic
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "S001"))
+      .setSubCharacteristicId(null)
+      .setRemediationFunction(null).setRemediationCoefficient(null)
+      .setDefaultSubCharacteristicId(null)
+      .setDefaultRemediationFunction(null).setDefaultRemediationCoefficient(null));
+    // Rule with disabled debt characteristic
+    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("xoo", "S002"))
+      .setSubCharacteristicId(-1)
+      .setRemediationFunction(null).setRemediationCoefficient(null)
+      .setDefaultSubCharacteristicId(null)
+      .setDefaultRemediationFunction(null).setDefaultRemediationCoefficient(null));
     dbSession.commit();
 
-    // 0. assert base case
-    assertThat(index.search(new RuleQuery(), new QueryContext()).getTotal()).isEqualTo(2);
-    assertThat(db.debtCharacteristicDao().selectCharacteristics()).hasSize(2);
-
-    // 1. assert hasSubChar filter
-    assertThat(index.search(new RuleQuery().setHasDebtCharacteristic(true), new QueryContext()).getTotal())
-      .isEqualTo(2);
+    assertThat(index.search(new RuleQuery().setHasDebtCharacteristic(null), new QueryContext()).getTotal()).isEqualTo(4);
+    assertThat(index.search(new RuleQuery().setHasDebtCharacteristic(true), new QueryContext()).getTotal()).isEqualTo(2);
+    assertThat(index.search(new RuleQuery().setHasDebtCharacteristic(false), new QueryContext()).getTotal()).isEqualTo(2);
   }
 
   @Test