]> source.dussan.org Git - sonarqube.git/commitdiff
Add the possibility to search for all rules (used by SQALE Sunbirst widget)
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Jul 2014 12:50:13 +0000 (14:50 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Fri, 4 Jul 2014 12:54:52 +0000 (14:54 +0200)
sonar-server/src/main/java/org/sonar/server/rule/RubyRuleService.java
sonar-server/src/test/java/org/sonar/server/rule/RubyRuleServiceTest.java

index 5ea09af178c77c46c2cbef65e9a03e2eaea30bc7..c276aa55d7baad90da7701030013eda371a88cf3 100644 (file)
@@ -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()));
+    }
   }
 
   /**
index 9cfc9ca41a235a742e34a66acb01933360e31f9a..c16f5c4d890b1a52cd195546b5d6727bc74e5598 100644 (file)
@@ -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
@@ -57,6 +73,73 @@ public class RubyRuleServiceTest {
     verify(ruleService).getByKey(RuleKey.of("squid", "S001"));
   }
 
+  @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));