Search in sources :

Example 56 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class ChangeParentActionMediumTest method replace_existing_parent.

@Test
public void replace_existing_parent() throws Exception {
    QualityProfileDto parent1 = createProfile("xoo", "Parent 1");
    QualityProfileDto parent2 = createProfile("xoo", "Parent 2");
    QualityProfileDto child = createProfile("xoo", "Child");
    RuleDto rule1 = createRule("xoo", "rule1");
    RuleDto rule2 = createRule("xoo", "rule2");
    createActiveRule(rule1, parent1);
    createActiveRule(rule2, parent2);
    session.commit();
    ruleIndexer.index();
    activeRuleIndexer.index();
    // Set parent 1
    tester.get(RuleActivator.class).setParent(session, child.getKey(), parent1.getKey());
    session.clearCache();
    // Set parent 2 through WS
    wsTester.newPostRequest(QProfilesWs.API_ENDPOINT, "change_parent").setParam(QProfileRef.PARAM_PROFILE_KEY, child.getKey()).setParam("parentKey", parent2.getKey()).execute();
    session.clearCache();
    // Check rule 2 enabled
    List<ActiveRuleDto> activeRules2 = db.activeRuleDao().selectByProfileKey(session, child.getKey());
    assertThat(activeRules2).hasSize(1);
    assertThat(activeRules2.get(0).getKey().ruleKey().rule()).isEqualTo("rule2");
    assertThat(ruleIndex.search(new RuleQuery().setActivation(true).setQProfileKey(child.getKey()), new SearchOptions()).getIds()).hasSize(1);
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleActivator(org.sonar.server.qualityprofile.RuleActivator) RuleQuery(org.sonar.server.rule.index.RuleQuery) SearchOptions(org.sonar.server.es.SearchOptions) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 57 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class RegisterQualityProfilesMediumTest method register_existing_profile_definitions.

@Test
public void register_existing_profile_definitions() {
    tester = new ServerTester().withEsIndexes().withStartupTasks().addXoo().addComponents(XooRulesDefinition.class, XooProfileDefinition.class);
    tester.start();
    dbSession = dbClient().openSession(false);
    // Check Profile in DB
    QualityProfileDao qualityProfileDao = dbClient().qualityProfileDao();
    assertThat(qualityProfileDao.selectAll(dbSession)).hasSize(1);
    QualityProfileDto profile = qualityProfileDao.selectByNameAndLanguage("Basic", "xoo", dbSession);
    assertThat(profile).isNotNull();
    // Check ActiveRules in DB
    ActiveRuleDao activeRuleDao = dbClient().activeRuleDao();
    assertThat(activeRuleDao.selectByProfileKey(dbSession, profile.getKey())).hasSize(2);
    RuleKey ruleKey = RuleKey.of("xoo", "x1");
    ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), ruleKey);
    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
    // Check in ES
    assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
    tester.get(Platform.class).restart();
    assertThat(activeRuleDao.selectByKey(dbSession, activeRuleKey)).isPresent();
    // Check ActiveRules
    ActiveRuleDto activeRule = activeRuleDao.selectByKey(dbSession, activeRuleKey).get();
    assertThat(activeRule.getKey().qProfile()).isEqualTo(profile.getKee());
    assertThat(activeRule.getKey().ruleKey()).isEqualTo(ruleKey);
    assertThat(activeRule.getSeverityString()).isEqualTo(Severity.CRITICAL);
    // Check in ES
    assertThat(tester.get(RuleIndex.class).search(new RuleQuery().setActivation(true), new SearchOptions()).getIds()).containsOnly(ruleKey, RuleKey.of("xoo", "x2"));
    // TODO
    // Check ActiveRuleParameters in DB
    Map<String, ActiveRuleParamDto> params = ActiveRuleParamDto.groupByKey(activeRuleDao.selectParamsByActiveRuleId(dbSession, activeRule.getId()));
    assertThat(params).hasSize(2);
    // set by profile
    assertThat(params.get("acceptWhitespace").getValue()).isEqualTo("true");
    // default value
    assertThat(params.get("max").getValue()).isEqualTo("10");
}
Also used : QualityProfileDao(org.sonar.db.qualityprofile.QualityProfileDao) Platform(org.sonar.server.platform.Platform) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) RuleKey(org.sonar.api.rule.RuleKey) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) ServerTester(org.sonar.server.tester.ServerTester) ActiveRuleDao(org.sonar.db.qualityprofile.ActiveRuleDao) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleKey(org.sonar.db.qualityprofile.ActiveRuleKey) ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) QualityProfileDto(org.sonar.db.qualityprofile.QualityProfileDto) Test(org.junit.Test)

Example 58 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class RuleIndexTest method search_by_is_template.

@Test
public void search_by_is_template() {
    indexRules(newDoc(RuleKey.of("java", "S001")).setIsTemplate(false), newDoc(RuleKey.of("java", "S002")).setIsTemplate(true));
    // find all
    RuleQuery query = new RuleQuery();
    SearchIdResult results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).hasSize(2);
    // Only template
    query = new RuleQuery().setIsTemplate(true);
    results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).containsOnly(RuleKey.of("java", "S002"));
    // Only not template
    query = new RuleQuery().setIsTemplate(false);
    results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).containsOnly(RuleKey.of("java", "S001"));
    // null => no filter
    query = new RuleQuery().setIsTemplate(null);
    assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Example 59 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class RuleIndexTest method search_by_any_of_repositories.

@Test
public void search_by_any_of_repositories() {
    indexRules(newDoc(RuleKey.of("findbugs", "S001")), newDoc(RuleKey.of("pmd", "S002")));
    List<SearchHit> docs = tester.getDocuments(INDEX_TYPE_RULE);
    for (SearchHit doc : docs) {
        System.out.println(doc.getSourceAsString());
    }
    RuleQuery query = new RuleQuery().setRepositories(asList("checkstyle", "pmd"));
    SearchIdResult results = index.search(query, new SearchOptions());
    assertThat(results.getIds()).containsOnly(RuleKey.of("pmd", "S002"));
    // no results
    query = new RuleQuery().setRepositories(singletonList("checkstyle"));
    assertThat(index.search(query, new SearchOptions()).getIds()).isEmpty();
    // empty list => no filter
    query = new RuleQuery().setRepositories(Collections.emptyList());
    assertThat(index.search(query, new SearchOptions()).getIds()).hasSize(2);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Example 60 with SearchOptions

use of org.sonar.server.es.SearchOptions in project sonarqube by SonarSource.

the class RuleIndexTest method paging.

@Test
public void paging() {
    indexRules(newDoc(RuleKey.of("java", "S001")), newDoc(RuleKey.of("java", "S002")), newDoc(RuleKey.of("java", "S003")));
    // from 0 to 1 included
    SearchOptions options = new SearchOptions();
    options.setOffset(0).setLimit(2);
    SearchIdResult results = index.search(new RuleQuery(), options);
    assertThat(results.getTotal()).isEqualTo(3);
    assertThat(results.getIds()).hasSize(2);
    // from 0 to 9 included
    options.setOffset(0).setLimit(10);
    results = index.search(new RuleQuery(), options);
    assertThat(results.getTotal()).isEqualTo(3);
    assertThat(results.getIds()).hasSize(3);
    // from 2 to 11 included
    options.setOffset(2).setLimit(10);
    results = index.search(new RuleQuery(), options);
    assertThat(results.getTotal()).isEqualTo(3);
    assertThat(results.getIds()).hasSize(1);
    // from 2 to 11 included
    options.setOffset(2).setLimit(0);
    results = index.search(new RuleQuery(), options);
    assertThat(results.getTotal()).isEqualTo(3);
    assertThat(results.getIds()).hasSize(1);
}
Also used : SearchOptions(org.sonar.server.es.SearchOptions) SearchIdResult(org.sonar.server.es.SearchIdResult) Test(org.junit.Test)

Aggregations

SearchOptions (org.sonar.server.es.SearchOptions)143 Test (org.junit.Test)127 ComponentDto (org.sonar.db.component.ComponentDto)62 Facets (org.sonar.server.es.Facets)22 RuleQuery (org.sonar.server.rule.index.RuleQuery)22 RuleDto (org.sonar.db.rule.RuleDto)16 RuleKey (org.sonar.api.rule.RuleKey)14 OrganizationDto (org.sonar.db.organization.OrganizationDto)11 IssueQuery (org.sonar.server.issue.IssueQuery)11 ActiveRuleKey (org.sonar.db.qualityprofile.ActiveRuleKey)10 ActiveRuleDto (org.sonar.db.qualityprofile.ActiveRuleDto)9 QualityProfileDto (org.sonar.db.qualityprofile.QualityProfileDto)9 SearchIdResult (org.sonar.server.es.SearchIdResult)9 DbSession (org.sonar.db.DbSession)8 OrganizationTesting.newOrganizationDto (org.sonar.db.organization.OrganizationTesting.newOrganizationDto)7 JsonWriter (org.sonar.api.utils.text.JsonWriter)5 MetricCriterion (org.sonar.server.measure.index.ProjectMeasuresQuery.MetricCriterion)5 RulesDefinition (org.sonar.api.server.rule.RulesDefinition)4 RuleParamDto (org.sonar.db.rule.RuleParamDto)4 UserDto (org.sonar.db.user.UserDto)3