From 9e6bc9e42f5fbee6e39b5746a8fa8ad20609f201 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Mon, 14 Apr 2014 16:19:52 +0200 Subject: [PATCH] SONAR-5195 Reset custom rules when resting debt model and copy default debt definitions from template rule when creating custom rules --- .../sonar/server/debt/DebtModelBackup.java | 20 +++++++++++-- .../org/sonar/server/rule/RegisterRules.java | 2 +- .../org/sonar/server/rule/RuleOperations.java | 4 +++ .../server/debt/DebtModelBackupTest.java | 30 +++++++++++++++---- .../sonar/server/rule/RuleOperationsTest.java | 7 ++++- 5 files changed, 54 insertions(+), 9 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 978fe32a40d..e0931dd8dda 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 @@ -164,8 +164,15 @@ public class DebtModelBackup implements ServerComponent { for (RuleDto rule : ruleDtos) { // Restore default debt definitions - RulesDefinition.Rule ruleDef = ruleDef(rule.getRepositoryKey(), rule.getRuleKey(), rules); - // Custom rules will not be found in rules definition + + RulesDefinition.Rule ruleDef; + if (rule.getParentId() != null) { + RuleDto templateRule = rule(rule.getParentId(), ruleDtos); + ruleDef = ruleDef(templateRule.getRepositoryKey(), templateRule.getRuleKey(), rules); + } else { + ruleDef = ruleDef(rule.getRepositoryKey(), rule.getRuleKey(), rules); + } + if (ruleDef != null) { String subCharacteristicKey = ruleDef.debtSubCharacteristic(); CharacteristicDto subCharacteristicDto = characteristicByKey(subCharacteristicKey, allCharacteristicDtos, false); @@ -331,6 +338,15 @@ public class DebtModelBackup implements ServerComponent { }, null); } + private static RuleDto rule(final Integer id, List rules) { + return Iterables.find(rules, new Predicate() { + @Override + public boolean apply(@Nullable RuleDto input) { + return input != null && id.equals(input.getId()); + } + }); + } + @CheckForNull private static RulesDefinition.Rule ruleDef(final String repo, final String key, List rules) { return Iterables.find(rules, new Predicate() { diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java b/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java index b899b08df8d..a9f8c3a924b 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -415,7 +415,7 @@ public class RegisterRules implements Startable { for (Integer unprocessedRuleId : buffer.unprocessedRuleIds) { RuleDto ruleDto = buffer.rulesById.get(unprocessedRuleId); boolean toBeRemoved = true; - // Update copy of template rules from template + // Update custom rules from template if (ruleDto.getParentId() != null) { RuleDto parent = buffer.rulesById.get(ruleDto.getParentId()); if (parent != null && !Rule.STATUS_REMOVED.equals(parent.getStatus())) { 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 be2b3022cce..acde41b673b 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 @@ -145,6 +145,10 @@ public class RuleOperations implements ServerComponent { .setCardinality(Cardinality.SINGLE) .setStatus(Rule.STATUS_READY) .setLanguage(templateRule.getLanguage()) + .setDefaultSubCharacteristicId(templateRule.getDefaultSubCharacteristicId()) + .setDefaultRemediationFunction(templateRule.getDefaultRemediationFunction()) + .setDefaultRemediationCoefficient(templateRule.getDefaultRemediationCoefficient()) + .setDefaultRemediationOffset(templateRule.getDefaultRemediationOffset()) .setCreatedAt(new Date(system.now())) .setUpdatedAt(new Date(system.now())); ruleDao.insert(rule, session); 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 8f986648bfc..54b0c1f5093 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 @@ -512,25 +512,45 @@ public class DebtModelBackupTest { )); when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList( - // Custom rule will not be found in rules definitions - new RuleDto().setRepositoryKey("squid").setRuleKey("XPath_1369910135").setParentId(5) + // Template rule + new RuleDto().setId(5).setRepositoryKey("squid").setRuleKey("XPath") + .setSubCharacteristicId(2).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("2h").setRemediationOffset("15min") + .setCreatedAt(oldDate).setUpdatedAt(oldDate), + // Custom rule + new RuleDto().setId(6).setRepositoryKey("squid").setRuleKey("XPath_1369910135").setParentId(5) .setSubCharacteristicId(2).setRemediationFunction("LINEAR_OFFSET").setRemediationCoefficient("2h").setRemediationOffset("15min") .setCreatedAt(oldDate).setUpdatedAt(oldDate) )); - // No rule found RulesDefinition.Context context = new RulesDefinition.Context(); + // Template rule + RulesDefinition.NewRepository repo = context.createRepository("squid", "java").setName("XPath"); + RulesDefinition.NewRule newRule = repo.createRule("XPath") + .setName("XPath") + .setHtmlDescription("XPath") + .setSeverity(Severity.BLOCKER) + .setStatus(RuleStatus.BETA) + .setDebtSubCharacteristic("COMPILER"); + newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().linearWithOffset("4h", "20min")); + repo.done(); when(defLoader.load()).thenReturn(context); debtModelBackup.reset(); verify(ruleDao).selectEnablesAndNonManual(session); - verify(ruleDao).update(ruleCaptor.capture(), eq(session)); + verify(ruleDao, times(2)).update(ruleCaptor.capture(), eq(session)); verifyNoMoreInteractions(ruleDao); verify(ruleRegistry).reindex(ruleCaptor.getAllValues(), session); verify(session).commit(); - RuleDto rule = ruleCaptor.getValue(); + RuleDto rule = ruleCaptor.getAllValues().get(1); + + assertThat(rule.getId()).isEqualTo(6); + assertThat(rule.getDefaultSubCharacteristicId()).isEqualTo(2); + assertThat(rule.getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET"); + assertThat(rule.getDefaultRemediationCoefficient()).isEqualTo("4h"); + assertThat(rule.getDefaultRemediationOffset()).isEqualTo("20min"); + assertThat(rule.getUpdatedAt()).isEqualTo(now); assertThat(rule.getSubCharacteristicId()).isNull(); assertThat(rule.getRemediationFunction()).isNull(); 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 39cf65daaf2..3ccce8bb567 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 @@ -216,7 +216,8 @@ public class RuleOperationsTest { @Test public void create_custom_rule() throws Exception { - RuleDto templateRule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle").setConfigKey("Xpath"); + RuleDto templateRule = new RuleDto().setId(10).setRepositoryKey("squid").setRuleKey("AvoidCycle").setConfigKey("Xpath") + .setDefaultSubCharacteristicId(2).setDefaultRemediationFunction("LINEAR_OFFSET").setDefaultRemediationCoefficient("2h").setDefaultRemediationOffset("15min"); when(ruleDao.selectParametersByRuleId(eq(10), eq(session))).thenReturn(newArrayList(new RuleParamDto().setId(20).setName("max").setDefaultValue("10"))); when(ruleDao.selectTagsByRuleIds(eq(10), eq(session))).thenReturn(newArrayList(new RuleRuleTagDto().setId(30L).setTag("style").setType(RuleTagType.SYSTEM))); @@ -235,6 +236,10 @@ public class RuleOperationsTest { assertThat(ruleArgument.getValue().getRuleKey()).startsWith("AvoidCycle"); assertThat(ruleArgument.getValue().getStatus()).isEqualTo("READY"); assertThat(ruleArgument.getValue().getCardinality()).isEqualTo(Cardinality.SINGLE); + assertThat(ruleArgument.getValue().getDefaultSubCharacteristicId()).isEqualTo(2); + assertThat(ruleArgument.getValue().getDefaultRemediationFunction()).isEqualTo("LINEAR_OFFSET"); + assertThat(ruleArgument.getValue().getDefaultRemediationCoefficient()).isEqualTo("2h"); + assertThat(ruleArgument.getValue().getDefaultRemediationOffset()).isEqualTo("15min"); ArgumentCaptor ruleParamArgument = ArgumentCaptor.forClass(RuleParamDto.class); verify(ruleDao).insert(ruleParamArgument.capture(), eq(session)); -- 2.39.5