import com.google.common.collect.Iterables;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.ObjectUtils;
-import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.ibatis.session.SqlSession;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.rule.RuleDefinitionsLoader;
+import org.sonar.server.rule.RuleOperations;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.UserSession;
private final CharacteristicDao dao;
private final RuleDao ruleDao;
private final DebtModelOperations debtModelOperations;
+ private final RuleOperations ruleOperations;
private final DebtModelPluginRepository debtModelPluginRepository;
private final DebtCharacteristicsXMLImporter characteristicsXMLImporter;
private final DebtRulesXMLImporter rulesXMLImporter;
private final RuleDefinitionsLoader defLoader;
private final System2 system2;
- public DebtModelBackup(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, DebtModelPluginRepository debtModelPluginRepository,
- DebtCharacteristicsXMLImporter characteristicsXMLImporter, DebtRulesXMLImporter rulesXMLImporter,
+ public DebtModelBackup(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, RuleOperations ruleOperations,
+ DebtModelPluginRepository debtModelPluginRepository, DebtCharacteristicsXMLImporter characteristicsXMLImporter, DebtRulesXMLImporter rulesXMLImporter,
DebtModelXMLExporter debtModelXMLExporter, RuleRegistry ruleRegistry, RuleDefinitionsLoader defLoader) {
- this(mybatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, characteristicsXMLImporter, rulesXMLImporter, debtModelXMLExporter, ruleRegistry, defLoader,
- System2.INSTANCE);
+ this(mybatis, dao, ruleDao, debtModelOperations, ruleOperations, debtModelPluginRepository, characteristicsXMLImporter, rulesXMLImporter, debtModelXMLExporter, ruleRegistry,
+ defLoader, System2.INSTANCE);
}
@VisibleForTesting
- DebtModelBackup(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, DebtModelPluginRepository debtModelPluginRepository,
- DebtCharacteristicsXMLImporter characteristicsXMLImporter, DebtRulesXMLImporter rulesXMLImporter, DebtModelXMLExporter debtModelXMLExporter,
- RuleRegistry ruleRegistry, RuleDefinitionsLoader defLoader, System2 system2) {
+ DebtModelBackup(MyBatis mybatis, CharacteristicDao dao, RuleDao ruleDao, DebtModelOperations debtModelOperations, RuleOperations ruleOperations,
+ DebtModelPluginRepository debtModelPluginRepository, DebtCharacteristicsXMLImporter characteristicsXMLImporter, DebtRulesXMLImporter rulesXMLImporter,
+ DebtModelXMLExporter debtModelXMLExporter, RuleRegistry ruleRegistry, RuleDefinitionsLoader defLoader, System2 system2) {
this.mybatis = mybatis;
this.dao = dao;
this.ruleDao = ruleDao;
this.debtModelOperations = debtModelOperations;
+ this.ruleOperations = ruleOperations;
this.debtModelPluginRepository = debtModelPluginRepository;
this.characteristicsXMLImporter = characteristicsXMLImporter;
this.rulesXMLImporter = rulesXMLImporter;
ValidationMessages validationMessages, Date updateDate, SqlSession session) {
for (RuleDto rule : rules) {
RuleDebt ruleDebt = ruleDebt(rule.getRepositoryKey(), rule.getRuleKey(), ruleDebts);
- if (ruleDebt == null) {
- // rule does not exists in the XML
- disabledOverriddenRuleDebt(rule);
- } else {
- CharacteristicDto subCharacteristicDto = characteristicByKey(ruleDebt.subCharacteristicKey(), allCharacteristicDtos, true);
- boolean isSameCharacteristicAsDefault = subCharacteristicDto.getId().equals(rule.getDefaultSubCharacteristicId());
- boolean isSameFunctionAsDefault = isSameRemediationFunction(ruleDebt, rule);
-
- // Update only if given characteristic is not the same as the default one or if given function is not the same as the default one
- if (!isSameCharacteristicAsDefault || !isSameFunctionAsDefault) {
- rule.setSubCharacteristicId(subCharacteristicDto.getId());
- rule.setRemediationFunction(ruleDebt.function().name());
- rule.setRemediationCoefficient(ruleDebt.coefficient());
- rule.setRemediationOffset(ruleDebt.offset());
- } else {
- rule.setSubCharacteristicId(null);
- rule.setRemediationFunction(null);
- rule.setRemediationCoefficient(null);
- rule.setRemediationOffset(null);
- }
- }
+ String subCharacteristicKey = ruleDebt != null ? ruleDebt.subCharacteristicKey() : null;
+ CharacteristicDto subCharacteristicDto = subCharacteristicKey != null ? characteristicByKey(ruleDebt.subCharacteristicKey(), allCharacteristicDtos, true) : null;
+ ruleOperations.updateRule(rule, subCharacteristicDto,
+ ruleDebt != null ? ruleDebt.function() : null,
+ ruleDebt != null ? ruleDebt.coefficient() :null,
+ ruleDebt != null ? ruleDebt.offset() :null, updateDate, session);
rule.setUpdatedAt(updateDate);
- ruleDao.update(rule, session);
-
ruleDebts.remove(ruleDebt);
}
ruleRegistry.reindex(rules, session);
}
}
- private static boolean isSameRemediationFunction(RuleDebt ruleDebt, RuleDto rule) {
- return new EqualsBuilder()
- .append(ruleDebt.function().name(), rule.getDefaultRemediationFunction())
- .append(ruleDebt.coefficient(), rule.getDefaultRemediationCoefficient())
- .append(ruleDebt.offset(), rule.getDefaultRemediationOffset())
- .isEquals();
- }
-
- private void disabledOverriddenRuleDebt(RuleDto rule) {
- // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin update
- rule.setSubCharacteristicId(rule.getDefaultSubCharacteristicId() != null ? RuleDto.DISABLED_CHARACTERISTIC_ID : null);
- rule.setRemediationFunction(null);
- rule.setRemediationCoefficient(null);
- rule.setRemediationOffset(null);
- }
-
private DebtModel loadModelFromPlugin(String pluginKey) {
Reader xmlFileReader = null;
try {
String effectiveCoefficient = coefficient != null ? coefficient : rule.getDefaultRemediationCoefficient();
String effectiveOffset = offset != null ? offset : rule.getDefaultRemediationOffset();
- ruleDebt.setFunction(DebtRemediationFunction.Type.valueOf(function));
+ ruleDebt.setFunction(function);
ruleDebt.setCoefficient(effectiveCoefficient);
ruleDebt.setOffset(effectiveOffset);
return ruleDebt;
import org.sonar.api.ServerComponent;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.server.debt.DebtCharacteristic;
-import org.sonar.api.server.debt.DebtRemediationFunction;
import org.sonar.api.server.debt.internal.DefaultDebtCharacteristic;
import org.xml.sax.InputSource;
String coefficient = rule.coefficient();
String offset = rule.offset();
- processProperty(PROPERTY_FUNCTION, null, rule.function().name(), xml);
+ processProperty(PROPERTY_FUNCTION, null, rule.function(), xml);
if (coefficient != null) {
String[] values = getValues(coefficient);
processProperty(PROPERTY_COEFFICIENT, values[0], values[1], xml);
public static class RuleDebt {
private RuleKey ruleKey;
private String subCharacteristicKey;
- private DebtRemediationFunction.Type type;
+ private String function;
private String coefficient;
private String offset;
return this;
}
- public DebtRemediationFunction.Type function() {
- return type;
+ public String function() {
+ return function;
}
- public RuleDebt setFunction(DebtRemediationFunction.Type type) {
- this.type = type;
+ public RuleDebt setFunction(String function) {
+ this.function = function;
return this;
}
return "RuleDebt{" +
"ruleKey=" + ruleKey +
", subCharacteristicKey='" + subCharacteristicKey + '\'' +
- ", type=" + type +
+ ", function=" + function +
", coefficient='" + coefficient + '\'' +
", offset='" + offset + '\'' +
'}';
private RuleDebt createRuleDebt(RuleKey ruleKey, String function, @Nullable String coefficient, @Nullable String offset, ValidationMessages validationMessages) {
if ("linear_threshold".equals(function) && coefficient != null) {
validationMessages.addWarningText(String.format("Linear with threshold function is no longer used, remediation function of '%s' is replaced by linear.", ruleKey));
- return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.Type.LINEAR).setCoefficient(coefficient);
+ return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.Type.LINEAR.name()).setCoefficient(coefficient);
} else if ("constant_resource".equals(function)) {
validationMessages.addWarningText(String.format("Constant/file function is no longer used, technical debt definitions on '%s' are ignored.", ruleKey));
} else if (DebtRemediationFunction.Type.CONSTANT_ISSUE.name().equalsIgnoreCase(function) && coefficient != null && offset == null) {
- return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE).setOffset(coefficient);
+ return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()).setOffset(coefficient);
} else {
- return new RuleDebt().setRuleKey(ruleKey).setFunction(DebtRemediationFunction.Type.valueOf(function.toUpperCase())).setCoefficient(coefficient).setOffset(offset);
+ return new RuleDebt().setRuleKey(ruleKey).setFunction(function.toUpperCase()).setCoefficient(coefficient).setOffset(offset);
}
return null;
}
RuleDebt ruleDebt = findRequirement(ruleDebts, repoKey, ruleKey);
if (ruleDebt != null) {
newRule.setDebtSubCharacteristic(ruleDebt.subCharacteristicKey());
- DebtRemediationFunction.Type function = ruleDebt.function();
+ String function = ruleDebt.function();
String coefficient = ruleDebt.coefficient();
String offset = ruleDebt.offset();
- if (DebtRemediationFunction.Type.LINEAR.equals(function) && coefficient != null) {
+ if (DebtRemediationFunction.Type.LINEAR.name().equals(function) && coefficient != null) {
newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linear(coefficient));
- } else if (DebtRemediationFunction.Type.CONSTANT_ISSUE.equals(function) && offset != null) {
+ } else if (DebtRemediationFunction.Type.CONSTANT_ISSUE.name().equals(function) && offset != null) {
newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().constantPerIssue(offset));
- } else if (DebtRemediationFunction.Type.LINEAR_OFFSET.equals(function) && coefficient != null && offset != null) {
+ } else if (DebtRemediationFunction.Type.LINEAR_OFFSET.name().equals(function) && coefficient != null && offset != null) {
newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linearWithOffset(coefficient, offset));
} else {
throw new IllegalArgumentException(String.format("Debt definition on rule '%s:%s' is invalid", repoKey, ruleKey));
checkPermission(userSession);
SqlSession session = myBatis.openSession();
try {
- boolean needUpdate = false;
-
RuleDto ruleDto = ruleDao.selectByKey(ruleChange.ruleKey(), session);
if (ruleDto == null) {
throw new NotFoundException(String.format("Unknown rule '%s'", ruleChange.ruleKey()));
}
String subCharacteristicKey = ruleChange.debtCharacteristicKey();
+ CharacteristicDto subCharacteristic = null;
// A sub-characteristic is given -> update rule debt if given values are different from overridden ones and from default ones
if (!Strings.isNullOrEmpty(subCharacteristicKey)) {
- CharacteristicDto subCharacteristic = characteristicDao.selectByKey(subCharacteristicKey, session);
+ subCharacteristic = characteristicDao.selectByKey(subCharacteristicKey, session);
if (subCharacteristic == null) {
throw new NotFoundException(String.format("Unknown sub characteristic '%s'", ruleChange.debtCharacteristicKey()));
}
-
- boolean iSameAsOverriddenValues = subCharacteristic.getId().equals(ruleDto.getSubCharacteristicId())
- && isSameRemediationFunction(ruleChange, ruleDto.getRemediationFunction(), ruleDto.getRemediationCoefficient(), ruleDto.getRemediationOffset());
- boolean iSameAsDefaultValues = subCharacteristic.getId().equals(ruleDto.getDefaultSubCharacteristicId())
- && isSameRemediationFunction(ruleChange, ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationCoefficient(), ruleDto.getDefaultRemediationOffset());
-
- // New sub characteristic is not equals to existing one and not equals to default value
- // and new remediation function is not equals to existing one and not equals to default value -> update overridden values
- if (!iSameAsOverriddenValues && !iSameAsDefaultValues) {
- ruleDto.setSubCharacteristicId(subCharacteristic.getId());
-
- DefaultDebtRemediationFunction debtRemediationFunction = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(ruleChange.debtRemediationFunction()),
- ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset());
- ruleDto.setRemediationFunction(debtRemediationFunction.type().name());
- ruleDto.setRemediationCoefficient(debtRemediationFunction.coefficient());
- ruleDto.setRemediationOffset(debtRemediationFunction.offset());
- needUpdate = true;
- }
-
- // No sub-characteristic is given -> disable rule debt if not already disabled
- } else {
- // Rule characteristic is not already disabled -> update it
- if (!RuleDto.DISABLED_CHARACTERISTIC_ID.equals(ruleDto.getSubCharacteristicId())) {
- ruleDto.setSubCharacteristicId(RuleDto.DISABLED_CHARACTERISTIC_ID);
- ruleDto.setRemediationFunction(null);
- ruleDto.setRemediationCoefficient(null);
- ruleDto.setRemediationOffset(null);
- needUpdate = true;
- }
}
+ boolean needUpdate = updateRule(ruleDto, subCharacteristic, ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset(),
+ new Date(system.now()), session);
if (needUpdate) {
- ruleDto.setUpdatedAt(new Date(system.now()));
- ruleDao.update(ruleDto, session);
session.commit();
reindexRule(ruleDto, session);
}
}
}
- private static boolean isSameRemediationFunction(RuleChange ruleChange, String function, @Nullable String coefficient, @Nullable String offset) {
+ public boolean updateRule(RuleDto ruleDto, @Nullable CharacteristicDto newSubCharacteristic, @Nullable String newFunction,
+ @Nullable String newCoefficient, @Nullable String newOffset, Date updateDate, SqlSession session){
+ boolean needUpdate = false;
+
+ // A sub-characteristic is given -> update rule debt
+ if (newSubCharacteristic != null) {
+ boolean iSameAsDefaultValues = newSubCharacteristic.getId().equals(ruleDto.getDefaultSubCharacteristicId())
+ && isSameRemediationFunction(newFunction, newCoefficient, newOffset,
+ ruleDto.getDefaultRemediationFunction(), ruleDto.getDefaultRemediationCoefficient(), ruleDto.getDefaultRemediationOffset());
+ boolean iSameAsOverriddenValues = newSubCharacteristic.getId().equals(ruleDto.getSubCharacteristicId())
+ && isSameRemediationFunction(newFunction, newCoefficient, newOffset,
+ ruleDto.getRemediationFunction(), ruleDto.getRemediationCoefficient(), ruleDto.getRemediationOffset());
+
+ // New values are the same as the default values -> set overridden values to null
+ if (iSameAsDefaultValues) {
+ ruleDto.setSubCharacteristicId(null);
+ ruleDto.setRemediationFunction(null);
+ ruleDto.setRemediationCoefficient(null);
+ ruleDto.setRemediationOffset(null);
+ needUpdate = true;
+
+ // New values are not the same as the overridden values -> update overridden values with new values
+ } else if (!iSameAsOverriddenValues) {
+ ruleDto.setSubCharacteristicId(newSubCharacteristic.getId());
+
+ DefaultDebtRemediationFunction debtRemediationFunction = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(newFunction), newCoefficient, newOffset);
+ ruleDto.setRemediationFunction(debtRemediationFunction.type().name());
+ ruleDto.setRemediationCoefficient(debtRemediationFunction.coefficient());
+ ruleDto.setRemediationOffset(debtRemediationFunction.offset());
+ needUpdate = true;
+ }
+
+ // No sub-characteristic is given -> disable rule debt if not already disabled
+ } else {
+ // Rule characteristic is not already disabled -> update it
+ if (ruleDto.getSubCharacteristicId() == null || !RuleDto.DISABLED_CHARACTERISTIC_ID.equals(ruleDto.getSubCharacteristicId())) {
+ // If default characteristic is not defined, set the overridden characteristic to null in order to be able to track debt plugin update
+ ruleDto.setSubCharacteristicId(ruleDto.getDefaultSubCharacteristicId() != null ? RuleDto.DISABLED_CHARACTERISTIC_ID : null);
+ ruleDto.setRemediationFunction(null);
+ ruleDto.setRemediationCoefficient(null);
+ ruleDto.setRemediationOffset(null);
+ needUpdate = true;
+ }
+ }
+
+ if (needUpdate) {
+ ruleDto.setUpdatedAt(updateDate);
+ ruleDao.update(ruleDto, session);
+ }
+ return needUpdate;
+ }
+
+ private static boolean isSameRemediationFunction(@Nullable String newFunction, @Nullable String newCoefficient, @Nullable String newOffset,
+ String oldFunction, @Nullable String oldCoefficient, @Nullable String oldOffset) {
return new EqualsBuilder()
- .append(function, ruleChange.debtRemediationFunction())
- .append(coefficient, ruleChange.debtRemediationCoefficient())
- .append(offset, ruleChange.debtRemediationOffset())
+ .append(oldFunction, newFunction)
+ .append(oldCoefficient, newCoefficient)
+ .append(oldOffset, newOffset)
.isEquals();
}
import org.sonar.core.technicaldebt.db.CharacteristicDao;
import org.sonar.core.technicaldebt.db.CharacteristicDto;
import org.sonar.server.rule.RuleDefinitionsLoader;
+import org.sonar.server.rule.RuleOperations;
import org.sonar.server.rule.RuleRegistry;
import org.sonar.server.user.MockUserSession;
@Mock
DebtModelOperations debtModelOperations;
+ @Mock
+ RuleOperations ruleOperations;
+
@Mock
DebtCharacteristicsXMLImporter characteristicsXMLImporter;
Reader defaultModelReader = mock(Reader.class);
when(debtModelPluginRepository.createReaderForXMLFile("technical-debt")).thenReturn(defaultModelReader);
- debtModelBackup = new DebtModelBackup(myBatis, dao, ruleDao, debtModelOperations, debtModelPluginRepository, characteristicsXMLImporter, rulesXMLImporter,
+ debtModelBackup = new DebtModelBackup(myBatis, dao, ruleDao, debtModelOperations, ruleOperations, debtModelPluginRepository, characteristicsXMLImporter, rulesXMLImporter,
debtModelXMLExporter, ruleRegistry, defLoader, system2);
}
assertThat(rule.ruleKey().repository()).isEqualTo("squid");
assertThat(rule.ruleKey().rule()).isEqualTo("UselessImportCheck");
assertThat(rule.subCharacteristicKey()).isEqualTo("COMPILER");
- assertThat(rule.function().name()).isEqualTo("LINEAR_OFFSET");
+ assertThat(rule.function()).isEqualTo("LINEAR_OFFSET");
assertThat(rule.coefficient()).isEqualTo("2h");
assertThat(rule.offset()).isEqualTo("15min");
assertThat(rule.ruleKey().repository()).isEqualTo("squid");
assertThat(rule.ruleKey().rule()).isEqualTo("AvoidNPE");
assertThat(rule.subCharacteristicKey()).isEqualTo("COMPILER");
- assertThat(rule.function().name()).isEqualTo("LINEAR");
+ assertThat(rule.function()).isEqualTo("LINEAR");
assertThat(rule.coefficient()).isEqualTo("2h");
assertThat(rule.offset()).isNull();
}
assertThat(rule.ruleKey().repository()).isEqualTo("squid");
assertThat(rule.ruleKey().rule()).isEqualTo("UselessImportCheck");
assertThat(rule.subCharacteristicKey()).isEqualTo("COMPILER");
- assertThat(rule.function().name()).isEqualTo("CONSTANT_ISSUE");
+ assertThat(rule.function()).isEqualTo("CONSTANT_ISSUE");
assertThat(rule.coefficient()).isNull();
assertThat(rule.offset()).isEqualTo("15min");
}
}
@Test
- public void restore_from_xml_with_different_characteristic_and_same_function_as_default_values() throws Exception {
+ public void restore_from_xml() throws Exception {
when(characteristicsXMLImporter.importXML(anyString())).thenReturn(new DebtModel()
.addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
.addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY"));
+ CharacteristicDto compiler = new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate);
when(dao.selectEnabledCharacteristics(session)).thenReturn(newArrayList(
new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(3).setKey("HARDWARE").setName("Hardware").setParentId(1).setCreatedAt(oldDate)));
+ new CharacteristicDto().setId(3).setKey("HARDWARE").setName("Hardware").setParentId(1).setCreatedAt(oldDate),
+ compiler));
when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(newArrayList(new RuleDebt()
.setRuleKey(RuleKey.of("squid", "UselessImportCheck"))
.setSubCharacteristicKey("COMPILER")
- .setFunction(DebtRemediationFunction.Type.LINEAR).setCoefficient("2h")));
+ .setFunction(DebtRemediationFunction.Type.LINEAR.name()).setCoefficient("2h")));
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
debtModelBackup.restoreFromXml("<xml/>");
- verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
- verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
- verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- assertThat(rule.getSubCharacteristicId()).isEqualTo(2);
- assertThat(rule.getRemediationFunction()).isEqualTo("LINEAR");
- assertThat(rule.getRemediationCoefficient()).isEqualTo("2h");
- assertThat(rule.getRemediationOffset()).isNull();
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
- }
-
- @Test
- public void restore_from_xml_with_same_characteristic_and_different_function_as_default_values() throws Exception {
- when(characteristicsXMLImporter.importXML(anyString())).thenReturn(new DebtModel()
- .addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
- .addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY"));
-
- when(dao.selectEnabledCharacteristics(session)).thenReturn(newArrayList(
- new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate)));
-
- when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(newArrayList(new RuleDebt()
- .setRuleKey(RuleKey.of("squid", "UselessImportCheck"))
- .setSubCharacteristicKey("COMPILER")
- .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET).setCoefficient("12h").setOffset("11min")));
-
- when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
- new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
- .setDefaultSubCharacteristicId(2).setDefaultRemediationFunction("LINEAR").setDefaultRemediationCoefficient("2h")
- .setCreatedAt(oldDate).setUpdatedAt(oldDate)
- ));
-
- debtModelBackup.restoreFromXml("<xml/>");
+ verify(ruleOperations).updateRule(ruleCaptor.capture(), eq(compiler), eq("LINEAR"), eq("2h"), isNull(String.class), eq(now), eq(session));
verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- assertThat(rule.getSubCharacteristicId()).isEqualTo(2);
- assertThat(rule.getRemediationFunction()).isEqualTo("LINEAR_OFFSET");
- assertThat(rule.getRemediationCoefficient()).isEqualTo("12h");
- assertThat(rule.getRemediationOffset()).isEqualTo("11min");
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
- }
-
- @Test
- public void restore_from_xml_with_same_characteristic_and_same_function_as_default_values() throws Exception {
- when(characteristicsXMLImporter.importXML(anyString())).thenReturn(new DebtModel()
- .addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
- .addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY"));
-
- when(dao.selectEnabledCharacteristics(session)).thenReturn(newArrayList(
- new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate)));
-
- when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(newArrayList(new RuleDebt()
- .setRuleKey(RuleKey.of("squid", "UselessImportCheck")).setSubCharacteristicKey("COMPILER").setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET).setCoefficient("2h").setOffset("15min")));
-
- when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
- new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
- .setDefaultSubCharacteristicId(2).setDefaultRemediationFunction("LINEAR_OFFSET").setDefaultRemediationCoefficient("2h").setDefaultRemediationOffset("15min")
- .setCreatedAt(oldDate).setUpdatedAt(oldDate)
- ));
-
- debtModelBackup.restoreFromXml("<xml/>");
-
- verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
- verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
- verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- assertThat(rule.getSubCharacteristicId()).isNull();
- assertThat(rule.getRemediationFunction()).isNull();
- assertThat(rule.getRemediationCoefficient()).isNull();
- assertThat(rule.getRemediationOffset()).isNull();
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
}
@Test
debtModelBackup.restoreFromXml("<xml/>");
- verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
- verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
- verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- assertThat(rule.getSubCharacteristicId()).isEqualTo(-1);
- assertThat(rule.getRemediationFunction()).isNull();
- assertThat(rule.getRemediationCoefficient()).isNull();
- assertThat(rule.getRemediationOffset()).isNull();
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
- }
-
- @Test
- public void restore_from_xml_set_no_rule_debt_when_not_in_xml_and_rule_has_no_default_debt_values() throws Exception {
- when(characteristicsXMLImporter.importXML(anyString())).thenReturn(new DebtModel()
- .addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
- .addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY"));
-
- when(dao.selectEnabledCharacteristics(session)).thenReturn(newArrayList(
- new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate)));
-
- when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
- new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
- .setCreatedAt(oldDate).setUpdatedAt(oldDate)
- ));
-
- debtModelBackup.restoreFromXml("<xml/>");
+ verify(ruleOperations).updateRule(ruleCaptor.capture(), isNull(CharacteristicDto.class), isNull(String.class), isNull(String.class), isNull(String.class), eq(now), eq(session));
verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- // As rule has no debt value, characteristic is set to null
- assertThat(rule.getSubCharacteristicId()).isNull();
- assertThat(rule.getRemediationFunction()).isNull();
- assertThat(rule.getRemediationCoefficient()).isNull();
- assertThat(rule.getRemediationOffset()).isNull();
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
}
@Test
.addRootCharacteristic(new DefaultDebtCharacteristic().setKey("PORTABILITY").setName("Portability").setOrder(1))
.addSubCharacteristic(new DefaultDebtCharacteristic().setKey("COMPILER").setName("Compiler"), "PORTABILITY"));
+ CharacteristicDto compiler = new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate);
when(dao.selectEnabledCharacteristics(session)).thenReturn(newArrayList(
new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(1).setCreatedAt(oldDate),
- new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate)));
+ compiler));
when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(newArrayList(new RuleDebt()
- .setRuleKey(RuleKey.of("squid", "UselessImportCheck")).setSubCharacteristicKey("COMPILER").setFunction(DebtRemediationFunction.Type.LINEAR).setCoefficient("2h")));
+ .setRuleKey(RuleKey.of("squid", "UselessImportCheck")).setSubCharacteristicKey("COMPILER").setFunction(DebtRemediationFunction.Type.LINEAR.name()).setCoefficient("2h")));
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck").setLanguage("java")
debtModelBackup.restoreFromXml("<xml/>", "java");
+ verify(ruleOperations).updateRule(ruleCaptor.capture(), eq(compiler), eq("LINEAR"), eq("2h"), isNull(String.class), eq(now), eq(session));
+
verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
}
@Test
when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(Collections.<RuleDebt>emptyList());
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
- // Rule does not exitss in XML -> debt will be disabled
+ // Rule does not exits in XML -> debt will be disabled
new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck").setLanguage("java")
.setDefaultSubCharacteristicId(2).setDefaultRemediationFunction("LINEAR").setDefaultRemediationCoefficient("2h")
.setSubCharacteristicId(2).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("2h").setRemediationOffset("15min")
debtModelBackup.restoreFromXml("<xml/>", "java");
+ verify(ruleOperations).updateRule(ruleCaptor.capture(), isNull(CharacteristicDto.class), isNull(String.class), isNull(String.class), isNull(String.class), eq(now), eq(session));
+
verify(ruleDao).selectEnablesAndNonManual(session);
- verify(ruleDao).update(ruleCaptor.capture(), eq(session));
- verifyNoMoreInteractions(ruleDao);
verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session);
verify(session).commit();
-
- RuleDto rule = ruleCaptor.getValue();
- assertThat(rule.getId()).isEqualTo(1);
- assertThat(rule.getSubCharacteristicId()).isEqualTo(-1);
- assertThat(rule.getRemediationFunction()).isNull();
- assertThat(rule.getRemediationCoefficient()).isNull();
- assertThat(rule.getRemediationOffset()).isNull();
- assertThat(rule.getUpdatedAt()).isEqualTo(now);
}
@Test
new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1).setCreatedAt(oldDate)));
when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(newArrayList(new RuleDebt()
- .setRuleKey(RuleKey.of("squid", "UselessImportCheck")).setSubCharacteristicKey("COMPILER").setFunction(DebtRemediationFunction.Type.LINEAR).setCoefficient("2h")));
+ .setRuleKey(RuleKey.of("squid", "UselessImportCheck")).setSubCharacteristicKey("COMPILER").setFunction(DebtRemediationFunction.Type.LINEAR.name()).setCoefficient("2h")));
when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(Collections.<RuleDto>emptyList());
List<RuleDebt> rules = newArrayList(
new RuleDebt().setRuleKey(RuleKey.of("checkstyle", "Regexp"))
- .setSubCharacteristicKey("MEMORY_USE").setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET).setCoefficient("3d").setOffset("15min")
+ .setSubCharacteristicKey("MEMORY_USE").setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()).setCoefficient("3d").setOffset("15min")
);
TestUtils.assertSimilarXml(getFileContent("export_xml.xml"), xmlExporter.export(debtModel, rules));
List<RuleDebt> rules = newArrayList(
new RuleDebt().setRuleKey(RuleKey.of("checkstyle", "Regexp"))
- .setSubCharacteristicKey("MEMORY_USE").setFunction(DebtRemediationFunction.Type.LINEAR).setCoefficient("3d")
+ .setSubCharacteristicKey("MEMORY_USE").setFunction(DebtRemediationFunction.Type.LINEAR.name()).setCoefficient("3d")
);
assertThat(xmlExporter.export(debtModel, rules)).isEqualTo(
"<sqale>" + SystemUtils.LINE_SEPARATOR +
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isNull();
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isNull();
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isEqualTo("1min");
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
assertThat(ruleDebt.coefficient()).isNull();
assertThat(ruleDebt.offset()).isEqualTo("3d");
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR_OFFSET.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3d");
assertThat(ruleDebt.offset()).isEqualTo("1d");
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3min");
assertThat(ruleDebt.offset()).isNull();
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isNull();
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isNull();
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name());
assertThat(ruleDebt.coefficient()).isNull();
assertThat(ruleDebt.offset()).isEqualTo("3h");
}
RuleDebt ruleDebt = results.get(0);
assertThat(ruleDebt.subCharacteristicKey()).isEqualTo("MEMORY_EFFICIENCY");
assertThat(ruleDebt.ruleKey()).isEqualTo(RuleKey.of("checkstyle", "Regexp"));
- assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR);
+ assertThat(ruleDebt.function()).isEqualTo(DebtRemediationFunction.Type.LINEAR.name());
assertThat(ruleDebt.coefficient()).isEqualTo("3h");
assertThat(ruleDebt.offset()).isNull();
}
new DebtModelXMLExporter.RuleDebt()
.setSubCharacteristicKey("MEMORY_EFFICIENCY")
.setRuleKey(RuleKey.of("checkstyle", "ConstantName"))
- .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET)
+ .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setCoefficient("1d")
.setOffset("10min")
);
new DebtModelXMLExporter.RuleDebt()
.setSubCharacteristicKey("MEMORY_EFFICIENCY")
.setRuleKey(RuleKey.of("checkstyle", "ConstantName"))
- .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET)
+ .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name())
.setCoefficient("1d")
);
assertThat(result.getUpdatedAt()).isEqualTo(now);
}
+ @Test
+ public void update_rule_set_overridden_values_to_null_when_new_values_are_equals_to_default_values() throws Exception {
+ RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
+ .setSubCharacteristicId(6).setRemediationFunction("CONSTANT_ISSUE").setRemediationOffset("10min")
+ .setDefaultSubCharacteristicId(2).setDefaultRemediationFunction("CONSTANT_ISSUE").setDefaultRemediationOffset("10min");
+ RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
+
+ when(ruleDao.selectByKey(ruleKey, session)).thenReturn(dto);
+
+ CharacteristicDto subCharacteristic = new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler").setParentId(1);
+ when(characteristicDao.selectByKey("COMPILER", session)).thenReturn(subCharacteristic);
+ when(characteristicDao.selectById(2, session)).thenReturn(subCharacteristic);
+ CharacteristicDto characteristic = new CharacteristicDto().setId(1).setKey("PORTABILITY").setName("Portability").setOrder(2);
+ when(characteristicDao.selectById(1, session)).thenReturn(characteristic);
+
+ operations.updateRule(
+ // Same value as default values -> overridden values will be set to null
+ new RuleChange().setRuleKey(ruleKey).setDebtCharacteristicKey("COMPILER")
+ .setDebtRemediationFunction("CONSTANT_ISSUE").setDebtRemediationOffset("10min"),
+ authorizedUserSession
+ );
+
+ verify(ruleDao).update(ruleCaptor.capture(), eq(session));
+ verify(ruleRegistry).reindex(eq(ruleCaptor.getValue()), eq(session));
+ verify(session).commit();
+
+ RuleDto result = ruleCaptor.getValue();
+
+ assertThat(result.getId()).isEqualTo(1);
+ assertThat(result.getSubCharacteristicId()).isNull();
+ assertThat(result.getRemediationFunction()).isNull();
+ assertThat(result.getRemediationCoefficient()).isNull();
+ assertThat(result.getRemediationOffset()).isNull();
+ assertThat(result.getUpdatedAt()).isEqualTo(now);
+ }
+
@Test
public void not_update_rule_if_same_sub_characteristic_and_function() throws Exception {
RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
@Test
public void disable_characteristic_and_remove_remediation_function_when_update_rule_with_no_sub_characteristic() throws Exception {
RuleDto dto = new RuleDto().setId(1).setRepositoryKey("squid").setRuleKey("UselessImportCheck")
+ .setDefaultSubCharacteristicId(6).setDefaultRemediationFunction("LINEAR").setDefaultRemediationCoefficient("10min")
.setSubCharacteristicId(6).setRemediationFunction("CONSTANT_ISSUE").setRemediationOffset("10min");
RuleKey ruleKey = RuleKey.of("squid", "UselessImportCheck");
RuleDto result = ruleCaptor.getValue();
assertThat(result.getId()).isEqualTo(1);
- assertThat(result.getSubCharacteristicId()).isEqualTo(-1);
+ assertThat(result.getSubCharacteristicId()).isNull();
assertThat(result.getRemediationFunction()).isNull();
assertThat(result.getRemediationCoefficient()).isNull();
assertThat(result.getRemediationOffset()).isNull();