TestAccountUpdate: Support modifying secondary emails

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I26b5f3e5e03fb877bfc8bc2c3468030c4d86059f
This commit is contained in:
Edwin Kempin
2020-06-23 10:07:11 +02:00
parent dbe7168687
commit e1602938b0
2 changed files with 81 additions and 4 deletions

View File

@@ -17,6 +17,8 @@ package com.google.gerrit.acceptance.testsuite.account;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.ImmutableSet.toImmutableSet;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.gerrit.entities.Account;
import com.google.gerrit.server.ServerInitiated;
import com.google.gerrit.server.account.AccountState;
@@ -61,8 +63,8 @@ public class AccountOperationsImpl implements AccountOperations {
private Account.Id createAccount(TestAccountCreation accountCreation) throws Exception {
AccountsUpdate.AccountUpdater accountUpdater =
(account, updateBuilder) ->
fillBuilder(updateBuilder, accountCreation, account.account().id());
(accountState, updateBuilder) ->
fillBuilder(updateBuilder, accountCreation, accountState.account().id());
AccountState createdAccount = createAccount(accountUpdater);
return createdAccount.account().id();
}
@@ -154,7 +156,7 @@ public class AccountOperationsImpl implements AccountOperations {
private void updateAccount(TestAccountUpdate accountUpdate)
throws IOException, ConfigInvalidException {
AccountsUpdate.AccountUpdater accountUpdater =
(account, updateBuilder) -> fillBuilder(updateBuilder, accountUpdate, accountId);
(accountState, updateBuilder) -> fillBuilder(updateBuilder, accountUpdate, accountState);
Optional<AccountState> updatedAccount = updateAccount(accountUpdater);
checkState(updatedAccount.isPresent(), "Tried to update non-existing test account");
}
@@ -167,13 +169,50 @@ public class AccountOperationsImpl implements AccountOperations {
private void fillBuilder(
InternalAccountUpdate.Builder builder,
TestAccountUpdate accountUpdate,
Account.Id accountId) {
AccountState accountState) {
accountUpdate.fullname().ifPresent(builder::setFullName);
accountUpdate.preferredEmail().ifPresent(e -> setPreferredEmail(builder, accountId, e));
String httpPassword = accountUpdate.httpPassword().orElse(null);
accountUpdate.username().ifPresent(u -> setUsername(builder, accountId, u, httpPassword));
accountUpdate.status().ifPresent(builder::setStatus);
accountUpdate.active().ifPresent(builder::setActive);
ImmutableSet<String> secondaryEmails;
ImmutableSet<String> allEmails =
ExternalId.getEmails(accountState.externalIds()).collect(toImmutableSet());
if (accountUpdate.preferredEmail().isPresent()) {
secondaryEmails =
ImmutableSet.copyOf(
Sets.difference(allEmails, ImmutableSet.of(accountUpdate.preferredEmail().get())));
} else if (accountState.account().preferredEmail() != null) {
secondaryEmails =
ImmutableSet.copyOf(
Sets.difference(
allEmails, ImmutableSet.of(accountState.account().preferredEmail())));
} else {
secondaryEmails = allEmails;
}
ImmutableSet<String> newSecondaryEmails =
ImmutableSet.copyOf(accountUpdate.secondaryEmailsModification().apply(secondaryEmails));
if (!secondaryEmails.equals(newSecondaryEmails)) {
// delete all external IDs of SCHEME_MAILTO scheme, then add back SCHEME_MAILTO external IDs
// for the new secondary emails and the preferred email
builder.deleteExternalIds(
accountState.externalIds().stream()
.filter(e -> e.isScheme(ExternalId.SCHEME_MAILTO))
.collect(toImmutableSet()));
builder.addExternalIds(
newSecondaryEmails.stream()
.map(secondaryEmail -> ExternalId.createEmail(accountId, secondaryEmail))
.collect(toImmutableSet()));
if (accountUpdate.preferredEmail().isPresent()) {
builder.addExternalId(
ExternalId.createEmail(accountId, accountUpdate.preferredEmail().get()));
} else if (accountState.account().preferredEmail() != null) {
builder.addExternalId(
ExternalId.createEmail(accountId, accountState.account().preferredEmail()));
}
}
}
@Override

View File

@@ -15,8 +15,12 @@
package com.google.gerrit.acceptance.testsuite.account;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Sets;
import com.google.gerrit.acceptance.testsuite.ThrowingConsumer;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
@AutoValue
public abstract class TestAccountUpdate {
@@ -32,11 +36,14 @@ public abstract class TestAccountUpdate {
public abstract Optional<Boolean> active();
public abstract Function<ImmutableSet<String>, Set<String>> secondaryEmailsModification();
abstract ThrowingConsumer<TestAccountUpdate> accountUpdater();
public static Builder builder(ThrowingConsumer<TestAccountUpdate> accountUpdater) {
return new AutoValue_TestAccountUpdate.Builder()
.accountUpdater(accountUpdater)
.secondaryEmailsModification(in -> in)
.httpPassword("http-pass");
}
@@ -82,6 +89,37 @@ public abstract class TestAccountUpdate {
return active(false);
}
abstract Builder secondaryEmailsModification(
Function<ImmutableSet<String>, Set<String>> secondaryEmailsModification);
abstract Function<ImmutableSet<String>, Set<String>> secondaryEmailsModification();
public Builder clearSecondaryEmails() {
return secondaryEmailsModification(originalSecondaryEmail -> ImmutableSet.of());
}
public Builder addSecondaryEmail(String secondaryEmail) {
Function<ImmutableSet<String>, Set<String>> secondaryEmailsModification =
secondaryEmailsModification();
secondaryEmailsModification(
originalSecondaryEmails ->
Sets.union(
secondaryEmailsModification.apply(originalSecondaryEmails),
ImmutableSet.of(secondaryEmail)));
return this;
}
public Builder removeSecondaryEmail(String secondaryEmail) {
Function<ImmutableSet<String>, Set<String>> previousModification =
secondaryEmailsModification();
secondaryEmailsModification(
originalSecondaryEmails ->
Sets.difference(
previousModification.apply(originalSecondaryEmails),
ImmutableSet.of(secondaryEmail)));
return this;
}
abstract Builder accountUpdater(ThrowingConsumer<TestAccountUpdate> accountUpdater);
abstract TestAccountUpdate autoBuild();