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);
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()