Merge changes I26b5f3e5,I67c8bee7,I26844e8a,Ib60243df

* changes:
  TestAccountUpdate: Support modifying secondary emails
  TestAccountCreation: Support specifying secondary emails
  TestAccount: Add method to get secondary emails
  TestAccount: Add method to get all emails
This commit is contained in:
Alice Kober-Sotzek
2020-06-23 10:02:37 +00:00
committed by Gerrit Code Review
4 changed files with 124 additions and 6 deletions

View File

@@ -15,7 +15,10 @@
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;
@@ -60,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();
}
@@ -82,6 +85,11 @@ public class AccountOperationsImpl implements AccountOperations {
accountCreation.username().ifPresent(u -> setUsername(builder, accountId, u, httpPassword));
accountCreation.status().ifPresent(builder::setStatus);
accountCreation.active().ifPresent(builder::setActive);
accountCreation
.secondaryEmails()
.forEach(
secondaryEmail ->
builder.addExternalId(ExternalId.createEmail(accountId, secondaryEmail)));
}
private static InternalAccountUpdate.Builder setPreferredEmail(
@@ -136,6 +144,7 @@ public class AccountOperationsImpl implements AccountOperations {
.fullname(Optional.ofNullable(account.fullName()))
.username(accountState.userName())
.active(accountState.account().isActive())
.emails(ExternalId.getEmails(accountState.externalIds()).collect(toImmutableSet()))
.build();
}
@@ -147,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");
}
@@ -160,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,6 +15,8 @@
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.entities.Account;
import java.util.Optional;
@@ -30,6 +32,16 @@ public abstract class TestAccount {
public abstract boolean active();
public abstract ImmutableSet<String> emails();
public ImmutableSet<String> secondaryEmails() {
if (!preferredEmail().isPresent()) {
return emails();
}
return ImmutableSet.copyOf(Sets.difference(emails(), ImmutableSet.of(preferredEmail().get())));
}
static Builder builder() {
return new AutoValue_TestAccount.Builder();
}
@@ -46,6 +58,8 @@ public abstract class TestAccount {
abstract Builder active(boolean active);
abstract Builder emails(ImmutableSet<String> emails);
abstract TestAccount build();
}
}

View File

@@ -14,7 +14,10 @@
package com.google.gerrit.acceptance.testsuite.account;
import static com.google.common.base.Preconditions.checkState;
import com.google.auto.value.AutoValue;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.acceptance.testsuite.ThrowingFunction;
import com.google.gerrit.entities.Account;
import java.util.Optional;
@@ -33,6 +36,8 @@ public abstract class TestAccountCreation {
public abstract Optional<Boolean> active();
public abstract ImmutableSet<String> secondaryEmails();
abstract ThrowingFunction<TestAccountCreation, Account.Id> accountCreator();
public static Builder builder(ThrowingFunction<TestAccountCreation, Account.Id> accountCreator) {
@@ -83,14 +88,29 @@ public abstract class TestAccountCreation {
return active(false);
}
public abstract Builder secondaryEmails(ImmutableSet<String> secondaryEmails);
abstract ImmutableSet.Builder<String> secondaryEmailsBuilder();
public Builder addSecondaryEmail(String secondaryEmail) {
secondaryEmailsBuilder().add(secondaryEmail);
return this;
}
abstract Builder accountCreator(
ThrowingFunction<TestAccountCreation, Account.Id> accountCreator);
abstract TestAccountCreation autoBuild();
public Account.Id create() {
TestAccountCreation accountUpdate = autoBuild();
return accountUpdate.accountCreator().applyAndThrowSilently(accountUpdate);
TestAccountCreation accountCreation = autoBuild();
if (accountCreation.preferredEmail().isPresent()) {
checkState(
!accountCreation.secondaryEmails().contains(accountCreation.preferredEmail().get()),
"preferred email %s cannot be secondary email at the same time",
accountCreation.preferredEmail().get());
}
return accountCreation.accountCreator().applyAndThrowSilently(accountCreation);
}
}
}

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();