Move GroupRebuilder and GroupBundle into schema package

GroupRebuilder and GroupBundle are supposed to be only used by schema
migrations. Make sure that they are not used otherwise by moving them
into the schema package.

Change-Id: I094043259720edec9b60309f0ec0535bf0505d9e
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-03-20 15:12:43 +01:00
parent e41ac7a198
commit 44cb0fd77a
11 changed files with 256 additions and 90 deletions

View File

@@ -46,13 +46,13 @@ import org.slf4j.LoggerFactory;
/** NoteDb reader for group audit log. */
@Singleton
class AuditLogReader {
public class AuditLogReader {
private static final Logger log = LoggerFactory.getLogger(AuditLogReader.class);
private final String serverId;
@Inject
AuditLogReader(@GerritServerId String serverId) {
public AuditLogReader(@GerritServerId String serverId) {
this.serverId = serverId;
}
@@ -60,8 +60,8 @@ class AuditLogReader {
// ReviewDb. Once ReviewDb is gone, the audit record interface becomes more flexible and we can
// revisit this, e.g. to do only a single walk, or even change the record types.
ImmutableList<AccountGroupMemberAudit> getMembersAudit(Repository repo, AccountGroup.UUID uuid)
throws IOException, ConfigInvalidException {
public ImmutableList<AccountGroupMemberAudit> getMembersAudit(
Repository repo, AccountGroup.UUID uuid) throws IOException, ConfigInvalidException {
return getMembersAudit(getGroupId(repo, uuid), parseCommits(repo, uuid));
}
@@ -97,8 +97,8 @@ class AuditLogReader {
return result.build();
}
ImmutableList<AccountGroupByIdAud> getSubgroupsAudit(Repository repo, AccountGroup.UUID uuid)
throws IOException, ConfigInvalidException {
public ImmutableList<AccountGroupByIdAud> getSubgroupsAudit(
Repository repo, AccountGroup.UUID uuid) throws IOException, ConfigInvalidException {
return getSubgroupsAudit(getGroupId(repo, uuid), parseCommits(repo, uuid));
}

View File

@@ -228,7 +228,7 @@ public class GroupConfig extends VersionedMetaData {
* which don't always necessarily have a name. Nowadays, we enforce that groups always have names.
* When we remove the migration code, we can probably remove this method as well.
*/
void setAllowSaveEmptyName() {
public void setAllowSaveEmptyName() {
this.allowSaveEmptyName = true;
}
@@ -243,7 +243,7 @@ public class GroupConfig extends VersionedMetaData {
}
@Override
protected String getRefName() {
public String getRefName() {
return ref;
}

View File

@@ -145,7 +145,7 @@ public abstract class InternalGroupUpdate {
* #setMemberModification(MemberModification)} in order to combine multiple member additions,
* deletions, or other modifications into one update.
*/
abstract MemberModification getMemberModification();
public abstract MemberModification getMemberModification();
/** @see #getSubgroupModification() */
public abstract Builder setSubgroupModification(SubgroupModification subgroupModification);
@@ -158,7 +158,7 @@ public abstract class InternalGroupUpdate {
* #setSubgroupModification(SubgroupModification)} in order to combine multiple subgroup
* additions, deletions, or other modifications into one update.
*/
abstract SubgroupModification getSubgroupModification();
public abstract SubgroupModification getSubgroupModification();
/** @see #getUpdatedOn() */
public abstract Builder setUpdatedOn(Timestamp timestamp);

View File

@@ -14,48 +14,17 @@
package com.google.gerrit.server.group.db.testing;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.git.CommitUtil;
import com.google.gerrit.server.git.GitRepositoryManager;
import java.io.IOException;
import org.eclipse.jgit.junit.TestRepository;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk;
/** Test utilities for low-level NoteDb groups. */
public class GroupTestUtil {
// TODO(dborowitz): Move somewhere even more common.
public static ImmutableList<CommitInfo> log(Repository repo, String refName) throws Exception {
try (RevWalk rw = new RevWalk(repo)) {
Ref ref = repo.exactRef(refName);
if (ref != null) {
rw.sort(RevSort.REVERSE);
rw.markStart(rw.parseCommit(ref.getObjectId()));
return Streams.stream(rw)
.map(
c -> {
try {
return CommitUtil.toCommitInfo(c);
} catch (IOException e) {
throw new IllegalStateException(
"unexpected state when converting commit " + c.getName(), e);
}
})
.collect(toImmutableList());
}
}
return ImmutableList.of();
}
public static void updateGroupFile(
GitRepositoryManager repoManager,
AllUsersName allUsersName,

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.group.db;
package com.google.gerrit.server.schema;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.collect.ImmutableList.toImmutableList;
@@ -40,6 +40,8 @@ import com.google.gerrit.reviewdb.client.AccountGroupMemberAudit;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.reviewdb.server.ReviewDbWrapper;
import com.google.gerrit.server.group.InternalGroup;
import com.google.gerrit.server.group.db.AuditLogReader;
import com.google.gerrit.server.group.db.GroupConfig;
import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject;
@@ -68,7 +70,7 @@ import org.slf4j.LoggerFactory;
* instead.
*/
@AutoValue
public abstract class GroupBundle {
abstract class GroupBundle {
private static final Logger log = LoggerFactory.getLogger(GroupBundle.class);
static {

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.group.db;
package com.google.gerrit.server.schema;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkState;
@@ -35,6 +35,10 @@ import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.VersionedMetaData.BatchMetaDataUpdate;
import com.google.gerrit.server.group.db.AuditLogFormatter;
import com.google.gerrit.server.group.db.GroupConfig;
import com.google.gerrit.server.group.db.InternalGroupCreation;
import com.google.gerrit.server.group.db.InternalGroupUpdate;
import com.google.gerrit.server.group.db.InternalGroupUpdate.MemberModification;
import com.google.gerrit.server.group.db.InternalGroupUpdate.SubgroupModification;
import com.google.gwtorm.server.OrmDuplicateKeyException;
@@ -54,7 +58,7 @@ import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
/** Helper for rebuilding an entire group's NoteDb refs. */
public class GroupRebuilder {
class GroupRebuilder {
private final PersonIdent serverIdent;
private final AllUsersName allUsers;
private final AuditLogFormatter auditLogFormatter;

View File

@@ -0,0 +1,51 @@
// Copyright (C) 2018 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.testing;
import static com.google.common.collect.ImmutableList.toImmutableList;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Streams;
import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.server.git.CommitUtil;
import java.io.IOException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk;
public class GitTestUtil {
public static ImmutableList<CommitInfo> log(Repository repo, String refName) throws Exception {
try (RevWalk rw = new RevWalk(repo)) {
Ref ref = repo.exactRef(refName);
if (ref != null) {
rw.sort(RevSort.REVERSE);
rw.markStart(rw.parseCommit(ref.getObjectId()));
return Streams.stream(rw)
.map(
c -> {
try {
return CommitUtil.toCommitInfo(c);
} catch (IOException e) {
throw new IllegalStateException(
"unexpected state when converting commit " + c.getName(), e);
}
})
.collect(toImmutableList());
}
}
return ImmutableList.of();
}
}

View File

@@ -34,8 +34,8 @@ import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.config.AllUsersNameProvider;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.group.db.testing.GroupTestUtil;
import com.google.gerrit.server.update.RefUpdateUtil;
import com.google.gerrit.testing.GitTestUtil;
import com.google.gerrit.testing.TestTimeUtil;
import com.google.gerrit.truth.ListSubject;
import com.google.gerrit.truth.OptionalSubject;
@@ -572,7 +572,7 @@ public class GroupNameNotesTest {
}
private ImmutableList<CommitInfo> log() throws Exception {
return GroupTestUtil.log(repo, REFS_GROUPNAMES);
return GitTestUtil.log(repo, REFS_GROUPNAMES);
}
private String readNameNote(GroupReference g) throws Exception {

View File

@@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.group.db;
package com.google.gerrit.server.schema;
import static com.google.common.truth.Truth.assertThat;
@@ -23,7 +23,7 @@ import com.google.gerrit.reviewdb.client.AccountGroupById;
import com.google.gerrit.reviewdb.client.AccountGroupByIdAud;
import com.google.gerrit.reviewdb.client.AccountGroupMember;
import com.google.gerrit.reviewdb.client.AccountGroupMemberAudit;
import com.google.gerrit.server.group.db.GroupBundle.Source;
import com.google.gerrit.server.schema.GroupBundle.Source;
import com.google.gerrit.testing.GerritBaseTests;
import com.google.gerrit.testing.TestTimeUtil;
import java.sql.Timestamp;

View File

@@ -12,39 +12,53 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.acceptance.api.group;
package com.google.gerrit.server.schema;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.TruthJUnit.assume;
import static com.google.gerrit.extensions.common.testing.CommitInfoSubject.assertThat;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.acceptance.AbstractDaemonTest;
import com.google.gerrit.acceptance.NoHttpd;
import com.google.gerrit.acceptance.TestAccount;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.extensions.api.GerritApi;
import com.google.gerrit.extensions.api.accounts.AccountInput;
import com.google.gerrit.extensions.common.AccountInfo;
import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.extensions.common.GroupInfo;
import com.google.gerrit.extensions.restapi.RestApiException;
import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.AccountGroupById;
import com.google.gerrit.reviewdb.client.AccountGroupByIdAud;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.ServerInitiated;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountsUpdate;
import com.google.gerrit.server.account.GroupBackend;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.config.GerritServerId;
import com.google.gerrit.server.config.GerritServerIdProvider;
import com.google.gerrit.server.git.CommitUtil;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.group.db.AuditLogFormatter;
import com.google.gerrit.server.group.db.GroupBundle;
import com.google.gerrit.server.group.db.GroupRebuilder;
import com.google.gerrit.server.notedb.GroupsMigration;
import com.google.gerrit.testing.GerritBaseTests;
import com.google.gerrit.testing.InMemoryTestEnvironment;
import com.google.gerrit.testing.TestTimeUtil;
import com.google.gerrit.testing.TestTimeUtil.TempClockStep;
import com.google.inject.Inject;
import com.google.inject.Provider;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.RefUpdate;
import org.eclipse.jgit.lib.Repository;
@@ -53,25 +67,48 @@ import org.eclipse.jgit.revwalk.RevSort;
import org.eclipse.jgit.revwalk.RevWalk;
import org.junit.After;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@NoHttpd
public class GroupRebuilderIT extends AbstractDaemonTest {
@Inject @GerritServerId private String serverId;
@Inject private GroupBundle.Factory bundleFactory;
public class GroupRebuilderIT extends GerritBaseTests {
private static Config createConfigWithServerId() {
Config config = new Config();
config.setString(GerritServerIdProvider.SECTION, null, GerritServerIdProvider.KEY, "1234567");
return config;
}
@Rule
public InMemoryTestEnvironment testEnv =
new InMemoryTestEnvironment(GroupRebuilderIT::createConfigWithServerId);
@Inject private GroupsMigration migration;
@Inject private GerritApi gApi;
@Inject private ReviewDb db;
@Inject private GitRepositoryManager repoManager;
@Inject private AllUsersName allUsersName;
@Inject private IdentifiedUser currentUser;
@Inject private @GerritServerId String serverId;
@Inject private AccountCache accountCache;
@Inject private @ServerInitiated AccountsUpdate accountsUpdate;
@Inject private GroupBackend groupBackend;
@Inject private GroupBundle.Factory bundleFactory;
@Inject private @GerritPersonIdent Provider<PersonIdent> serverIdent;
private GroupRebuilder rebuilder;
@Before
public void setUp() {
public void setup() throws Exception {
// This test is explicitly testing the migration from ReviewDb to NoteDb, and handles reading
// from NoteDb manually. It should work regardless of the value of noteDb.groups.write, however.
assume().that(migration.readFromNoteDb()).isFalse();
accountsUpdate.update(
"Set Name for CurrentUser", currentUser.getAccountId(), u -> u.setFullName("current"));
AuditLogFormatter auditLogFormatter =
AuditLogFormatter.createBackedBy(accountCache, groupBackend, serverId);
rebuilder = new GroupRebuilder(serverIdent.get(), allUsers, auditLogFormatter);
rebuilder = new GroupRebuilder(serverIdent.get(), allUsersName, auditLogFormatter);
}
@Before
@@ -86,22 +123,25 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
@Test
public void basicGroupProperties() throws Exception {
GroupInfo createdGroup = gApi.groups().create(name("group")).get();
GroupInfo createdGroup = gApi.groups().create("group").get();
GroupBundle reviewDbBundle =
GroupBundle.Factory.fromReviewDb(db, new AccountGroup.UUID(createdGroup.id));
deleteGroupRefs(reviewDbBundle);
deleteGroupRefs(reviewDbBundle);
assertMigratedCleanly(rebuild(reviewDbBundle), reviewDbBundle);
}
@Test
public void logFormat() throws Exception {
TestAccount user2 = accountCreator.user2();
GroupInfo group1 = gApi.groups().create(name("group1")).get();
GroupInfo group2 = gApi.groups().create(name("group2")).get();
AccountInfo user1 = createAccount("user1");
AccountInfo user2 = createAccount("user2");
GroupInfo group1 = gApi.groups().create("group1").get();
GroupInfo group2 = gApi.groups().create("group2").get();
try (TempClockStep step = TestTimeUtil.freezeClock()) {
gApi.groups().id(group1.id).addMembers(user.id.toString(), user2.id.toString());
gApi.groups()
.id(group1.id)
.addMembers(Integer.toString(user1._accountId), Integer.toString(user2._accountId));
}
TimeUtil.nowTs();
@@ -128,9 +168,16 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
assertThat(log.get(1))
.message()
.isEqualTo("Update group\n\nAdd: Administrator <" + admin.id + "@" + serverId + ">");
assertThat(log.get(1)).author().name().isEqualTo(admin.fullName);
assertThat(log.get(1)).author().email().isEqualTo(admin.id + "@" + serverId);
.isEqualTo(
"Update group\n\nAdd: "
+ currentUser.getName()
+ " <"
+ currentUser.getAccountId()
+ "@"
+ serverId
+ ">");
assertThat(log.get(1)).author().name().isEqualTo(currentUser.getName());
assertThat(log.get(1)).author().email().isEqualTo(currentUser.getAccountId() + "@" + serverId);
assertThat(log.get(1)).committer().hasSameDateAs(log.get(1).author);
assertThat(log.get(2))
@@ -138,10 +185,10 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
.isEqualTo(
"Update group\n"
+ "\n"
+ ("Add: User <" + user.id + "@" + serverId + ">\n")
+ ("Add: User2 <" + user2.id + "@" + serverId + ">"));
assertThat(log.get(2)).author().name().isEqualTo(admin.fullName);
assertThat(log.get(2)).author().email().isEqualTo(admin.id + "@" + serverId);
+ ("Add: user1 <" + user1._accountId + "@" + serverId + ">\n")
+ ("Add: user2 <" + user2._accountId + "@" + serverId + ">"));
assertThat(log.get(2)).author().name().isEqualTo(currentUser.getName());
assertThat(log.get(2)).author().email().isEqualTo(currentUser.getAccountId() + "@" + serverId);
assertThat(log.get(2)).committer().hasSameDateAs(log.get(2).author);
assertThat(log.get(3))
@@ -151,14 +198,14 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
+ "\n"
+ ("Add-group: " + group2.name + " <" + group2.id + ">\n")
+ ("Add-group: Registered Users <global:Registered-Users>"));
assertThat(log.get(3)).author().name().isEqualTo(admin.fullName);
assertThat(log.get(3)).author().email().isEqualTo(admin.id + "@" + serverId);
assertThat(log.get(3)).author().name().isEqualTo(currentUser.getName());
assertThat(log.get(3)).author().email().isEqualTo(currentUser.getAccountId() + "@" + serverId);
assertThat(log.get(3)).committer().hasSameDateAs(log.get(3).author);
}
@Test
public void unknownGroupUuid() throws Exception {
GroupInfo group = gApi.groups().create(name("group")).get();
GroupInfo group = gApi.groups().create("group").get();
AccountGroup.UUID subgroupUuid = new AccountGroup.UUID("mybackend:foo");
@@ -168,7 +215,8 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
assertThat(groupBackend.handles(byId.getIncludeUUID())).isFalse();
db.accountGroupById().insert(Collections.singleton(byId));
AccountGroupByIdAud audit = new AccountGroupByIdAud(byId, admin.id, TimeUtil.nowTs());
AccountGroupByIdAud audit =
new AccountGroupByIdAud(byId, currentUser.getAccountId(), TimeUtil.nowTs());
db.accountGroupByIdAud().insert(Collections.singleton(audit));
GroupBundle reviewDbBundle =
@@ -184,14 +232,21 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
assertThat(log.get(0)).message().isEqualTo("Create group");
assertThat(log.get(1))
.message()
.isEqualTo("Update group\n\nAdd: Administrator <" + admin.id + "@" + serverId + ">");
.isEqualTo(
"Update group\n\nAdd: "
+ currentUser.getName()
+ " <"
+ currentUser.getAccountId()
+ "@"
+ serverId
+ ">");
assertThat(log.get(2))
.message()
.isEqualTo("Update group\n\nAdd-group: mybackend:foo <mybackend:foo>");
}
private void deleteGroupRefs(GroupBundle bundle) throws Exception {
try (Repository repo = repoManager.openRepository(allUsers)) {
try (Repository repo = repoManager.openRepository(allUsersName)) {
String refName = RefNames.refsGroups(bundle.uuid());
RefUpdate ru = repo.updateRef(refName);
ru.setForceUpdate(true);
@@ -206,7 +261,7 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
}
private GroupBundle rebuild(GroupBundle reviewDbBundle) throws Exception {
try (Repository repo = repoManager.openRepository(allUsers)) {
try (Repository repo = repoManager.openRepository(allUsersName)) {
rebuilder.rebuild(repo, reviewDbBundle, null);
return bundleFactory.fromNoteDb(repo, reviewDbBundle.uuid());
}
@@ -216,10 +271,17 @@ public class GroupRebuilderIT extends AbstractDaemonTest {
assertThat(GroupBundle.compareWithAudits(expectedReviewDbBundle, noteDbBundle)).isEmpty();
}
private AccountInfo createAccount(String name) throws RestApiException {
AccountInput accountInput = new AccountInput();
accountInput.username = name;
accountInput.name = name;
return gApi.accounts().create(accountInput).get();
}
private ImmutableList<CommitInfo> log(GroupInfo g) throws Exception {
ImmutableList.Builder<CommitInfo> result = ImmutableList.builder();
List<Date> commitDates = new ArrayList<>();
try (Repository repo = repoManager.openRepository(allUsers);
try (Repository repo = repoManager.openRepository(allUsersName);
RevWalk rw = new RevWalk(repo)) {
Ref ref = repo.exactRef(RefNames.refsGroups(new AccountGroup.UUID(g.id)));
if (ref != null) {

View File

@@ -12,15 +12,18 @@
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.group.db;
package com.google.gerrit.server.schema;
import static com.google.common.collect.ImmutableList.toImmutableList;
import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assert_;
import static com.google.gerrit.extensions.common.testing.CommitInfoSubject.assertThat;
import static com.google.gerrit.reviewdb.client.RefNames.REFS_GROUPNAMES;
import com.google.common.collect.ImmutableList;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.TimeUtil;
import com.google.gerrit.common.data.GroupDescription;
import com.google.gerrit.common.data.GroupReference;
import com.google.gerrit.extensions.common.CommitInfo;
import com.google.gerrit.reviewdb.client.Account;
@@ -30,23 +33,38 @@ import com.google.gerrit.reviewdb.client.AccountGroupByIdAud;
import com.google.gerrit.reviewdb.client.AccountGroupMember;
import com.google.gerrit.reviewdb.client.AccountGroupMemberAudit;
import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.group.db.testing.GroupTestUtil;
import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.config.AllUsersNameProvider;
import com.google.gerrit.server.group.db.AuditLogFormatter;
import com.google.gerrit.server.group.db.AuditLogReader;
import com.google.gerrit.server.group.db.GroupNameNotes;
import com.google.gerrit.server.update.RefUpdateUtil;
import com.google.gerrit.testing.GerritBaseTests;
import com.google.gerrit.testing.GitTestUtil;
import com.google.gerrit.testing.InMemoryRepositoryManager;
import com.google.gerrit.testing.TestTimeUtil;
import com.google.gwtorm.server.OrmDuplicateKeyException;
import java.sql.Timestamp;
import java.util.Optional;
import java.util.TimeZone;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.IntStream;
import org.eclipse.jgit.lib.BatchRefUpdate;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectInserter;
import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.lib.Repository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class GroupRebuilderTest extends AbstractGroupTest {
public class GroupRebuilderTest extends GerritBaseTests {
private static final TimeZone TZ = TimeZone.getTimeZone("America/Los_Angeles");
private static final String SERVER_ID = "server-id";
private static final String SERVER_NAME = "Gerrit Server";
private static final String SERVER_EMAIL = "noreply@gerritcodereview.com";
private AtomicInteger idCounter;
private Repository repo;
private GroupRebuilder rebuilder;
@@ -56,14 +74,14 @@ public class GroupRebuilderTest extends AbstractGroupTest {
public void setUp() throws Exception {
TestTimeUtil.resetWithClockStep(1, TimeUnit.SECONDS);
idCounter = new AtomicInteger();
repo = repoManager.createRepository(allUsersName);
AllUsersName allUsersName = new AllUsersName(AllUsersNameProvider.DEFAULT);
repo = new InMemoryRepositoryManager().createRepository(allUsersName);
rebuilder =
new GroupRebuilder(
GroupRebuilderTest.newPersonIdent(),
allUsersName,
// Note that the expected name/email values in tests are not necessarily realistic,
// since they use these trivial name/email functions. GroupRebuilderIT checks the actual
// values.
// since they use these trivial name/email functions.
getAuditLogFormatter());
bundleFactory = new GroupBundle.Factory(new AuditLogReader(SERVER_ID));
}
@@ -656,14 +674,74 @@ public class GroupRebuilderTest extends AbstractGroupTest {
}
private ImmutableList<CommitInfo> log(AccountGroup g) throws Exception {
return GroupTestUtil.log(repo, RefNames.refsGroups(g.getGroupUUID()));
return GitTestUtil.log(repo, RefNames.refsGroups(g.getGroupUUID()));
}
private ImmutableList<CommitInfo> logGroupNames() throws Exception {
return GroupTestUtil.log(repo, REFS_GROUPNAMES);
return GitTestUtil.log(repo, REFS_GROUPNAMES);
}
private static GroupBundle.Builder builder() {
return GroupBundle.builder().source(GroupBundle.Source.REVIEW_DB);
}
private static PersonIdent newPersonIdent() {
return new PersonIdent(SERVER_NAME, SERVER_EMAIL, TimeUtil.nowTs(), TZ);
}
private static void assertServerCommit(CommitInfo commitInfo, String expectedMessage) {
assertCommit(commitInfo, expectedMessage, SERVER_NAME, SERVER_EMAIL);
}
private static void assertCommit(
CommitInfo commitInfo, String expectedMessage, String expectedName, String expectedEmail) {
assertThat(commitInfo).message().isEqualTo(expectedMessage);
assertThat(commitInfo).author().name().isEqualTo(expectedName);
assertThat(commitInfo).author().email().isEqualTo(expectedEmail);
// Committer should always be the server, regardless of author.
assertThat(commitInfo).committer().name().isEqualTo(SERVER_NAME);
assertThat(commitInfo).committer().email().isEqualTo(SERVER_EMAIL);
assertThat(commitInfo).committer().date().isEqualTo(commitInfo.author.date);
assertThat(commitInfo).committer().tz().isEqualTo(commitInfo.author.tz);
}
private static AuditLogFormatter getAuditLogFormatter() {
return AuditLogFormatter.create(
GroupRebuilderTest::getAccount, GroupRebuilderTest::getGroup, SERVER_ID);
}
private static Optional<Account> getAccount(Account.Id id) {
Account account = new Account(id, TimeUtil.nowTs());
account.setFullName("Account " + id);
return Optional.of(account);
}
private static Optional<GroupDescription.Basic> getGroup(AccountGroup.UUID uuid) {
GroupDescription.Basic group =
new GroupDescription.Basic() {
@Override
public AccountGroup.UUID getGroupUUID() {
return uuid;
}
@Override
public String getName() {
return "Group " + uuid;
}
@Nullable
@Override
public String getEmailAddress() {
return null;
}
@Nullable
@Override
public String getUrl() {
return null;
}
};
return Optional.of(group);
}
}