aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-ws
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@sonarsource.com>2017-04-20 11:06:01 +0200
committerJulien Lancelot <julien.lancelot@sonarsource.com>2017-04-21 16:01:31 +0200
commit90613366ab07018248f7c2974367bcb6afd37499 (patch)
treeb7974f270db52cea65e6aa396286c6314686b720 /sonar-ws
parent7c5b10e2d94f3707418495214300f9e9c3bf11a7 (diff)
downloadsonarqube-90613366ab07018248f7c2974367bcb6afd37499.tar.gz
sonarqube-90613366ab07018248f7c2974367bcb6afd37499.zip
SONAR-9128 Use protobuf in api/users/search
Diffstat (limited to 'sonar-ws')
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/user/SearchRequest.java100
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersService.java14
-rw-r--r--sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java1
-rw-r--r--sonar-ws/src/main/protobuf/ws-users.proto28
-rw-r--r--sonar-ws/src/test/java/org/sonarqube/ws/client/user/UsersServiceTest.java20
5 files changed, 163 insertions, 0 deletions
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/user/SearchRequest.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/SearchRequest.java
new file mode 100644
index 00000000000..52e19f34607
--- /dev/null
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/SearchRequest.java
@@ -0,0 +1,100 @@
+/*
+ * 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.user;
+
+import java.util.ArrayList;
+import java.util.List;
+import javax.annotation.CheckForNull;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.Immutable;
+
+@Immutable
+public class SearchRequest {
+
+ private final Integer page;
+ private final Integer pageSize;
+ private final String query;
+ private final List<String> possibleFields;
+
+ private SearchRequest(Builder builder) {
+ this.page = builder.page;
+ this.pageSize = builder.pageSize;
+ this.query = builder.query;
+ this.possibleFields = builder.additionalFields;
+ }
+
+ @CheckForNull
+ public Integer getPage() {
+ return page;
+ }
+
+ @CheckForNull
+ public Integer getPageSize() {
+ return pageSize;
+ }
+
+ @CheckForNull
+ public String getQuery() {
+ return query;
+ }
+
+ public List<String> getPossibleFields() {
+ return possibleFields;
+ }
+
+ public static Builder builder() {
+ return new Builder();
+ }
+
+ public static class Builder {
+ private Integer page;
+ private Integer pageSize;
+ private String query;
+ private List<String> additionalFields = new ArrayList<>();
+
+ private Builder() {
+ // enforce factory method use
+ }
+
+ public Builder setPage(@Nullable Integer page) {
+ this.page = page;
+ return this;
+ }
+
+ public Builder setPageSize(@Nullable Integer pageSize) {
+ this.pageSize = pageSize;
+ return this;
+ }
+
+ public Builder setQuery(@Nullable String query) {
+ this.query = query;
+ return this;
+ }
+
+ public Builder setPossibleFields(List<String> possibleFields) {
+ this.additionalFields = possibleFields;
+ return this;
+ }
+
+ public SearchRequest build() {
+ return new SearchRequest(this);
+ }
+ }
+}
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersService.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersService.java
index d984c5ea55b..8467336d1c0 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersService.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersService.java
@@ -19,18 +19,22 @@
*/
package org.sonarqube.ws.client.user;
+import java.util.List;
import org.sonarqube.ws.WsUsers.CreateWsResponse;
import org.sonarqube.ws.WsUsers.GroupsWsResponse;
+import org.sonarqube.ws.WsUsers.SearchWsResponse;
import org.sonarqube.ws.client.BaseService;
import org.sonarqube.ws.client.GetRequest;
import org.sonarqube.ws.client.PostRequest;
import org.sonarqube.ws.client.WsConnector;
+import static org.sonar.api.server.ws.WebService.Param.FIELDS;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_CREATE;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_GROUPS;
+import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_SEARCH;
import static org.sonarqube.ws.client.user.UsersWsParameters.ACTION_UPDATE;
import static org.sonarqube.ws.client.user.UsersWsParameters.CONTROLLER_USERS;
import static org.sonarqube.ws.client.user.UsersWsParameters.PARAM_EMAIL;
@@ -48,6 +52,16 @@ public class UsersService extends BaseService {
super(wsConnector, CONTROLLER_USERS);
}
+ public SearchWsResponse search(SearchRequest request) {
+ List<String> additionalFields = request.getPossibleFields();
+ return call(new GetRequest(path(ACTION_SEARCH))
+ .setParam(PAGE, request.getPage())
+ .setParam(PAGE_SIZE, request.getPageSize())
+ .setParam(TEXT_QUERY, request.getQuery())
+ .setParam(FIELDS, !additionalFields.isEmpty() ? inlineMultipleParamValue(additionalFields) : null),
+ SearchWsResponse.parser());
+ }
+
public CreateWsResponse create(CreateRequest request) {
return call(new PostRequest(path(ACTION_CREATE))
.setParam(PARAM_LOGIN, request.getLogin())
diff --git a/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java
index ba674caf9d3..6f153218db9 100644
--- a/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java
+++ b/sonar-ws/src/main/java/org/sonarqube/ws/client/user/UsersWsParameters.java
@@ -23,6 +23,7 @@ public class UsersWsParameters {
public static final String CONTROLLER_USERS = "api/users";
+ public static final String ACTION_SEARCH = "search";
public static final String ACTION_CREATE = "create";
public static final String ACTION_UPDATE = "update";
public static final String ACTION_GROUPS = "groups";
diff --git a/sonar-ws/src/main/protobuf/ws-users.proto b/sonar-ws/src/main/protobuf/ws-users.proto
index d34882789a5..21aba165b08 100644
--- a/sonar-ws/src/main/protobuf/ws-users.proto
+++ b/sonar-ws/src/main/protobuf/ws-users.proto
@@ -26,6 +26,34 @@ option java_package = "org.sonarqube.ws";
option java_outer_classname = "WsUsers";
option optimize_for = SPEED;
+// WS api/users/search
+message SearchWsResponse {
+ optional sonarqube.ws.commons.Paging paging = 1;
+ repeated User users = 2;
+
+ message User {
+ optional string login = 1;
+ optional string name = 2;
+ optional bool active = 3;
+ optional string email = 4;
+ optional ScmAccounts scmAccounts = 5;
+ optional Groups groups = 6;
+ optional int32 tokensCount = 7;
+ optional bool local = 8;
+ optional string externalIdentity = 9;
+ optional string externalProvider = 10;
+ optional string avatar = 11;
+ }
+
+ message Groups {
+ repeated string groups = 1;
+ }
+
+ message ScmAccounts {
+ repeated string scmAccounts = 1;
+ }
+}
+
// WS api/users/identity_providers
message IdentityProvidersWsResponse {
repeated IdentityProvider identityProviders = 1;
diff --git a/sonar-ws/src/test/java/org/sonarqube/ws/client/user/UsersServiceTest.java b/sonar-ws/src/test/java/org/sonarqube/ws/client/user/UsersServiceTest.java
index c92f7619ffe..0933d16b26c 100644
--- a/sonar-ws/src/test/java/org/sonarqube/ws/client/user/UsersServiceTest.java
+++ b/sonar-ws/src/test/java/org/sonarqube/ws/client/user/UsersServiceTest.java
@@ -21,6 +21,7 @@ package org.sonarqube.ws.client.user;
import org.junit.Rule;
import org.junit.Test;
+import org.sonarqube.ws.WsUsers;
import org.sonarqube.ws.WsUsers.CreateWsResponse;
import org.sonarqube.ws.WsUsers.GroupsWsResponse;
import org.sonarqube.ws.client.ServiceTester;
@@ -29,6 +30,7 @@ import org.sonarqube.ws.client.WsConnector;
import static java.util.Arrays.asList;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
+import static org.sonar.api.server.ws.WebService.Param.FIELDS;
import static org.sonar.api.server.ws.WebService.Param.PAGE;
import static org.sonar.api.server.ws.WebService.Param.PAGE_SIZE;
import static org.sonar.api.server.ws.WebService.Param.TEXT_QUERY;
@@ -49,6 +51,24 @@ public class UsersServiceTest {
private UsersService underTest = serviceTester.getInstanceUnderTest();
@Test
+ public void search() {
+ underTest.search(SearchRequest.builder()
+ .setQuery("john")
+ .setPage(10)
+ .setPageSize(50)
+ .setPossibleFields(asList("email", "name"))
+ .build());
+
+ assertThat(serviceTester.getGetParser()).isSameAs(WsUsers.SearchWsResponse.parser());
+ serviceTester.assertThat(serviceTester.getGetRequest())
+ .hasParam(TEXT_QUERY, "john")
+ .hasParam(PAGE, 10)
+ .hasParam(PAGE_SIZE, 50)
+ .hasParam(FIELDS, "email,name")
+ .andNoOtherParam();
+ }
+
+ @Test
public void create() {
underTest.create(CreateRequest.builder()
.setLogin("john")