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();
}
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);
}
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"));
}
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"));
}
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();
}
}
Aggregations