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";
// 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) {
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;
.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