aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2014-07-04 14:50:13 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2014-07-04 14:54:52 +0200
commitefd87445594729b4ae9658567c7af22caa7387e6 (patch)
tree03864ca6733b932e25f4fb027234cc6746645c39
parentee0c3738e9b83bea8358d289bed9a2b1cb3fdfdd (diff)
downloadsonarqube-efd87445594729b4ae9658567c7af22caa7387e6.tar.gz
sonarqube-efd87445594729b4ae9658567c7af22caa7387e6.zip
Add the possibility to search for all rules (used by SQALE Sunbirst widget)
-rw-r--r--sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java21
-rw-r--r--sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java83
2 files changed, 97 insertions, 7 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java b/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
index 5ea09af178c..c276aa55d7b 100644
--- a/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
+++ b/sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
@@ -69,9 +69,10 @@ public class RubyRuleService implements ServerComponent, Startable {
/**
* Used in SQALE
+ * If 'pageSize' params is set no -1, all rules are returned (using scrolling)
*/
public PagedResult<Rule> find(Map<String, Object> params) {
- RuleQuery query = service.newRuleQuery();
+ RuleQuery query = new RuleQuery();
query.setQueryText(Strings.emptyToNull((String) params.get("searchQuery")));
query.setKey(Strings.emptyToNull((String) params.get("key")));
query.setLanguages(RubyUtils.toStrings(params.get("languages")));
@@ -84,13 +85,19 @@ public class RubyRuleService implements ServerComponent, Startable {
query.setSortField(RuleNormalizer.RuleField.NAME);
QueryOptions options = new QueryOptions();
- Integer page = RubyUtils.toInteger(params.get("p"));
- int pageIndex = page != null ? page : 1;
Integer pageSize = RubyUtils.toInteger(params.get("pageSize"));
- options.setPage(pageIndex, pageSize != null ? pageSize : 50);
-
- Result<Rule> result = service.search(query, options);
- return new PagedResult<Rule>(result.getHits(), PagingResult.create(options.getLimit(), pageIndex, result.getTotal()));
+ int size = pageSize != null ? pageSize : 50;
+ if (size > -1) {
+ Integer page = RubyUtils.toInteger(params.get("p"));
+ int pageIndex = page != null ? page : 1;
+ options.setPage(pageIndex, size);
+ Result<Rule> result = service.search(query, options);
+ return new PagedResult<Rule>(result.getHits(), PagingResult.create(options.getLimit(), pageIndex, result.getTotal()));
+ } else {
+ options = new QueryOptions().setScroll(true);
+ List<Rule> rules = newArrayList(service.search(query, options).scroll());
+ return new PagedResult<Rule>(rules, PagingResult.create(Integer.MAX_VALUE, 1, rules.size()));
+ }
}
/**
diff --git a/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java b/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java
index 9cfc9ca41a2..c16f5c4d890 100644
--- a/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java
@@ -24,14 +24,24 @@ import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.rule.RuleKey;
+import org.sonar.api.rule.RuleStatus;
+import org.sonar.server.paging.PagedResult;
import org.sonar.server.rule.index.RuleQuery;
import org.sonar.server.search.QueryOptions;
import org.sonar.server.search.Result;
import org.sonar.server.user.UserSession;
+import java.util.HashMap;
+import java.util.List;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static com.google.common.collect.Maps.newHashMap;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.*;
@@ -44,6 +54,12 @@ public class RubyRuleServiceTest {
@Mock
RuleUpdater updater;
+ @Captor
+ ArgumentCaptor<QueryOptions> optionsCaptor;
+
+ @Captor
+ ArgumentCaptor<RuleQuery> ruleQueryCaptor;
+
RubyRuleService service;
@Before
@@ -58,6 +74,73 @@ public class RubyRuleServiceTest {
}
@Test
+ public void search_rules() throws Exception {
+ when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(mock(Result.class));
+
+ HashMap<String, Object> params = newHashMap();
+ params.put("searchQuery", "Exception");
+ params.put("key", "S001");
+ params.put("languages", "xoo,js");
+ params.put("repositories", "checkstyle,pmd");
+ params.put("severities", "MAJOR,MINOR");
+ params.put("statuses", "BETA,READY");
+ params.put("tags", "tag1,tag2");
+ params.put("debtCharacteristics", "char1,char2");
+ params.put("hasDebtCharacteristic", "true");
+ params.put("p", "1");
+ params.put("pageSize", "40");
+ service.find(params);
+
+ verify(ruleService).search(ruleQueryCaptor.capture(), optionsCaptor.capture());
+
+ assertThat(ruleQueryCaptor.getValue().getQueryText()).isEqualTo("Exception");
+ assertThat(ruleQueryCaptor.getValue().getKey()).isEqualTo("S001");
+ assertThat(ruleQueryCaptor.getValue().getLanguages()).containsOnly("xoo", "js");
+ assertThat(ruleQueryCaptor.getValue().getRepositories()).containsOnly("checkstyle", "pmd");
+ assertThat(ruleQueryCaptor.getValue().getRepositories()).containsOnly("checkstyle", "pmd");
+ assertThat(ruleQueryCaptor.getValue().getSeverities()).containsOnly("MAJOR", "MINOR");
+ assertThat(ruleQueryCaptor.getValue().getStatuses()).containsOnly(RuleStatus.BETA, RuleStatus.READY);
+ assertThat(ruleQueryCaptor.getValue().getTags()).containsOnly("tag1", "tag2");
+ assertThat(ruleQueryCaptor.getValue().getDebtCharacteristics()).containsOnly("char1", "char2");
+ assertThat(ruleQueryCaptor.getValue().getHasDebtCharacteristic()).isTrue();
+
+ assertThat(optionsCaptor.getValue().getLimit()).isEqualTo(40);
+ assertThat(optionsCaptor.getValue().getOffset()).isEqualTo(0);
+ }
+
+ @Test
+ public void search_rules_without_page_size_param() throws Exception {
+ when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(mock(Result.class));
+
+ HashMap<String, Object> params = newHashMap();
+ params.put("p", "1");
+ service.find(params);
+
+ verify(ruleService).search(ruleQueryCaptor.capture(), optionsCaptor.capture());
+
+ assertThat(optionsCaptor.getValue().getLimit()).isEqualTo(50);
+ assertThat(optionsCaptor.getValue().getOffset()).isEqualTo(0);
+ }
+
+ @Test
+ public void search_all_rules() throws Exception {
+ List<Rule> rules = newArrayList(mock(Rule.class));
+ Result serviceResult = mock(Result.class);
+ when(serviceResult.scroll()).thenReturn(rules.iterator());
+
+ when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(serviceResult);
+
+ HashMap<String, Object> params = newHashMap();
+ params.put("pageSize", "-1");
+ PagedResult<Rule> result = service.find(params);
+
+ verify(serviceResult).scroll();
+
+ verify(ruleService).search(ruleQueryCaptor.capture(), optionsCaptor.capture());
+ assertThat(result.paging().pageSize()).isEqualTo(Integer.MAX_VALUE);
+ }
+
+ @Test
public void update_rule() throws Exception {
when(ruleService.search(any(RuleQuery.class), any(QueryOptions.class))).thenReturn(mock(Result.class));