Create ChangeUpdates with IdentifiedUsers rather than Accounts

Allows the user to be passed into MetaDataUpdate.create() so we can
create updates by arbitrary users, e.g. when posting a review on
behalf of someone else.

Change-Id: I71968d252e04651d7b597034fa17273e9749d0b0
This commit is contained in:
Dave Borowitz
2014-01-07 14:46:03 -08:00
parent be84639566
commit 4ceec6ebec
5 changed files with 240 additions and 134 deletions

View File

@@ -74,13 +74,14 @@ public class IdentifiedUser extends CurrentUser {
private final GroupBackend groupBackend; private final GroupBackend groupBackend;
@Inject @Inject
GenericFactory( public GenericFactory(
CapabilityControl.Factory capabilityControlFactory, @Nullable CapabilityControl.Factory capabilityControlFactory,
final AuthConfig authConfig, AuthConfig authConfig,
final @AnonymousCowardName String anonymousCowardName, @AnonymousCowardName String anonymousCowardName,
final @CanonicalWebUrl Provider<String> canonicalUrl, @CanonicalWebUrl Provider<String> canonicalUrl,
final Realm realm, final AccountCache accountCache, Realm realm,
final GroupBackend groupBackend) { AccountCache accountCache,
GroupBackend groupBackend) {
this.capabilityControlFactory = capabilityControlFactory; this.capabilityControlFactory = capabilityControlFactory;
this.authConfig = authConfig; this.authConfig = authConfig;
this.anonymousCowardName = anonymousCowardName; this.anonymousCowardName = anonymousCowardName;

View File

@@ -49,19 +49,24 @@ public class MetaDataUpdate {
} }
public PersonIdent getUserPersonIdent() { public PersonIdent getUserPersonIdent() {
return createPersonIdent(); return createPersonIdent(identifiedUser.get());
} }
public MetaDataUpdate create(Project.NameKey name) public MetaDataUpdate create(Project.NameKey name)
throws RepositoryNotFoundException, IOException { throws RepositoryNotFoundException, IOException {
return create(name, identifiedUser.get());
}
public MetaDataUpdate create(Project.NameKey name, IdentifiedUser user)
throws RepositoryNotFoundException, IOException {
MetaDataUpdate md = factory.create(name, mgr.openRepository(name)); MetaDataUpdate md = factory.create(name, mgr.openRepository(name));
md.getCommitBuilder().setAuthor(createPersonIdent()); md.getCommitBuilder().setAuthor(createPersonIdent(user));
md.getCommitBuilder().setCommitter(serverIdent); md.getCommitBuilder().setCommitter(serverIdent);
return md; return md;
} }
private PersonIdent createPersonIdent() { private PersonIdent createPersonIdent(IdentifiedUser user) {
return identifiedUser.get().newCommitterIdent( return user.newCommitterIdent(
serverIdent.getWhen(), serverIdent.getTimeZone()); serverIdent.getWhen(), serverIdent.getTimeZone());
} }
} }

View File

@@ -22,7 +22,6 @@ import static com.google.gerrit.server.notedb.ChangeNoteUtil.GERRIT_PLACEHOLDER_
import com.google.common.annotations.VisibleForTesting; import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.Maps; import com.google.common.collect.Maps;
import com.google.gerrit.common.Nullable;
import com.google.gerrit.common.data.LabelType; import com.google.gerrit.common.data.LabelType;
import com.google.gerrit.common.data.LabelTypes; import com.google.gerrit.common.data.LabelTypes;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
@@ -36,9 +35,8 @@ import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.git.VersionedMetaData; import com.google.gerrit.server.git.VersionedMetaData;
import com.google.gerrit.server.project.ProjectCache; import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.util.LabelVote; import com.google.gerrit.server.util.LabelVote;
import com.google.inject.Inject; import com.google.inject.assistedinject.Assisted;
import com.google.inject.Provider; import com.google.inject.assistedinject.AssistedInject;
import com.google.inject.Singleton;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.lib.CommitBuilder; import org.eclipse.jgit.lib.CommitBuilder;
@@ -63,41 +61,10 @@ import java.util.TimeZone;
* This class is not thread-safe. * This class is not thread-safe.
*/ */
public class ChangeUpdate extends VersionedMetaData { public class ChangeUpdate extends VersionedMetaData {
@Singleton public interface Factory {
public static class Factory { ChangeUpdate create(Change change);
private final GitRepositoryManager repoManager; ChangeUpdate create(Change change, Date when);
private final AccountCache accountCache; ChangeUpdate create(Change change, Date when, IdentifiedUser user);
private final MetaDataUpdate.User updateFactory;
private final ProjectCache projectCache;
private final Provider<IdentifiedUser> user;
private final PersonIdent serverIdent;
@Inject
Factory(
GitRepositoryManager repoManager,
AccountCache accountCache,
MetaDataUpdate.User updateFactory,
ProjectCache projectCache,
Provider<IdentifiedUser> user,
@GerritPersonIdent PersonIdent serverIdent) {
this.repoManager = repoManager;
this.accountCache = accountCache;
this.updateFactory = updateFactory;
this.projectCache = projectCache;
this.user = user;
this.serverIdent = serverIdent;
}
public ChangeUpdate create(Change change) {
return create(change, serverIdent.getWhen());
}
public ChangeUpdate create(Change change, Date when) {
return new ChangeUpdate(
repoManager, accountCache, updateFactory,
projectCache.get(change.getProject()).getLabelTypes(),
change, user.get().getAccount(), when, serverIdent.getTimeZone());
}
} }
private final GitRepositoryManager repoManager; private final GitRepositoryManager repoManager;
@@ -105,7 +72,7 @@ public class ChangeUpdate extends VersionedMetaData {
private final MetaDataUpdate.User updateFactory; private final MetaDataUpdate.User updateFactory;
private final LabelTypes labelTypes; private final LabelTypes labelTypes;
private final Change change; private final Change change;
private final Account account; private final IdentifiedUser user;
private final Date when; private final Date when;
private final TimeZone tz; private final TimeZone tz;
private final Map<String, Short> approvals; private final Map<String, Short> approvals;
@@ -113,32 +80,72 @@ public class ChangeUpdate extends VersionedMetaData {
private String subject; private String subject;
private PatchSet.Id psId; private PatchSet.Id psId;
@VisibleForTesting @AssistedInject
ChangeUpdate(GitRepositoryManager repoManager, AccountCache accountCache, ChangeUpdate(
LabelTypes labelTypes, Change change, Account account, Date when, @GerritPersonIdent PersonIdent serverIdent,
TimeZone tz) { GitRepositoryManager repoManager,
this(repoManager, accountCache, null, labelTypes, change, account, when, AccountCache accountCache,
tz); MetaDataUpdate.User updateFactory,
ProjectCache projectCache,
IdentifiedUser user,
@Assisted Change change) {
this(serverIdent, repoManager, accountCache, updateFactory, projectCache,
user, change, serverIdent.getWhen());
} }
private ChangeUpdate(GitRepositoryManager repoManager, @AssistedInject
AccountCache accountCache, @Nullable MetaDataUpdate.User updateFactory, ChangeUpdate(
LabelTypes labelTypes, Change change, Account account, Date when, @GerritPersonIdent PersonIdent serverIdent,
TimeZone tz) { GitRepositoryManager repoManager,
AccountCache accountCache,
MetaDataUpdate.User updateFactory,
ProjectCache projectCache,
IdentifiedUser user,
@Assisted Change change,
@Assisted Date when) {
this(serverIdent, repoManager, accountCache, updateFactory, projectCache,
change, when, user);
}
@AssistedInject
ChangeUpdate(
@GerritPersonIdent PersonIdent serverIdent,
GitRepositoryManager repoManager,
AccountCache accountCache,
MetaDataUpdate.User updateFactory,
ProjectCache projectCache,
@Assisted Change change,
@Assisted Date when,
@Assisted IdentifiedUser user) {
this(serverIdent, repoManager, accountCache, updateFactory,
projectCache.get(change.getDest().getParentKey()).getLabelTypes(),
change, when, user);
}
@VisibleForTesting
ChangeUpdate(
PersonIdent serverIdent,
GitRepositoryManager repoManager,
AccountCache accountCache,
MetaDataUpdate.User updateFactory,
LabelTypes labelTypes,
Change change,
Date when,
IdentifiedUser user) {
this.repoManager = repoManager; this.repoManager = repoManager;
this.accountCache = accountCache; this.accountCache = accountCache;
this.updateFactory = updateFactory; this.updateFactory = updateFactory;
this.labelTypes = labelTypes; this.labelTypes = labelTypes;
this.change = change; this.change = change;
this.account = account; this.user = user;
this.when = when; this.when = when;
this.tz = tz; this.tz = serverIdent.getTimeZone();
this.approvals = Maps.newTreeMap(labelTypes.nameComparator()); this.approvals = Maps.newTreeMap(labelTypes.nameComparator());
this.reviewers = Maps.newLinkedHashMap(); this.reviewers = Maps.newLinkedHashMap();
} }
public Account getAccount() { public IdentifiedUser getUser() {
return account; return user;
} }
public Date getWhen() { public Date getWhen() {
@@ -169,7 +176,7 @@ public class ChangeUpdate extends VersionedMetaData {
public RevCommit commit() throws IOException { public RevCommit commit() throws IOException {
return commit(checkNotNull(updateFactory, "MetaDataUpdate.Factory") return commit(checkNotNull(updateFactory, "MetaDataUpdate.Factory")
.create(change.getProject())); .create(change.getProject(), user));
} }
@Override @Override
@@ -197,7 +204,7 @@ public class ChangeUpdate extends VersionedMetaData {
} }
public PersonIdent newCommitter() { public PersonIdent newCommitter() {
return newIdent(account); return newIdent(user.getAccount());
} }
@Override @Override

View File

@@ -14,8 +14,11 @@
package com.google.gerrit.server.notedb; package com.google.gerrit.server.notedb;
import static com.google.gerrit.server.notedb.ReviewerState.CC;
import static com.google.gerrit.server.notedb.ReviewerState.REVIEWER;
import static com.google.gerrit.server.project.Util.category; import static com.google.gerrit.server.project.Util.category;
import static com.google.gerrit.server.project.Util.value; import static com.google.gerrit.server.project.Util.value;
import static com.google.inject.Scopes.SINGLETON;
import static java.util.concurrent.TimeUnit.DAYS; import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.MILLISECONDS; import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.MINUTES;
@@ -35,15 +38,31 @@ import com.google.gerrit.reviewdb.client.PatchSetApproval;
import com.google.gerrit.reviewdb.client.PatchSetInfo; import com.google.gerrit.reviewdb.client.PatchSetInfo;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.ChangeUtil; import com.google.gerrit.server.ChangeUtil;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.CapabilityControl;
import com.google.gerrit.server.account.GroupBackend;
import com.google.gerrit.server.account.Realm;
import com.google.gerrit.server.config.AnonymousCowardName;
import com.google.gerrit.server.config.AnonymousCowardNameProvider;
import com.google.gerrit.server.config.CanonicalWebUrl;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated; import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.MetaDataUpdate; import com.google.gerrit.server.git.MetaDataUpdate;
import com.google.gerrit.server.group.SystemGroupBackend;
import com.google.gerrit.server.util.TimeUtil; import com.google.gerrit.server.util.TimeUtil;
import com.google.gerrit.testutil.FakeAccountCache; import com.google.gerrit.testutil.FakeAccountCache;
import com.google.gerrit.testutil.FakeRealm;
import com.google.gerrit.testutil.InMemoryRepositoryManager; import com.google.gerrit.testutil.InMemoryRepositoryManager;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.util.Providers;
import org.eclipse.jgit.errors.ConfigInvalidException; import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository; import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.lib.PersonIdent;
import org.eclipse.jgit.revwalk.RevCommit; import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk; import org.eclipse.jgit.revwalk.RevWalk;
@@ -64,16 +83,8 @@ public class ChangeNotesTest {
private static final TimeZone TZ = private static final TimeZone TZ =
TimeZone.getTimeZone("America/Los_Angeles"); TimeZone.getTimeZone("America/Los_Angeles");
private static final Account CHANGE_OWNER; private static final PersonIdent SERVER_IDENT =
private static final Account OTHER_ACCOUNT; new PersonIdent("Gerrit Server", "noreply@gerrit.com", new Date(), TZ);
static {
CHANGE_OWNER = new Account(new Account.Id(1), TimeUtil.nowTs());
CHANGE_OWNER.setFullName("Change Owner");
CHANGE_OWNER.setPreferredEmail("change@owner.com");
OTHER_ACCOUNT = new Account(new Account.Id(2), TimeUtil.nowTs());
OTHER_ACCOUNT.setFullName("Other Account");
OTHER_ACCOUNT.setPreferredEmail("other@account.com");
}
private static final LabelTypes LABEL_TYPES = new LabelTypes(ImmutableList.of( private static final LabelTypes LABEL_TYPES = new LabelTypes(ImmutableList.of(
category("Verified", category("Verified",
@@ -89,6 +100,8 @@ public class ChangeNotesTest {
private InMemoryRepositoryManager repoManager; private InMemoryRepositoryManager repoManager;
private InMemoryRepository repo; private InMemoryRepository repo;
private FakeAccountCache accountCache; private FakeAccountCache accountCache;
private IdentifiedUser changeOwner;
private IdentifiedUser otherUser;
private volatile long clockStepMs; private volatile long clockStepMs;
@Before @Before
@@ -97,8 +110,36 @@ public class ChangeNotesTest {
repoManager = new InMemoryRepositoryManager(); repoManager = new InMemoryRepositoryManager();
repo = repoManager.createRepository(project); repo = repoManager.createRepository(project);
accountCache = new FakeAccountCache(); accountCache = new FakeAccountCache();
accountCache.put(CHANGE_OWNER); Account co = new Account(new Account.Id(1), TimeUtil.nowTs());
accountCache.put(OTHER_ACCOUNT); co.setFullName("Change Owner");
co.setPreferredEmail("change@owner.com");
accountCache.put(co);
Account ou = new Account(new Account.Id(2), TimeUtil.nowTs());
ou.setFullName("Other Account");
ou.setPreferredEmail("other@account.com");
accountCache.put(ou);
Injector injector = Guice.createInjector(new AbstractModule() {
@Override
public void configure() {
bind(CapabilityControl.Factory.class)
.toProvider(Providers.<CapabilityControl.Factory> of(null));
bind(Config.class).annotatedWith(GerritServerConfig.class)
.toInstance(new Config());
bind(String.class).annotatedWith(AnonymousCowardName.class)
.toProvider(AnonymousCowardNameProvider.class);
bind(String.class).annotatedWith(CanonicalWebUrl.class)
.toInstance("http://localhost:8080/");
bind(Realm.class).to(FakeRealm.class);
bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON);
bind(AccountCache.class).toInstance(accountCache);
}
});
IdentifiedUser.GenericFactory userFactory =
injector.getInstance(IdentifiedUser.GenericFactory.class);
changeOwner = userFactory.create(co.getId());
otherUser = userFactory.create(ou.getId());
} }
@Before @Before
@@ -124,11 +165,11 @@ public class ChangeNotesTest {
@Test @Test
public void approvalsCommitFormat() throws Exception { public void approvalsCommitFormat() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) -1); update.putApproval("Code-Review", (short) -1);
update.putApproval("Verified", (short) 1); update.putApproval("Verified", (short) 1);
update.putReviewer(CHANGE_OWNER.getId(), ReviewerState.REVIEWER); update.putReviewer(changeOwner.getAccount().getId(), REVIEWER);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.CC); update.putReviewer(otherUser.getAccount().getId(), CC);
commit(update); commit(update);
assertEquals("refs/changes/01/1/meta", update.getRefName()); assertEquals("refs/changes/01/1/meta", update.getRefName());
@@ -165,12 +206,12 @@ public class ChangeNotesTest {
@Test @Test
public void approvalsOnePatchSet() throws Exception { public void approvalsOnePatchSet() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) -1); update.putApproval("Code-Review", (short) -1);
update.putApproval("Verified", (short) 1); update.putApproval("Verified", (short) 1);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
assertEquals(1, notes.getApprovals().keySet().size()); assertEquals(1, notes.getApprovals().keySet().size());
List<PatchSetApproval> psas = List<PatchSetApproval> psas =
notes.getApprovals().get(c.currentPatchSetId()); notes.getApprovals().get(c.currentPatchSetId());
@@ -192,18 +233,18 @@ public class ChangeNotesTest {
@Test @Test
public void approvalsMultiplePatchSets() throws Exception { public void approvalsMultiplePatchSets() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) -1); update.putApproval("Code-Review", (short) -1);
commit(update); commit(update);
PatchSet.Id ps1 = c.currentPatchSetId(); PatchSet.Id ps1 = c.currentPatchSetId();
incrementPatchSet(c); incrementPatchSet(c);
update = newUpdate(c, CHANGE_OWNER); update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) 1); update.putApproval("Code-Review", (short) 1);
commit(update); commit(update);
PatchSet.Id ps2 = c.currentPatchSetId(); PatchSet.Id ps2 = c.currentPatchSetId();
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
ListMultimap<PatchSet.Id, PatchSetApproval> psas = notes.getApprovals(); ListMultimap<PatchSet.Id, PatchSetApproval> psas = notes.getApprovals();
assertEquals(2, notes.getApprovals().keySet().size()); assertEquals(2, notes.getApprovals().keySet().size());
@@ -225,21 +266,21 @@ public class ChangeNotesTest {
@Test @Test
public void approvalsMultipleApprovals() throws Exception { public void approvalsMultipleApprovals() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) -1); update.putApproval("Code-Review", (short) -1);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
PatchSetApproval psa = Iterables.getOnlyElement( PatchSetApproval psa = Iterables.getOnlyElement(
notes.getApprovals().get(c.currentPatchSetId())); notes.getApprovals().get(c.currentPatchSetId()));
assertEquals("Code-Review", psa.getLabel()); assertEquals("Code-Review", psa.getLabel());
assertEquals((short) -1, psa.getValue()); assertEquals((short) -1, psa.getValue());
update = newUpdate(c, CHANGE_OWNER); update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) 1); update.putApproval("Code-Review", (short) 1);
commit(update); commit(update);
notes = newNotes(c); notes = newChange(c);
psa = Iterables.getOnlyElement( psa = Iterables.getOnlyElement(
notes.getApprovals().get(c.currentPatchSetId())); notes.getApprovals().get(c.currentPatchSetId()));
assertEquals("Code-Review", psa.getLabel()); assertEquals("Code-Review", psa.getLabel());
@@ -249,15 +290,15 @@ public class ChangeNotesTest {
@Test @Test
public void approvalsMultipleUsers() throws Exception { public void approvalsMultipleUsers() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) -1); update.putApproval("Code-Review", (short) -1);
commit(update); commit(update);
update = newUpdate(c, OTHER_ACCOUNT); update = newUpdate(c, otherUser);
update.putApproval("Code-Review", (short) 1); update.putApproval("Code-Review", (short) 1);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
assertEquals(1, notes.getApprovals().keySet().size()); assertEquals(1, notes.getApprovals().keySet().size());
List<PatchSetApproval> psas = List<PatchSetApproval> psas =
notes.getApprovals().get(c.currentPatchSetId()); notes.getApprovals().get(c.currentPatchSetId());
@@ -279,85 +320,85 @@ public class ChangeNotesTest {
@Test @Test
public void multipleReviewers() throws Exception { public void multipleReviewers() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putReviewer(CHANGE_OWNER.getId(), ReviewerState.REVIEWER); update.putReviewer(changeOwner.getAccount().getId(), REVIEWER);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.REVIEWER); update.putReviewer(otherUser.getAccount().getId(), REVIEWER);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
assertEquals(ImmutableSetMultimap.of( assertEquals(ImmutableSetMultimap.of(
ReviewerState.REVIEWER, new Account.Id(1), REVIEWER, new Account.Id(1),
ReviewerState.REVIEWER, new Account.Id(2)), REVIEWER, new Account.Id(2)),
notes.getReviewers()); notes.getReviewers());
} }
@Test @Test
public void reviewerTypes() throws Exception { public void reviewerTypes() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putReviewer(CHANGE_OWNER.getId(), ReviewerState.REVIEWER); update.putReviewer(changeOwner.getAccount().getId(), REVIEWER);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.CC); update.putReviewer(otherUser.getAccount().getId(), CC);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
assertEquals(ImmutableSetMultimap.of( assertEquals(ImmutableSetMultimap.of(
ReviewerState.REVIEWER, new Account.Id(1), REVIEWER, new Account.Id(1),
ReviewerState.CC, new Account.Id(2)), CC, new Account.Id(2)),
notes.getReviewers()); notes.getReviewers());
} }
@Test @Test
public void oneReviewerMultipleTypes() throws Exception { public void oneReviewerMultipleTypes() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.REVIEWER); update.putReviewer(otherUser.getAccount().getId(), REVIEWER);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
assertEquals(ImmutableSetMultimap.of( assertEquals(ImmutableSetMultimap.of(
ReviewerState.REVIEWER, new Account.Id(2)), REVIEWER, new Account.Id(2)),
notes.getReviewers()); notes.getReviewers());
update = newUpdate(c, OTHER_ACCOUNT); update = newUpdate(c, otherUser);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.CC); update.putReviewer(otherUser.getAccount().getId(), CC);
commit(update); commit(update);
notes = newNotes(c); notes = newChange(c);
assertEquals(ImmutableSetMultimap.of( assertEquals(ImmutableSetMultimap.of(
ReviewerState.CC, new Account.Id(2)), CC, new Account.Id(2)),
notes.getReviewers()); notes.getReviewers());
} }
@Test @Test
public void removeReviewer() throws Exception { public void removeReviewer() throws Exception {
Change c = newChange(); Change c = newChange();
ChangeUpdate update = newUpdate(c, CHANGE_OWNER); ChangeUpdate update = newUpdate(c, changeOwner);
update.putReviewer(OTHER_ACCOUNT.getId(), ReviewerState.REVIEWER); update.putReviewer(otherUser.getAccount().getId(), REVIEWER);
commit(update); commit(update);
update = newUpdate(c, CHANGE_OWNER); update = newUpdate(c, changeOwner);
update.putApproval("Code-Review", (short) 1); update.putApproval("Code-Review", (short) 1);
commit(update); commit(update);
update = newUpdate(c, OTHER_ACCOUNT); update = newUpdate(c, otherUser);
update.putApproval("Code-Review", (short) 1); update.putApproval("Code-Review", (short) 1);
commit(update); commit(update);
ChangeNotes notes = newNotes(c); ChangeNotes notes = newChange(c);
List<PatchSetApproval> psas = List<PatchSetApproval> psas =
notes.getApprovals().get(c.currentPatchSetId()); notes.getApprovals().get(c.currentPatchSetId());
assertEquals(2, psas.size()); assertEquals(2, psas.size());
assertEquals(CHANGE_OWNER.getId(), psas.get(0).getAccountId()); assertEquals(changeOwner.getAccount().getId(), psas.get(0).getAccountId());
assertEquals(OTHER_ACCOUNT.getId(), psas.get(1).getAccountId()); assertEquals(otherUser.getAccount().getId(), psas.get(1).getAccountId());
update = newUpdate(c, CHANGE_OWNER); update = newUpdate(c, changeOwner);
update.removeReviewer(OTHER_ACCOUNT.getId()); update.removeReviewer(otherUser.getAccount().getId());
commit(update); commit(update);
notes = newNotes(c); notes = newChange(c);
psas = notes.getApprovals().get(c.currentPatchSetId()); psas = notes.getApprovals().get(c.currentPatchSetId());
assertEquals(1, psas.size()); assertEquals(1, psas.size());
assertEquals(CHANGE_OWNER.getId(), psas.get(0).getAccountId()); assertEquals(changeOwner.getAccount().getId(), psas.get(0).getAccountId());
} }
private Change newChange() { private Change newChange() {
@@ -365,20 +406,20 @@ public class ChangeNotesTest {
Change c = new Change( Change c = new Change(
new Change.Key("Iabcd1234abcd1234abcd1234abcd1234abcd1234"), new Change.Key("Iabcd1234abcd1234abcd1234abcd1234abcd1234"),
changeId, changeId,
CHANGE_OWNER.getId(), changeOwner.getAccount().getId(),
new Branch.NameKey(project, "master"), new Branch.NameKey(project, "master"),
TimeUtil.nowTs()); TimeUtil.nowTs());
incrementPatchSet(c); incrementPatchSet(c);
return c; return c;
} }
private ChangeUpdate newUpdate(Change c, Account account) private ChangeUpdate newUpdate(Change c, IdentifiedUser user)
throws ConfigInvalidException, IOException { throws ConfigInvalidException, IOException {
return new ChangeUpdate(repoManager, accountCache, LABEL_TYPES, c, account, return new ChangeUpdate(SERVER_IDENT, repoManager, accountCache, null,
TimeUtil.nowTs(), TZ); LABEL_TYPES, c, TimeUtil.nowTs(), user);
} }
private ChangeNotes newNotes(Change c) throws OrmException { private ChangeNotes newChange(Change c) throws OrmException {
return new ChangeNotes(repoManager, c).load(); return new ChangeNotes(repoManager, c).load();
} }
@@ -401,10 +442,8 @@ public class ChangeNotesTest {
private RevCommit commit(ChangeUpdate update) throws IOException { private RevCommit commit(ChangeUpdate update) throws IOException {
MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED, MetaDataUpdate md = new MetaDataUpdate(GitReferenceUpdated.DISABLED,
project, repo); project, repo);
md.getCommitBuilder().setAuthor(new PersonIdent( md.getCommitBuilder().setAuthor(
update.getAccount().getFullName(), update.getUser().newCommitterIdent(update.getWhen(), TZ));
update.getAccount().getPreferredEmail(),
update.getWhen(), TZ));
return update.commit(md); return update.commit(md);
} }
} }

View File

@@ -0,0 +1,54 @@
// Copyright (C) 2014 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.testutil;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Account.FieldName;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.account.AuthRequest;
import com.google.gerrit.server.account.Realm;
/** Fake implementation of {@link Realm} for testing. */
public class FakeRealm implements Realm {
@Override
public boolean allowsEdit(FieldName field) {
return false;
}
@Override
public AuthRequest authenticate(AuthRequest who) {
return who;
}
@Override
public AuthRequest link(ReviewDb db, Account.Id to, AuthRequest who) {
return who;
}
@Override
public AuthRequest unlink(ReviewDb db, Account.Id to, AuthRequest who) {
return who;
}
@Override
public void onCreateAccount(AuthRequest who, Account account) {
// Do nothing.
}
@Override
public Account.Id lookup(String accountName) {
return null;
}
}