Skip to content

Commit

Permalink
SONAR-6019 Refactor tech debt related attributes on rules
Browse files Browse the repository at this point in the history
  • Loading branch information
jblievremont committed Jan 14, 2015
1 parent ed9951f commit 465ac63
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

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;
Expand Down Expand Up @@ -183,9 +184,9 @@ public boolean apply(@Nullable RuleParam input) {

@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
Expand All @@ -203,33 +204,23 @@ public String defaultDebtSubCharacteristicKey() {
@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
Expand All @@ -240,7 +231,7 @@ public DebtRemediationFunction debtRemediationFunction() {
}
final String function = getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE.field());
if (function == null || function.isEmpty()) {
return defaultDebtRemediationFunction();
return null;
} else {
return new DebtRemediationFunction() {
@Override
Expand Down Expand Up @@ -287,6 +278,11 @@ public String offset() {
}
}

@CheckForNull
public Boolean isDebtRemediationFunctionOverridden() {
return (Boolean) getNullableField(RuleNormalizer.RuleField.DEBT_FUNCTION_TYPE_OVERLOADED.field());
}

@Override
@CheckForNull
public String markdownNote() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,7 @@ protected Map<String, FilterBuilder> getFilters(RuleQuery query, QueryContext op
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)))
)
))
));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@
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> {

Expand Down Expand Up @@ -80,16 +84,23 @@ public static final class RuleField extends Indexable {
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");
Expand All @@ -102,6 +113,7 @@ public static final class RuleField extends Indexable {
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);

/**
Expand Down Expand Up @@ -187,6 +199,9 @@ public List<UpdateRequest> normalize(RuleDto rule) {
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);
Expand All @@ -195,8 +210,10 @@ public List<UpdateRequest> normalize(RuleDto rule) {
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);
}
}
}
Expand All @@ -220,9 +237,13 @@ public List<UpdateRequest> normalize(RuleDto rule) {
}
}
}
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) {
Expand All @@ -239,10 +260,12 @@ public List<UpdateRequest> normalize(RuleDto rule) {
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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,8 @@ private void mapDebtFields() {
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());
Expand All @@ -120,12 +118,9 @@ private void mapDebtFields() {
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> {
Expand Down Expand Up @@ -205,7 +200,7 @@ private boolean needDebtSubCharacteristicNames(@Nullable QueryContext context) {

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
Expand All @@ -216,7 +211,7 @@ public void write(JsonWriter json, RuleDoc rule, RuleMappingContext context) {

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
Expand All @@ -225,7 +220,12 @@ public void write(JsonWriter json, RuleDoc rule, RuleMappingContext context) {
}
}

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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,7 @@ public void toQueryOptions_debt_requires_group_of_fields() throws Exception {
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
Expand All @@ -115,7 +112,21 @@ public void toQueryOptions_debt_characteristics() throws Exception {
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());
}
}

0 comments on commit 465ac63

Please sign in to comment.