From 1de32aa4798f3246861214ec7f52bfc5f76fd0bb Mon Sep 17 00:00:00 2001 From: Teryk Bellahsene Date: Fri, 13 Nov 2015 15:28:02 +0100 Subject: [PATCH] SONAR-6947 api/components/search use SearchWsRequest --- .../server/component/ws/SearchAction.java | 48 +++++++----- .../server/component/ws/SearchActionTest.java | 8 +- .../client/component/ComponentsWsClient.java | 49 +++++++++++++ .../ws/client/component/SearchWsRequest.java | 73 +++++++++++++++++++ .../ws/client/component/package-info.java | 25 +++++++ .../permission/PermissionsWsClient.java | 10 +-- .../QualityProfilesWsClient.java | 6 +- .../src/main/protobuf/ws-components.proto | 2 +- .../org/sonarqube/ws/client/WsClientTest.java | 8 +- 9 files changed, 197 insertions(+), 32 deletions(-) create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/component/SearchWsRequest.java create mode 100644 sonar-ws/src/main/java/org/sonarqube/ws/client/component/package-info.java diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java index 5d1ffda5a58..43807a65a3a 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java @@ -38,16 +38,18 @@ import org.sonar.db.DbSession; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentQuery; import org.sonar.server.user.UserSession; -import org.sonarqube.ws.WsComponents.WsSearchResponse; +import org.sonarqube.ws.WsComponents; +import org.sonarqube.ws.WsComponents.SearchWsResponse; +import org.sonarqube.ws.client.component.SearchWsRequest; import static com.google.common.collect.FluentIterable.from; import static com.google.common.collect.Ordering.natural; import static java.lang.String.format; import static org.sonar.server.component.ResourceTypeFunctions.RESOURCE_TYPE_TO_QUALIFIER; import static org.sonar.server.component.ws.WsComponentsParameters.PARAM_QUALIFIERS; -import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER; import static org.sonar.server.ws.WsUtils.checkRequest; import static org.sonar.server.ws.WsUtils.writeProtobuf; +import static org.sonarqube.ws.client.permission.PermissionsWsParameters.PARAM_QUALIFIER; public class SearchAction implements ComponentsWsAction { private static final String QUALIFIER_PROPERTY_PREFIX = "qualifiers."; @@ -83,23 +85,35 @@ public class SearchAction implements ComponentsWsAction { @Override public void handle(Request wsRequest, Response wsResponse) throws Exception { + SearchWsResponse searchWsResponse = doHandle(toSearchWsRequest(wsRequest)); + writeProtobuf(searchWsResponse, wsRequest, wsResponse); + } + + private SearchWsResponse doHandle(SearchWsRequest request) { userSession.checkLoggedIn().checkGlobalPermission(GlobalPermissions.SYSTEM_ADMIN); - List qualifiers = wsRequest.mandatoryParamAsStrings(PARAM_QUALIFIERS); + List qualifiers = request.getQualifiers(); validateQualifiers(qualifiers); DbSession dbSession = dbClient.openSession(false); try { - ComponentQuery query = buildQuery(wsRequest, qualifiers); - Paging paging = buildPaging(dbSession, wsRequest, query); + ComponentQuery query = buildQuery(request, qualifiers); + Paging paging = buildPaging(dbSession, request, query); List components = searchComponents(dbSession, query, paging); - WsSearchResponse response = buildResponse(components, paging); - writeProtobuf(response, wsRequest, wsResponse); + return buildResponse(components, paging); } finally { dbClient.closeSession(dbSession); } } + private static SearchWsRequest toSearchWsRequest(Request request) { + return new SearchWsRequest() + .setQualifiers(request.mandatoryParamAsStrings(PARAM_QUALIFIERS)) + .setQuery(request.param(Param.TEXT_QUERY)) + .setPage(request.mandatoryParamAsInt(Param.PAGE)) + .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE)); + } + private List searchComponents(DbSession dbSession, ComponentQuery query, Paging paging) { return dbClient.componentDao().selectByQuery( dbSession, @@ -108,8 +122,8 @@ public class SearchAction implements ComponentsWsAction { paging.pageSize()); } - private WsSearchResponse buildResponse(List components, Paging paging) { - WsSearchResponse.Builder responseBuilder = WsSearchResponse.newBuilder(); + private SearchWsResponse buildResponse(List components, Paging paging) { + WsComponents.SearchWsResponse.Builder responseBuilder = SearchWsResponse.newBuilder(); responseBuilder.getPagingBuilder() .setPageIndex(paging.pageIndex()) .setPageSize(paging.pageSize()) @@ -123,16 +137,16 @@ public class SearchAction implements ComponentsWsAction { return responseBuilder.build(); } - private Paging buildPaging(DbSession dbSession, Request wsRequest, ComponentQuery query) { + private Paging buildPaging(DbSession dbSession, SearchWsRequest request, ComponentQuery query) { int total = dbClient.componentDao().countByQuery(dbSession, query); - return Paging.forPageIndex(wsRequest.mandatoryParamAsInt(Param.PAGE)) - .withPageSize(wsRequest.mandatoryParamAsInt(Param.PAGE_SIZE)) + return Paging.forPageIndex(request.getPage()) + .withPageSize(request.getPageSize()) .andTotal(total); } - private ComponentQuery buildQuery(Request wsRequest, List qualifiers) { + private ComponentQuery buildQuery(SearchWsRequest request, List qualifiers) { return new ComponentQuery( - wsRequest.param(Param.TEXT_QUERY), + request.getQuery(), qualifiers.toArray(new String[qualifiers.size()])); } @@ -166,12 +180,12 @@ public class SearchAction implements ComponentsWsAction { return i18n.message(userSession.locale(), QUALIFIER_PROPERTY_PREFIX + qualifier, ""); } - private enum ComponentDToComponentResponseFunction implements Function { + private enum ComponentDToComponentResponseFunction implements Function { INSTANCE; @Override - public WsSearchResponse.Component apply(@Nonnull ComponentDto dto) { - return WsSearchResponse.Component.newBuilder() + public WsComponents.SearchWsResponse.Component apply(@Nonnull ComponentDto dto) { + return SearchWsResponse.Component.newBuilder() .setId(dto.uuid()) .setKey(dto.key()) .setName(dto.name()) diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java index d72da18b211..140843d463e 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java @@ -41,11 +41,11 @@ import org.sonar.server.exceptions.BadRequestException; import org.sonar.server.exceptions.ForbiddenException; import org.sonar.server.exceptions.UnauthorizedException; import org.sonar.server.i18n.I18nRule; -import org.sonarqube.ws.MediaTypes; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.TestRequest; import org.sonar.server.ws.WsActionTester; -import org.sonarqube.ws.WsComponents.WsSearchResponse; +import org.sonarqube.ws.MediaTypes; +import org.sonarqube.ws.WsComponents.SearchWsResponse; import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; @@ -130,7 +130,7 @@ public class SearchActionTest { .setParam(Param.PAGE_SIZE, "3") .execute() .getInputStream(); - WsSearchResponse response = WsSearchResponse.parseFrom(responseStream); + SearchWsResponse response = SearchWsResponse.parseFrom(responseStream); assertThat(response.getComponentsCount()).isEqualTo(3); assertThat(response.getComponentsList()).extracting("id").containsExactly("project-uuid-4", "project-uuid-5", "project-uuid-6"); @@ -145,7 +145,7 @@ public class SearchActionTest { InputStream responseStream = newRequest(Qualifiers.PROJECT) .setParam(Param.TEXT_QUERY, "project-_%") .execute().getInputStream(); - WsSearchResponse response = WsSearchResponse.parseFrom(responseStream); + SearchWsResponse response = SearchWsResponse.parseFrom(responseStream); assertThat(response.getComponentsCount()).isEqualTo(1); assertThat(response.getComponentsList()).extracting("key").containsExactly("project-_%-key"); diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java new file mode 100644 index 00000000000..1b081fe6ebe --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java @@ -0,0 +1,49 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.component; + +import org.sonarqube.ws.WsComponents.SearchWsResponse; +import org.sonarqube.ws.client.WsClient; + +import static org.sonarqube.ws.client.WsRequest.newGetRequest; + +public class ComponentsWsClient { + private static final String ENDPOINT = "api/components/"; + private final WsClient wsClient; + + public ComponentsWsClient(WsClient wsClient) { + this.wsClient = wsClient; + } + + public SearchWsResponse search(SearchWsRequest request) { + return wsClient.execute( + newGetRequest(action("search")) + .setParam("qualifiers", request.getQualifiers()) + .setParam("p", request.getPage()) + .setParam("ps", request.getPageSize()) + .setParam("q", request.getQuery()), + SearchWsResponse.parser()); + } + + private static String action(String action) { + return ENDPOINT + action; + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/component/SearchWsRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/SearchWsRequest.java new file mode 100644 index 00000000000..f537c1d22ac --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/SearchWsRequest.java @@ -0,0 +1,73 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.component; + +import java.util.List; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; + +import static java.util.Objects.requireNonNull; + +public class SearchWsRequest { + private List qualifiers; + private Integer page; + private Integer pageSize; + private String query; + + public List getQualifiers() { + return qualifiers; + } + + public SearchWsRequest setQualifiers(List qualifiers) { + this.qualifiers = requireNonNull(qualifiers); + return this; + } + + @CheckForNull + public Integer getPage() { + return page; + } + + public SearchWsRequest setPage(int page) { + this.page = page; + return this; + } + + @CheckForNull + public Integer getPageSize() { + return pageSize; + } + + public SearchWsRequest setPageSize(int pageSize) { + this.pageSize = pageSize; + return this; + } + + @CheckForNull + public String getQuery() { + return query; + } + + public SearchWsRequest setQuery(@Nullable String query) { + this.query = query; + return this; + } +} diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/component/package-info.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/package-info.java new file mode 100644 index 00000000000..aa64612df56 --- /dev/null +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/component/package-info.java @@ -0,0 +1,25 @@ +/* + * SonarQube, open source software quality management tool. + * Copyright (C) 2008-2014 SonarSource + * mailto:contact AT sonarsource DOT com + * + * SonarQube 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. + * + * SonarQube 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.sonarqube.ws.client.component; + +import javax.annotation.ParametersAreNonnullByDefault; + diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java index 5eb82edcf1c..61a8dcd28c5 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java @@ -163,7 +163,7 @@ public class PermissionsWsClient { public SearchProjectPermissionsWsResponse searchProjectPermissions(SearchProjectPermissionsWsRequest request) { return wsClient.execute( - newGetRequest("search_project_permissions") + newGetRequest(action("search_project_permissions")) .setParam(PARAM_PROJECT_ID, request.getProjectId()) .setParam(PARAM_PROJECT_KEY, request.getProjectKey()) .setParam("p", request.getPage()) @@ -174,14 +174,14 @@ public class PermissionsWsClient { public SearchTemplatesWsResponse searchTemplates(SearchTemplatesWsRequest request) { return wsClient.execute( - newGetRequest("search_templates") + newGetRequest(action("search_templates")) .setParam("q", request.getQuery()), SearchTemplatesWsResponse.parser()); } public void setDefaultTemplate(SetDefaultTemplateWsRequest request) { wsClient.execute( - newPostRequest("set_default_template") + newPostRequest(action("set_default_template")) .setParam(PARAM_QUALIFIER, request.getQualifier()) .setParam(PARAM_TEMPLATE_ID, request.getTemplateId()) .setParam(PARAM_TEMPLATE_NAME, request.getTemplateName())); @@ -189,7 +189,7 @@ public class PermissionsWsClient { public UpdateTemplateWsResponse updateTemplate(UpdateTemplateWsRequest request) { return wsClient.execute( - newPostRequest("update_template") + newPostRequest(action("update_template")) .setParam(PARAM_DESCRIPTION, request.getDescription()) .setParam(PARAM_ID, request.getId()) .setParam(PARAM_NAME, request.getName()) @@ -199,7 +199,7 @@ public class PermissionsWsClient { public UsersWsResponse users(UsersWsRequest request) { return wsClient.execute( - newGetRequest("users") + newGetRequest(action("users")) .setParam(PARAM_PERMISSION, request.getPermission()) .setParam(PARAM_PROJECT_ID, request.getProjectId()) .setParam(PARAM_PROJECT_KEY, request.getProjectKey()) diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java index 2adb7528af2..dcbdec4951c 100644 --- a/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java +++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java @@ -34,11 +34,15 @@ public class QualityProfilesWsClient { public SearchWsResponse search(SearchWsRequest request) { return wsClient.execute( - newGetRequest("search") + newGetRequest(action("search")) .setParam("defaults", request.getDefaults()) .setParam("language", request.getLanguage()) .setParam("profileName", request.getProfileName()) .setParam("projectKey", request.getProjectKey()), SearchWsResponse.parser()); } + + private static String action(String action) { + return "api/qualityprofiles/" + action; + } } diff --git a/sonar-ws/src/main/protobuf/ws-components.proto b/sonar-ws/src/main/protobuf/ws-components.proto index 5b7d68bb8ae..2f94e41f670 100644 --- a/sonar-ws/src/main/protobuf/ws-components.proto +++ b/sonar-ws/src/main/protobuf/ws-components.proto @@ -27,7 +27,7 @@ option java_outer_classname = "WsComponents"; option optimize_for = SPEED; // WS api/components/search -message WsSearchResponse { +message SearchWsResponse { message Component { optional string id = 1; optional string key = 2; diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java index 3f2beff0d2c..a3bdf4967f6 100644 --- a/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java +++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java @@ -65,18 +65,18 @@ public class WsClientTest { @Test public void return_protobuf_response() throws Exception { server.doReturnBody( - WsComponents.WsSearchResponse + WsComponents.SearchWsResponse .newBuilder() - .addComponents(WsComponents.WsSearchResponse.Component.getDefaultInstance()) + .addComponents(WsComponents.SearchWsResponse.Component.getDefaultInstance()) .build() .toByteArray()); server.doReturnStatus(HTTP_OK); server.doReturnContentType(MediaTypes.PROTOBUF); - WsComponents.WsSearchResponse response = underTest.execute( + WsComponents.SearchWsResponse response = underTest.execute( newGetRequest("api/components/search") .setMediaType(WsRequest.MediaType.PROTOBUF), - WsComponents.WsSearchResponse.parser()); + WsComponents.SearchWsResponse.parser()); assertThat(response.getComponentsCount()).isEqualTo(1); assertThat(server.requestHeaders().get(HttpHeaders.ACCEPT)) -- 2.39.5