Search in sources :

Example 66 with SearchOptions

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

the class RuleUpdaterMediumTest method update_custom_rule.

@Test
public void update_custom_rule() {
    // Create template rule
    RuleDto templateRule = RuleTesting.newTemplateRule(RuleKey.of("java", "S001"));
    ruleDao.insert(dbSession, templateRule);
    RuleParamDto templateRuleParam1 = RuleParamDto.createFor(templateRule).setName("regex").setType("STRING").setDescription("Reg ex").setDefaultValue(".*");
    RuleParamDto templateRuleParam2 = RuleParamDto.createFor(templateRule).setName("format").setType("STRING").setDescription("Format");
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam1);
    ruleDao.insertRuleParam(dbSession, templateRule, templateRuleParam2);
    // Create custom rule
    RuleDto customRule = RuleTesting.newCustomRule(templateRule).setName("Old name").setDescription("Old description").setSeverity(Severity.MINOR).setStatus(RuleStatus.BETA);
    ruleDao.insert(dbSession, customRule);
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam1.setDefaultValue("a.*"));
    ruleDao.insertRuleParam(dbSession, customRule, templateRuleParam2.setDefaultValue(null));
    dbSession.commit();
    // Update custom rule
    RuleUpdate update = RuleUpdate.createForCustomRule(customRule.getKey()).setName("New name").setMarkdownDescription("New description").setSeverity("MAJOR").setStatus(RuleStatus.READY).setParameters(ImmutableMap.of("regex", "b.*"));
    underTest.update(update, userSessionRule);
    dbSession.clearCache();
    // Verify custom rule is updated
    RuleDto customRuleReloaded = ruleDao.selectOrFailByKey(dbSession, customRule.getKey());
    assertThat(customRuleReloaded).isNotNull();
    assertThat(customRuleReloaded.getName()).isEqualTo("New name");
    assertThat(customRuleReloaded.getDescription()).isEqualTo("New description");
    assertThat(customRuleReloaded.getSeverityString()).isEqualTo("MAJOR");
    assertThat(customRuleReloaded.getStatus()).isEqualTo(RuleStatus.READY);
    List<RuleParamDto> params = ruleDao.selectRuleParamsByRuleKey(dbSession, customRuleReloaded.getKey());
    assertThat(params).hasSize(2);
    assertThat(params.get(0).getDefaultValue()).isEqualTo("b.*");
    assertThat(params.get(1).getDefaultValue()).isNull();
    // Verify in index
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("New name"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("New description"), new SearchOptions()).getIds()).containsOnly(customRule.getKey());
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old name"), new SearchOptions()).getTotal()).isZero();
    assertThat(ruleIndex.search(new RuleQuery().setQueryText("Old description"), new SearchOptions()).getTotal()).isZero();
}
Also used : ActiveRuleDto(org.sonar.db.qualityprofile.ActiveRuleDto) RuleDto(org.sonar.db.rule.RuleDto) RuleQuery(org.sonar.server.rule.index.RuleQuery) ActiveRuleParamDto(org.sonar.db.qualityprofile.ActiveRuleParamDto) RuleParamDto(org.sonar.db.rule.RuleParamDto) SearchOptions(org.sonar.server.es.SearchOptions) Test(org.junit.Test)

Example 67 with SearchOptions

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

the class RuleIndexTest method search_by_key.

@Test
public void search_by_key() {
    RuleKey js1 = RuleKey.of("javascript", "X001");
    RuleKey cobol1 = RuleKey.of("cobol", "X001");
    RuleKey php2 = RuleKey.of("php", "S002");
    indexRules(newDoc(js1).setName("First rule").setHtmlDescription("The first rule"), newDoc(cobol1).setName("Second rule").setHtmlDescription("The second rule"), newDoc(php2).setName("Third rule").setHtmlDescription("The third rule"));
    // key
    RuleQuery query = new RuleQuery().setQueryText("X001");
    assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(js1, cobol1);
    // partial key does not match
    query = new RuleQuery().setQueryText("X00");
    assertThat(index.search(query, new SearchOptions()).getIds()).isEmpty();
    // repo:key -> nice-to-have !
    query = new RuleQuery().setQueryText("javascript:X001");
    assertThat(index.search(query, new SearchOptions()).getIds()).containsOnly(js1);
}
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 68 with SearchOptions

use of org.sonar.server.es.SearchOptions 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 69 with SearchOptions

use of org.sonar.server.es.SearchOptions 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 70 with SearchOptions

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

the class SearchAction method handle.

@Override
public void handle(Request request, Response response) throws Exception {
    SearchOptions searchOptions = new SearchOptions().setPage(request.mandatoryParamAsInt(Param.PAGE), request.mandatoryParamAsInt(Param.PAGE_SIZE));
    Boolean isCustom = request.paramAsBoolean(PARAM_IS_CUSTOM);
    try (DbSession dbSession = dbClient.openSession(false)) {
        List<MetricDto> metrics = dbClient.metricDao().selectEnabled(dbSession, isCustom, searchOptions.getOffset(), searchOptions.getLimit());
        int nbMetrics = dbClient.metricDao().countEnabled(dbSession, isCustom);
        JsonWriter json = response.newJsonWriter();
        json.beginObject();
        Set<String> desiredFields = desiredFields(request.paramAsStrings(Param.FIELDS));
        writeMetrics(json, metrics, desiredFields);
        searchOptions.writeJson(json, nbMetrics);
        json.endObject();
        json.close();
    }
}
Also used : DbSession(org.sonar.db.DbSession) MetricDto(org.sonar.db.metric.MetricDto) SearchOptions(org.sonar.server.es.SearchOptions) JsonWriter(org.sonar.api.utils.text.JsonWriter)

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