diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-08 14:44:28 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2014-01-08 14:44:28 +0100 |
commit | 6126633392cec83b597fc89c7e907a084e032f60 (patch) | |
tree | 7e21804e5556a7589054d0d52b86d94f302fa68e /sonar-server/src | |
parent | 25964e74a25d85d0c1361510a1f40c0216646519 (diff) | |
download | sonarqube-6126633392cec83b597fc89c7e907a084e032f60.tar.gz sonarqube-6126633392cec83b597fc89c7e907a084e032f60.zip |
SONAR-4923 Try to get all ids from profile rule query (Not work for the moment)
Diffstat (limited to 'sonar-server/src')
4 files changed, 60 insertions, 12 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java index ede48fd0127..0402d82a117 100644 --- a/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java +++ b/sonar-server/src/main/java/org/sonar/server/rule/ProfileRules.java @@ -19,6 +19,8 @@ */ package org.sonar.server.rule; +import com.google.common.base.Function; +import com.google.common.collect.Iterables; import org.apache.commons.lang.StringUtils; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.get.MultiGetItemResponse; @@ -39,10 +41,13 @@ import org.sonar.server.qualityprofile.QProfileRule; import org.sonar.server.qualityprofile.QProfileRuleResult; import org.sonar.server.search.SearchIndex; +import javax.annotation.Nullable; + import java.util.Collection; import java.util.List; import java.util.Map; +import static com.google.common.collect.Lists.newArrayList; import static org.elasticsearch.index.query.FilterBuilders.*; import static org.elasticsearch.index.query.QueryBuilders.multiMatchQuery; import static org.sonar.server.rule.RuleRegistry.*; @@ -107,6 +112,23 @@ public class ProfileRules implements ServerExtension { return new QProfileRuleResult(result, PagingResult.create(paging.pageSize(), paging.pageIndex(), hits.getTotalHits())); } + // FIXME Due to a bug in E/S, As the query filter contain a filter with has_parent, nothing will be returned + public List<Integer> searchProfileRuleIds(ProfileRuleQuery query) { + BoolFilterBuilder filter = activeRuleFilter(query); + + SearchRequestBuilder builder = index.client() + .prepareSearch(INDEX_RULES) + .setTypes(TYPE_ACTIVE_RULE) + .setFilter(filter); + List<String> documentIds = index.findDocumentIds(builder, 2); + return newArrayList(Iterables.transform(documentIds, new Function<String, Integer>() { + @Override + public Integer apply(@Nullable String input) { + return Integer.valueOf(input); + } + })); + } + public QProfileRuleResult searchInactiveProfileRules(ProfileRuleQuery query, Paging paging) { BoolFilterBuilder filter = parentRuleFilter(query); addMustTermOrTerms(filter, RuleDocument.FIELD_SEVERITY, query.severities()); diff --git a/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java b/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java index a1b6af0f0f2..bff6f252c90 100644 --- a/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java +++ b/sonar-server/src/main/java/org/sonar/server/search/SearchIndex.java @@ -151,7 +151,6 @@ public class SearchIndex { } } - public void addMappingFromClasspath(String index, String type, String resourcePath) { try { URL resource = getClass().getResource(resourcePath); @@ -191,15 +190,19 @@ public class SearchIndex { } public List<String> findDocumentIds(SearchQuery searchQuery) { + SearchRequestBuilder builder = searchQuery.toBuilder(client); + return findDocumentIds(builder, searchQuery.scrollSize()); + } + + public List<String> findDocumentIds(SearchRequestBuilder builder, int scrollSize) { List<String> result = Lists.newArrayList(); final int scrollTime = 100; - SearchRequestBuilder builder = searchQuery.toBuilder(client); StopWatch watch = createWatch(); SearchResponse scrollResp = builder.addField("_id") - .setSearchType(SearchType.SCAN) - .setScroll(new TimeValue(scrollTime)) - .setSize(searchQuery.scrollSize()).execute().actionGet(); + .setSearchType(SearchType.SCAN) + .setScroll(new TimeValue(scrollTime)) + .setSize(scrollSize).execute().actionGet(); //Scroll until no hits are returned while (true) { scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(scrollTime)).execute().actionGet(); diff --git a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java index 6ed7a80107d..8f24536f6da 100644 --- a/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java +++ b/sonar-server/src/test/java/org/sonar/server/rule/ProfileRulesTest.java @@ -24,6 +24,7 @@ import org.apache.commons.io.IOUtils; import org.elasticsearch.client.Requests; import org.junit.After; import org.junit.Before; +import org.junit.Ignore; import org.junit.Test; import org.sonar.api.config.Settings; import org.sonar.api.rules.RulePriority; @@ -78,8 +79,7 @@ public class ProfileRulesTest { } @Test - public void should_find_active_rules() throws Exception { - + public void find_profile_rules() { Paging paging = Paging.create(10, 1); // All rules for profile 1 @@ -116,12 +116,20 @@ public class ProfileRulesTest { } @Test - public void should_get_from_active_rule() { + @Ignore("bug in E/S : fail to do a scroll when filter contain has_parent -> return good total_hits but hits is empty") + public void find_profile_rule_ids() { + // All rules for profile 1 + List<Integer> result = profileRules.searchProfileRuleIds(ProfileRuleQuery.create(1)); + assertThat(result).hasSize(3); + assertThat(result.get(0)).isEqualTo(1); } + + @Test + public void get_from_active_rule() { assertThat(profileRules.getFromActiveRuleId(391)).isNotNull(); } @Test - public void should_get_from_rule() { + public void get_from_rule() { assertThat(profileRules.getFromActiveRuleId(25)).isNotNull(); } diff --git a/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java b/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java index 7ee79bb5d68..439635a9358 100644 --- a/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java +++ b/sonar-server/src/test/java/org/sonar/server/search/SearchIndexTest.java @@ -33,9 +33,7 @@ import org.sonar.core.profiling.Profiling; import java.util.List; import static org.fest.assertions.Assertions.assertThat; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; public class SearchIndexTest { @@ -130,6 +128,23 @@ public class SearchIndexTest { assertThat(docIds).hasSize(numberOfDocuments); } + @Test + public void should_iterate_over_small_dataset() throws Exception { + final int numberOfDocuments = 3; + + searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json"); + String[] ids = new String[numberOfDocuments]; + BytesStream[] sources = new BytesStream[numberOfDocuments]; + for (int i=0; i<numberOfDocuments; i++) { + ids[i] = Integer.toString(i); + sources[i] = XContentFactory.jsonBuilder().startObject().field("value", Integer.toString(i)).endObject(); + } + searchIndex.bulkIndex("index", "type1", ids, sources); + + List<String> docIds = searchIndex.findDocumentIds(SearchQuery.create().scrollSize(100)); + assertThat(docIds).hasSize(numberOfDocuments); + } + @Test(expected = StrictDynamicMappingException.class) public void should_forbid_dynamic_mapping() throws Exception { searchIndex.addMappingFromClasspath("index", "type1", "/org/sonar/server/search/SearchIndexTest/correct_mapping1.json"); |