From 32de25202356fdb88963f02e3fde0ec133bf85c7 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 2 Mar 2015 13:53:07 +0100 Subject: [PATCH] SONAR-6233 Searching for rules without debt characteristic is not working --- .../sonar/server/rule/index/RuleIndex.java | 28 +++++++++++----- .../rule/index/RuleIndexMediumTest.java | 32 +++++++++---------- 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java index d3432183f19..81df870ca2a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/index/RuleIndex.java @@ -61,6 +61,7 @@ import static com.google.common.collect.Lists.newArrayList; public class RuleIndex extends BaseIndex { 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 { // 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) { diff --git a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java index 50c8bf802ba..97b36033208 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/rule/index/RuleIndexMediumTest.java @@ -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 -- 2.39.5