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;
@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
@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
}
final String function = getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field());
if (function == null || function.isEmpty()) {
- return defaultDebtRemediationFunction();
+ return null;
} else {
return new DebtRemediationFunction() {
@Override
}
}
+ @CheckForNull
+ public Boolean isDebtRemediationFunctionOverridden() {
+ return (Boolean) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
+ }
+
@Override
@CheckForNull
public String markdownNote() {
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)))
- )
+ ))
));
}
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> {
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");
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);
/**
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);
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);
}
}
}
}
}
}
+ 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) {
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());
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());
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> {
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
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
}
}
- 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());
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
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());
}
}