]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 When restoring model from XML call RuleOperations to update restored rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Apr 2014 06:56:47 +0000 (08:56 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Apr 2014 06:56:47 +0000 (08:56 +0200)
sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java
sonar-server/src/main/java/org/sonar/server/debt/DebtModelXMLExporter.java
sonar-server/src/main/java/org/sonar/server/debt/DebtRulesXMLImporter.java
sonar-server/src/main/java/org/sonar/server/rule/DeprecatedRulesDefinition.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/debt/DebtModelXMLExporterTest.java
sonar-server/src/test/java/org/sonar/server/debt/DebtRulesXMLImporterTest.java
sonar-server/src/test/java/org/sonar/server/rule/DeprecatedRulesDefinitionTest.java
sonar-server/src/test/java/org/sonar/server/rule/RuleOperationsTest.java

index aeae487b8b9bf7f15849b4b7b1845047b0584594..0979ad5d54faf81a82c41722bc3c60ec79dbc7d5 100644 (file)
@@ -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;
index a10081174e96a51045453e1dcc6099da25dab7b0..4186a464672e75d32c902556d538ee8cddafb01c 100644 (file)
@@ -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 + '\'' +
         '}';
index 1e58ccc8419a822d4c2b24cf94afdfd7d84e1e61..66447c840cd46b38300bad042d2880fd0f4b45b1 100644 (file)
@@ -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;
   }
index b762a9abe8fffc43acd6f3613f64eb44c7206fc3..02a04434f313cc028a02d4c5800458884bd0d5ae 100644 (file)
@@ -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));
index bd5fd9df3c9b0c2746cf64b2ab66d78c389f9237..75c4b3c26e347f35e9d9dc11fda8e3bc060a917e 100644 (file)
@@ -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();
   }
 
index 9e20d4bea99ec6649560a8da6be2d89ffe4428dc..cf14a6bf46b7a92c6bf0dd30ccbb3abeb6ee193b 100644 (file)
@@ -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("<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
@@ -632,52 +555,11 @@ public class DebtModelBackupTest {
 
     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
@@ -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("<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
@@ -744,7 +624,7 @@ public class DebtModelBackupTest {
 
     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")
@@ -753,19 +633,11 @@ public class DebtModelBackupTest {
 
     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
@@ -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.<RuleDto>emptyList());
 
index b19669e265caf0fcb762268c301cb29174579a14..ff7cf0916252ee9fe38350e294de394de5e44a20 100644 (file)
@@ -60,7 +60,7 @@ public class DebtModelXMLExporterTest {
 
     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));
@@ -97,7 +97,7 @@ public class DebtModelXMLExporterTest {
 
     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 +
index a1dfd7062ae679248e3e1f259f2b63c338e5742e..be069aa1b91ad8c417611104373a5307def93e39 100644 (file)
@@ -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();
   }
index aa4402c8b99a9d8a59c98b060f052c3e17b1cfa7..59bfb301f1ed4822fa779998c84f866f0d88262a 100644 (file)
@@ -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")
     );
 
index 99663635a0523bc846ff38f2a9912daf4a2519e9..0bd6dea327ffc2d7a5d9017788705c996a18c5fb 100644 (file)
@@ -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();