From 1b1a14babbbd403bfcb5707a2e9535279672cc78 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 3 Apr 2014 12:06:26 +0200 Subject: [PATCH] SONAR-5056 Add unit test when reseting debt model on custom rules --- .../sonar/server/debt/DebtModelBackup.java | 1 + .../server/debt/DebtModelBackupTest.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+) 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 05663999d7a..bf0e9c7ca76 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 @@ -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); 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 1ff603cd064..9e20d4bea99 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 @@ -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() -- 2.39.5