diff options
7 files changed, 175 insertions, 80 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java index 422deba4482..ebc507aa31f 100644 --- a/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java +++ b/sonar-server/src/main/java/org/sonar/server/qualityprofile/RuleActivator.java @@ -97,7 +97,7 @@ public class RuleActivator implements ServerComponent { } } - List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) { + public List<ActiveRuleChange> activate(DbSession dbSession, RuleActivation activation) { RuleActivatorContext context = contextFactory.create(activation.getKey(), dbSession); context.verifyForActivation(); List<ActiveRuleChange> changes = Lists.newArrayList(); @@ -255,7 +255,10 @@ public class RuleActivator implements ServerComponent { } } - private List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) { + /** + * Deactivate a rule on a Quality profile WITHOUT committing db session, WITHOUT checking permissions, and forcing removal of inherited rules + */ + public List<ActiveRuleChange> deactivate(DbSession dbSession, RuleDto ruleDto) { List<ActiveRuleChange> changes = Lists.newArrayList(); List<ActiveRuleDto> activeRules = db.activeRuleDao().findByRule(dbSession, ruleDto); for (ActiveRuleDto activeRule : activeRules) { @@ -268,7 +271,7 @@ public class RuleActivator implements ServerComponent { /** * @param force if true then inherited rules are deactivated */ - private List<ActiveRuleChange> deactivate(DbSession dbSession, ActiveRuleKey key, boolean force) { + public List<ActiveRuleChange> deactivate(DbSession dbSession, ActiveRuleKey key, boolean force) { return cascadeDeactivation(key, dbSession, false, force); } diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java index b103076d8c1..398ad66e6df 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleDeleter.java @@ -45,7 +45,7 @@ public class RuleDeleter implements ServerComponent { if (rule.getParentId() == null) { throw new IllegalStateException("Only custom rules can be deleted"); } - ruleActivator.deactivate(rule); + ruleActivator.deactivate(dbSession, rule); rule.setStatus(RuleStatus.REMOVED); dbClient.ruleDao().update(dbSession, rule); diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java index cf6bddc1d2a..584053fa04f 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdate.java @@ -179,6 +179,10 @@ public class RuleUpdate { return parameters.get(paramKey); } + boolean isCustomRule() { + return isCustomRule; + } + public boolean isChangeTags() { return changeTags; } @@ -216,8 +220,11 @@ public class RuleUpdate { } public boolean isEmpty() { - return !changeMarkdownNote && !changeTags && !changeDebtSubCharacteristic && !changeDebtRemediationFunction && - !changeName && !changeDescription && !changeSeverity && !changeStatus && !changeParameters; + return !changeMarkdownNote && !changeTags && !changeDebtSubCharacteristic && !changeDebtRemediationFunction && isCustomRuleFieldsEmpty(); + } + + private boolean isCustomRuleFieldsEmpty(){ + return !changeName && !changeDescription && !changeSeverity && !changeStatus && !changeParameters; } private void checkCustomRule(){ diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java index 80dac2aa2e8..2087f840c6e 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/RuleUpdater.java @@ -29,10 +29,13 @@ import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.utils.System2; import org.sonar.core.persistence.DbSession; +import org.sonar.core.qualityprofile.db.ActiveRuleDto; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.technicaldebt.db.CharacteristicDto; import org.sonar.server.db.DbClient; +import org.sonar.server.qualityprofile.RuleActivation; +import org.sonar.server.qualityprofile.RuleActivator; import org.sonar.server.user.UserSession; import java.util.Collections; @@ -44,10 +47,12 @@ import static com.google.common.collect.Lists.newArrayList; public class RuleUpdater implements ServerComponent { + private final RuleActivator ruleActivator; private final DbClient dbClient; private final System2 system; - public RuleUpdater(DbClient dbClient, System2 system) { + public RuleUpdater(RuleActivator ruleActivator, DbClient dbClient, System2 system) { + this.ruleActivator = ruleActivator; this.dbClient = dbClient; this.system = system; } @@ -66,6 +71,8 @@ public class RuleUpdater implements ServerComponent { for (RuleParamDto ruleParamDto : context.parameters) { dbClient.ruleDao().updateRuleParam(dbSession, context.rule, ruleParamDto); } + // update related active rules (for custom rules only) + updateActiveRule(dbSession, update, context.rule); dbSession.commit(); return true; @@ -74,6 +81,17 @@ public class RuleUpdater implements ServerComponent { } } + private void updateActiveRule(DbSession dbSession, RuleUpdate update, RuleDto rule) { + if (update.isCustomRule() && update.isChangeParameters()) { + for (ActiveRuleDto activeRuleDto : dbClient.activeRuleDao().findByRule(dbSession, rule)) { + RuleActivation ruleActivation = new RuleActivation(activeRuleDto.getKey()) + .setSeverity(activeRuleDto.getSeverityString()) + .setParameters(update.getParameters()); + ruleActivator.activate(dbSession, ruleActivation); + } + } + } + /** * Load all the DTOs required for validating changes and updating rule */ @@ -176,12 +194,16 @@ public class RuleUpdater implements ServerComponent { } } + /** + * Only update existing parameters, ignore the ones that are not existing in the list + */ private void updateParameters(RuleUpdate update, Context context) { for (RuleParamDto ruleParamDto : context.parameters) { String value = update.parameter(ruleParamDto.getName()); if (!Strings.isNullOrEmpty(value)) { ruleParamDto.setDefaultValue(value); } + // Ignore parameter not existing in update.getParameters() } } diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java index 6fd83dc94ee..5d26d525adb 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleDeleterMediumTest.java @@ -20,7 +20,6 @@ package org.sonar.server.rule; -import com.google.common.collect.ImmutableMap; import org.junit.After; import org.junit.Before; import org.junit.ClassRule; @@ -28,11 +27,11 @@ import org.junit.Test; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rule.Severity; -import org.sonar.check.Cardinality; import org.sonar.core.persistence.DbSession; import org.sonar.core.qualityprofile.db.ActiveRuleKey; import org.sonar.core.qualityprofile.db.QualityProfileDto; import org.sonar.core.qualityprofile.db.QualityProfileKey; +import org.sonar.core.rule.RuleDto; import org.sonar.server.db.DbClient; import org.sonar.server.qualityprofile.ActiveRule; import org.sonar.server.qualityprofile.RuleActivation; @@ -72,35 +71,29 @@ public class RuleDeleterMediumTest { @Test public void delete_custom_rule() throws Exception { // Create template rule - RuleKey templateRuleKey = RuleKey.of("java", "S001"); - dao.insert(dbSession, RuleTesting.newDto(templateRuleKey) - .setCardinality(Cardinality.MULTIPLE) - .setLanguage("xoo")); - dbSession.commit(); + RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo"); + dao.insert(dbSession, templateRule); // Create custom rule - NewRule newRule = new NewRule() - .setTemplateKey(templateRuleKey) - .setName("My custom") - .setHtmlDescription("Some description") - .setSeverity(Severity.MAJOR) - .setStatus(RuleStatus.READY) - .setParameters(ImmutableMap.of("regex", "a.*")); - RuleKey customRuleKey = creator.create(newRule); + RuleDto customRule = RuleTesting.newCustomRule(templateRule).setLanguage("xoo"); + dao.insert(dbSession, customRule); - // Activate the custom rule + // Create a quality profile QualityProfileDto profileDto = QualityProfileDto.createFor(QualityProfileKey.of("P1", "xoo")); db.qualityProfileDao().insert(dbSession, profileDto); + dbSession.commit(); + + // Activate the custom rule tester.get(RuleActivator.class).activate( - new RuleActivation(ActiveRuleKey.of(profileDto.getKey(), customRuleKey)).setSeverity(Severity.BLOCKER) + new RuleActivation(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey())).setSeverity(Severity.BLOCKER) ); // Delete custom rule - deleter.delete(customRuleKey); + deleter.delete(customRule.getKey()); // Verify custom rule have status REMOVED - Rule customRuleReloaded = index.getByKey(customRuleKey); + Rule customRuleReloaded = index.getByKey(customRule.getKey()); assertThat(customRuleReloaded).isNotNull(); assertThat(customRuleReloaded.status()).isEqualTo(RuleStatus.REMOVED); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleTesting.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleTesting.java index 695b1883bce..392e52b1c4c 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleTesting.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleTesting.java @@ -27,6 +27,8 @@ import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.check.Cardinality; import org.sonar.core.rule.RuleDto; +import java.util.Date; + public class RuleTesting { private RuleTesting() { @@ -63,4 +65,15 @@ public class RuleTesting { .setEffortToFixDescription(ruleKey.repository() + "." + ruleKey.rule() + ".effortToFix"); } + public static RuleDto newTemplateRule(RuleKey ruleKey){ + return newDto(ruleKey) + .setCardinality(Cardinality.MULTIPLE); + } + + public static RuleDto newCustomRule(RuleDto templateRule){ + return newDto(RuleKey.of(templateRule.getRepositoryKey(), templateRule.getRuleKey() + "_" + new Date().getTime())) + .setCardinality(Cardinality.SINGLE) + .setParentId(templateRule.getId()); + } + } diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java index d9516d28cd6..b7ac34b4639 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/RuleUpdaterMediumTest.java @@ -28,20 +28,28 @@ import org.junit.ClassRule; import org.junit.Test; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; +import org.sonar.api.rule.Severity; import org.sonar.api.server.debt.DebtRemediationFunction; import org.sonar.api.server.debt.internal.DefaultDebtRemediationFunction; import org.sonar.core.persistence.DbSession; +import org.sonar.core.qualityprofile.db.ActiveRuleKey; +import org.sonar.core.qualityprofile.db.QualityProfileDto; +import org.sonar.core.qualityprofile.db.QualityProfileKey; import org.sonar.core.rule.RuleDto; import org.sonar.core.rule.RuleParamDto; import org.sonar.core.technicaldebt.db.CharacteristicDto; import org.sonar.server.db.DbClient; import org.sonar.server.debt.DebtTesting; +import org.sonar.server.qualityprofile.ActiveRule; +import org.sonar.server.qualityprofile.RuleActivation; +import org.sonar.server.qualityprofile.RuleActivator; +import org.sonar.server.qualityprofile.index.ActiveRuleIndex; +import org.sonar.server.rule.db.RuleDao; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.tester.ServerTester; import org.sonar.server.user.MockUserSession; import org.sonar.server.user.UserSession; -import java.util.List; import java.util.Set; import static org.fest.assertions.Assertions.assertThat; @@ -55,7 +63,9 @@ public class RuleUpdaterMediumTest { public static ServerTester tester = new ServerTester(); DbClient db = tester.get(DbClient.class); + RuleDao ruleDao = tester.get(RuleDao.class); DbSession dbSession; + RuleIndex ruleIndex = tester.get(RuleIndex.class); RuleUpdater updater = tester.get(RuleUpdater.class); int reliabilityId, softReliabilityId, hardReliabilityId; @@ -72,7 +82,7 @@ public class RuleUpdaterMediumTest { @Test public void do_not_update_rule_with_removed_status() throws Exception { - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY).setStatus(RuleStatus.REMOVED)); + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY).setStatus(RuleStatus.REMOVED)); dbSession.commit(); RuleUpdate update = new RuleUpdate(RULE_KEY).setTags(Sets.newHashSet("java9")); @@ -86,7 +96,7 @@ public class RuleUpdaterMediumTest { @Test public void no_changes() throws Exception { - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) // the following fields are not supposed to be updated .setNoteData("my *note*") .setNoteUserLogin("me") @@ -102,7 +112,7 @@ public class RuleUpdaterMediumTest { updater.update(update, UserSession.get()); dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getNoteData()).isEqualTo("my *note*"); assertThat(rule.getNoteUserLogin()).isEqualTo("me"); assertThat(rule.getTags()).containsOnly("tag1"); @@ -116,7 +126,7 @@ public class RuleUpdaterMediumTest { public void set_markdown_note() throws Exception { MockUserSession.set().setLogin("me"); - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setNoteData(null) .setNoteUserLogin(null) @@ -133,7 +143,7 @@ public class RuleUpdaterMediumTest { updater.update(update, UserSession.get()); dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getNoteData()).isEqualTo("my *note*"); assertThat(rule.getNoteUserLogin()).isEqualTo("me"); assertThat(rule.getNoteCreatedAt()).isNotNull(); @@ -148,7 +158,7 @@ public class RuleUpdaterMediumTest { @Test public void remove_markdown_note() throws Exception { - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setNoteData("my *note*") .setNoteUserLogin("me")); dbSession.commit(); @@ -157,7 +167,7 @@ public class RuleUpdaterMediumTest { updater.update(update, UserSession.get()); dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getNoteData()).isNull(); assertThat(rule.getNoteUserLogin()).isNull(); assertThat(rule.getNoteCreatedAt()).isNull(); @@ -167,7 +177,7 @@ public class RuleUpdaterMediumTest { @Test public void set_tags() throws Exception { // insert db - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setTags(Sets.newHashSet("security")) .setSystemTags(Sets.newHashSet("java8", "javadoc"))); dbSession.commit(); @@ -177,7 +187,7 @@ public class RuleUpdaterMediumTest { updater.update(update, UserSession.get()); dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getTags()).containsOnly("bug"); assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); @@ -188,7 +198,7 @@ public class RuleUpdaterMediumTest { @Test public void remove_tags() throws Exception { - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setTags(Sets.newHashSet("security")) .setSystemTags(Sets.newHashSet("java8", "javadoc"))); dbSession.commit(); @@ -197,7 +207,7 @@ public class RuleUpdaterMediumTest { updater.update(update, UserSession.get()); dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getTags()).isEmpty(); assertThat(rule.getSystemTags()).containsOnly("java8", "javadoc"); @@ -209,7 +219,7 @@ public class RuleUpdaterMediumTest { @Test public void override_debt() throws Exception { insertDebtCharacteristics(dbSession); - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setDefaultSubCharacteristicId(hardReliabilityId) .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) .setDefaultRemediationCoefficient("1d") @@ -227,7 +237,7 @@ public class RuleUpdaterMediumTest { // verify db dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getSubCharacteristicId()).isEqualTo(softReliabilityId); assertThat(rule.getRemediationFunction()).isEqualTo(DebtRemediationFunction.Type.CONSTANT_ISSUE.name()); assertThat(rule.getRemediationCoefficient()).isNull(); @@ -245,7 +255,7 @@ public class RuleUpdaterMediumTest { @Test public void reset_debt() throws Exception { insertDebtCharacteristics(dbSession); - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setDefaultSubCharacteristicId(hardReliabilityId) .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) .setDefaultRemediationCoefficient("1d") @@ -262,7 +272,7 @@ public class RuleUpdaterMediumTest { // verify db dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getSubCharacteristicId()).isNull(); assertThat(rule.getRemediationFunction()).isNull(); assertThat(rule.getRemediationCoefficient()).isNull(); @@ -280,7 +290,7 @@ public class RuleUpdaterMediumTest { @Test public void unset_debt() throws Exception { insertDebtCharacteristics(dbSession); - db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + ruleDao.insert(dbSession, RuleTesting.newDto(RULE_KEY) .setDefaultSubCharacteristicId(hardReliabilityId) .setDefaultRemediationFunction(DebtRemediationFunction.Type.LINEAR.name()) .setDefaultRemediationCoefficient("1d") @@ -297,7 +307,7 @@ public class RuleUpdaterMediumTest { // verify db dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); + RuleDto rule = ruleDao.getNullableByKey(dbSession, RULE_KEY); assertThat(rule.getSubCharacteristicId()).isEqualTo(-1); assertThat(rule.getRemediationFunction()).isNull(); assertThat(rule.getRemediationCoefficient()).isNull(); @@ -310,55 +320,102 @@ public class RuleUpdaterMediumTest { //TODO pb with db code -1 ? assertThat(indexedRule.debtRemediationFunction()).isNull(); } - private void insertDebtCharacteristics(DbSession dbSession) { - CharacteristicDto reliability = DebtTesting.newCharacteristicDto("RELIABILITY"); - db.debtCharacteristicDao().insert(reliability, dbSession); - reliabilityId = reliability.getId(); - - CharacteristicDto softReliability = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY") - .setParentId(reliability.getId()); - db.debtCharacteristicDao().insert(softReliability, dbSession); - softReliabilityId = softReliability.getId(); - - CharacteristicDto hardReliability = DebtTesting.newCharacteristicDto("HARD_RELIABILITY") - .setParentId(reliability.getId()); - db.debtCharacteristicDao().insert(hardReliability, dbSession); - hardReliabilityId = hardReliability.getId(); - } - @Test public void update_custom_rule() throws Exception { - insertDebtCharacteristics(dbSession); - RuleDto ruledto = db.ruleDao().insert(dbSession, RuleTesting.newDto(RULE_KEY) + // Create template rule + RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")); + ruleDao.insert(dbSession, templateRule); + RuleParamDto templateRuleParam = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"); + ruleDao.addRuleParam(dbSession, templateRule, templateRuleParam); + + // Create custom rule + RuleDto customRule = RuleTesting.newCustomRule(templateRule) .setName("Old name") .setDescription("Old description") - .setSeverity("MINOR") - .setStatus(RuleStatus.BETA) - ); - RuleParamDto ruleParamDto = RuleParamDto.createFor(ruledto).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"); - db.ruleDao().addRuleParam(dbSession, ruledto, ruleParamDto); + .setSeverity(Severity.MINOR) + .setStatus(RuleStatus.BETA); + ruleDao.insert(dbSession, customRule); + ruleDao.addRuleParam(dbSession, customRule, templateRuleParam.setDefaultValue("a.*")); dbSession.commit(); - RuleUpdate update = RuleUpdate.createForCustomRule(RULE_KEY) + // Update custom rule + RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()) .setName("New name") .setHtmlDescription("New description") .setSeverity("MAJOR") .setStatus(RuleStatus.READY) - .setParameters(ImmutableMap.of("regex", "a.*")); + .setParameters(ImmutableMap.of("regex", "b.*")); updater.update(update, UserSession.get()); - // verify db dbSession.clearCache(); - RuleDto rule = db.ruleDao().getNullableByKey(dbSession, RULE_KEY); - assertThat(rule.getName()).isEqualTo("New name"); - assertThat(rule.getDescription()).isEqualTo("New description"); - assertThat(rule.getSeverityString()).isEqualTo("MAJOR"); - assertThat(rule.getStatus()).isEqualTo(RuleStatus.READY); - - List<RuleParamDto> params = db.ruleDao().findRuleParamsByRuleKey(dbSession, RULE_KEY); - assertThat(params).hasSize(1); - RuleParamDto param = params.get(0); - assertThat(param.getDefaultValue()).isEqualTo("a.*"); + + // 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); + assertThat(customRuleReloaded.params()).hasSize(1); + + RuleParam param = customRuleReloaded.params().get(0); + assertThat(param.defaultValue()).isEqualTo("b.*"); + } + + @Test + public void update_active_rule_parameters_when_updating_custom_rule() throws Exception { + // Create template rule with a parameter + RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001")).setLanguage("xoo"); + ruleDao.insert(dbSession, templateRule); + RuleParamDto templateRuleParam = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*"); + ruleDao.addRuleParam(dbSession, templateRule, templateRuleParam); + + // Create custom rule with a parameter + RuleDto customRule = RuleTesting.newCustomRule(templateRule).setSeverity(Severity.MAJOR).setLanguage("xoo"); + ruleDao.insert(dbSession, customRule); + ruleDao.addRuleParam(dbSession, customRule, templateRuleParam.setDefaultValue("a.*")); + + // Create a quality profile + QualityProfileKey qualityProfileKey = QualityProfileKey.of("P1", "xoo"); + QualityProfileDto profileDto = QualityProfileDto.createFor(qualityProfileKey); + db.qualityProfileDao().insert(dbSession, profileDto); + + dbSession.commit(); + + // Activate the custom rule + tester.get(RuleActivator.class).activate( + new RuleActivation(ActiveRuleKey.of(profileDto.getKey(), customRule.getKey())).setSeverity(Severity.BLOCKER) + ); + + dbSession.clearCache(); + + // Update custom rule parameter + RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()) + .setParameters(ImmutableMap.of("regex", "b.*")); + updater.update(update, UserSession.get()); + + // Verify active rule parameter has been updated + ActiveRule activeRule = tester.get(ActiveRuleIndex.class).getByKey(ActiveRuleKey.of(qualityProfileKey, customRule.getKey())); + assertThat(activeRule.params().get("regex")).isEqualTo("b.*"); + + // Verify that severity has not changed + assertThat(activeRule.severity()).isEqualTo(Severity.BLOCKER); + } + + private void insertDebtCharacteristics(DbSession dbSession) { + CharacteristicDto reliability = DebtTesting.newCharacteristicDto("RELIABILITY"); + db.debtCharacteristicDao().insert(reliability, dbSession); + reliabilityId = reliability.getId(); + + CharacteristicDto softReliability = DebtTesting.newCharacteristicDto("SOFT_RELIABILITY") + .setParentId(reliability.getId()); + db.debtCharacteristicDao().insert(softReliability, dbSession); + softReliabilityId = softReliability.getId(); + + CharacteristicDto hardReliability = DebtTesting.newCharacteristicDto("HARD_RELIABILITY") + .setParentId(reliability.getId()); + db.debtCharacteristicDao().insert(hardReliability, dbSession); + hardReliabilityId = hardReliability.getId(); } } |