use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class QProfileResetMediumTest method reset_language_profile_param_when_rule_definition_has_changed.
@Test
public void reset_language_profile_param_when_rule_definition_has_changed() {
RulesProfile defProfile = RulesProfile.create("Basic", ServerTester.Xoo.KEY);
defProfile.activateRule(org.sonar.api.rules.Rule.create("xoo", "x1"), null);
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
x1.createParam("acceptWhitespace").setDefaultValue("false").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
QualityProfileDto profile = tester.get(QualityProfileDao.class).selectByNameAndLanguage("Basic", ServerTester.Xoo.KEY, dbSession);
ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), RuleKey.of("xoo", "x1"));
// Change param in the rule def
register(new Rules() {
@Override
public void init(RulesDefinition.NewRepository repository) {
RulesDefinition.NewRule x1 = repository.createRule("x1").setName("x1 name").setHtmlDescription("x1 desc").setSeverity(MAJOR);
x1.createParam("acceptWhitespace").setDefaultValue("true").setType(RuleParamType.BOOLEAN).setDescription("Accept whitespaces on the line");
}
}, defProfile);
reset.resetLanguage(ServerTester.Xoo.KEY);
// Parameter value come back to origin after reset
ActiveRuleDto activeRuleDto = tester.get(ActiveRuleDao.class).selectOrFailByKey(dbSession, activeRuleKey);
List<ActiveRuleParamDto> params = tester.get(ActiveRuleDao.class).selectParamsByActiveRuleId(dbSession, activeRuleDto.getId());
assertThat(params).hasSize(1);
assertThat(params.get(0).getKey()).isEqualTo("acceptWhitespace");
assertThat(params.get(0).getValue()).isEqualTo("true");
}
use of org.sonar.api.profiles.RulesProfile in project sonar-web by SonarSource.
the class SonarWayProfileTest method test.
@Test
public void test() {
ValidationMessages validationMessages = ValidationMessages.create();
RulesProfile profile = new SonarWayProfile(ruleFinder()).createProfile(validationMessages);
Assertions.assertThat(profile.getName()).isEqualTo("Sonar way");
Assertions.assertThat(profile.getLanguage()).isEqualTo(WebConstants.LANGUAGE_KEY);
Assertions.assertThat(profile.getActiveRules()).onProperty("repositoryKey").containsOnly(WebRulesDefinition.REPOSITORY_KEY);
Assertions.assertThat(profile.getActiveRules().size()).isGreaterThan(10);
assertThat(validationMessages.hasErrors(), is(false));
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class QProfileReset method loadDefinitionsGroupedByName.
private ListMultimap<QProfileName, RulesProfile> loadDefinitionsGroupedByName(String language) {
ListMultimap<QProfileName, RulesProfile> profilesByName = ArrayListMultimap.create();
for (ProfileDefinition definition : definitions) {
ValidationMessages validation = ValidationMessages.create();
RulesProfile profile = definition.createProfile(validation);
if (language.equals(profile.getLanguage())) {
processValidationMessages(validation);
profilesByName.put(new QProfileName(profile.getLanguage(), profile.getName()), profile);
}
}
return profilesByName;
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class QProfileReset method resetLanguage.
/**
* Reset built-in profiles for the given language. Missing profiles are created and
* existing ones are updated.
*/
public void resetLanguage(String language) {
DbSession dbSession = db.openSession(false);
try {
ListMultimap<QProfileName, RulesProfile> profilesByName = loadDefinitionsGroupedByName(language);
for (Map.Entry<QProfileName, Collection<RulesProfile>> entry : profilesByName.asMap().entrySet()) {
QProfileName profileName = entry.getKey();
QualityProfileDto profile = factory.getOrCreate(dbSession, profileName);
List<RuleActivation> activations = Lists.newArrayList();
for (RulesProfile def : entry.getValue()) {
for (ActiveRule activeRule : def.getActiveRules()) {
RuleActivation activation = new RuleActivation(RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey()));
activation.setSeverity(activeRule.getSeverity().name());
if (!activeRule.getActiveRuleParams().isEmpty()) {
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
activation.setParameter(param.getParamKey(), param.getValue());
}
} else {
for (RuleParamDto param : db.ruleDao().selectRuleParamsByRuleKey(dbSession, activeRule.getRule().ruleKey())) {
activation.setParameter(param.getName(), param.getDefaultValue());
}
}
activations.add(activation);
}
}
doReset(dbSession, profile, activations);
}
} finally {
dbSession.close();
}
}
use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.
the class RegisterQualityProfiles method register.
private List<ActiveRuleChange> register(QProfileName name, Collection<RulesProfile> profiles, DbSession session) {
LOGGER.info("Register profile " + name);
List<ActiveRuleChange> changes = new ArrayList<>();
QualityProfileDto profileDto = dbClient.qualityProfileDao().selectByNameAndLanguage(name.getName(), name.getLanguage(), session);
if (profileDto != null) {
changes.addAll(profileFactory.delete(session, profileDto.getKey(), true));
}
profileFactory.create(session, name);
for (RulesProfile profile : profiles) {
for (org.sonar.api.rules.ActiveRule activeRule : profile.getActiveRules()) {
RuleKey ruleKey = RuleKey.of(activeRule.getRepositoryKey(), activeRule.getRuleKey());
RuleActivation activation = new RuleActivation(ruleKey);
activation.setSeverity(activeRule.getSeverity() != null ? activeRule.getSeverity().name() : null);
for (ActiveRuleParam param : activeRule.getActiveRuleParams()) {
activation.setParameter(param.getKey(), param.getValue());
}
changes.addAll(ruleActivator.activate(session, activation, name));
}
}
LoadedTemplateDto template = new LoadedTemplateDto(templateKey(name), LoadedTemplateDto.QUALITY_PROFILE_TYPE);
dbClient.loadedTemplateDao().insert(template, session);
session.commit();
return changes;
}
Aggregations