]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-6947 api/components/search use SearchWsRequest
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 13 Nov 2015 14:28:02 +0000 (15:28 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 17 Nov 2015 12:41:25 +0000 (13:41 +0100)
server/sonar-server/src/main/java/org/sonar/server/component/ws/SearchAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/SearchActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/component/ComponentsWsClient.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/component/SearchWsRequest.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/component/package-info.java [new file with mode: 0644]
sonar-ws/src/main/java/org/sonarqube/ws/client/permission/PermissionsWsClient.java
sonar-ws/src/main/java/org/sonarqube/ws/client/qualityprofile/QualityProfilesWsClient.java
sonar-ws/src/main/protobuf/ws-components.proto
sonar-ws/src/test/java/org/sonarqube/ws/client/WsClientTest.java

index 5d1ffda5a582c42c9b79d4c2a0e5d5ad813842e5..43807a65a3a8a8375ca322214a0e7111387e70c4 100644 (file)
@@ -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<String> qualifiers = wsRequest.mandatoryParamAsStrings(PARAM_QUALIFIERS);
+    List<String> 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<ComponentDto> 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<ComponentDto> 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<ComponentDto> components, Paging paging) {
-    WsSearchResponse.Builder responseBuilder = WsSearchResponse.newBuilder();
+  private SearchWsResponse buildResponse(List<ComponentDto> 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<String> qualifiers) {
+  private ComponentQuery buildQuery(SearchWsRequest request, List<String> 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<ComponentDto, WsSearchResponse.Component> {
+  private enum ComponentDToComponentResponseFunction implements Function<ComponentDto, WsComponents.SearchWsResponse.Component> {
     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())
index d72da18b2115ae6de87e5ccf4841606d069be613..140843d463e05de02ff4364c120fbeb384afeee5 100644 (file)
@@ -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 (file)
index 0000000..1b081fe
--- /dev/null
@@ -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 (file)
index 0000000..f537c1d
--- /dev/null
@@ -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<String> qualifiers;
+  private Integer page;
+  private Integer pageSize;
+  private String query;
+
+  public List<String> getQualifiers() {
+    return qualifiers;
+  }
+
+  public SearchWsRequest setQualifiers(List<String> 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 (file)
index 0000000..aa64612
--- /dev/null
@@ -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;
+
index 5eb82edcf1c9399d450f54a6ae1f57be7a514ddb..61a8dcd28c57d53b8f8d34bbd84162bb93a62b04 100644 (file)
@@ -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())
index 2adb7528af225c19e91ba4d12b8affa69bc7a2ce..dcbdec4951cdcb2c31666dddee00aa05547e83d1 100644 (file)
@@ -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;
+  }
 }
index 5b7d68bb8ae4d3d284b86ca6f7b824f6931de501..2f94e41f670dc7655c479647ff81f7160ea89c1e 100644 (file)
@@ -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;
index 3f2beff0d2c80a7e1a49d7f779d0d4a8ac4c1048..a3bdf4967f6890ecacb925a64eee5fd81be35c95 100644 (file)
@@ -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))