]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5195 Reset custom rules when resting debt model and copy default debt definitio...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 14 Apr 2014 14:19:52 +0000 (16:19 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Mon, 14 Apr 2014 14:20:01 +0000 (16:20 +0200)
sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java
sonar-server/src/main/java/org/sonar/server/rule/RegisterRules.java
sonar-server/src/main/java/org/sonar/server/rule/RuleOperations.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java

index 978fe32a40dc057b80ca61a5ed0745622071ae6a..e0931dd8dda57452382343a2054f19ee945537ed 100644 (file)
@@ -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<RuleDto> rules) {
+    return Iterables.find(rules, new Predicate<RuleDto>() {
+      @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<RulesDefinition.Rule> rules) {
     return Iterables.find(rules, new Predicate<RulesDefinition.Rule>() {
index b899b08df8d4935ab58a907431584060516a7f8b..a9f8c3a924b986a7f5781e3c8f45fa256485d2b1 100644 (file)
@@ -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())) {
index be2b3022ccedf8f34bc3e6716d53a534e444d911..acde41b673b3a6e414c95a0090d4ae36fb8bf833 100644 (file)
@@ -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);
index 8f986648bfc9b84b50e1d3e092f1249756175a70..54b0c1f5093d67df56a96f4da573d8250d255e15 100644 (file)
@@ -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();
index 39cf65daaf2595b8f70bf27d841f5d0210cb3f8a..3ccce8bb5671eabe04d44702fef5faeb7b63c927 100644 (file)
@@ -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<RuleParamDto> ruleParamArgument = ArgumentCaptor.forClass(RuleParamDto.class);
     verify(ruleDao).insert(ruleParamArgument.capture(), eq(session));