Merge branch 'stable-3.0'

* stable-3.0:
  Bump Bazel version to 2.2.0
  Re-add support for projects with anonymous HTTP access

Adapt to project cache returning Optional in latest master. Switch to
snapshot API so this builds again.

Change-Id: I86e5acd26de085a6e82c59d3560d1ad9cc6793b8
diff --git a/.gitignore b/.gitignore
index f3a0d53..a832ec3 100644
--- a/.gitignore
+++ b/.gitignore
@@ -3,3 +3,4 @@
 /.settings
 /bazel-*
 /eclipse-out
+/.apt_generated/
diff --git a/WORKSPACE b/WORKSPACE
index 48c34ee..4faf63f 100644
--- a/WORKSPACE
+++ b/WORKSPACE
@@ -3,24 +3,24 @@
 load("//:bazlets.bzl", "load_bazlets")
 
 load_bazlets(
-    commit = "96f691ebbf4ef1c46b798e871ed5acd9d844651c",
+    commit = "f30a992da9fc855dce819875afb59f9dd6f860cd",
     #local_path = "/home/<user>/projects/bazlets",
 )
 
 # Snapshot Plugin API
-#load(
-#    "@com_googlesource_gerrit_bazlets//:gerrit_api_maven_local.bzl",
-#    "gerrit_api_maven_local",
-#)
-
-# Load snapshot Plugin API
-#gerrit_api_maven_local()
-
-# Release Plugin API
 load(
-    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
-    "gerrit_api",
+    "@com_googlesource_gerrit_bazlets//:gerrit_api_maven_local.bzl",
+    "gerrit_api_maven_local",
 )
 
+# Load snapshot Plugin API
+gerrit_api_maven_local()
+
+# Release Plugin API
+#load(
+#    "@com_googlesource_gerrit_bazlets//:gerrit_api.bzl",
+#    "gerrit_api",
+#)
+
 # Load release Plugin API
-gerrit_api()
+#gerrit_api()
diff --git a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
index d321774..93e4ba2 100644
--- a/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
+++ b/src/main/java/com/ericsson/gerrit/plugins/goimport/GoImportFilter.java
@@ -16,11 +16,11 @@
 
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Strings;
+import com.google.gerrit.entities.BranchNameKey;
+import com.google.gerrit.entities.Project;
+import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.httpd.AllRequestFilter;
 import com.google.gerrit.httpd.HtmlDomUtil;
-import com.google.gerrit.reviewdb.client.Branch;
-import com.google.gerrit.reviewdb.client.Project;
-import com.google.gerrit.reviewdb.client.RefNames;
 import com.google.gerrit.server.AnonymousUser;
 import com.google.gerrit.server.config.CanonicalWebUrl;
 import com.google.gerrit.server.permissions.PermissionBackend;
@@ -29,14 +29,15 @@
 import com.google.gerrit.server.project.ProjectState;
 import com.google.gerrit.util.http.CacheHeaders;
 import com.google.inject.Inject;
-import com.google.inject.Singleton;
 import com.google.inject.Provider;
+import com.google.inject.Singleton;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.net.URI;
 import java.net.URISyntaxException;
 import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.Optional;
 import java.util.regex.Pattern;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
@@ -174,7 +175,7 @@
         + (authenticated ? "a/" : "")
         + projectName
         + " git "
-	+ getRepoRoot(projectName, authenticated);
+        + getRepoRoot(projectName, authenticated);
   }
 
   private String getRepoRoot(String projectName, boolean authenticated) {
@@ -186,14 +187,13 @@
 
   private boolean allowsAnonymousAccess(String projectName) {
     AnonymousUser anonymous = anonProvider.get();
-    Branch.NameKey heads =
-        new Branch.NameKey(new Project.NameKey(projectName), RefNames.REFS_HEADS);
+    BranchNameKey heads = BranchNameKey.create(Project.nameKey(projectName), RefNames.REFS_HEADS);
 
     return permissions.user(anonymous).ref(heads).testOrFalse(RefPermission.READ);
   }
 
   private boolean projectExists(String projectName) {
-    ProjectState p = projectCache.get(new Project.NameKey(projectName));
+    Optional<ProjectState> p = projectCache.get(Project.nameKey(projectName));
     return p != null;
   }
 }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
index 0939935..39f01df 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/goimport/GoImportFilterTest.java
@@ -17,14 +17,14 @@
 import static com.ericsson.gerrit.plugins.goimport.GoImportFilter.CONTENT_PLH;
 import static com.ericsson.gerrit.plugins.goimport.GoImportFilter.PAGE_200;
 import static com.google.common.truth.Truth.assertThat;
-import static org.junit.Assert.fail;
+import static com.google.gerrit.testing.GerritJUnit.assertThrows;
 import static org.mockito.Mockito.any;
 import static org.mockito.Mockito.doThrow;
 import static org.mockito.Mockito.times;
 import static org.mockito.Mockito.verify;
 import static org.mockito.Mockito.when;
 
-import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.entities.Project;
 import com.google.gerrit.server.AnonymousUser;
 import com.google.gerrit.server.permissions.PermissionBackend;
 import com.google.gerrit.server.permissions.RefPermission;
@@ -33,6 +33,7 @@
 import com.google.inject.Provider;
 import java.io.IOException;
 import java.net.URISyntaxException;
+import java.util.Optional;
 import javax.servlet.FilterChain;
 import javax.servlet.ServletException;
 import javax.servlet.ServletOutputStream;
@@ -140,7 +141,8 @@
   public void testDoFilterWithExistingProject() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME);
     when(mockRequest.getParameter("go-get")).thenReturn("1");
-    when(mockProjectCache.get(new Project.NameKey(PROJECT_NAME))).thenReturn(mockProjectState);
+    when(mockProjectCache.get(Project.nameKey(PROJECT_NAME)))
+        .thenReturn(Optional.of(mockProjectState));
     when(mockPermsForRef.testOrFalse(RefPermission.READ)).thenReturn(false);
     unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
     verify(mockOutputStream, times(1)).write(response200(false, false));
@@ -154,7 +156,8 @@
   public void testDoFilterAuthenticatedWithExistingProject() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/a/" + PROJECT_NAME);
     when(mockRequest.getParameter("go-get")).thenReturn("1");
-    when(mockProjectCache.get(new Project.NameKey(PROJECT_NAME))).thenReturn(mockProjectState);
+    when(mockProjectCache.get(Project.nameKey(PROJECT_NAME)))
+        .thenReturn(Optional.of(mockProjectState));
     when(mockPermsForRef.testOrFalse(RefPermission.READ)).thenReturn(false);
     unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
     verify(mockOutputStream, times(1)).write(response200(false, true));
@@ -168,7 +171,6 @@
   public void testDoFilterWithExistingProjectAndPackage() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME + "/my/package");
     when(mockRequest.getParameter("go-get")).thenReturn("1");
-    when(mockProjectCache.get(new Project.NameKey(PROJECT_NAME))).thenReturn(mockProjectState);
     when(mockPermsForRef.testOrFalse(RefPermission.READ)).thenReturn(false);
     unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
     verify(mockOutputStream, times(1)).write(response200(false, false));
@@ -181,7 +183,8 @@
   public void testDoFilterWithAnonymousAccessibleProject() throws Exception {
     when(mockRequest.getServletPath()).thenReturn("/projectName");
     when(mockRequest.getParameter("go-get")).thenReturn("1");
-    when(mockProjectCache.get(new Project.NameKey("projectName"))).thenReturn(mockProjectState);
+    when(mockProjectCache.get(Project.nameKey("projectName")))
+        .thenReturn(Optional.of(mockProjectState));
     when(mockPermsForRef.testOrFalse(RefPermission.READ)).thenReturn(true);
     unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
     verify(mockOutputStream, times(1)).write(response200(true, false));
@@ -209,26 +212,22 @@
     when(mockRequest.getServletPath()).thenReturn("/" + PROJECT_NAME);
     when(mockRequest.getParameter("go-get")).thenReturn("1");
     doThrow(new IOException(msg)).when(mockOutputStream).write(any(byte[].class));
-    when(mockProjectCache.get(any(Project.NameKey.class))).thenReturn(mockProjectState);
-    try {
-      unitUnderTest.doFilter(mockRequest, mockResponse, mockChain);
-      fail("IOException should occur!");
-    } catch (IOException e) {
-      assertThat(msg).isEqualTo(e.getMessage());
-      verify(mockOutputStream, times(1)).write(any(byte[].class));
-    }
+    when(mockProjectCache.get(any(Project.NameKey.class)))
+        .thenReturn(Optional.of(mockProjectState));
+    IOException thrown =
+        assertThrows(
+            IOException.class, () -> unitUnderTest.doFilter(mockRequest, mockResponse, mockChain));
+    assertThat(thrown).hasMessageThat().isEqualTo(msg);
+    verify(mockOutputStream, times(1)).write(any(byte[].class));
   }
 
   @Test
   public void testDoFilterWithServletException() throws Exception {
     String msg = "test-serv-error";
     doThrow(new ServletException(msg)).when(mockChain).doFilter(null, null);
-    try {
-      unitUnderTest.doFilter(null, null, mockChain);
-      fail("ServletException should occur!");
-    } catch (ServletException e) {
-      assertThat(msg).isEqualTo(e.getMessage());
-      verify(mockChain, times(1)).doFilter(null, null);
-    }
+    ServletException thrown =
+        assertThrows(ServletException.class, () -> unitUnderTest.doFilter(null, null, mockChain));
+    assertThat(thrown).hasMessageThat().isEqualTo(msg);
+    verify(mockChain, times(1)).doFilter(null, null);
   }
 }
diff --git a/src/test/java/com/ericsson/gerrit/plugins/goimport/HttpModuleTest.java b/src/test/java/com/ericsson/gerrit/plugins/goimport/HttpModuleTest.java
index a0a107d..021c154 100644
--- a/src/test/java/com/ericsson/gerrit/plugins/goimport/HttpModuleTest.java
+++ b/src/test/java/com/ericsson/gerrit/plugins/goimport/HttpModuleTest.java
@@ -51,7 +51,7 @@
     GoImportFilter filter1 = injector.getInstance(GoImportFilter.class);
     assertThat(filter1).isNotNull();
     GoImportFilter filter2 = injector.getInstance(GoImportFilter.class);
-    assertThat(filter1).isSameAs(filter2);
+    assertThat(filter1).isSameInstanceAs(filter2);
   }
 
   public class TestModule extends AbstractModule {