Fix nits in accounts test API to set secondary emails

* TestAccountCreation:
  Accept any Set in setter for secondary emails. This way callers are
  not forced to create an ImmutableSet.

* AccountOperationsImpl:
  Factor out some code from the fillBuilder(
  InternalAccountUpdate.Builder, TestAccountUpdate, AccountState) method
  into separate methods to improve code readability.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I29b60fa83ac92a5f3ad1edaf425d5000b1952717
This commit is contained in:
Edwin Kempin
2020-06-23 12:12:00 +02:00
parent 915c2fff19
commit cdba7c81f7
2 changed files with 42 additions and 33 deletions

View File

@@ -177,41 +177,49 @@ public class AccountOperationsImpl implements AccountOperations {
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> secondaryEmails = getSecondaryEmails(accountUpdate, accountState);
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()));
}
setSecondaryEmails(builder, accountUpdate, accountState, newSecondaryEmails);
}
}
private ImmutableSet<String> getSecondaryEmails(
TestAccountUpdate accountUpdate, AccountState accountState) {
ImmutableSet<String> allEmails =
ExternalId.getEmails(accountState.externalIds()).collect(toImmutableSet());
if (accountUpdate.preferredEmail().isPresent()) {
return ImmutableSet.copyOf(
Sets.difference(allEmails, ImmutableSet.of(accountUpdate.preferredEmail().get())));
} else if (accountState.account().preferredEmail() != null) {
return ImmutableSet.copyOf(
Sets.difference(allEmails, ImmutableSet.of(accountState.account().preferredEmail())));
}
return allEmails;
}
private void setSecondaryEmails(
InternalAccountUpdate.Builder builder,
TestAccountUpdate accountUpdate,
AccountState accountState,
ImmutableSet<String> 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()));
}
}

View File

@@ -21,6 +21,7 @@ import com.google.common.collect.ImmutableSet;
import com.google.gerrit.acceptance.testsuite.ThrowingFunction;
import com.google.gerrit.entities.Account;
import java.util.Optional;
import java.util.Set;
@AutoValue
public abstract class TestAccountCreation {
@@ -88,7 +89,7 @@ public abstract class TestAccountCreation {
return active(false);
}
public abstract Builder secondaryEmails(ImmutableSet<String> secondaryEmails);
public abstract Builder secondaryEmails(Set<String> secondaryEmails);
abstract ImmutableSet.Builder<String> secondaryEmailsBuilder();