Skip to content

Commit

Permalink
SONAR-10575 fix flag to include external rules in rules search
Browse files Browse the repository at this point in the history
  • Loading branch information
dbmeneses authored and SonarTech committed Apr 26, 2018
1 parent 4373ca6 commit 126f70e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 50 deletions.
Expand Up @@ -302,10 +302,10 @@ private static Map<String, QueryBuilder> buildFilters(RuleQuery query) {
QueryBuilders.termQuery(FIELD_RULE_IS_TEMPLATE, Boolean.toString(isTemplate)));
}

Boolean isExternal = query.includeExternal();
if (isExternal != null) {
boolean includeExternal = query.includeExternal();
if (!includeExternal) {
filters.put(FIELD_RULE_IS_EXTERNAL,
QueryBuilders.termQuery(FIELD_RULE_IS_EXTERNAL, Boolean.toString(isExternal)));
QueryBuilders.termQuery(FIELD_RULE_IS_EXTERNAL, false));
}

String template = query.templateKey();
Expand Down
Expand Up @@ -54,7 +54,7 @@ public class RuleQuery {
private String internalKey;
private String ruleKey;
private OrganizationDto organization;
private Boolean includeExternal;
private boolean includeExternal;

@CheckForNull
public QProfileDto getQProfile() {
Expand Down Expand Up @@ -206,11 +206,11 @@ public RuleQuery setIsTemplate(@Nullable Boolean b) {
return this;
}

public Boolean includeExternal() {
public boolean includeExternal() {
return includeExternal;
}

public RuleQuery setIncludeExternal(@Nullable Boolean b) {
public RuleQuery setIncludeExternal(boolean b) {
this.includeExternal = b;
return this;
}
Expand Down
Expand Up @@ -135,7 +135,7 @@ public void define(WebService.NewController controller) {
.setChangelog(new Change("7.1", "The field 'scope' has been added to the response"))
.setChangelog(new Change("7.1", "The field 'scope' has been added to the 'f' parameter"))
.setChangelog(new Change("7.2", "The field 'isExternal' has been added to the response"))
.setChangelog(new Change("7.2", "The field 'isExternal' has been added to the 'f' parameter"));
.setChangelog(new Change("7.2", "The field 'includeExternal' has been added to the 'f' parameter"));

action.createParam(FACETS)
.setDescription("Comma-separated list of the facets to be computed. No facet is computed by default.")
Expand Down
Expand Up @@ -40,17 +40,15 @@
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.QProfileDto;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.db.rule.RuleDto.Scope;
import org.sonar.db.rule.RuleMetadataDto;
import org.sonar.server.es.EsTester;
import org.sonar.server.es.Facets;
import org.sonar.server.es.SearchIdResult;
import org.sonar.server.es.SearchOptions;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;

import static com.google.common.collect.ImmutableSet.of;
import static com.google.common.collect.Sets.newHashSet;
import static java.util.Arrays.asList;
import static java.util.Collections.emptyList;
import static java.util.Collections.emptySet;
Expand All @@ -67,8 +65,8 @@
import static org.sonar.api.rules.RuleType.CODE_SMELL;
import static org.sonar.api.rules.RuleType.VULNERABILITY;
import static org.sonar.db.rule.RuleTesting.setCreatedAt;
import static org.sonar.db.rule.RuleTesting.setIsTemplate;
import static org.sonar.db.rule.RuleTesting.setIsExternal;
import static org.sonar.db.rule.RuleTesting.setIsTemplate;
import static org.sonar.db.rule.RuleTesting.setLanguage;
import static org.sonar.db.rule.RuleTesting.setName;
import static org.sonar.db.rule.RuleTesting.setOrganization;
Expand Down Expand Up @@ -217,30 +215,6 @@ public void search_name_with_protected_chars() {
assertThat(results).containsOnly(rule.getId());
}

@Test
public void search_external_rule() {

RuleDefinitionDto rule = new RuleDefinitionDto()
.setId(123)
.setRuleKey("S001")
.setRepositoryKey("external_xoo")
.setName("xoo:S001")
.setSeverity(Severity.BLOCKER)
.setStatus(RuleStatus.READY)
.setIsTemplate(false)
.setIsExternal(true)
.setType(RuleType.BUG)
.setScope(Scope.ALL)
.setCreatedAt(1500000000000L)
.setUpdatedAt(1600000000000L);

db.rules().insert(rule);
index();

assertThat(underTest.search(new RuleQuery().setQueryText("external_xoo:S001"), new SearchOptions()).getIds())
.containsExactlyInAnyOrder(rule.getId());
}

@Test
public void search_content_by_query() {
RuleDefinitionDto rule1 = createJavaRule(rule -> rule.setRuleKey("123").setDescription("My great rule CWE-123 which makes your code 1000 times better!"));
Expand Down Expand Up @@ -421,25 +395,15 @@ public void search_by_is_external() {
RuleDefinitionDto ruleIsExternal = createRule(setIsExternal(true));
index();

// find all
RuleQuery query = new RuleQuery();
SearchIdResult<Integer> results = underTest.search(query, new SearchOptions());
assertThat(results.getIds()).hasSize(2);

// Only external
query = new RuleQuery().setIncludeExternal(true);
results = underTest.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(ruleIsExternal.getId());
RuleQuery query = new RuleQuery().setIncludeExternal(true);
SearchIdResult<Integer> results = underTest.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(ruleIsExternal.getId(), ruleIsNotExternal.getId());

// Only not external
query = new RuleQuery().setIncludeExternal(false);
results = underTest.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(ruleIsNotExternal.getId());

// null => no filter
query = new RuleQuery().setIncludeExternal(null);
results = underTest.search(query, new SearchOptions());
assertThat(results.getIds()).containsOnly(ruleIsExternal.getId(), ruleIsNotExternal.getId());
}

@Test
Expand Down
Expand Up @@ -101,7 +101,7 @@ public void create_empty_query() {
assertThat(result.isAscendingSort()).isTrue();
assertThat(result.getAvailableSinceLong()).isNull();
assertThat(result.getInheritance()).isNull();
assertThat(result.includeExternal()).isNull();
assertThat(result.includeExternal()).isFalse();
assertThat(result.isTemplate()).isNull();
assertThat(result.getLanguages()).isNull();
assertThat(result.getQueryText()).isNull();
Expand Down Expand Up @@ -191,7 +191,7 @@ public void create_query() {
ASCENDING, "false");

assertResult(result, qualityProfile, compareToQualityProfile);
assertThat(result.includeExternal()).isNull();
assertThat(result.includeExternal()).isFalse();
}

private void assertResult(RuleQuery result, QProfileDto qualityProfile, QProfileDto compareToQualityProfile) {
Expand Down

0 comments on commit 126f70e

Please sign in to comment.