Search in sources :

Example 51 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RuleIndexTest method sort_by_name.

@Test
public void sort_by_name() {
    indexRules(newDoc(RuleKey.of("java", "S001")).setName("abcd"), newDoc(RuleKey.of("java", "S002")).setName("ABC"), newDoc(RuleKey.of("java", "S003")).setName("FGH"));
    // ascending
    RuleQuery query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME);
    SearchIdResult<RuleKey> results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).containsExactly(RuleKey.of("java", "S002"), RuleKey.of("java", "S001"), RuleKey.of("java", "S003"));
    // descending
    query = new RuleQuery().setSortField(RuleIndexDefinition.FIELD_RULE_NAME).setAscendingSort(false);
    results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).containsExactly(RuleKey.of("java", "S003"), RuleKey.of("java", "S001"), RuleKey.of("java", "S002"));
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 52 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RuleIndexTest method default_sort_is_by_updated_at_desc.

@Test
public void default_sort_is_by_updated_at_desc() {
    indexRules(newDoc(RuleKey.of("java", "S001")).setCreatedAt(1000L).setUpdatedAt(1000L), newDoc(RuleKey.of("java", "S002")).setCreatedAt(1000L).setUpdatedAt(3000L), newDoc(RuleKey.of("java", "S003")).setCreatedAt(1000L).setUpdatedAt(2000L));
    SearchIdResult<RuleKey> results = index.search(new RuleQuery(), new SearchOptions());
    assertThat(results.getIds()).containsExactly(RuleKey.of("java", "S002"), RuleKey.of("java", "S003"), RuleKey.of("java", "S001"));
}
Also used : ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 53 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class RuleCreatorMediumTest method create_custom_rule_with_no_parameter_value.

@Test
public void create_custom_rule_with_no_parameter_value() {
    // insert template rule
    RuleDto templateRule = createTemplateRuleWithIntArrayParam();
    NewCustomRule newRule = NewCustomRule.createForCustomRule("CUSTOM_RULE", templateRule.getKey()).setName("My custom").setHtmlDescription("Some description").setSeverity(Severity.MAJOR).setStatus(RuleStatus.READY);
    RuleKey customRuleKey = creator.create(newRule);
    dbSession.clearCache();
    List<RuleParamDto> params = db.ruleDao().selectRuleParamsByRuleKey(dbSession, customRuleKey);
    assertThat(params).hasSize(1);
    RuleParamDto param = params.get(0);
    assertThat(param.getName()).isEqualTo("myIntegers");
    assertThat(param.getDescription()).isEqualTo("My Integers");
    assertThat(param.getType()).isEqualTo("INTEGER,multiple=true,values=1;2;3");
    assertThat(param.getDefaultValue()).isNull();
}
Also used : RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) RuleParamDto(org.sonar.db.rule.RuleParamDto) Test(org.junit.Test)

Example 54 with RuleKey

use of org.sonar.api.rule.RuleKey in project sonarqube by SonarSource.

the class ShowActionMediumTest method encode_html_description_of_custom_rule.

@Test
public void encode_html_description_of_custom_rule() throws Exception {
    // Template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(session, templateRule);
    session.commit();
    // Custom rule
    NewCustomRule customRule = NewCustomRule.createForCustomRule("MY_CUSTOM", templateRule.getKey()).setName("My custom").setSeverity(MINOR).setStatus(RuleStatus.READY).setMarkdownDescription("<div>line1\nline2</div>");
    RuleKey customRuleKey = tester.get(RuleCreator.class).create(customRule);
    session.clearCache();
    WsTester.TestRequest request = wsTester.newGetRequest("api/rules", "show").setParam("key", customRuleKey.toString());
    request.execute().assertJson(getClass(), "encode_html_description_of_custom_rule.json");
}
Also used : WsTester(org.sonar.server.ws.WsTester) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleKey(org.sonar.api.rule.RuleKey) NewCustomRule(org.sonar.server.rule.NewCustomRule) RuleCreator(org.sonar.server.rule.RuleCreator) Test(org.junit.Test)

Example 55 with RuleKey

use of org.sonar.api.rule.RuleKey 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;
}
Also used : RulesProfile(org.sonar.api.profiles.RulesProfile) ActiveRuleParam(org.sonar.api.rules.ActiveRuleParam) RuleKey(org.sonar.api.rule.RuleKey) ArrayList(java.util.ArrayList) LoadedTemplateDto(org.sonar.db.loadedtemplate.LoadedTemplateDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Aggregations

RuleKey (org.sonar.api.rule.RuleKey)95 Test (org.junit.Test)48 RuleDto (org.sonar.db.rule.RuleDto)24 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)22 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)17 SearchOptions (org.sonar.server.es.SearchOptions)14 RuleParamDto (org.sonar.db.rule.RuleParamDto)10 NewIssue (org.sonar.api.batch.sensor.issue.NewIssue)9 ArrayList (java.util.ArrayList)5 DbSession (org.sonar.db.DbSession)5 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ComponentDto (org.sonar.db.component.ComponentDto)4 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)4 Rule (org.sonar.api.batch.rule.Rule)3 WildcardPattern (org.sonar.api.utils.WildcardPattern)3 DefaultIssue (org.sonar.core.issue.DefaultIssue)3 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)3 QualityProfileDao (org.sonar.db.qualityprofile.QualityProfileDao)3 IssuePattern (org.sonar.scanner.issue.ignore.pattern.IssuePattern)3