Use InternalUser for checking projects list

Introduce an one-off context for using the internal user
of the plugin for checking the healthiness of the projects
list.

Change-Id: I4c77cffac6094b233d3bc69d5875e5998e8e5dea
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
index c18009a..78cff2a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ProjectsListHealthCheck.java
@@ -19,7 +19,10 @@
 import com.google.common.util.concurrent.ListeningExecutorService;
 import com.google.gerrit.extensions.common.ProjectInfo;
 import com.google.gerrit.server.restapi.project.ListProjects;
+import com.google.gerrit.server.util.ManualRequestContext;
+import com.google.gerrit.server.util.OneOffRequestContext;
 import com.google.inject.Inject;
+import com.google.inject.Provider;
 import com.google.inject.Singleton;
 import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
 import com.googlesource.gerrit.plugins.healthcheck.HealthCheckMetrics;
@@ -31,34 +34,40 @@
 public class ProjectsListHealthCheck extends AbstractHealthCheck {
   private static final Logger log = LoggerFactory.getLogger(ProjectsListHealthCheck.class);
   private static final int PROJECTS_LIST_LIMIT = 100;
-  private final ListProjects listProjects;
+  private final Provider<ListProjects> listProjectsProvider;
+  private final OneOffRequestContext oneOffCtx;
 
   @Inject
   public ProjectsListHealthCheck(
       ListeningExecutorService executor,
       HealthCheckConfig config,
-      ListProjects listProjects,
+      Provider<ListProjects> listProjectsProvider,
+      OneOffRequestContext oneOffCtx,
       HealthCheckMetrics.Factory healthCheckMetricsFactory) {
     super(executor, config, PROJECTSLIST, healthCheckMetricsFactory);
 
-    this.listProjects = listProjects;
+    this.listProjectsProvider = listProjectsProvider;
+    this.oneOffCtx = oneOffCtx;
   }
 
   @Override
   protected Result doCheck() {
-    listProjects.setStart(0);
-    listProjects.setLimit(PROJECTS_LIST_LIMIT);
-    listProjects.setShowDescription(true);
-    listProjects.setMatchPrefix("All-");
-    try {
-      SortedMap<String, ProjectInfo> projects = listProjects.apply();
-      if (projects != null && projects.size() > 0) {
-        return Result.PASSED;
+    try (ManualRequestContext ctx = oneOffCtx.open()) {
+      ListProjects listProjects = listProjectsProvider.get();
+      listProjects.setStart(0);
+      listProjects.setLimit(PROJECTS_LIST_LIMIT);
+      listProjects.setShowDescription(true);
+      listProjects.setMatchPrefix("All-");
+      try {
+        SortedMap<String, ProjectInfo> projects = listProjects.apply();
+        if (projects != null && projects.size() > 0) {
+          return Result.PASSED;
+        }
+        log.warn("Empty or null projects list: Gerrit should always have at least 1 project");
+      } catch (Exception e) {
+        log.warn("Unable to list projects", e);
       }
-      log.warn("Empty or null projects list: Gerrit should always have at least 1 project");
-    } catch (Exception e) {
-      log.warn("Unable to list projects", e);
+      return Result.FAILED;
     }
-    return Result.FAILED;
   }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
index c8d25b6..58009ea 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
@@ -21,8 +21,11 @@
 import com.google.gerrit.extensions.common.ProjectInfo;
 import com.google.gerrit.extensions.restapi.BadRequestException;
 import com.google.gerrit.server.restapi.project.ListProjects;
+import com.google.gerrit.server.util.OneOffRequestContext;
 import com.google.inject.Guice;
 import com.google.inject.Inject;
+import com.google.inject.Provider;
+import com.google.inject.util.Providers;
 import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheck.Result;
 import com.googlesource.gerrit.plugins.healthcheck.check.ProjectsListHealthCheck;
 import java.util.SortedMap;
@@ -30,7 +33,11 @@
 import org.eclipse.jgit.lib.Config;
 import org.junit.Before;
 import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnitRunner;
 
+@RunWith(MockitoJUnitRunner.class)
 public class ProjectsListHealthCheckTest {
   @Inject private ListeningExecutorService executor;
 
@@ -38,6 +45,8 @@
 
   private Config gerritConfig = new Config();
 
+  @Mock private OneOffRequestContext mockOneOffCtx;
+
   @Before
   public void setUp() throws Exception {
     Guice.createInjector(new HealthCheckModule()).injectMembers(this);
@@ -47,7 +56,11 @@
   public void shouldBeHealthyWhenListProjectsWorks() {
     ProjectsListHealthCheck jGitHealthCheck =
         new ProjectsListHealthCheck(
-            executor, DEFAULT_CONFIG, getWorkingProjectList(0), healthCheckMetricsFactory);
+            executor,
+            DEFAULT_CONFIG,
+            getWorkingProjectList(0),
+            mockOneOffCtx,
+            healthCheckMetricsFactory);
     assertThat(jGitHealthCheck.run().result).isEqualTo(Result.PASSED);
   }
 
@@ -55,7 +68,11 @@
   public void shouldBeUnhealthyWhenListProjectsIsFailing() {
     ProjectsListHealthCheck jGitHealthCheck =
         new ProjectsListHealthCheck(
-            executor, DEFAULT_CONFIG, getFailingProjectList(), healthCheckMetricsFactory);
+            executor,
+            DEFAULT_CONFIG,
+            getFailingProjectList(),
+            mockOneOffCtx,
+            healthCheckMetricsFactory);
     assertThat(jGitHealthCheck.run().result).isEqualTo(Result.FAILED);
   }
 
@@ -66,34 +83,37 @@
             executor,
             DEFAULT_CONFIG,
             getWorkingProjectList(DEFAULT_CONFIG.getTimeout() * 2),
+            mockOneOffCtx,
             healthCheckMetricsFactory);
     assertThat(jGitHealthCheck.run().result).isEqualTo(Result.TIMEOUT);
   }
 
-  private ListProjects getFailingProjectList() {
-    return new ListProjects(null, null, null, null, null, null, null, null, null, gerritConfig) {
+  private Provider<ListProjects> getFailingProjectList() {
+    return Providers.of(
+        new ListProjects(null, null, null, null, null, null, null, null, null, gerritConfig) {
 
-      @Override
-      public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
-        throw new IllegalArgumentException("Unable to return project list");
-      }
-    };
+          @Override
+          public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
+            throw new IllegalArgumentException("Unable to return project list");
+          }
+        });
   }
 
-  private ListProjects getWorkingProjectList(long execTime) {
-    return new ListProjects(null, null, null, null, null, null, null, null, null, gerritConfig) {
+  private Provider<ListProjects> getWorkingProjectList(long execTime) {
+    return Providers.of(
+        new ListProjects(null, null, null, null, null, null, null, null, null, gerritConfig) {
 
-      @Override
-      public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
-        SortedMap<String, ProjectInfo> projects = new TreeMap<>();
-        projects.put("testproject", new ProjectInfo());
-        try {
-          Thread.sleep(execTime);
-        } catch (InterruptedException e) {
-          throw new IllegalStateException(e);
-        }
-        return projects;
-      }
-    };
+          @Override
+          public SortedMap<String, ProjectInfo> apply() throws BadRequestException {
+            SortedMap<String, ProjectInfo> projects = new TreeMap<>();
+            projects.put("testproject", new ProjectInfo());
+            try {
+              Thread.sleep(execTime);
+            } catch (InterruptedException e) {
+              throw new IllegalStateException(e);
+            }
+            return projects;
+          }
+        });
   }
 }