Search in sources :

Example 16 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class QProfileExporters method importXml.

private QProfileResult importXml(QualityProfileDto profileDto, String importerKey, Reader xml, DbSession dbSession) {
    QProfileResult result = new QProfileResult();
    ValidationMessages messages = ValidationMessages.create();
    ProfileImporter importer = getProfileImporter(importerKey);
    RulesProfile rulesProfile = importer.importProfile(xml, messages);
    List<ActiveRuleChange> changes = importProfile(profileDto, rulesProfile, dbSession);
    result.addChanges(changes);
    processValidationMessages(messages, result);
    return result;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ProfileImporter(org.sonar.api.profiles.ProfileImporter) ValidationMessages(org.sonar.api.utils.ValidationMessages)

Example 17 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class RulesProfileProvider method select.

private static RulesProfile select(QProfile qProfile, ActiveRules activeRules) {
    RulesProfile deprecatedProfile = new RulesProfile();
    deprecatedProfile.setName(qProfile.getName());
    deprecatedProfile.setLanguage(qProfile.getLanguage());
    for (org.sonar.api.batch.rule.ActiveRule activeRule : activeRules.findByLanguage(qProfile.getLanguage())) {
        Rule rule = Rule.create(activeRule.ruleKey().repository(), activeRule.ruleKey().rule());
        rule.setConfigKey(activeRule.internalKey());
        // SONAR-6706
        if (activeRule.templateRuleKey() != null) {
            rule.setTemplate(Rule.create(activeRule.ruleKey().repository(), activeRule.templateRuleKey()));
        }
        ActiveRule deprecatedActiveRule = deprecatedProfile.activateRule(rule, RulePriority.valueOf(activeRule.severity()));
        for (Map.Entry<String, String> param : activeRule.params().entrySet()) {
            rule.createParameter(param.getKey());
            deprecatedActiveRule.setParameter(param.getKey(), param.getValue());
        }
    }
    return deprecatedProfile;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRule(org.sonar.api.rules.ActiveRule) Rule(org.sonar.api.rules.Rule) ActiveRule(org.sonar.api.rules.ActiveRule) Map(java.util.Map)

Example 18 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class RulesProfileProviderTest method keep_compatibility_with_single_language_projects.

@Test
public void keep_compatibility_with_single_language_projects() {
    settings.setProperty("sonar.language", "java");
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findByLanguage("java")).thenReturn(qProfile);
    RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings);
    // no merge, directly the old hibernate profile
    assertThat(profile).isNotNull();
    assertThat(profile.getLanguage()).isEqualTo("java");
    assertThat(profile.getName()).isEqualTo("Sonar way");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Example 19 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class RulesProfileProviderTest method merge_profiles.

@Test
public void merge_profiles() {
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile));
    RulesProfile profile = provider.provide(qProfiles, new ActiveRulesBuilder().build(), settings);
    // merge of all profiles
    assertThat(profile).isNotNull().isInstanceOf(RulesProfileWrapper.class);
    assertThat(profile.getLanguage()).isEqualTo("");
    assertThat(profile.getName()).isEqualTo("SonarQube");
    assertThat(profile.getActiveRules()).isEmpty();
    try {
        profile.getId();
        fail();
    } catch (IllegalStateException e) {
    // id must not be used at all
    }
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Example 20 with RulesProfile

use of org.sonar.api.profiles.RulesProfile in project sonarqube by SonarSource.

the class RulesProfileProviderTest method support_rule_templates.

@Test
public void support_rule_templates() {
    QProfile qProfile = new QProfile().setKey("java-sw").setName("Sonar way").setLanguage("java");
    when(qProfiles.findAll()).thenReturn(Arrays.asList(qProfile));
    ActiveRulesBuilder activeRulesBuilder = new ActiveRulesBuilder();
    activeRulesBuilder.create(RuleKey.of("java", "S001")).setTemplateRuleKey("T001").setLanguage("java").activate();
    RulesProfile profile = provider.provide(qProfiles, activeRulesBuilder.build(), settings);
    assertThat(profile.getActiveRule("java", "S001").getRule().getTemplate().getKey()).isEqualTo("T001");
}
Also used : ActiveRulesBuilder(org.sonar.api.batch.rule.internal.ActiveRulesBuilder) RulesProfile(org.sonar.api.profiles.RulesProfile) Test(org.junit.Test)

Aggregations

RulesProfile (org.sonar.api.profiles.RulesProfile)21 Test (org.junit.Test)6 ValidationMessages (org.sonar.api.utils.ValidationMessages)5 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 ActiveRulesBuilder (org.sonar.api.batch.rule.internal.ActiveRulesBuilder)3 DbSession (org.sonar.db.DbSession)3 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)3 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)3 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 ProfileDefinition (org.sonar.api.profiles.ProfileDefinition)2 ProfileImporter (org.sonar.api.profiles.ProfileImporter)2 RuleKey (org.sonar.api.rule.RuleKey)2 ActiveRule (org.sonar.api.rules.ActiveRule)2 ActiveRuleParam (org.sonar.api.rules.ActiveRuleParam)2 Rule (org.sonar.api.rules.Rule)2 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)2 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)2 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)2 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)2