Merge branch 'stable-3.1' into 'stable-3.2'

* stable-3.1:
  Upgrade bazlets to latest stable-3.1 to build with 3.1.11 API

Change-Id: Ia207810c5b756941a26b6eb60692b910c62fd169
diff --git a/WORKSPACE b/WORKSPACE
index 3c2940e..0218c47 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,7 +3,7 @@
 load("//:bazlets.bzl", "load_bazlets")
 
 load_bazlets(
-    commit = "20079f696c22c733053077b13c0dc6d9902e6b8e",
+    commit = "cf0bbc90e09a8a1d7c042d79f1555e3fa40984e1",
     #local_path = "/home/<user>/projects/bazlets",
 )
 
diff --git a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
index 7f0942b..b491d16 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRights.java
@@ -15,6 +15,7 @@
 package com.ericsson.gerrit.plugins.projectgroupstructure;
 
 import com.google.gerrit.common.data.AccessSection;
+import com.google.gerrit.common.data.GroupReference;
 import com.google.gerrit.common.data.Permission;
 import com.google.gerrit.common.data.PermissionRule;
 import com.google.gerrit.entities.AccountGroup;
@@ -97,15 +98,16 @@
       return;
     }
 
-    ProjectState project = projectCache.get(Project.NameKey.parse(projectName));
-    if (project == null) {
+    Optional<ProjectState> project = projectCache.get(Project.NameKey.parse(projectName));
+    if (!project.isPresent()) {
       log.error("Could not retrieve projet {} from cache", projectName);
       return;
     }
 
-    try (MetaDataUpdate md = metaDataUpdateFactory.create(project.getProject().getNameKey())) {
+    try (MetaDataUpdate md =
+        metaDataUpdateFactory.create(project.get().getProject().getNameKey())) {
       ProjectConfig config = projectConfigFactory.read(md);
-      setAccessRights(config, project);
+      setAccessRights(config, project.get());
       md.setMessage("Set default access rights\n");
       config.commit(md);
     } catch (Exception e) {
@@ -197,7 +199,7 @@
           log.error("Group {} not found", rule.getGroup().getName());
           continue;
         }
-        rule.getGroup().setUUID(group.get().getGroupUUID());
+        rule.setGroup(new GroupReference(group.get().getGroupUUID(), rule.getGroup().getName()));
       }
       perm.add(rule);
     }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
index 5d562e0..2f93c5b 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/DefaultAccessRightsIT.java
@@ -32,6 +32,7 @@
 import com.google.gerrit.server.project.ProjectState;
 import com.google.inject.Inject;
 import java.nio.file.Files;
+import java.util.Optional;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -83,10 +84,11 @@
     in.permissionsOnly = true;
     String projectName = name("someProject");
     userRestSession.put("/projects/" + projectName, in).assertCreated();
+    projectCache.evict(Project.nameKey(projectName));
 
-    ProjectState projectState = projectCache.get(Project.nameKey(projectName));
-    AccountGroup.UUID ownerUUID = projectState.getOwners().iterator().next();
-    ProjectConfig projectConfig = projectState.getConfig();
+    Optional<ProjectState> projectState = projectCache.get(Project.nameKey(projectName));
+    AccountGroup.UUID ownerUUID = projectState.get().getOwners().iterator().next();
+    ProjectConfig projectConfig = projectState.get().getConfig();
 
     assertThat(projectConfig.getAccessSections().size()).isEqualTo(2);
 
@@ -139,7 +141,12 @@
     adminRestSession.put("/projects/" + Url.encode(projectName), in).assertCreated();
 
     assertThat(
-            projectCache.get(Project.nameKey(projectName)).getConfig().getAccessSections().size())
+            projectCache
+                .get(Project.nameKey(projectName))
+                .get()
+                .getConfig()
+                .getAccessSections()
+                .size())
         .isEqualTo(0);
   }
 }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
index b190c59..f85ab27 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/projectgroupstructure/ProjectCreationValidatorIT.java
@@ -35,6 +35,7 @@
 import com.google.gerrit.server.project.ProjectConfig;
 import com.google.gerrit.server.project.ProjectState;
 import com.google.inject.Inject;
+import java.util.Optional;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -172,9 +173,9 @@
     ProjectInput in = new ProjectInput();
     in.permissionsOnly = true;
     userRestSession.put("/projects/" + rootProject, in).assertCreated();
-    ProjectState projectState = projectCache.get(Project.nameKey(rootProject));
-    assertThat(projectState.getOwners().size()).isEqualTo(1);
-    assertThat(projectState.getOwners())
+    Optional<ProjectState> projectState = projectCache.get(Project.nameKey(rootProject));
+    assertThat(projectState.get().getOwners().size()).isEqualTo(1);
+    assertThat(projectState.get().getOwners())
         .contains(
             groupCache.get(AccountGroup.nameKey(rootProject + "-admins")).get().getGroupUUID());
 
@@ -184,7 +185,7 @@
     gApi.groups().create(existingGroupName);
     userRestSession.put("/projects/" + rootProject, in).assertCreated();
     projectState = projectCache.get(Project.nameKey(rootProject));
-    assertThat(projectState.getOwners().size()).isEqualTo(1);
+    assertThat(projectState.get().getOwners().size()).isEqualTo(1);
     String expectedOwnerGroup =
         existingGroupName
             + "-"
@@ -192,7 +193,7 @@
                 .hashString(existingGroupName, Charsets.UTF_8)
                 .toString()
                 .substring(0, 7);
-    assertThat(projectState.getOwners())
+    assertThat(projectState.get().getOwners())
         .contains(groupCache.get(AccountGroup.nameKey(expectedOwnerGroup)).get().getGroupUUID());
   }
 
@@ -264,9 +265,9 @@
     in.parent = parent;
     String childProject = parent + "/childProject";
     userRestSession.put("/projects/" + Url.encode(childProject), in).assertCreated();
-    ProjectState projectState = projectCache.get(Project.nameKey(childProject));
-    assertThat(projectState.getOwners().size()).isEqualTo(1);
-    assertThat(projectState.getOwners())
+    Optional<ProjectState> projectState = projectCache.get(Project.nameKey(childProject));
+    assertThat(projectState.get().getOwners().size()).isEqualTo(1);
+    assertThat(projectState.get().getOwners())
         .contains(
             groupCache.get(AccountGroup.nameKey(childProject + "-admins")).get().getGroupUUID());
 
@@ -276,7 +277,7 @@
     gApi.groups().create(existingGroupName);
     userRestSession.put("/projects/" + Url.encode(childProject2), in).assertCreated();
     projectState = projectCache.get(Project.nameKey(childProject2));
-    assertThat(projectState.getOwners().size()).isEqualTo(1);
+    assertThat(projectState.get().getOwners().size()).isEqualTo(1);
     String expectedOwnerGroup =
         existingGroupName
             + "-"
@@ -284,7 +285,7 @@
                 .hashString(existingGroupName, Charsets.UTF_8)
                 .toString()
                 .substring(0, 7);
-    assertThat(projectState.getOwners())
+    assertThat(projectState.get().getOwners())
         .contains(groupCache.get(AccountGroup.nameKey(expectedOwnerGroup)).get().getGroupUUID());
   }
 
@@ -316,8 +317,8 @@
     in.parent = parent;
     String childProject = parent + "/childProject";
     userRestSession.put("/projects/" + Url.encode(childProject), in).assertCreated();
-    ProjectState projectState = projectCache.get(Project.nameKey(childProject));
-    assertThat(projectState.getOwners().size()).isEqualTo(0);
+    Optional<ProjectState> projectState = projectCache.get(Project.nameKey(childProject));
+    assertThat(projectState.get().getOwners().size()).isEqualTo(0);
   }
 
   @Test
@@ -483,6 +484,8 @@
     dGroup.addMembers(user.username());
     // the group is in the project.config
     Project.NameKey parentNameKey = Project.nameKey(parent);
+    projectCache.evict(parentNameKey);
+
     try (ProjectConfigUpdate cfgUpdate = updateProject(parentNameKey)) {
       ProjectConfig cfg = cfgUpdate.getConfig();
 
@@ -492,10 +495,10 @@
               ProjectCreationValidator.DELEGATE_PROJECT_CREATION_TO,
               new GroupReference(AccountGroup.UUID.parse(gId), delegatingGroup));
       cfgUpdate.save();
-    }
 
-    String newDelegatingGroup = name("groupC");
-    gApi.groups().id(delegatingGroup).name(newDelegatingGroup);
+      String newDelegatingGroup = name("groupC");
+      gApi.groups().id(delegatingGroup).name(newDelegatingGroup);
+    }
 
     userRestSession.put("/projects/" + Url.encode(parent + "/childProject"), in).assertCreated();
   }