diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-18 17:49:04 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2017-01-18 20:47:59 +0100 |
commit | 29b386604e338b8ec560b5615cbdb5346dccf09e (patch) | |
tree | 30145a7d46dd871daa8a22882b9ab5092806d154 | |
parent | 2316a0a5101e7593587f534be726d44a09192128 (diff) | |
download | sonarqube-29b386604e338b8ec560b5615cbdb5346dccf09e.tar.gz sonarqube-29b386604e338b8ec560b5615cbdb5346dccf09e.zip |
Remove dead code related to Elasticsearch utilities
11 files changed, 16 insertions, 536 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java index 248f9655292..711f73f3e53 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/IssueQuery.java @@ -28,11 +28,11 @@ import javax.annotation.CheckForNull; import javax.annotation.Nullable; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.sonar.api.rule.RuleKey; -import org.sonar.server.search.QueryContext; import org.sonar.server.user.UserSession; import static com.google.common.base.Preconditions.checkArgument; import static com.google.common.collect.Sets.newHashSet; +import static org.sonar.server.es.SearchOptions.MAX_LIMIT; /** * @since 3.6 @@ -475,7 +475,7 @@ public class IssueQuery { public IssueQuery build() { if (issueKeys != null) { - checkArgument(issueKeys.size() <= QueryContext.MAX_LIMIT, "Number of issue keys must be less than " + QueryContext.MAX_LIMIT + " (got " + issueKeys.size() + ")"); + checkArgument(issueKeys.size() <= MAX_LIMIT, "Number of issue keys must be less than " + MAX_LIMIT + " (got " + issueKeys.size() + ")"); } return new IssueQuery(this); } diff --git a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java index 5214b3285fa..ab9b9be3c0f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/issue/ws/BulkChangeAction.java @@ -73,6 +73,7 @@ import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_02; import static org.sonar.core.util.stream.Collectors.toList; import static org.sonar.core.util.stream.Collectors.uniqueIndex; +import static org.sonar.server.es.SearchOptions.MAX_LIMIT; import static org.sonar.server.issue.AbstractChangeTagsAction.TAGS_PARAMETER; import static org.sonar.server.issue.AssignAction.ASSIGNEE_PARAMETER; import static org.sonar.server.issue.CommentAction.COMMENT_KEY; @@ -83,7 +84,6 @@ import static org.sonar.server.issue.SetTypeAction.SET_TYPE_KEY; import static org.sonar.server.issue.SetTypeAction.TYPE_PARAMETER; import static org.sonar.server.issue.TransitionAction.DO_TRANSITION_KEY; import static org.sonar.server.issue.TransitionAction.TRANSITION_PARAMETER; -import static org.sonar.server.search.QueryContext.MAX_LIMIT; import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.client.issue.IssuesWsParameters.ACTION_BULK_CHANGE; import static org.sonarqube.ws.client.issue.IssuesWsParameters.PARAM_ACTIONS; diff --git a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java index dca7ae7671d..17ec1a656d6 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/rule/ws/SearchAction.java @@ -154,7 +154,7 @@ public class SearchAction implements RulesWsAction { DbSession dbSession = dbClient.openSession(false); try { SearchWsRequest searchWsRequest = toSearchWsRequest(request); - SearchOptions context = getQueryContext(searchWsRequest); + SearchOptions context = buildSearchOptions(searchWsRequest); RuleQuery query = ruleQueryFactory.createRuleQuery(request); SearchResult searchResult = doSearch(dbSession, query, context); SearchResponse responseBuilder = buildResponse(dbSession, searchWsRequest, context, searchResult, query); @@ -322,17 +322,17 @@ public class SearchAction implements RulesWsAction { } } - protected SearchOptions getQueryContext(SearchWsRequest request) { + protected SearchOptions buildSearchOptions(SearchWsRequest request) { SearchOptions context = loadCommonContext(request); - SearchOptions searchQueryContext = new SearchOptions() + SearchOptions searchOptions = new SearchOptions() .setLimit(context.getLimit()) .setOffset(context.getOffset()); if (context.getFacets().contains(RuleIndex.FACET_OLD_DEFAULT)) { - searchQueryContext.addFacets(DEFAULT_FACETS); + searchOptions.addFacets(DEFAULT_FACETS); } else { - searchQueryContext.addFacets(context.getFacets()); + searchOptions.addFacets(context.getFacets()); } - return searchQueryContext; + return searchOptions; } private static SearchOptions loadCommonContext(SearchWsRequest request) { diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/QueryContext.java b/server/sonar-server/src/main/java/org/sonar/server/search/QueryContext.java deleted file mode 100644 index 233a8cd72e4..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/search/QueryContext.java +++ /dev/null @@ -1,183 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.search; - -import com.google.common.base.Preconditions; -import java.util.Arrays; -import java.util.Collection; -import java.util.Set; -import javax.annotation.Nullable; -import org.sonar.server.user.UserSession; - -import static com.google.common.collect.Sets.newHashSet; -import static com.google.common.collect.Sets.newLinkedHashSet; - -/** - * Various Elasticsearch request options: paging, fields and facets - * - * @since 4.4 - */ -public class QueryContext { - - public static final int DEFAULT_OFFSET = 0; - public static final int DEFAULT_LIMIT = 10; - public static final int MAX_LIMIT = 500; - - private int offset = DEFAULT_OFFSET; - private int limit = DEFAULT_LIMIT; - private Set<String> facets = newLinkedHashSet(); - private Set<String> fieldsToReturn = newHashSet(); - private boolean scroll = false; - private boolean showFullResult = false; - private String userLogin; - private Set<String> userGroups = newHashSet(); - - public QueryContext(UserSession userSession) { - this.userLogin = userSession.getLogin(); - this.userGroups = userSession.getUserGroups(); - } - - /** - * Whether or not the search returns facets for the domain. - */ - public boolean isFacet() { - return !facets.isEmpty(); - } - - /** - * Selects facets to return for the domain. - */ - public QueryContext addFacets(Collection<String> facets) { - this.facets.addAll(facets); - return this; - } - - /** - * Lists selected facets. - */ - public Collection<String> facets() { - return facets; - } - - /** - * Whether or not the search result will be scrollable using an iterator - */ - public boolean isScroll() { - return scroll; - } - - /** - * Sets whether or not the search result will be scrollable using an iterator - */ - public QueryContext setScroll(boolean scroll) { - this.scroll = scroll; - return this; - } - - /** - * Offset of the first result to return. Defaults to {@link #DEFAULT_OFFSET} - */ - public int getOffset() { - return offset; - } - - /** - * Sets the offset of the first result to return (zero-based). - */ - public QueryContext setOffset(int offset) { - Preconditions.checkArgument(offset >= 0, "Offset must be positive"); - this.offset = offset; - return this; - } - - /** - * Set offset and limit according to page approach - */ - public QueryContext setPage(int page, int pageSize) { - Preconditions.checkArgument(page >= 1, "Page must be greater or equal to 1 (got " + page + ")"); - Preconditions.checkArgument(pageSize >= 0, "Page size must be greater or equal to 0 (got " + pageSize + ")"); - setLimit(pageSize); - setOffset((page * getLimit()) - getLimit()); - return this; - } - - public int getPage() { - int currentLimit = getLimit(); - return currentLimit > 0 ? (int) Math.ceil((double) (getOffset() + 1) / (double) currentLimit) : 0; - } - - /** - * Limit on the number of results to return. Defaults to {@link #DEFAULT_LIMIT}. - */ - public int getLimit() { - return showFullResult ? 999_999 : limit; - } - - /** - * Sets the limit on the number of results to return. - */ - public QueryContext setLimit(int limit) { - this.limit = Math.min(limit, MAX_LIMIT); - return this; - } - - public QueryContext setMaxLimit() { - this.limit = MAX_LIMIT; - return this; - } - - /** - * Careful use, this could lead to massive data transport ! - */ - public void setShowFullResult(boolean showFullResult) { - this.showFullResult = showFullResult; - } - - public Set<String> getFieldsToReturn() { - return fieldsToReturn; - } - - public QueryContext setFieldsToReturn(@Nullable Collection<String> c) { - fieldsToReturn.clear(); - if (c != null) { - this.fieldsToReturn = newHashSet(c); - } - return this; - } - - public QueryContext addFieldsToReturn(@Nullable Collection<String> c) { - if (c != null) { - fieldsToReturn.addAll(c); - } - return this; - } - - public QueryContext addFieldsToReturn(String... c) { - return addFieldsToReturn(Arrays.asList(c)); - } - - public String getUserLogin() { - return userLogin; - } - - public Set<String> getUserGroups() { - return userGroups; - } -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/Result.java b/server/sonar-server/src/main/java/org/sonar/server/search/Result.java index 4e1ccb72ce7..53c4c4ef878 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/search/Result.java +++ b/server/sonar-server/src/main/java/org/sonar/server/search/Result.java @@ -23,7 +23,6 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Map; -import javax.annotation.CheckForNull; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.elasticsearch.action.search.SearchResponse; @@ -51,20 +50,6 @@ public class Result<K> { return this.facets.getFacets(); } - public Facets getFacetsObject() { - return this.facets; - } - - @CheckForNull - public Collection<FacetValue> getFacetValues(String facetName) { - return this.facets.getFacetValues(facetName); - } - - @CheckForNull - public List<String> getFacetKeys(String facetName) { - return this.facets.getFacetKeys(facetName); - } - @Override public String toString() { return ReflectionToStringBuilder.toString(this); diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/ws/BaseMapping.java b/server/sonar-server/src/main/java/org/sonar/server/search/ws/BaseMapping.java deleted file mode 100644 index db221363f5d..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/search/ws/BaseMapping.java +++ /dev/null @@ -1,149 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.search.ws; - -import com.google.common.collect.LinkedHashMultimap; -import com.google.common.collect.Multimap; -import java.util.Arrays; -import java.util.Set; -import javax.annotation.Nullable; -import org.sonar.api.server.ServerSide; -import org.sonar.api.utils.text.JsonWriter; -import org.sonar.server.es.BaseDoc; -import org.sonar.server.search.IndexUtils; -import org.sonar.server.search.QueryContext; - -/** - * Mapping of search documents (see {@link BaseDoc}) to WS JSON responses - * @deprecated no more "framework" to convert ES docs to WS responses. - */ -@ServerSide -@Deprecated -public abstract class BaseMapping<DOC extends BaseDoc, CTX> { - - private final Multimap<String, String> indexFieldsByWsFields = LinkedHashMultimap.create(); - private final Multimap<String, Mapper> mappers = LinkedHashMultimap.create(); - - protected BaseMapping() { - // Nothing here - } - - /** - * All the WS supported fields - */ - public Set<String> supportedFields() { - return mappers.keySet(); - } - - /** - * Write only requested document fields - */ - protected void doWrite(DOC doc, @Nullable CTX context, JsonWriter json, @Nullable QueryContext queryContext) { - json.beginObject(); - json.prop("key", doc.keyField()); - if (queryContext == null || queryContext.getFieldsToReturn().isEmpty()) { - // return all fields - for (Mapper mapper : mappers.values()) { - mapper.write(json, doc, context); - } - } else { - for (String optionField : queryContext.getFieldsToReturn()) { - for (Mapper mapper : mappers.get(optionField)) { - mapper.write(json, doc, context); - } - } - } - json.endObject(); - } - - protected BaseMapping map(String key, String indexKey) { - return map(key, new IndexStringMapper(key, indexKey)); - } - - protected BaseMapping mapDateTime(String key, String indexKey) { - return map(key, new IndexDatetimeMapper(key, indexKey)); - } - - protected BaseMapping map(String key, Mapper mapper) { - mappers.put(key, mapper); - if (mapper instanceof IndexMapper) { - IndexMapper indexField = (IndexMapper) mapper; - indexFieldsByWsFields.putAll(key, Arrays.asList(indexField.indexFields())); - } - return this; - } - - @FunctionalInterface - public interface Mapper<DOC extends BaseDoc, CTX> { - void write(JsonWriter json, DOC doc, CTX context); - } - - public abstract static class IndexMapper<DOC extends BaseDoc, CTX> implements Mapper<DOC, CTX> { - protected final String[] indexFields; - - protected IndexMapper(String... indexFields) { - this.indexFields = indexFields; - } - - String[] indexFields() { - return indexFields; - } - } - - /** - * String field - */ - public static class IndexStringMapper<DOC extends BaseDoc, CTX> extends IndexMapper<DOC, CTX> { - protected final String key; - - public IndexStringMapper(String key, String indexKey) { - super(indexKey); - this.key = key; - } - - @Override - public void write(JsonWriter json, DOC doc, CTX context) { - Object val = doc.getNullableField(indexFields[0]); - if (val == null && indexFields.length == 2) { - // There is an alternative value - val = doc.getNullableField(indexFields[1]); - } - json.prop(key, val != null ? val.toString() : null); - } - } - - public static class IndexDatetimeMapper<DOC extends BaseDoc, CTX> extends IndexMapper<DOC, CTX> { - private final String key; - - public IndexDatetimeMapper(String key, String indexKey) { - super(indexKey); - this.key = key; - } - - @Override - public void write(JsonWriter json, DOC doc, CTX context) { - String val = doc.getNullableField(indexFields[0]); - if (val != null) { - json.propDateTime(key, IndexUtils.parseDateTime(val)); - } - } - } - -} diff --git a/server/sonar-server/src/main/java/org/sonar/server/search/ws/package-info.java b/server/sonar-server/src/main/java/org/sonar/server/search/ws/package-info.java deleted file mode 100644 index 23e56f3f6c0..00000000000 --- a/server/sonar-server/src/main/java/org/sonar/server/search/ws/package-info.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -@ParametersAreNonnullByDefault -package org.sonar.server.search.ws; - -import javax.annotation.ParametersAreNonnullByDefault; diff --git a/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java b/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java index d3a45193714..b3754c04f13 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/es/SearchOptionsTest.java @@ -24,7 +24,6 @@ import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.sonar.api.utils.text.JsonWriter; -import org.sonar.server.search.QueryContext; import org.sonar.test.JsonAssert; import static org.assertj.core.api.Assertions.assertThat; @@ -94,7 +93,7 @@ public class SearchOptionsTest { assertThat(options.getLimit()).isEqualTo(42); options.setLimit(SearchOptions.MAX_LIMIT + 10); - assertThat(options.getLimit()).isEqualTo(QueryContext.MAX_LIMIT); + assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); } @Test @@ -105,9 +104,9 @@ public class SearchOptionsTest { @Test public void max_page_size() { - SearchOptions options = new SearchOptions().setPage(3, QueryContext.MAX_LIMIT + 10); - assertThat(options.getOffset()).isEqualTo(QueryContext.MAX_LIMIT * 2); - assertThat(options.getLimit()).isEqualTo(QueryContext.MAX_LIMIT); + SearchOptions options = new SearchOptions().setPage(3, SearchOptions.MAX_LIMIT + 10); + assertThat(options.getOffset()).isEqualTo(SearchOptions.MAX_LIMIT * 2); + assertThat(options.getLimit()).isEqualTo(SearchOptions.MAX_LIMIT); } @Test diff --git a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java index 95afe917eff..e8b9117ffe5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionMediumTest.java @@ -49,6 +49,7 @@ import org.sonar.db.rule.RuleDao; import org.sonar.db.rule.RuleDto; import org.sonar.db.rule.RuleTesting; import org.sonar.db.user.UserDto; +import org.sonar.server.es.SearchOptions; import org.sonar.server.issue.IssueQuery; import org.sonar.server.issue.index.IssueIndexer; import org.sonar.server.organization.DefaultOrganization; @@ -57,7 +58,6 @@ import org.sonar.server.permission.GroupPermissionChange; import org.sonar.server.permission.PermissionChange; import org.sonar.server.permission.PermissionUpdater; import org.sonar.server.permission.ProjectId; -import org.sonar.server.search.QueryContext; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.usergroups.ws.GroupIdOrAnyone; @@ -325,7 +325,7 @@ public class SearchActionMediumTest { ComponentDto project = insertComponent(ComponentTesting.newProjectDto(otherOrganization2, "PROJECT_ID").setKey("PROJECT_KEY")); setDefaultProjectPermission(project); ComponentDto file = insertComponent(ComponentTesting.newFileDto(project, null, "FILE_ID").setKey("FILE_KEY")); - for (int i = 0; i < QueryContext.MAX_LIMIT + 1; i++) { + for (int i = 0; i < SearchOptions.MAX_LIMIT + 1; i++) { IssueDto issue = IssueTesting.newDto(rule, file, project); tester.get(IssueDao.class).insert(session, issue); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java index 1796c7e088e..fe922d32ac5 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/RuleActivatorMediumTest.java @@ -50,7 +50,6 @@ import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; import org.sonar.server.rule.index.RuleIndex; import org.sonar.server.rule.index.RuleIndexer; import org.sonar.server.rule.index.RuleQuery; -import org.sonar.server.search.QueryContext; import org.sonar.server.tester.ServerTester; import org.sonar.server.tester.UserSessionRule; @@ -900,7 +899,7 @@ public class RuleActivatorMediumTest { @Test public void bulk_activation() { // Generate more rules than the search's max limit - int bulkSize = QueryContext.MAX_LIMIT + 10; + int bulkSize = SearchOptions.MAX_LIMIT + 10; for (int i = 0; i < bulkSize; i++) { db.ruleDao().insert(dbSession, newDto(RuleKey.of("bulk", "r_" + i)).setLanguage("xoo")); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/search/QueryContextTest.java b/server/sonar-server/src/test/java/org/sonar/server/search/QueryContextTest.java deleted file mode 100644 index 7f8d800dde0..00000000000 --- a/server/sonar-server/src/test/java/org/sonar/server/search/QueryContextTest.java +++ /dev/null @@ -1,148 +0,0 @@ -/* - * SonarQube - * Copyright (C) 2009-2016 SonarSource SA - * mailto:contact AT sonarsource DOT com - * - * This program is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 3 of the License, or (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with this program; if not, write to the Free Software Foundation, - * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - */ -package org.sonar.server.search; - -import com.google.common.collect.ImmutableList; -import java.util.Arrays; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.server.tester.UserSessionRule; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.Assert.fail; - -public class QueryContextTest { - @Rule - public UserSessionRule userSessionRule = UserSessionRule.standalone().login("john").setUserGroups("sonar-users"); - - QueryContext options; - - @Before - public void setUp() { - options = new QueryContext(userSessionRule); - } - - @Test - public void user_and_groups() { - assertThat(options.getUserLogin()).isEqualTo("john"); - assertThat(options.getUserGroups()).containsOnly("sonar-users", "Anyone"); - } - - @Test - public void page_shortcut_for_limit_and_offset() { - options.setPage(3, 10); - - assertThat(options.getLimit()).isEqualTo(10); - assertThat(options.getOffset()).isEqualTo(20); - } - - @Test - public void page_starts_at_one() { - options.setPage(1, 10); - assertThat(options.getLimit()).isEqualTo(10); - assertThat(options.getOffset()).isEqualTo(0); - assertThat(options.getPage()).isEqualTo(1); - } - - @Test - public void with_zero_page_size() { - options.setPage(1, 0); - assertThat(options.getLimit()).isEqualTo(0); - assertThat(options.getOffset()).isEqualTo(0); - assertThat(options.getPage()).isEqualTo(0); - } - - @Test - public void page_must_be_strictly_positive() { - try { - options.setPage(0, 10); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Page must be greater or equal to 1 (got 0)"); - } - } - - @Test - public void page_size_must_be_positive() { - try { - options.setPage(2, -1); - fail(); - } catch (IllegalArgumentException e) { - assertThat(e).hasMessage("Page size must be greater or equal to 0 (got -1)"); - } - } - - @Test - public void max_limit() { - options.setLimit(42); - assertThat(options.getLimit()).isEqualTo(42); - - options.setLimit(QueryContext.MAX_LIMIT + 10); - assertThat(options.getLimit()).isEqualTo(QueryContext.MAX_LIMIT); - } - - @Test - public void set_max_limit() { - options.setMaxLimit(); - assertThat(options.getLimit()).isEqualTo(QueryContext.MAX_LIMIT); - } - - @Test - public void max_page_size() { - options.setPage(3, QueryContext.MAX_LIMIT + 10); - assertThat(options.getOffset()).isEqualTo(QueryContext.MAX_LIMIT * 2); - assertThat(options.getLimit()).isEqualTo(QueryContext.MAX_LIMIT); - } - - @Test - public void getFieldsToReturn() { - assertThat(options.getFieldsToReturn()).isEmpty(); - - options.setFieldsToReturn(Arrays.asList("one", "two")); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two"); - - options.addFieldsToReturn(Arrays.asList("three")); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two", "three"); - - options.addFieldsToReturn("four"); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two", "three", "four"); - } - - @Test - public void support_immutable_fields() { - options.setFieldsToReturn(ImmutableList.of("one", "two")); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two"); - - options.addFieldsToReturn(ImmutableList.of("three")); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two", "three"); - - options.addFieldsToReturn("four"); - assertThat(options.getFieldsToReturn()).containsOnly("one", "two", "three", "four"); - } - - @Test - public void do_not_request_facets_by_default() { - assertThat(options.isFacet()).isFalse(); - - options.addFacets(Arrays.asList("polop")); - assertThat(options.isFacet()).isTrue(); - } -} |