FromAddressGeneratorProvider: Use AccountCache#maybeGet instead of AccountCache#get

AccountCache#get returns an empty AccountState to represent a missing
account and the full name and preferred email of the empty AccountState
are always null. Handle the missing account case explicitly instead of
using an empty AccountState.

Change-Id: I03d7eeb4139e8756f25b4aebd33b88cd13eeb41c
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2018-01-30 12:54:42 +01:00
parent 4405a7bf05
commit 08b85e0d67
2 changed files with 9 additions and 6 deletions

View File

@@ -20,6 +20,7 @@ import com.google.gerrit.common.data.ParameterizedString;
import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.account.AccountState;
import com.google.gerrit.server.config.AnonymousCowardName;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.mail.Address;
@@ -29,6 +30,7 @@ import com.google.inject.Provider;
import com.google.inject.Singleton;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Optional;
import java.util.regex.Pattern;
import org.apache.commons.codec.binary.Base64;
import org.eclipse.jgit.lib.Config;
@@ -121,9 +123,9 @@ public class FromAddressGeneratorProvider implements Provider<FromAddressGenerat
public Address from(Account.Id fromId) {
String senderName;
if (fromId != null) {
Account a = accountCache.get(fromId).getAccount();
String fullName = a.getFullName();
String userEmail = a.getPreferredEmail();
Optional<Account> a = accountCache.maybeGet(fromId).map(AccountState::getAccount);
String fullName = a.map(Account::getFullName).orElse(null);
String userEmail = a.map(Account::getPreferredEmail).orElse(null);
if (canRelay(userEmail)) {
return new Address(fullName, userEmail);
}
@@ -206,8 +208,8 @@ public class FromAddressGeneratorProvider implements Provider<FromAddressGenerat
final String senderName;
if (fromId != null) {
final Account account = accountCache.get(fromId).getAccount();
String fullName = account.getFullName();
String fullName =
accountCache.maybeGet(fromId).map(a -> a.getAccount().getFullName()).orElse(null);
if (fullName == null || "".equals(fullName)) {
fullName = anonymousCowardName;
}

View File

@@ -30,6 +30,7 @@ import com.google.gerrit.server.config.AllUsersNameProvider;
import com.google.gerrit.server.mail.Address;
import java.util.Arrays;
import java.util.List;
import java.util.Optional;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.lib.PersonIdent;
import org.junit.Before;
@@ -368,7 +369,7 @@ public class FromAddressGeneratorProviderTest {
private Account.Id user(String name, String email) {
final AccountState s = makeUser(name, email);
expect(accountCache.get(eq(s.getAccount().getId()))).andReturn(s);
expect(accountCache.maybeGet(eq(s.getAccount().getId()))).andReturn(Optional.of(s));
return s.getAccount().getId();
}