Merge branch 'stable-2.16' into stable-3.0

* stable-2.16:
  Fix the testAllNumericUserGroup all-numeric username
  Test all-numeric username

Change-Id: I26b459a5c835f1fbf8d3d07107881fb1952ecc27
diff --git a/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java b/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
index 20538b9..5b2bed9 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroup.java
@@ -103,10 +103,11 @@
   @Override
   public GroupDescription.Basic get(AccountGroup.UUID uuid) {
     String ident = username(uuid);
-    Optional<AccountState> state;
+    Optional<AccountState> state = Optional.empty();
     if (ident.matches(ACCOUNT_ID_PATTERN)) {
       state = accountCache.get(new Account.Id(Integer.parseInt(ident)));
-    } else if (ExternalId.isValidUsername(ident)) {
+    }
+    if (!state.isPresent() && ExternalId.isValidUsername(ident)) {
       state = accountCache.getByUsername(ident);
     } else {
       return null;
diff --git a/src/test/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroupTest.java b/src/test/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroupTest.java
index c6e6e80..8d18233 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroupTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/singleusergroup/SingleUserGroupTest.java
@@ -18,9 +18,14 @@
 
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
 import com.google.gerrit.acceptance.NoHttpd;
+import com.google.gerrit.acceptance.TestAccount;
 import com.google.gerrit.acceptance.TestPlugin;
 import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
+import com.google.gerrit.common.data.GroupDescription;
 import com.google.gerrit.extensions.common.GroupInfo;
+import com.google.gerrit.reviewdb.client.AccountGroup;
+import com.google.gerrit.server.IdentifiedUser;
+import com.google.gerrit.server.account.GroupBackend;
 import com.google.inject.Inject;
 import java.util.Map;
 import org.junit.Test;
@@ -32,6 +37,8 @@
 public class SingleUserGroupTest extends LightweightPluginDaemonTest {
   @Inject private RequestScopeOperations requestScopeOperations;
 
+  @Inject private GroupBackend groupBackend;
+
   @Test
   public void testSuggestion() throws Exception {
     // No ability to modify account and therefore no ACL to see secondary email
@@ -39,4 +46,18 @@
     Map<String, GroupInfo> groups = gApi.groups().list().withSuggest("adm").getAsMap();
     assertThat(groups).containsKey("user/Administrator (admin)");
   }
+
+  @Test
+  public void testAllNumericUserGroup() throws Exception {
+    String numericUsername = "123456";
+    TestAccount numericAccount = accountCreator.create(numericUsername);
+    IdentifiedUser numericUser = identifiedUserFactory.create(numericAccount.id());
+
+    GroupDescription.Basic numericUserGroup =
+        groupBackend.get(AccountGroup.UUID.parse("user:" + numericUsername));
+    assertThat(numericUserGroup).isNotNull();
+
+    assertThat(groupBackend.membershipsOf(numericUser).contains(numericUserGroup.getGroupUUID()))
+        .isTrue();
+  }
 }