VersionedAccountQueries: Support writing queries file
Support for writing queries in the VersionedAccountQueries is a preparatory step before adding support for managing user queries in the account API. Implement the onSave method which writes the contents of the current query list to the queries file, and add a new method to set the query list from text. Modify AbstractQueryChangesTest#userQuery to use the new functionality to set the test queries, thus implicitly adding test coverage of it. AbstractQueryChangesTest is not the appropriate place to fully test this functionality, so we don't test the handling of invalid input and other uses cases. This will be added when we later extend the account API. Change-Id: I5bca1bae6b0b7cf295a4ce9591d452a4ded3e5ee
This commit is contained in:
@@ -14,11 +14,17 @@
|
||||
|
||||
package com.google.gerrit.server.account;
|
||||
|
||||
import static java.util.stream.Collectors.joining;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.flogger.FluentLogger;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.RefNames;
|
||||
import com.google.gerrit.server.git.ValidationError;
|
||||
import com.google.gerrit.server.git.meta.VersionedMetaData;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||
import org.eclipse.jgit.lib.CommitBuilder;
|
||||
|
||||
@@ -46,6 +52,16 @@ public class VersionedAccountQueries extends VersionedMetaData {
|
||||
return queryList;
|
||||
}
|
||||
|
||||
public void setQueryList(String text) throws IOException, ConfigInvalidException {
|
||||
List<ValidationError> errors = new ArrayList<>();
|
||||
QueryList newQueryList = QueryList.parse(text, error -> errors.add(error));
|
||||
if (!errors.isEmpty()) {
|
||||
String messages = errors.stream().map(ValidationError::getMessage).collect(joining(", "));
|
||||
throw new ConfigInvalidException("Invalid named queries: " + messages);
|
||||
}
|
||||
queryList = newQueryList;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onLoad() throws IOException, ConfigInvalidException {
|
||||
queryList =
|
||||
@@ -58,6 +74,10 @@ public class VersionedAccountQueries extends VersionedMetaData {
|
||||
|
||||
@Override
|
||||
protected boolean onSave(CommitBuilder commit) throws IOException, ConfigInvalidException {
|
||||
throw new UnsupportedOperationException("Cannot yet save named queries");
|
||||
if (Strings.isNullOrEmpty(commit.getMessage())) {
|
||||
commit.setMessage("Updated named queries\n");
|
||||
}
|
||||
saveUTF8(QueryList.FILE_NAME, queryList.asText());
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
@@ -91,6 +91,7 @@ import com.google.gerrit.server.account.AccountManager;
|
||||
import com.google.gerrit.server.account.Accounts;
|
||||
import com.google.gerrit.server.account.AccountsUpdate;
|
||||
import com.google.gerrit.server.account.AuthRequest;
|
||||
import com.google.gerrit.server.account.VersionedAccountQueries;
|
||||
import com.google.gerrit.server.account.externalids.ExternalId;
|
||||
import com.google.gerrit.server.change.ChangeInserter;
|
||||
import com.google.gerrit.server.change.ChangeTriplet;
|
||||
@@ -2953,19 +2954,19 @@ public abstract class AbstractQueryChangesTest extends GerritServerTests {
|
||||
Change change1 = insert(repo, newChange(repo));
|
||||
Change change2 = insert(repo, newChangeForBranch(repo, "stable"));
|
||||
|
||||
String queries =
|
||||
String queryListText =
|
||||
"query1\tproject:repo\n"
|
||||
+ "query2\tproject:repo status:open\n"
|
||||
+ "query3\tproject:repo branch:stable\n"
|
||||
+ "query4\tproject:repo branch:other";
|
||||
|
||||
try (TestRepository<Repo> allUsers =
|
||||
new TestRepository<>(repoManager.openRepository(allUsersName))) {
|
||||
String refsUsers = RefNames.refsUsers(userId);
|
||||
allUsers.branch(refsUsers).commit().add("queries", queries).create();
|
||||
|
||||
Ref userRef = allUsers.getRepository().exactRef(refsUsers);
|
||||
assertThat(userRef).isNotNull();
|
||||
new TestRepository<>(repoManager.openRepository(allUsersName));
|
||||
MetaDataUpdate md = metaDataUpdateFactory.create(allUsersName)) {
|
||||
VersionedAccountQueries queries = VersionedAccountQueries.forUser(userId);
|
||||
queries.load(md);
|
||||
queries.setQueryList(queryListText);
|
||||
queries.commit(md);
|
||||
}
|
||||
|
||||
assertThatQueryException("query:foo").hasMessageThat().isEqualTo("Unknown named query: foo");
|
||||
|
Reference in New Issue
Block a user