import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimaps;
import com.google.common.collect.Sets;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
Profiler profiler = Profiler.create(Loggers.get(getClass())).startInfo("Register quality profiles");
DbSession session = dbClient.openSession(false);
try {
+ activeRuleIndexer.setEnabled(true);
ListMultimap<String, RulesProfile> profilesByLanguage = profilesByLanguage();
for (String language : profilesByLanguage.keySet()) {
List<RulesProfile> defs = profilesByLanguage.get(language);
registerProfilesForLanguage(session, language, defs);
}
}
- activeRuleIndexer.setEnabled(true).index();
+ activeRuleIndexer.index();
profiler.stopDebug();
} finally {
QProfileName profileName = new QProfileName(language, name);
if (shouldRegister(profileName, session)) {
register(profileName, entry.getValue(), session);
- session.commit();
}
builtInProfiles.put(language, name);
}
QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), session);
if (profileDto != null) {
- profileFactory.delete(session, profileDto.getKey(), true);
+ // When deleting the profile, we also remove active rule from index in order to have to deal with conflicts when activating new rules
+ // (for instance, if same rule is removed then activated)
+ List<ActiveRuleChange> deleteChanges = profileFactory.delete(session, profileDto.getKey(), true);
+ session.commit();
+ activeRuleIndexer.index(deleteChanges);
}
profileFactory.create(session, name);
+ List<ActiveRuleChange> changes = new ArrayList<>();
for (RulesProfile profile : profiles) {
for (org.sonar.api.rules.ActiveRule activeRule : profile.getActiveRules()) {
RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
activation.setParameter(param.getKey(), param.getValue());
}
- ruleActivator.activate(session, activation, name);
+ changes.addAll(ruleActivator.activate(session, activation, name));
}
}
LoadedTemplateDto template = new LoadedTemplateDto(templateKey(name), LoadedTemplateDto.QUALITY_PROFILE_TYPE);
dbClient.loadedTemplateDao().insert(template, session);
+ session.commit();
+ activeRuleIndexer.index(changes);
}
private void setDefault(String language, List<RulesProfile> profileDefs, DbSession session) {
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.guava.api.Assertions.assertThat;
+// TODO replace this MediumTest by DbTester and EsTester
public class RegisterQualityProfilesMediumTest {
ServerTester tester;
@Test
public void register_existing_profile_definitions() {
- tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+ tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
// Check in ES
- assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
+ assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
tester.get(Platform.class).restart();
assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
// Check in ES
- assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey);
+ assertThat(tester.get(RuleIndex2.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
// TODO
// Check ActiveRuleParameters in DB
- Map<String, ActiveRuleParamDto> params =
- ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
+ Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleKey(dbSession, activeRule.getKey()));
assertThat(params).hasSize(2);
// set by profile
assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
@Test
public void register_profile_definitions() {
- tester = new ServerTester().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+ tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
@Test
public void do_not_register_profile_if_missing_language() {
// xoo language is not installed
- tester = new ServerTester().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+ tester = new ServerTester().withEsIndexes().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);
@Test
public void fail_if_two_definitions_are_marked_as_default_on_the_same_language() {
- tester = new ServerTester().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
+ tester = new ServerTester().withEsIndexes().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", true));
try {
tester.start();
@Test
public void mark_profile_as_default() {
- tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
+ tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", false), new SimpleProfileDefinition("two", true));
tester.start();
verifyDefaultProfile("xoo", "two");
@Test
public void use_sonar_way_as_default_profile_if_none_are_marked_as_default() {
- tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
+ tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("Sonar way", false), new SimpleProfileDefinition("Other way", false));
tester.start();
verifyDefaultProfile("xoo", "Sonar way");
@Test
public void do_not_reset_default_profile_if_still_valid() {
- tester = new ServerTester().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
+ tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(new SimpleProfileDefinition("one", true), new SimpleProfileDefinition("two", false));
tester.start();
QualityProfileDao profileDao = dbClient().qualityProfileDao();
*/
@Test
public void clean_up_profiles_if_missing_loaded_template() {
- tester = new ServerTester().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
+ tester = new ServerTester().withEsIndexes().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
tester.start();
dbSession = dbClient().openSession(false);