*/
package org.sonar.server.qualityprofile;
+import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
Map<QProfileName, RulesProfileDto> persistedRuleProfiles = loadPersistedProfiles(dbSession);
+ List<ActiveRuleChange> changes = new ArrayList<>();
builtInQProfiles.forEach(builtIn -> {
RulesProfileDto ruleProfile = persistedRuleProfiles.get(builtIn.getQProfileName());
if (ruleProfile == null) {
register(dbSession, batchDbSession, builtIn);
} else {
- update(dbSession, builtIn, ruleProfile);
+ changes.addAll(update(dbSession, builtIn, ruleProfile));
}
});
+ if (!changes.isEmpty()) {
+ builtInQualityProfilesNotification.send();
+ }
}
profiler.stopDebug();
}
builtInQProfileInsert.create(dbSession, batchDbSession, builtIn);
}
- private void update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto ruleProfile) {
+ private List<ActiveRuleChange> update(DbSession dbSession, BuiltInQProfile builtIn, RulesProfileDto ruleProfile) {
LOGGER.info("Update profile {}", builtIn.getQProfileName());
- List<ActiveRuleChange> activeRuleChanges = builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile);
-
- if (!activeRuleChanges.isEmpty()) {
- builtInQualityProfilesNotification.send();
- }
+ return builtInQProfileUpdate.update(dbSession, builtIn, ruleProfile);
}
/**
verify(builtInQualityProfilesNotification).send();
}
+ @Test
+ public void only_send_one_notification_when_several_built_in_profiles_contain_new_rules() {
+ String language = newLanguageKey();
+ RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(language));
+ RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(language));
+
+ RulesProfileDto dbProfile1 = insertBuiltInProfile(language);
+ activateRuleInDb(dbProfile1, existingRule, MAJOR);
+ addPluginProfile(dbProfile1, existingRule, newRule);
+
+ RulesProfileDto dbProfile2 = insertBuiltInProfile(language);
+ activateRuleInDb(dbProfile2, existingRule, MAJOR);
+ addPluginProfile(dbProfile2, existingRule, newRule);
+ builtInQProfileRepositoryRule.initialize();
+
+ underTest.start();
+
+ verify(builtInQualityProfilesNotification).send();
+ }
+
private void addPluginProfile(RulesProfileDto dbProfile, RuleDefinitionDto... dbRules) {
RulesProfile pluginProfile = RulesProfile.create(dbProfile.getName(), dbProfile.getLanguage());
Arrays.stream(dbRules).forEach(dbRule -> pluginProfile.activateRule(create(dbRule.getRepositoryKey(), dbRule.getRuleKey()), MAJOR));