Search in sources :

Example 1 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RuleQueryFactory method createRuleQuery.

/**
   * Create a {@link RuleQuery} from a {@link Request}.
   * When a profile key is set, the language of the profile is automatically set in the query
   */
public RuleQuery createRuleQuery(Request request) {
    RuleQuery ruleQuery = createRuleQuery(new RuleQuery(), request);
    String qProfileKey = ruleQuery.getQProfileKey();
    if (qProfileKey != null) {
        QualityProfileDto qProfile = getProfileByKey(qProfileKey);
        if (qProfile != null) {
            ruleQuery.setLanguages(ImmutableList.of(qProfile.getLanguage()));
        }
    }
    return ruleQuery;
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto)

Example 2 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    try (DbSession dbSession = dbClient.openSession(false)) {
        SearchWsRequest searchWsRequest = toSearchWsRequest(request);
        SearchOptions context = buildSearchOptions(searchWsRequest);
        RuleQuery query = ruleQueryFactory.createRuleQuery(request);
        SearchResult searchResult = doSearch(dbSession, query, context);
        SearchResponse responseBuilder = buildResponse(dbSession, searchWsRequest, context, searchResult, query);
        writeProtobuf(responseBuilder, request, response);
    }
}
Also used : DbSession(org.sonar.db.DbSession) SearchWsRequest(org.sonarqube.ws.client.rule.SearchWsRequest) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) SearchResponse(org.sonarqube.ws.Rules.SearchResponse)

Example 3 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method bulk_activation.

@Test
public void bulk_activation() {
    // Generate more rules than the search's max limit
    int bulkSize = SearchOptions.MAX_LIMIT + 10;
    for (int i = 0; i < bulkSize; i++) {
        db.ruleDao().insert(dbSession, newDto(RuleKey.of("bulk", "r_" + i)).setLanguage("xoo"));
    }
    dbSession.commit();
    ruleIndexer.index();
    // 0. No active rules so far (base case) and plenty rules available
    verifyZeroActiveRules(XOO_P1_KEY);
    assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setRepositories(Arrays.asList("bulk")), new SearchOptions()).getTotal()).isEqualTo(bulkSize);
    // 1. bulk activate all the rules
    BulkChangeResult result = ruleActivator.bulkActivate(new RuleQuery().setRepositories(Arrays.asList("bulk")), XOO_P1_KEY, "MINOR");
    // 2. assert that all activation has been commit to DB and ES
    dbSession.clearCache();
    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
    assertThat(db.activeRuleDao().selectByProfileKey(dbSession, XOO_P1_KEY)).hasSize(bulkSize);
    assertThat(result.countSucceeded()).isEqualTo(bulkSize);
    assertThat(result.countFailed()).isEqualTo(0);
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 4 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method verifyZeroActiveRules.

private void verifyZeroActiveRules(String key) {
    // verify db
    dbSession.clearCache();
    List<ActiveRuleDto> activeRuleDtos = db.activeRuleDao().selectByProfileKey(dbSession, key);
    assertThat(activeRuleDtos).isEmpty();
    // verify es
    assertThat(tester.get(RuleIndex.class).searchAll(new RuleQuery().setQProfileKey(key).setActivation(true))).isEmpty();
}
Also used : ActiveRuleIndex(org.sonar.server.qualityprofile.index.ActiveRuleIndex) RuleIndex(org.sonar.server.rule.index.RuleIndex) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto)

Example 5 with RuleQuery

use of org.sonar.server.rule.index.RuleQuery in project sonarqube by SonarSource.

the class RuleActivatorMediumTest method bulk_deactivation_ignores_errors.

@Test
public void bulk_deactivation_ignores_errors() {
    // activate on parent profile P1
    createChildProfiles();
    activate(new RuleActivation(XOO_X1), XOO_P1_KEY);
    assertThat(countActiveRules(XOO_P2_KEY)).isEqualTo(1);
    // bulk deactivate on child profile P2 -> not possible
    BulkChangeResult result = ruleActivator.bulkDeactivate(new RuleQuery().setActivation(true).setQProfileKey(XOO_P2_KEY), XOO_P2_KEY);
    dbSession.clearCache();
    assertThat(countActiveRules(XOO_P2_KEY)).isEqualTo(1);
    assertThat(result.countFailed()).isEqualTo(1);
    assertThat(result.countSucceeded()).isEqualTo(0);
    assertThat(result.getChanges()).hasSize(0);
}
Also used : RuleQuery(org.sonar.server.rule.index.RuleQuery) Test(org.junit.Test)

Aggregations

RuleQuery (org.sonar.server.rule.index.RuleQuery)39 Test (org.junit.Test)34 SearchOptions (org.sonar.server.es.SearchOptions)22 RuleDto (org.sonar.db.rule.RuleDto)16 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)14 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)14 RuleIndex (org.sonar.server.rule.index.RuleIndex)7 ActiveRuleParamDto (org.sonar.db.qualityprofile.ActiveRuleParamDto)5 RuleKey (org.sonar.api.rule.RuleKey)4 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 ActiveRuleDao (org.sonar.db.qualityprofile.ActiveRuleDao)4 RuleParamDto (org.sonar.db.rule.RuleParamDto)4 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)3 RuleActivation (org.sonar.server.qualityprofile.RuleActivation)3 RuleActivator (org.sonar.server.qualityprofile.RuleActivator)3 ActiveRuleIndex (org.sonar.server.qualityprofile.index.ActiveRuleIndex)3 StringReader (java.io.StringReader)2 QProfileService (org.sonar.server.qualityprofile.QProfileService)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Map (java.util.Map)1