diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-02-16 17:45:52 +0100 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-02-17 09:43:54 +0100 |
commit | caf680a41651e04268b7bd1d26152d5428e574d9 (patch) | |
tree | 87ceea69b5cb80e48a9ce8d83b08fd29122abafc | |
parent | c00a059069b34fc14716a5fb40f6eaa5a2cddfe3 (diff) | |
download | sonarqube-caf680a41651e04268b7bd1d26152d5428e574d9.tar.gz sonarqube-caf680a41651e04268b7bd1d26152d5428e574d9.zip |
SONAR-8804 Use api/projects/search in projects admin page
7 files changed, 75 insertions, 15 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchAction.java index 1d7aa790a68..3ca4f103486 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/SearchAction.java @@ -44,6 +44,7 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; import static org.sonarqube.ws.WsProjects.SearchWsResponse.Component; import static org.sonarqube.ws.WsProjects.SearchWsResponse.newBuilder; import static org.sonarqube.ws.client.project.ProjectsWsParameters.ACTION_SEARCH; +import static org.sonarqube.ws.client.project.ProjectsWsParameters.MAX_PAGE_SIZE; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_ORGANIZATION; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS; @@ -68,7 +69,7 @@ public class SearchAction implements ProjectsWsAction { .setDescription("Search for projects or views.<br>" + "Requires 'System Administrator' permission") .setInternal(true) - .addPagingParams(100) + .addPagingParams(100, MAX_PAGE_SIZE) .addSearchQuery("sona", "component names", "component keys") .setResponseExample(getClass().getResource("search-example.json")) .setHandler(this); diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchActionTest.java index 9b62ee686fd..1aa03aa609f 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/SearchActionTest.java @@ -24,7 +24,6 @@ import com.google.common.base.Throwables; import java.io.IOException; import java.net.URISyntaxException; import java.util.ArrayList; -import java.util.Collections; import java.util.List; import org.assertj.core.api.Assertions; import org.junit.Rule; @@ -64,7 +63,6 @@ import static org.sonar.db.component.ComponentTesting.newView; import static org.sonar.test.JsonAssert.assertJson; import static org.sonarqube.ws.MediaTypes.PROTOBUF; import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_ORGANIZATION; -import static org.sonarqube.ws.client.project.ProjectsWsParameters.PARAM_QUALIFIERS; public class SearchActionTest { @@ -253,7 +251,7 @@ public class SearchActionTest { WebService.Param psParam = action.param("ps"); assertThat(psParam.isRequired()).isFalse(); assertThat(psParam.defaultValue()).isEqualTo("100"); - assertThat(psParam.description()).isEqualTo("Page size. Must be greater than 0."); + assertThat(psParam.description()).isEqualTo("Page size. Must be greater than 0 and less than 500"); } @Test diff --git a/server/sonar-web/src/main/js/api/components.js b/server/sonar-web/src/main/js/api/components.js index e47820b33f7..53007d59a45 100644 --- a/server/sonar-web/src/main/js/api/components.js +++ b/server/sonar-web/src/main/js/api/components.js @@ -21,7 +21,7 @@ import { getJSON, postJSON, post } from '../helpers/request'; export function getComponents (data?: Object) { - const url = '/api/components/search'; + const url = '/api/projects/search'; return getJSON(url, data); } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Paging.java b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Paging.java index 4cc2da8bb21..2f650bffe61 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/utils/Paging.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/utils/Paging.java @@ -19,6 +19,8 @@ */ package org.sonar.api.utils; +import static com.google.common.base.Preconditions.checkArgument; + /** * @since 3.6 */ @@ -29,16 +31,9 @@ public class Paging { private final int total; private Paging(int pageSize, int pageIndex, int total) { - if (pageSize < 1) { - throw new IllegalArgumentException("Page size must be strictly positive. Got " + pageSize); - } - if (pageIndex < 1) { - throw new IllegalArgumentException("Page index must be strictly positive. Got " + pageIndex); - } - if (total < 0) { - throw new IllegalArgumentException("Total items must be positive. Got " + total); - } - + checkArgument(pageSize >= 1, "Page size must be strictly positive. Got %s", pageSize); + checkArgument(pageIndex >= 1, "Page index must be strictly positive. Got %s", pageIndex); + checkArgument(total >= 0, "Total items must be positive. Got %s", total); this.pageSize = pageSize; this.pageIndex = pageIndex; this.total = total; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java index 9f6f707479b..b87cb23a0d4 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/ProjectsWsParameters.java @@ -21,6 +21,8 @@ package org.sonarqube.ws.client.project; public class ProjectsWsParameters { + public static final int MAX_PAGE_SIZE = 500; + public static final String CONTROLLER = "api/projects"; public static final String ACTION_CREATE = "create"; diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchWsRequest.java index d6b18878ebb..0bb627a9c61 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchWsRequest.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/project/SearchWsRequest.java @@ -24,9 +24,12 @@ import java.util.List; import javax.annotation.CheckForNull; import javax.annotation.Nullable; +import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; +import static org.sonarqube.ws.client.project.ProjectsWsParameters.MAX_PAGE_SIZE; public class SearchWsRequest { + private final String organization; private final String query; private final List<String> qualifiers; @@ -102,6 +105,7 @@ public class SearchWsRequest { } public SearchWsRequest build() { + checkArgument(pageSize == null || pageSize <= MAX_PAGE_SIZE, "Page size must not be greater than %s", MAX_PAGE_SIZE); return new SearchWsRequest(this); } } diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/project/SearchWsRequestTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/SearchWsRequestTest.java new file mode 100644 index 00000000000..89f27374b0a --- /dev/null +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/project/SearchWsRequestTest.java @@ -0,0 +1,60 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info 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.sonarqube.ws.client.project; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; + +import static java.util.Arrays.asList; +import static org.assertj.core.api.Assertions.assertThat; + +public class SearchWsRequestTest { + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + + @Test + public void create_request() throws Exception { + SearchWsRequest underTest = SearchWsRequest.builder() + .setOrganization("orga") + .setQuery("project") + .setQualifiers(asList("TRK", "VW")) + .setPage(5) + .setPageSize(10) + .build(); + + assertThat(underTest.getOrganization()).isEqualTo("orga"); + assertThat(underTest.getQuery()).isEqualTo("project"); + assertThat(underTest.getQualifiers()).containsOnly("TRK", "VW"); + assertThat(underTest.getPage()).isEqualTo(5); + assertThat(underTest.getPageSize()).isEqualTo(10); + } + + @Test + public void fail_when_page_size_is_greather_then_500() throws Exception { + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("Page size must not be greater than 500"); + + SearchWsRequest.builder() + .setPageSize(10000) + .build(); + } +} |