}
@Test
public void update_custom_rule() throws Exception {
// Create template rule
RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
ruleDao.insert(dbSession, templateRule);
RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("Format");
ruleDao.addRuleParam(dbSession, templateRule, templateRuleParam1);
ruleDao.addRuleParam(dbSession, templateRule, templateRuleParam2);
// Create custom rule
RuleDto customRule = RuleTesting.newCustomRule(templateRule)
.setName("Old name")
.setDescription("Old description")
.setSeverity(Severity.MINOR)
.setStatus(RuleStatus.BETA);
ruleDao.insert(dbSession, customRule);
ruleDao.addRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
ruleDao.addRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue(null));
dbSession.commit();
// Update custom rule
RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey())
.setName("New name")
.setMarkdownDescription("New description")
.setSeverity("MAJOR")
.setStatus(RuleStatus.READY)
.setParameters(ImmutableMap.of("regex", "b.*"));
updater.update(update, UserSession.get());
dbSession.clearCache();
// Verify custom rule is updated
Rule customRuleReloaded = ruleIndex.getByKey(customRule.getKey());
assertThat(customRuleReloaded).isNotNull();
assertThat(customRuleReloaded.name()).isEqualTo("New name");
assertThat(customRuleReloaded.htmlDescription()).isEqualTo("New description");
assertThat(customRuleReloaded.severity()).isEqualTo("MAJOR");
assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.READY);