]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5056 Add unit test when reseting debt model on custom rules
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 10:06:26 +0000 (12:06 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 3 Apr 2014 10:06:26 +0000 (12:06 +0200)
sonar-server/src/main/java/org/sonar/server/debt/DebtModelBackup.java
sonar-server/src/test/java/org/sonar/server/debt/DebtModelBackupTest.java

index 05663999d7a6fb60d1871c26497d668773d118d5..bf0e9c7ca7695edd62aa7759444814edb619714f 100644 (file)
@@ -158,6 +158,7 @@ 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
         if (ruleDef != null) {
           String subCharacteristicKey = ruleDef.debtSubCharacteristic();
           CharacteristicDto subCharacteristicDto = characteristicByKey(subCharacteristicKey, allCharacteristicDtos, false);
index 1ff603cd0647be4c120b8e09bc442f7260dbcf27..9e20d4bea99ec6649560a8da6be2d89ffe4428dc 100644 (file)
@@ -462,6 +462,45 @@ public class DebtModelBackupTest {
     assertThat(rule.getUpdatedAt()).isEqualTo(now);
   }
 
+  @Test
+  public void reset_model_on_custom_rules() throws Exception {
+    when(characteristicsXMLImporter.importXML(any(Reader.class))).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 updated").setOrder(2).setCreatedAt(oldDate),
+      new CharacteristicDto().setId(2).setKey("COMPILER").setName("Compiler updated").setParentId(1).setCreatedAt(oldDate)
+    ));
+
+    when(ruleDao.selectEnablesAndNonManual(session)).thenReturn(newArrayList(
+      // Custom rule will not be found in rules definitions
+      new RuleDto().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();
+    when(defLoader.load()).thenReturn(context);
+
+    debtModelBackup.reset();
+
+    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.getSubCharacteristicId()).isNull();
+    assertThat(rule.getRemediationFunction()).isNull();
+    assertThat(rule.getRemediationCoefficient()).isNull();
+    assertThat(rule.getRemediationOffset()).isNull();
+    assertThat(rule.getUpdatedAt()).isEqualTo(now);
+  }
+
   @Test
   public void restore_from_xml_with_different_characteristic_and_same_function_as_default_values() throws Exception {
     when(characteristicsXMLImporter.importXML(anyString())).thenReturn(new DebtModel()