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);
}, 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>() {
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())) {
.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);
));
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();
@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)));
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));