import com.google.common.base.Function;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Iterables;
+import com.google.common.collect.Maps;
import com.google.common.collect.Sets;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.System2;
import org.sonar.api.utils.TimeProfiler;
import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleDto;
+import org.sonar.core.qualityprofile.db.ActiveRuleParamDto;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.core.technicaldebt.db.CharacteristicDao;
private void mergeParams(RulesDefinition.Rule ruleDef, RuleDto rule, DbSession session) {
List<RuleParamDto> paramDtos = dbClient.ruleDao().findRuleParamsByRuleKey(session, rule.getKey());
- List<String> existingParamDtoNames = new ArrayList<String>();
+ Map<String, RuleParamDto> existingParamsByName = Maps.newHashMap();
for (RuleParamDto paramDto : paramDtos) {
RulesDefinition.Param paramDef = ruleDef.param(paramDto.getName());
if (paramDef == null) {
- // TODO cascade on the activeRule upon RuleDeletion
+ dbClient.activeRuleDao().deleteParamsByRuleParam(session, rule, paramDto.getName());
dbClient.ruleDao().removeRuleParam(session, rule, paramDto);
} else {
- // TODO validate that existing active rules still match constraints
- // TODO store param name
if (mergeParam(paramDto, paramDef)) {
dbClient.ruleDao().updateRuleParam(session, rule, paramDto);
}
- existingParamDtoNames.add(paramDto.getName());
+ existingParamsByName.put(paramDto.getName(), paramDto);
}
}
+
+ // Create newly parameters
for (RulesDefinition.Param param : ruleDef.params()) {
- if (!existingParamDtoNames.contains(param.key())) {
- RuleParamDto paramDto = RuleParamDto.createFor(rule)
+ RuleParamDto paramDto = existingParamsByName.get(param.key());
+ if (paramDto == null) {
+ paramDto = RuleParamDto.createFor(rule)
.setName(param.key())
.setDescription(param.description())
.setDefaultValue(param.defaultValue())
.setType(param.type().toString());
dbClient.ruleDao().addRuleParam(session, rule, paramDto);
+ if (!StringUtils.isEmpty(param.defaultValue())) {
+ // Propagate the default value to existing active rules
+ for (ActiveRuleDto activeRule : dbClient.activeRuleDao().findByRule(session, rule)) {
+ ActiveRuleParamDto activeParam = ActiveRuleParamDto.createFor(paramDto).setValue(param.defaultValue());
+ dbClient.activeRuleDao().addParam(session, activeRule, activeParam);
+ }
+ }
}
}
}
import org.sonar.api.utils.MessageException;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.core.persistence.DbSession;
+import org.sonar.core.qualityprofile.db.ActiveRuleKey;
import org.sonar.core.rule.RuleDto;
import org.sonar.core.rule.RuleParamDto;
import org.sonar.server.db.DbClient;
import org.sonar.server.platform.Platform;
+import org.sonar.server.qualityprofile.ActiveRule;
import org.sonar.server.qualityprofile.QProfileService;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.RuleActivation;
+import org.sonar.server.qualityprofile.index.ActiveRuleIndex;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.search.QueryOptions;
import java.util.Date;
import java.util.List;
+import java.util.Map;
import static com.google.common.collect.Sets.newHashSet;
import static org.fest.assertions.Assertions.assertThat;
@ClassRule
public static final ServerTester TESTER = new ServerTester().addComponents(RULE_DEFS);
- RuleIndex index;
+ RuleIndex ruleIndex;
+ ActiveRuleIndex activeRuleIndex;
DbClient db;
DbSession dbSession;
db = TESTER.get(DbClient.class);
dbSession = TESTER.get(DbClient.class).openSession(false);
dbSession.clearCache();
- index = TESTER.get(RuleIndex.class);
+ ruleIndex = TESTER.get(RuleIndex.class);
+ activeRuleIndex = TESTER.get(ActiveRuleIndex.class);
}
@After
db = TESTER.get(DbClient.class);
dbSession = TESTER.get(DbClient.class).openSession(false);
dbSession.clearCache();
- index = TESTER.get(RuleIndex.class);
+ ruleIndex = TESTER.get(RuleIndex.class);
}
@Test
assertThat(ruleParams).hasSize(2);
// verify es
- Result<Rule> searchResult = index.search(new RuleQuery(), new QueryOptions());
+ Result<Rule> searchResult = ruleIndex.search(new RuleQuery(), new QueryOptions());
assertThat(searchResult.getTotal()).isEqualTo(1);
assertThat(searchResult.getHits()).hasSize(1);
- Rule rule = index.getByKey(RuleKey.of("xoo", "x1"));
+ Rule rule = ruleIndex.getByKey(RuleKey.of("xoo", "x1"));
assertThat(rule.severity()).isEqualTo(Severity.MINOR);
assertThat(rule.name()).isEqualTo("x1 name");
assertThat(rule.htmlDescription()).isEqualTo("x1 desc");
register(rules);
// verify that rules are indexed
- Result<Rule> searchResult = index.search(new RuleQuery(), new QueryOptions());
- searchResult = index.search(new RuleQuery().setKey("xoo:x1"), new QueryOptions());
+ Result<Rule> searchResult = ruleIndex.search(new RuleQuery(), new QueryOptions());
+ searchResult = ruleIndex.search(new RuleQuery().setKey("xoo:x1"), new QueryOptions());
assertThat(searchResult.getTotal()).isEqualTo(1);
assertThat(searchResult.getHits()).hasSize(1);
assertThat(searchResult.getHits().get(0).key()).isEqualTo(RuleKey.of("xoo", "x1"));
}
});
- Rule rule = index.getByKey(RuleTesting.XOO_X1);
+ Rule rule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(rule.severity()).isEqualTo(Severity.INFO);
assertThat(rule.name()).isEqualTo("Name2");
assertThat(rule.htmlDescription()).isEqualTo("Desc2");
register(rules);
// Store updated at date
- Date updatedAt = index.getByKey(RuleTesting.XOO_X1).updatedAt();
+ Date updatedAt = ruleIndex.getByKey(RuleTesting.XOO_X1).updatedAt();
// Re-execute startup tasks
register(rules);
// Verify rule has not been updated
- Rule customRuleReloaded = index.getByKey(RuleTesting.XOO_X1);
+ Rule customRuleReloaded = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(DateUtils.isSameInstant(customRuleReloaded.updatedAt(), updatedAt));
}
@Test
- public void uninstall_and_reinstall_rules() {
+ public void disable_then_enable_rules() {
Rules rules = new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
register(null);
RuleDto rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
assertThat(rule.getStatus()).isEqualTo(RuleStatus.REMOVED);
- Rule indexedRule = index.getByKey(RuleTesting.XOO_X1);
+ Rule indexedRule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(indexedRule.status()).isEqualTo(RuleStatus.REMOVED);
// Re-install plugin
register(rules);
rule = db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1);
assertThat(rule.getStatus()).isEqualTo(RuleStatus.READY);
- indexedRule = index.getByKey(RuleTesting.XOO_X1);
+ indexedRule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(indexedRule.status()).isEqualTo(RuleStatus.READY);
}
// Create a profile and activate rule
MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
-
db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
dbSession.commit();
dbSession.clearCache();
repository.createRule("x2").setName("x2 name").setHtmlDescription("x2 desc");
}
});
- assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x1")).getStatus()).isEqualTo(RuleStatus.REMOVED);
- assertThat(db.ruleDao().getByKey(dbSession, RuleKey.of("xoo", "x2")).getStatus()).isEqualTo(RuleStatus.READY);
- assertThat(db.activeRuleDao().findByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(0);
+ assertThat(ruleIndex.getByKey(RuleKey.of("xoo", "x1")).status()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(ruleIndex.getByKey(RuleKey.of("xoo", "x2")).status()).isEqualTo(RuleStatus.READY);
+ assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(0);
}
@Test
- public void do_not_deactivate_removed_rules_if_repository_accidentaly_uninstalled() throws Exception {
+ public void do_not_deactivate_removed_rules_if_repository_accidentally_uninstalled() throws Exception {
Rules rules = new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
// Restart without xoo
register(null);
- assertThat(db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1).getStatus()).isEqualTo(RuleStatus.REMOVED);
- assertThat(db.activeRuleDao().findByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
+ assertThat(ruleIndex.getByKey(RuleTesting.XOO_X1).status()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(1);
// Re-install
register(rules);
- assertThat(db.ruleDao().getByKey(dbSession, RuleTesting.XOO_X1).getStatus()).isEqualTo(RuleStatus.READY);
- assertThat(db.activeRuleDao().findByProfileKey(dbSession, QProfileTesting.XOO_P1_KEY)).hasSize(1);
+ assertThat(ruleIndex.getByKey(RuleTesting.XOO_X1).status()).isEqualTo(RuleStatus.READY);
+ assertThat(activeRuleIndex.findByProfile(QProfileTesting.XOO_P1_KEY)).hasSize(1);
+ }
+
+ @Test
+ public void update_active_rules_on_param_changes() throws Exception {
+ register(new Rules() {
+ @Override
+ public void init(RulesDefinition.NewRepository repository) {
+ RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
+ // has default value
+ x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("5");
+ // no default value
+ x1Rule.createParam("format").setType(RuleParamType.STRING);
+ }
+ });
+
+ // Create profile and activate rule
+ MockUserSession.set().setGlobalPermissions(GlobalPermissions.QUALITY_PROFILE_ADMIN).setLogin("me");
+ db.qualityProfileDao().insert(dbSession, QProfileTesting.newXooP1());
+ dbSession.commit();
+ dbSession.clearCache();
+ RuleActivation activation = new RuleActivation(RuleTesting.XOO_X1);
+ activation.setParameter("format", "txt");
+ TESTER.get(QProfileService.class).activate(QProfileTesting.XOO_P1_KEY, activation);
+
+ // Default value of "min" is changed, "format" is removed, "format2" is added, "max" is added with a default value
+ register(new Rules() {
+ @Override
+ public void init(RulesDefinition.NewRepository repository) {
+ RulesDefinition.NewRule x1Rule = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc");
+ x1Rule.createParam("min").setType(RuleParamType.INTEGER).setDefaultValue("6");
+ x1Rule.createParam("format2").setType(RuleParamType.STRING);
+ x1Rule.createParam("max").setType(RuleParamType.INTEGER).setDefaultValue("10");
+ }
+ });
+
+ ActiveRule activeRule = activeRuleIndex.getByKey(ActiveRuleKey.of(QProfileTesting.XOO_P1_KEY, RuleTesting.XOO_X1));
+ Map<String, String> params = activeRule.params();
+ assertThat(params).hasSize(2);
+
+ // do not change default value on existing active rules -> keep min=5
+ assertThat(params.get("min")).isEqualTo("5");
+
+ // new param with default value
+ assertThat(params.get("max")).isEqualTo("10");
}
@Test
repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setTags("tag1");
}
});
- Rule rule = index.getByKey(RuleTesting.XOO_X1);
+ Rule rule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(rule.systemTags()).containsOnly("tag1");
assertThat(rule.tags()).isEmpty();
// User adds tag
TESTER.get(RuleUpdater.class).update(RuleUpdate.createForPluginRule(RuleTesting.XOO_X1).setTags(newHashSet("tag2")), UserSession.get());
dbSession.clearCache();
- rule = index.getByKey(RuleTesting.XOO_X1);
+ rule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(rule.systemTags()).containsOnly("tag1");
assertThat(rule.tags()).containsOnly("tag2");
repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setTags("tag1", "tag2");
}
});
- rule = index.getByKey(RuleTesting.XOO_X1);
+ rule = ruleIndex.getByKey(RuleTesting.XOO_X1);
assertThat(rule.systemTags()).containsOnly("tag1", "tag2");
assertThat(rule.tags()).isEmpty();
}
.setDescription("format parameter");
}
});
- Rule template = index.getByKey(RuleKey.of("xoo", "T1"));
+ Rule template = ruleIndex.getByKey(RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewRule.createForCustomRule("CUSTOM_RULE", template.key())
});
// Verify custom rule has been restore from the template
- Rule customRule = index.getByKey(customRuleKey);
+ Rule customRule = ruleIndex.getByKey(customRuleKey);
assertThat(customRule.language()).isEqualTo("xoo");
assertThat(customRule.internalKey()).isEqualTo("new_internal");
assertThat(customRule.severity()).isEqualTo(Severity.BLOCKER);
}
};
register(rules);
- Rule template = index.getByKey(RuleKey.of("xoo", "T1"));
+ Rule template = ruleIndex.getByKey(RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewRule.createForCustomRule("CUSTOM_RULE", template.key())
.setStatus(RuleStatus.READY)
.setParameters(ImmutableMap.of("format", "txt")));
- Date updatedAt = index.getByKey(customRuleKey).updatedAt();
+ Date updatedAt = ruleIndex.getByKey(customRuleKey).updatedAt();
register(rules);
// Verify custom rule has been restore from the template
- Rule customRuleReloaded = index.getByKey(customRuleKey);
+ Rule customRuleReloaded = ruleIndex.getByKey(customRuleKey);
assertThat(customRuleReloaded.updatedAt()).isEqualTo(updatedAt);
}
.setDescription("format parameter");
}
});
- Rule templateRule = index.getByKey(RuleKey.of("xoo", "T1"));
+ Rule templateRule = ruleIndex.getByKey(RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewRule.createForCustomRule("CUSTOM_RULE", templateRule.key())
.setHtmlDescription("template1 desc")
.setSeverity(Severity.MAJOR)
.setTemplate(true)
- // "format" removed, "format2" added
+ // "format" removed, "format2" added
.createParam("format2")
.setDefaultValue("csv")
.setType(RuleParamType.STRING)
});
// Verify custom rule param has not been changed!
- Rule customRuleReloaded = index.getByKey(customRuleKey);
+ Rule customRuleReloaded = ruleIndex.getByKey(customRuleKey);
assertThat(customRuleReloaded.params().get(0).key()).isEqualTo("format");
}
}
};
register(rules);
- Rule templateRule = index.getByKey(RuleKey.of("xoo", "T1"));
+ Rule templateRule = ruleIndex.getByKey(RuleKey.of("xoo", "T1"));
// Create custom rule
RuleKey customRuleKey = TESTER.get(RuleCreator.class).create(NewRule.createForCustomRule("CUSTOM_RULE", templateRule.key())
.setSeverity(Severity.MAJOR)
.setStatus(RuleStatus.READY)
.setParameters(ImmutableMap.of("format", "txt")));
- assertThat(index.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.READY);
+ assertThat(ruleIndex.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.READY);
// Restart without template
register(null);
// Verify custom rule is removed
- assertThat(index.getByKey(templateRule.key()).status()).isEqualTo(RuleStatus.REMOVED);
- assertThat(index.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(ruleIndex.getByKey(templateRule.key()).status()).isEqualTo(RuleStatus.REMOVED);
+ assertThat(ruleIndex.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.REMOVED);
// Re-install template
register(rules);
- assertThat(index.getByKey(templateRule.key()).status()).isEqualTo(RuleStatus.READY);
- assertThat(index.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.READY);
+ assertThat(ruleIndex.getByKey(templateRule.key()).status()).isEqualTo(RuleStatus.READY);
+ assertThat(ruleIndex.getByKey(customRuleKey).status()).isEqualTo(RuleStatus.READY);
}
@Test
.setHtmlDescription("Some description"));
dbSession.commit();
dbSession.clearCache();
- assertThat(index.getByKey(manualRuleKey).status()).isEqualTo(RuleStatus.READY);
+ assertThat(ruleIndex.getByKey(manualRuleKey).status()).isEqualTo(RuleStatus.READY);
// Restart
register(null);
// Verify manual rule is still ready
- assertThat(index.getByKey(manualRuleKey).status()).isEqualTo(RuleStatus.READY);
+ assertThat(ruleIndex.getByKey(manualRuleKey).status()).isEqualTo(RuleStatus.READY);
}
interface Rules {