From 0db34c2a2d299182676947a33c0dc742af70fc3d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 4 Apr 2014 08:56:47 +0200 Subject: [PATCH] SONAR-5056 When restoring model from XML call RuleOperations to update restored rules --- .../sonar/server/debt/DebtModelBackup.java | 65 ++----- .../server/debt/DebtModelXMLExporter.java | 15 +- .../server/debt/DebtRulesXMLImporter.java | 6 +- .../rule/DeprecatedRulesDefinition.java | 8 +- .../org/sonar/server/rule/RuleOperations.java | 99 ++++++---- .../server/debt/DebtModelBackupTest.java | 176 +++--------------- .../server/debt/DebtModelXMLExporterTest.java | 4 +- .../server/debt/DebtRulesXMLImporterTest.java | 20 +- .../rule/DeprecatedRulesDefinitionTest.java | 4 +- .../sonar/server/rule/RuleOperationsTest.java | 39 +++- 10 files changed, 168 insertions(+), 268 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java index aeae487b8b9..0979ad5d54f 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java @@ -24,7 +24,6 @@ import com.google.common.base.Predicate; 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; @@ -42,6 +41,7 @@ import org.sonar.core.technicaldebt.db.CharacteristicDao; 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; @@ -62,6 +62,7 @@ public class DebtModelBackup implements ServerComponent { 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; @@ -70,21 +71,22 @@ public class DebtModelBackup implements ServerComponent { 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; @@ -221,30 +223,13 @@ public class DebtModelBackup implements ServerComponent { 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); @@ -299,22 +284,6 @@ public class DebtModelBackup implements ServerComponent { } } - 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 { @@ -395,7 +364,7 @@ public class DebtModelBackup implements ServerComponent { 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; diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java index a10081174e9..4186a464672 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java @@ -29,7 +29,6 @@ import org.apache.commons.lang.StringUtils; 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; @@ -118,7 +117,7 @@ public class DebtModelXMLExporter implements ServerComponent { 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); @@ -266,7 +265,7 @@ public class DebtModelXMLExporter implements ServerComponent { public static class RuleDebt { private RuleKey ruleKey; private String subCharacteristicKey; - private DebtRemediationFunction.Type type; + private String function; private String coefficient; private String offset; @@ -288,12 +287,12 @@ public class DebtModelXMLExporter implements ServerComponent { 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; } @@ -322,7 +321,7 @@ public class DebtModelXMLExporter implements ServerComponent { return "RuleDebt{" + "ruleKey=" + ruleKey + ", subCharacteristicKey='" + subCharacteristicKey + '\'' + - ", type=" + type + + ", function=" + function + ", coefficient='" + coefficient + '\'' + ", offset='" + offset + '\'' + '}'; diff --git a/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java b/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java index 1e58ccc8419..66447c840cd 100644 --- a/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java +++ b/sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java @@ -174,13 +174,13 @@ public class DebtRulesXMLImporter implements ServerComponent { 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; } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java index b762a9abe8f..02a04434f31 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.java @@ -113,15 +113,15 @@ public class DeprecatedRulesDefinition implements RulesDefinition { 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)); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java index bd5fd9df3c9..75c4b3c26e3 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java @@ -258,54 +258,24 @@ public class RuleOperations implements ServerComponent { 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); } @@ -316,11 +286,64 @@ public class RuleOperations implements ServerComponent { } } - 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(); } diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java index 9e20d4bea99..cf14a6bf46b 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java @@ -46,6 +46,7 @@ import org.sonar.core.rule.RuleDto; 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; @@ -84,6 +85,9 @@ public class DebtModelBackupTest { @Mock DebtModelOperations debtModelOperations; + @Mock + RuleOperations ruleOperations; + @Mock DebtCharacteristicsXMLImporter characteristicsXMLImporter; @@ -140,7 +144,7 @@ public class DebtModelBackupTest { 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); } @@ -173,7 +177,7 @@ public class DebtModelBackupTest { 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"); @@ -181,7 +185,7 @@ public class DebtModelBackupTest { 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(); } @@ -236,7 +240,7 @@ public class DebtModelBackupTest { 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"); } @@ -502,20 +506,21 @@ public class DebtModelBackupTest { } @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") @@ -525,93 +530,11 @@ public class DebtModelBackupTest { debtModelBackup.restoreFromXml(""); - 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(""); + 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(""); - - 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 @@ -632,52 +555,11 @@ public class DebtModelBackupTest { debtModelBackup.restoreFromXml(""); - 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(""); + 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 @@ -686,12 +568,13 @@ public class DebtModelBackupTest { .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") @@ -705,14 +588,11 @@ public class DebtModelBackupTest { debtModelBackup.restoreFromXml("", "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 @@ -744,7 +624,7 @@ public class DebtModelBackupTest { when(rulesXMLImporter.importXML(anyString(), any(ValidationMessages.class))).thenReturn(Collections.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") @@ -753,19 +633,11 @@ public class DebtModelBackupTest { debtModelBackup.restoreFromXml("", "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 @@ -779,7 +651,7 @@ public class DebtModelBackupTest { 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.emptyList()); diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelXMLExporterTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelXMLExporterTest.java index b19669e265c..ff7cf091625 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtModelXMLExporterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtModelXMLExporterTest.java @@ -60,7 +60,7 @@ public class DebtModelXMLExporterTest { List 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)); @@ -97,7 +97,7 @@ public class DebtModelXMLExporterTest { List 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( "" + SystemUtils.LINE_SEPARATOR + diff --git a/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java b/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java index a1dfd7062ae..be069aa1b91 100644 --- a/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java +++ b/sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java @@ -59,7 +59,7 @@ public class DebtRulesXMLImporterTest { 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(); } @@ -74,7 +74,7 @@ public class DebtRulesXMLImporterTest { 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(); } @@ -88,7 +88,7 @@ public class DebtRulesXMLImporterTest { 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"); } @@ -102,7 +102,7 @@ public class DebtRulesXMLImporterTest { 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"); } @@ -116,7 +116,7 @@ public class DebtRulesXMLImporterTest { 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"); } @@ -130,7 +130,7 @@ public class DebtRulesXMLImporterTest { 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(); } @@ -145,7 +145,7 @@ public class DebtRulesXMLImporterTest { 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(); } @@ -159,7 +159,7 @@ public class DebtRulesXMLImporterTest { 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(); @@ -175,7 +175,7 @@ public class DebtRulesXMLImporterTest { 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"); } @@ -210,7 +210,7 @@ public class DebtRulesXMLImporterTest { 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(); } diff --git a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java index aa4402c8b99..59bfb301f1e 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java @@ -162,7 +162,7 @@ public class DeprecatedRulesDefinitionTest { 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") ); @@ -195,7 +195,7 @@ public class DeprecatedRulesDefinitionTest { new DebtModelXMLExporter.RuleDebt() .setSubCharacteristicKey("MEMORY_EFFICIENCY") .setRuleKey(RuleKey.of("checkstyle", "ConstantName")) - .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET) + .setFunction(DebtRemediationFunction.Type.LINEAR_OFFSET.name()) .setCoefficient("1d") ); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java index 99663635a05..0bd6dea327f 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java @@ -404,6 +404,42 @@ public class RuleOperationsTest { 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") @@ -499,6 +535,7 @@ public class RuleOperationsTest { @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"); @@ -536,7 +573,7 @@ public class RuleOperationsTest { 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(); -- 2.39.5