/**
* 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")));
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()));
+ }
}
/**
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.*;
@Mock
RuleUpdater updater;
+ @Captor
+ ArgumentCaptor<QueryOptions> optionsCaptor;
+
+ @Captor
+ ArgumentCaptor<RuleQuery> ruleQueryCaptor;
+
RubyRuleService service;
@Before
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));