Add project index tests for both Lucene and ElasticSearch
Change-Id: I38da120ba7eb50c545cb17d1111d915dd39c5a2d
This commit is contained in:
@@ -0,0 +1,65 @@
|
||||
// Copyright (C) 2017 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.elasticsearch;
|
||||
|
||||
import com.google.gerrit.elasticsearch.ElasticTestUtils.ElasticNodeInfo;
|
||||
import com.google.gerrit.server.query.project.AbstractQueryProjectsTest;
|
||||
import com.google.gerrit.testutil.InMemoryModule;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import java.util.concurrent.ExecutionException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.After;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
|
||||
public class ElasticQueryProjectsTest extends AbstractQueryProjectsTest {
|
||||
private static ElasticNodeInfo nodeInfo;
|
||||
|
||||
@BeforeClass
|
||||
public static void startIndexService() throws InterruptedException, ExecutionException {
|
||||
if (nodeInfo != null) {
|
||||
// do not start Elasticsearch twice
|
||||
return;
|
||||
}
|
||||
nodeInfo = ElasticTestUtils.startElasticsearchNode();
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void stopElasticsearchServer() {
|
||||
if (nodeInfo != null) {
|
||||
nodeInfo.node.close();
|
||||
nodeInfo.elasticDir.delete();
|
||||
nodeInfo = null;
|
||||
}
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupIndex() {
|
||||
if (nodeInfo != null) {
|
||||
ElasticTestUtils.deleteAllIndexes(nodeInfo);
|
||||
ElasticTestUtils.createAllIndexes(nodeInfo);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
Config elasticsearchConfig = new Config(config);
|
||||
InMemoryModule.setDefaults(elasticsearchConfig);
|
||||
ElasticTestUtils.configure(elasticsearchConfig, nodeInfo.port);
|
||||
return Guice.createInjector(new InMemoryModule(elasticsearchConfig, notesMigration));
|
||||
}
|
||||
}
|
@@ -20,12 +20,14 @@ import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CHANGES_PREFIX;
|
||||
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.CLOSED_CHANGES;
|
||||
import static com.google.gerrit.elasticsearch.ElasticChangeIndex.OPEN_CHANGES;
|
||||
import static com.google.gerrit.elasticsearch.ElasticGroupIndex.GROUPS_PREFIX;
|
||||
import static com.google.gerrit.elasticsearch.ElasticProjectIndex.PROJECTS_PREFIX;
|
||||
|
||||
import com.google.common.base.Strings;
|
||||
import com.google.common.io.Files;
|
||||
import com.google.gerrit.elasticsearch.ElasticAccountIndex.AccountMapping;
|
||||
import com.google.gerrit.elasticsearch.ElasticChangeIndex.ChangeMapping;
|
||||
import com.google.gerrit.elasticsearch.ElasticGroupIndex.GroupMapping;
|
||||
import com.google.gerrit.elasticsearch.ElasticProjectIndex.ProjectMapping;
|
||||
import com.google.gerrit.index.Schema;
|
||||
import com.google.gerrit.server.account.AccountState;
|
||||
import com.google.gerrit.server.group.InternalGroup;
|
||||
@@ -33,6 +35,8 @@ import com.google.gerrit.server.index.IndexModule.IndexType;
|
||||
import com.google.gerrit.server.index.account.AccountSchemaDefinitions;
|
||||
import com.google.gerrit.server.index.change.ChangeSchemaDefinitions;
|
||||
import com.google.gerrit.server.index.group.GroupSchemaDefinitions;
|
||||
import com.google.gerrit.server.index.project.ProjectSchemaDefinitions;
|
||||
import com.google.gerrit.server.project.ProjectState;
|
||||
import com.google.gerrit.server.query.change.ChangeData;
|
||||
import com.google.gson.FieldNamingPolicy;
|
||||
import com.google.gson.Gson;
|
||||
@@ -157,6 +161,18 @@ final class ElasticTestUtils {
|
||||
.addMapping(ElasticGroupIndex.GROUPS, gson.toJson(groupMapping))
|
||||
.execute()
|
||||
.actionGet();
|
||||
|
||||
Schema<ProjectState> projectSchema = ProjectSchemaDefinitions.INSTANCE.getLatest();
|
||||
ProjectMapping projectMapping = new ProjectMapping(projectSchema);
|
||||
nodeInfo
|
||||
.node
|
||||
.client()
|
||||
.admin()
|
||||
.indices()
|
||||
.prepareCreate(String.format("%s%04d", PROJECTS_PREFIX, projectSchema.getVersion()))
|
||||
.addMapping(ElasticProjectIndex.PROJECTS, gson.toJson(projectMapping))
|
||||
.execute()
|
||||
.actionGet();
|
||||
}
|
||||
|
||||
private static String getHttpPort(Node node) throws InterruptedException, ExecutionException {
|
||||
|
@@ -27,7 +27,7 @@ import com.google.gerrit.server.project.ProjectState;
|
||||
public class ProjectField {
|
||||
|
||||
public static final FieldDef<ProjectState, String> NAME =
|
||||
exact("name").build(p -> p.getProject().getName());
|
||||
exact("name").stored().build(p -> p.getProject().getName());
|
||||
|
||||
public static final FieldDef<ProjectState, String> DESCRIPTION =
|
||||
fullText("description").build(p -> p.getProject().getDescription());
|
||||
|
@@ -0,0 +1,326 @@
|
||||
// Copyright (C) 2017 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.query.project;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
import static java.util.stream.Collectors.toList;
|
||||
|
||||
import com.google.gerrit.extensions.api.GerritApi;
|
||||
import com.google.gerrit.extensions.api.projects.ProjectInput;
|
||||
import com.google.gerrit.extensions.api.projects.Projects.QueryRequest;
|
||||
import com.google.gerrit.extensions.common.AccountInfo;
|
||||
import com.google.gerrit.extensions.common.ProjectInfo;
|
||||
import com.google.gerrit.lifecycle.LifecycleManager;
|
||||
import com.google.gerrit.reviewdb.client.Account;
|
||||
import com.google.gerrit.reviewdb.client.Project;
|
||||
import com.google.gerrit.reviewdb.server.ReviewDb;
|
||||
import com.google.gerrit.server.AnonymousUser;
|
||||
import com.google.gerrit.server.CurrentUser;
|
||||
import com.google.gerrit.server.IdentifiedUser;
|
||||
import com.google.gerrit.server.account.AccountCache;
|
||||
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.config.AllProjectsName;
|
||||
import com.google.gerrit.server.query.account.InternalAccountQuery;
|
||||
import com.google.gerrit.server.schema.SchemaCreator;
|
||||
import com.google.gerrit.server.util.ManualRequestContext;
|
||||
import com.google.gerrit.server.util.OneOffRequestContext;
|
||||
import com.google.gerrit.server.util.RequestContext;
|
||||
import com.google.gerrit.server.util.ThreadLocalRequestContext;
|
||||
import com.google.gerrit.testutil.ConfigSuite;
|
||||
import com.google.gerrit.testutil.GerritServerTests;
|
||||
import com.google.gerrit.testutil.InMemoryDatabase;
|
||||
import com.google.inject.Inject;
|
||||
import com.google.inject.Injector;
|
||||
import com.google.inject.Provider;
|
||||
import com.google.inject.util.Providers;
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.Test;
|
||||
|
||||
@Ignore
|
||||
public abstract class AbstractQueryProjectsTest extends GerritServerTests {
|
||||
@ConfigSuite.Default
|
||||
public static Config defaultConfig() {
|
||||
Config cfg = new Config();
|
||||
cfg.setInt("index", null, "maxPages", 10);
|
||||
return cfg;
|
||||
}
|
||||
|
||||
@Inject protected Accounts accounts;
|
||||
|
||||
@Inject protected AccountsUpdate.Server accountsUpdate;
|
||||
|
||||
@Inject protected AccountCache accountCache;
|
||||
|
||||
@Inject protected AccountManager accountManager;
|
||||
|
||||
@Inject protected GerritApi gApi;
|
||||
|
||||
@Inject protected IdentifiedUser.GenericFactory userFactory;
|
||||
|
||||
@Inject private Provider<AnonymousUser> anonymousUser;
|
||||
|
||||
@Inject protected InMemoryDatabase schemaFactory;
|
||||
|
||||
@Inject protected SchemaCreator schemaCreator;
|
||||
|
||||
@Inject protected ThreadLocalRequestContext requestContext;
|
||||
|
||||
@Inject protected OneOffRequestContext oneOffRequestContext;
|
||||
|
||||
@Inject protected InternalAccountQuery internalAccountQuery;
|
||||
|
||||
@Inject protected AllProjectsName allProjects;
|
||||
|
||||
protected LifecycleManager lifecycle;
|
||||
protected Injector injector;
|
||||
protected ReviewDb db;
|
||||
protected AccountInfo currentUserInfo;
|
||||
protected CurrentUser user;
|
||||
|
||||
protected abstract Injector createInjector();
|
||||
|
||||
@Before
|
||||
public void setUpInjector() throws Exception {
|
||||
lifecycle = new LifecycleManager();
|
||||
injector = createInjector();
|
||||
lifecycle.add(injector);
|
||||
injector.injectMembers(this);
|
||||
lifecycle.start();
|
||||
setUpDatabase();
|
||||
}
|
||||
|
||||
protected void setUpDatabase() throws Exception {
|
||||
db = schemaFactory.open();
|
||||
schemaCreator.create(db);
|
||||
|
||||
Account.Id userId = createAccount("user", "User", "user@example.com", true);
|
||||
user = userFactory.create(userId);
|
||||
requestContext.setContext(newRequestContext(userId));
|
||||
currentUserInfo = gApi.accounts().id(userId.get()).get();
|
||||
}
|
||||
|
||||
protected RequestContext newRequestContext(Account.Id requestUserId) {
|
||||
final CurrentUser requestUser = userFactory.create(requestUserId);
|
||||
return new RequestContext() {
|
||||
@Override
|
||||
public CurrentUser getUser() {
|
||||
return requestUser;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider<ReviewDb> getReviewDbProvider() {
|
||||
return Providers.of(db);
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
protected void setAnonymous() {
|
||||
requestContext.setContext(
|
||||
new RequestContext() {
|
||||
@Override
|
||||
public CurrentUser getUser() {
|
||||
return anonymousUser.get();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Provider<ReviewDb> getReviewDbProvider() {
|
||||
return Providers.of(db);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDownInjector() {
|
||||
if (lifecycle != null) {
|
||||
lifecycle.stop();
|
||||
}
|
||||
requestContext.setContext(null);
|
||||
if (db != null) {
|
||||
db.close();
|
||||
}
|
||||
InMemoryDatabase.drop(schemaFactory);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void byName() throws Exception {
|
||||
assertQuery("name:project");
|
||||
assertQuery("name:non-existing");
|
||||
|
||||
ProjectInfo project = createProject(name("project"));
|
||||
|
||||
assertQuery("name:" + project.name, project);
|
||||
|
||||
// only exact match
|
||||
ProjectInfo projectWithHyphen = createProject(name("project-with-hyphen"));
|
||||
createProject(name("project-no-match-with-hyphen"));
|
||||
assertQuery("name:" + projectWithHyphen.name, projectWithHyphen);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withLimit() throws Exception {
|
||||
ProjectInfo project1 = createProject(name("project1"));
|
||||
ProjectInfo project2 = createProject(name("project2"));
|
||||
ProjectInfo project3 = createProject(name("project3"));
|
||||
|
||||
String query =
|
||||
"name:" + project1.name + " OR name:" + project2.name + " OR name:" + project3.name;
|
||||
List<ProjectInfo> result = assertQuery(query, project1, project2, project3);
|
||||
|
||||
result = assertQuery(newQuery(query).withLimit(2), result.subList(0, 2));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void withStart() throws Exception {
|
||||
ProjectInfo project1 = createProject(name("project1"));
|
||||
ProjectInfo project2 = createProject(name("project2"));
|
||||
ProjectInfo project3 = createProject(name("project3"));
|
||||
|
||||
String query =
|
||||
"name:" + project1.name + " OR name:" + project2.name + " OR name:" + project3.name;
|
||||
List<ProjectInfo> result = assertQuery(query, project1, project2, project3);
|
||||
|
||||
assertQuery(newQuery(query).withStart(1), result.subList(1, 3));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void asAnonymous() throws Exception {
|
||||
ProjectInfo project = createProject(name("project"));
|
||||
|
||||
setAnonymous();
|
||||
assertQuery("name:" + project.name);
|
||||
}
|
||||
|
||||
private Account.Id createAccount(String username, String fullName, String email, boolean active)
|
||||
throws Exception {
|
||||
try (ManualRequestContext ctx = oneOffRequestContext.open()) {
|
||||
Account.Id id = accountManager.authenticate(AuthRequest.forUser(username)).getAccountId();
|
||||
if (email != null) {
|
||||
accountManager.link(id, AuthRequest.forEmail(email));
|
||||
}
|
||||
accountsUpdate
|
||||
.create()
|
||||
.update(
|
||||
id,
|
||||
a -> {
|
||||
a.setFullName(fullName);
|
||||
a.setPreferredEmail(email);
|
||||
a.setActive(active);
|
||||
});
|
||||
return id;
|
||||
}
|
||||
}
|
||||
|
||||
protected ProjectInfo createProject(String name) throws Exception {
|
||||
ProjectInput in = new ProjectInput();
|
||||
in.name = name;
|
||||
return gApi.projects().create(in).get();
|
||||
}
|
||||
|
||||
protected ProjectInfo createProjectWithDescription(String name, String description)
|
||||
throws Exception {
|
||||
ProjectInput in = new ProjectInput();
|
||||
in.name = name;
|
||||
in.description = description;
|
||||
return gApi.projects().create(in).get();
|
||||
}
|
||||
|
||||
protected ProjectInfo getProject(Project.NameKey nameKey) throws Exception {
|
||||
return gApi.projects().name(nameKey.get()).get();
|
||||
}
|
||||
|
||||
protected List<ProjectInfo> assertQuery(Object query, ProjectInfo... projects) throws Exception {
|
||||
return assertQuery(newQuery(query), projects);
|
||||
}
|
||||
|
||||
protected List<ProjectInfo> assertQuery(QueryRequest query, ProjectInfo... projects)
|
||||
throws Exception {
|
||||
return assertQuery(query, Arrays.asList(projects));
|
||||
}
|
||||
|
||||
protected List<ProjectInfo> assertQuery(QueryRequest query, List<ProjectInfo> projects)
|
||||
throws Exception {
|
||||
List<ProjectInfo> result = query.get();
|
||||
Iterable<String> names = names(result);
|
||||
assertThat(names)
|
||||
.named(format(query, result, projects))
|
||||
.containsExactlyElementsIn(names(projects));
|
||||
return result;
|
||||
}
|
||||
|
||||
protected QueryRequest newQuery(Object query) {
|
||||
return gApi.projects().query(query.toString());
|
||||
}
|
||||
|
||||
protected String format(
|
||||
QueryRequest query, List<ProjectInfo> actualProjects, List<ProjectInfo> expectedProjects) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append("query '").append(query.getQuery()).append("' with expected projects ");
|
||||
b.append(format(expectedProjects));
|
||||
b.append(" and result ");
|
||||
b.append(format(actualProjects));
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
protected String format(Iterable<ProjectInfo> projects) {
|
||||
StringBuilder b = new StringBuilder();
|
||||
b.append("[");
|
||||
Iterator<ProjectInfo> it = projects.iterator();
|
||||
while (it.hasNext()) {
|
||||
ProjectInfo p = it.next();
|
||||
b.append("{")
|
||||
.append(p.id)
|
||||
.append(", ")
|
||||
.append("name=")
|
||||
.append(p.name)
|
||||
.append(", ")
|
||||
.append("parent=")
|
||||
.append(p.parent)
|
||||
.append(", ")
|
||||
.append("description=")
|
||||
.append(p.description)
|
||||
.append("}");
|
||||
if (it.hasNext()) {
|
||||
b.append(", ");
|
||||
}
|
||||
}
|
||||
b.append("]");
|
||||
return b.toString();
|
||||
}
|
||||
|
||||
protected static Iterable<String> names(ProjectInfo... projects) {
|
||||
return names(Arrays.asList(projects));
|
||||
}
|
||||
|
||||
protected static Iterable<String> names(List<ProjectInfo> projects) {
|
||||
return projects.stream().map(p -> p.name).collect(toList());
|
||||
}
|
||||
|
||||
protected String name(String name) {
|
||||
if (name == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return name + "_" + getSanitizedMethodName();
|
||||
}
|
||||
}
|
@@ -0,0 +1,44 @@
|
||||
// Copyright (C) 2017 The Android Open Source Project
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
package com.google.gerrit.server.query.project;
|
||||
|
||||
import com.google.gerrit.server.index.project.ProjectSchemaDefinitions;
|
||||
import com.google.gerrit.testutil.ConfigSuite;
|
||||
import com.google.gerrit.testutil.InMemoryModule;
|
||||
import com.google.gerrit.testutil.IndexVersions;
|
||||
import com.google.inject.Guice;
|
||||
import com.google.inject.Injector;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
|
||||
public class LuceneQueryProjectsTest extends AbstractQueryProjectsTest {
|
||||
@ConfigSuite.Configs
|
||||
public static Map<String, Config> againstPreviousIndexVersion() {
|
||||
// the current schema version is already tested by the inherited default config suite
|
||||
List<Integer> schemaVersions =
|
||||
IndexVersions.getWithoutLatest(
|
||||
com.google.gerrit.server.index.project.ProjectSchemaDefinitions.INSTANCE);
|
||||
return IndexVersions.asConfigMap(
|
||||
ProjectSchemaDefinitions.INSTANCE, schemaVersions, "againstIndexVersion", defaultConfig());
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Injector createInjector() {
|
||||
Config luceneConfig = new Config(config);
|
||||
InMemoryModule.setDefaults(luceneConfig);
|
||||
return Guice.createInjector(new InMemoryModule(luceneConfig, notesMigration));
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user