From eca362ab5cd410da5bfb3777d79607b1b5d6e08c Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 3 Apr 2014 09:08:52 +0200 Subject: [PATCH] SONAR-5056 When creating a custom rule, copy debt definitions from parent --- .../main/java/org/sonar/server/rule/RuleOperations.java | 6 ++++++ .../java/org/sonar/server/rule/RuleOperationsTest.java | 7 ++++++- 2 files changed, 12 insertions(+), 1 deletion(-) 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 afd53b9be24..5708ef4a757 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); @@ -272,12 +276,14 @@ public class RuleOperations implements ServerComponent { } // New sub characteristic is not equals to existing one -> update it + // TODO check characteristic is not equals to default one, if so do nothing if (!subCharacteristic.getId().equals(ruleDto.getSubCharacteristicId())) { ruleDto.setSubCharacteristicId(subCharacteristic.getId()); updated = true; } // New remediation function is not equals to existing one -> update it + // TODO check remediaiton function is not equals to default one, if so do nothing if (!isSameRemediationFunction(ruleChange.debtRemediationFunction(), ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset(), ruleDto)) { DefaultDebtRemediationFunction debtRemediationFunction = new DefaultDebtRemediationFunction(DebtRemediationFunction.Type.valueOf(ruleChange.debtRemediationFunction()), ruleChange.debtRemediationCoefficient(), ruleChange.debtRemediationOffset()); 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 5ccf3b58082..85b660c989e 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(20).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(20); + 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