Merge "Accounts API: Add methods to suggest accounts"

This commit is contained in:
Edwin Kempin
2015-02-23 09:19:19 +00:00
committed by Gerrit Code Review
4 changed files with 138 additions and 4 deletions

View File

@@ -22,6 +22,8 @@ import com.google.gerrit.extensions.common.AccountInfo;
import org.junit.Test;
import java.util.List;
public class AccountIT extends AbstractDaemonTest {
@Test
@@ -59,4 +61,21 @@ public class AccountIT extends AbstractDaemonTest {
.unstarChange(triplet);
assertThat(getChange(triplet).starred).isNull();
}
@Test
public void suggestAccounts() throws Exception {
String adminUsername = "admin";
List<AccountInfo> result = gApi.accounts()
.suggestAccounts().withQuery(adminUsername).get();
assertThat(result.size()).is(1);
assertThat(result.get(0).username.equals(adminUsername));
List<AccountInfo> resultShortcutApi = gApi.accounts()
.suggestAccounts(adminUsername).get();
assertThat(resultShortcutApi.size()).is(result.size());
List<AccountInfo> emptyResult = gApi.accounts()
.suggestAccounts("unknown").get();
assertThat(emptyResult).isEmpty();
}
}

View File

@@ -14,9 +14,12 @@
package com.google.gerrit.extensions.api.accounts;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.restapi.NotImplementedException;
import com.google.gerrit.extensions.restapi.RestApiException;
import java.util.List;
public interface Accounts {
/**
* Look up an account by ID.
@@ -41,6 +44,69 @@ public interface Accounts {
*/
AccountApi self() throws RestApiException;
/**
* Suggest users for a given query.
* <p>
* Example code:
* {@code suggestAccounts().withQuery("Reviewer").withLimit(5).get()}
*
* @return API for setting parameters and getting result.
*/
SuggestAccountsRequest suggestAccounts() throws RestApiException;
/**
* Suggest users for a given query.
* <p>
* Shortcut API for {@code suggestAccounts().withQuery(String)}.
*
* @see #suggestAccounts()
*/
SuggestAccountsRequest suggestAccounts(String query)
throws RestApiException;
/**
* API for setting parameters and getting result.
* Used for {@code suggestAccounts()}.
*
* @see #suggestAccounts()
*/
public abstract class SuggestAccountsRequest {
private String query;
private int limit;
/**
* Executes query and returns a list of accounts.
*/
public abstract List<AccountInfo> get() throws RestApiException;
/**
* Set query.
*
* @param query needs to be in human-readable form.
*/
public SuggestAccountsRequest withQuery(String query) {
this.query = query;
return this;
}
/**
* Set limit for returned list of accounts.
* Optional; server-default is used when not provided.
*/
public SuggestAccountsRequest withLimit(int limit) {
this.limit = limit;
return this;
}
public String getQuery() {
return query;
}
public int getLimit() {
return limit;
}
}
/**
* A default implementation which allows source compatibility
* when adding new methods to the interface.
@@ -55,5 +121,16 @@ public interface Accounts {
public AccountApi self() throws RestApiException {
throw new NotImplementedException();
}
@Override
public SuggestAccountsRequest suggestAccounts() throws RestApiException {
throw new NotImplementedException();
}
@Override
public SuggestAccountsRequest suggestAccounts(String query)
throws RestApiException {
throw new NotImplementedException();
}
}
}

View File

@@ -39,7 +39,7 @@ import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
class SuggestAccounts implements RestReadView<TopLevelResource> {
public class SuggestAccounts implements RestReadView<TopLevelResource> {
private static final int MAX_RESULTS = 100;
private static final String MAX_SUFFIX = "\u9fa5";
@@ -50,9 +50,10 @@ class SuggestAccounts implements RestReadView<TopLevelResource> {
private final int suggestFrom;
private int limit = 10;
private String query;
@Option(name = "--limit", aliases = {"-n"}, metaVar = "CNT", usage = "maximum number of users to return")
void setLimit(int n) {
public void setLimit(int n) {
if (n < 0) {
limit = 10;
} else if (n == 0) {
@@ -63,7 +64,9 @@ class SuggestAccounts implements RestReadView<TopLevelResource> {
}
@Option(name = "--query", aliases = {"-q"}, metaVar = "QUERY", usage = "match users")
private String query;
public void setQuery(String query) {
this.query = query;
}
@Inject
SuggestAccounts(AccountControl.Factory accountControlFactory,

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.server.api.accounts;
import com.google.gerrit.extensions.api.accounts.AccountApi;
import com.google.gerrit.extensions.api.accounts.Accounts;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.IdString;
import com.google.gerrit.extensions.restapi.RestApiException;
@@ -24,24 +25,30 @@ import com.google.gerrit.server.CurrentUser;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.account.AccountsCollection;
import com.google.gerrit.server.account.SuggestAccounts;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.util.List;
@Singleton
public class AccountsImpl extends Accounts.NotImplemented implements Accounts {
private final AccountsCollection accounts;
private final AccountApiImpl.Factory api;
private final Provider<CurrentUser> self;
private final Provider<SuggestAccounts> suggestAccountsProvider;
@Inject
AccountsImpl(AccountsCollection accounts,
AccountApiImpl.Factory api,
Provider<CurrentUser> self) {
Provider<CurrentUser> self,
Provider<SuggestAccounts> suggestAccountsProvider) {
this.accounts = accounts;
this.api = api;
this.self = self;
this.suggestAccountsProvider = suggestAccountsProvider;
}
@Override
@@ -61,4 +68,32 @@ public class AccountsImpl extends Accounts.NotImplemented implements Accounts {
}
return api.create(new AccountResource((IdentifiedUser)self.get()));
}
@Override
public SuggestAccountsRequest suggestAccounts() throws RestApiException {
return new SuggestAccountsRequest() {
@Override
public List<AccountInfo> get() throws RestApiException {
return AccountsImpl.this.suggestAccounts(this);
}
};
}
@Override
public SuggestAccountsRequest suggestAccounts(String query)
throws RestApiException {
return suggestAccounts().withQuery(query);
}
private List<AccountInfo> suggestAccounts(SuggestAccountsRequest r)
throws RestApiException {
try {
SuggestAccounts mySuggestAccounts = suggestAccountsProvider.get();
mySuggestAccounts.setQuery(r.getQuery());
mySuggestAccounts.setLimit(r.getLimit());
return mySuggestAccounts.apply(TopLevelResource.INSTANCE);
} catch (OrmException e) {
throw new RestApiException("Cannot retrieve suggested accounts", e);
}
}
}