Merge branch 'stable-2.16' into stable-3.0

* stable-2.16:
  Revert "HC returns 500 only with TIMEOUT or FAILED"
  HC returns 500 only with TIMEOUT or FAILED
  AuthHealthCheck: evict account cache by username
  Fix disabling of healthchecks
  Add ability to disable healthchecks
  Fix typo when getting auth username from configuration file

Change-Id: I7e0b68d5a610283399a0919eac2558d2233342b7
diff --git a/README.md b/README.md
index f375030..d5cb3db 100644
--- a/README.md
+++ b/README.md
@@ -37,9 +37,10 @@
 
 - ts: epoch timestamp in millis of the test
 - elapsed: elapsed time in millis to perform the check
+- querychanges: check that Gerrit can query changes
 - reviewdb: check that Gerrit can connect and query ReviewDb
 - projectslist: check that Gerrit can list projects
-- jgit: check that Gerrit can access repositories 
+- jgit: check that Gerrit can access repositories
 
 Each check returns a JSON payload with the following information:
 
@@ -60,9 +61,15 @@
 200 OK
 Content-Type: application/json
 
+)]}'
 {
   "ts": 139402910202,
   "elapsed": 100,
+  "querychanges": {
+    "ts": 139402910202,
+    "elapsed": 20,
+    "result": "passed"
+  },
   "reviewdb": {
     "ts": 139402910202,
     "elapsed": 50,
@@ -89,9 +96,15 @@
 500 ERROR
 Content-Type: application/json
 
+)]}'
 {
   "ts": 139402910202,
   "elapsed": 100,
+  "querychanges": {
+    "ts": 139402910202,
+    "elapsed": 20,
+    "result": "passed"
+  },
   "reviewdb": {
     "ts": 139402910202,
     "elapsed": 50,
@@ -118,4 +131,4 @@
 Some additional metrics are also produced to give extra insights on their result about results and latency of healthcheck
 sub component, such as jgit, reviewdb, etc.
 
-More information can be found in the [config.md](resources/Documentation/config.md) file.
\ No newline at end of file
+More information can be found in the [config.md](resources/Documentation/config.md) file.
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
index 6426fa1..a7eb01a 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckSubsystemsModule.java
@@ -22,13 +22,11 @@
 import com.googlesource.gerrit.plugins.healthcheck.check.JGitHealthCheck;
 import com.googlesource.gerrit.plugins.healthcheck.check.ProjectsListHealthCheck;
 import com.googlesource.gerrit.plugins.healthcheck.check.QueryChangesHealthCheck;
-import com.googlesource.gerrit.plugins.healthcheck.check.ReviewDbHealthCheck;
 
 public class HealthCheckSubsystemsModule extends AbstractModule {
 
   @Override
   protected void configure() {
-    bindChecker(ReviewDbHealthCheck.class);
     bindChecker(JGitHealthCheck.class);
     bindChecker(ProjectsListHealthCheck.class);
     bindChecker(QueryChangesHealthCheck.class);
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
index b160fc4..143d2b1 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/HealthCheckNames.java
@@ -15,7 +15,6 @@
 package com.googlesource.gerrit.plugins.healthcheck.check;
 
 public interface HealthCheckNames {
-  String REVIEWDB = "reviewdb";
   String JGIT = "jgit";
   String PROJECTSLIST = "projectslist";
   String QUERYCHANGES = "querychanges";
diff --git a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ReviewDbHealthCheck.java b/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ReviewDbHealthCheck.java
deleted file mode 100644
index 2a184c8..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/healthcheck/check/ReviewDbHealthCheck.java
+++ /dev/null
@@ -1,47 +0,0 @@
-// Copyright (C) 2019 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.healthcheck.check;
-
-import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.REVIEWDB;
-
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.gerrit.reviewdb.client.CurrentSchemaVersion;
-import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gwtorm.server.SchemaFactory;
-import com.google.inject.Inject;
-import com.google.inject.Singleton;
-import com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig;
-
-@Singleton
-public class ReviewDbHealthCheck extends AbstractHealthCheck {
-  private final SchemaFactory<ReviewDb> reviewDb;
-
-  @Inject
-  public ReviewDbHealthCheck(
-      ListeningExecutorService executor,
-      HealthCheckConfig config,
-      SchemaFactory<ReviewDb> reviewDb) {
-    super(executor, config, REVIEWDB);
-    this.reviewDb = reviewDb;
-  }
-
-  @Override
-  protected Result doCheck() throws Exception {
-    try (ReviewDb db = reviewDb.open()) {
-      db.schemaVersion().get(new CurrentSchemaVersion.Key());
-      return Result.PASSED;
-    }
-  }
-}
diff --git a/src/main/resources/Documentation/config.md b/src/main/resources/Documentation/config.md
index 2b7a224..91d1c0a 100644
--- a/src/main/resources/Documentation/config.md
+++ b/src/main/resources/Documentation/config.md
@@ -14,9 +14,9 @@
 {
   "ts": 139402910202,
   "elapsed": 100,
-  "reviewdb": {
+  "querychanges": {
     "ts": 139402910202,
-    "elapsed": 50,
+    "elapsed": 20,
     "result": "passed"
   },
   "projectslist": {
@@ -44,7 +44,7 @@
 
 The following check names are available:
 
-- `reviewdb` : check connectivity and ability to query ReviewDb
+- `querychanges`: check the ability to query changes
 - `jgit` : check connectivity to the filesystem and ability to open a JGit ref and object
 - `projectslist` : check the ability to list projects with their descriptions
 - `auth`: check the ability to authenticate with username and password
@@ -80,5 +80,5 @@
    Default: healthcheck
 
  - `healthcheck.auth.password` : Password to use for authentication
-
+ 
    Default: no password
\ No newline at end of file
diff --git a/src/resources/Documentation/config.md b/src/resources/Documentation/config.md
index ea51c10..aa59c29 100644
--- a/src/resources/Documentation/config.md
+++ b/src/resources/Documentation/config.md
@@ -48,9 +48,6 @@
 # TYPE plugins_healthcheck_projectslist_latest_measured_latency gauge
 plugins_healthcheck_projectslist_latest_measured_latency 5.0
 
-# HELP plugins_healthcheck_reviewdb_latest_measured_latency Generated from Dropwizard metric import (metric=plugins/healthcheck/reviewdb/latency, type=com.google.gerrit.metrics.dropwizard.CallbackMetricImpl0$1)
-# TYPE plugins_healthcheck_reviewdb_latest_measured_latency gauge
-plugins_healthcheck_reviewdb_latest_measured_latency 3.0
 
 # HELP plugins_healthcheck_jgit_failure_total Generated from Dropwizard metric import (metric=plugins/healthcheck/jgit/failure, type=com.codahale.metrics.Meter)
 # TYPE plugins_healthcheck_jgit_failure_total counter
@@ -59,10 +56,6 @@
 # HELP plugins_healthcheck_projectslist_failure_total Generated from Dropwizard metric import (metric=plugins/healthcheck/projectslist/failure, type=com.codahale.metrics.Meter)
 # TYPE plugins_healthcheck_projectslist_failure_total counter
 plugins_healthcheck_projectslist_failure_total 0.0
-
-# HELP plugins_healthcheck_reviewdb_failure_total Generated from Dropwizard metric import (metric=plugins/healthcheck/reviewdb/failure, type=com.codahale.metrics.Meter)
-# TYPE plugins_healthcheck_reviewdb_failure_total counter
-plugins_healthcheck_reviewdb_failure_total 0.0
 ```
 
 Metrics will be exposed to prometheus by the [metrics-reporter-prometheus](https://gerrit.googlesource.com/plugins/metrics-reporter-prometheus/) plugin.
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
index d23244b..148f8f5 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/HealthCheckIT.java
@@ -19,7 +19,6 @@
 import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.AUTH;
 import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.JGIT;
 import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.QUERYCHANGES;
-import static com.googlesource.gerrit.plugins.healthcheck.check.HealthCheckNames.REVIEWDB;
 
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
 import com.google.gerrit.acceptance.RestResponse;
@@ -63,23 +62,6 @@
   }
 
   @Test
-  public void shouldReturnReviewDbCheck() throws Exception {
-    RestResponse resp = getHealthCheckStatus();
-
-    resp.assertOK();
-    assertCheckResult(getResponseJson(resp), REVIEWDB, "passed");
-  }
-
-  @Test
-  public void shouldReturnReviewDbCheckAsDisabled() throws Exception {
-    disableCheck(REVIEWDB);
-    RestResponse resp = getHealthCheckStatus();
-
-    resp.assertOK();
-    assertCheckResult(getResponseJson(resp), REVIEWDB, "disabled");
-  }
-
-  @Test
   public void shouldReturnJGitCheck() throws Exception {
     RestResponse resp = getHealthCheckStatus();
 
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/JGitHealthCheckTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/JGitHealthCheckTest.java
index 1a8ec63..59dfd71 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/JGitHealthCheckTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/JGitHealthCheckTest.java
@@ -67,9 +67,9 @@
 
   @Test
   public void shouldBeHealthyWhenJGitIsWorking() {
-    JGitHealthCheck reviewDbCheck =
+    JGitHealthCheck check =
         new JGitHealthCheck(executor, DEFAULT_CONFIG, getWorkingRepositoryManager());
-    assertThat(reviewDbCheck.run().result).isEqualTo(Result.PASSED);
+    assertThat(check.run().result).isEqualTo(Result.PASSED);
   }
 
   @Test
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 cb41000..2ab0d83 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ProjectsListHealthCheckTest.java
@@ -34,6 +34,8 @@
 public class ProjectsListHealthCheckTest {
   @Inject private ListeningExecutorService executor;
 
+  private Config gerritConfig = new Config();
+
   @Before
   public void setUp() throws Exception {
     Guice.createInjector(new HealthCheckModule()).injectMembers(this);
@@ -62,7 +64,8 @@
   }
 
   private ListProjects getFailingProjectList() {
-    return new ListProjects(null, null, null, null, null, null, null, null, null, new Config()) {
+    return 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");
@@ -71,7 +74,8 @@
   }
 
   private ListProjects getWorkingProjectList(long execTime) {
-    return new ListProjects(null, null, null, null, null, null, null, null, null, new Config()) {
+    return 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<>();
diff --git a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ReviewDbHealthCheckTest.java b/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ReviewDbHealthCheckTest.java
deleted file mode 100644
index 5c85f42..0000000
--- a/src/test/java/com/googlesource/gerrit/plugins/healthcheck/ReviewDbHealthCheckTest.java
+++ /dev/null
@@ -1,68 +0,0 @@
-// Copyright (C) 2019 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-// http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package com.googlesource.gerrit.plugins.healthcheck;
-
-import static com.google.common.truth.Truth.assertThat;
-import static com.googlesource.gerrit.plugins.healthcheck.HealthCheckConfig.DEFAULT_CONFIG;
-
-import com.google.common.util.concurrent.ListeningExecutorService;
-import com.google.gerrit.lifecycle.LifecycleManager;
-import com.google.gerrit.reviewdb.server.ReviewDb;
-import com.google.gerrit.testing.DisabledReviewDb;
-import com.google.gerrit.testing.InMemoryDatabase;
-import com.google.gwtorm.server.SchemaFactory;
-import com.google.inject.Guice;
-import com.google.inject.Inject;
-import com.google.inject.Injector;
-import com.googlesource.gerrit.plugins.healthcheck.check.HealthCheck;
-import com.googlesource.gerrit.plugins.healthcheck.check.ReviewDbHealthCheck;
-import org.junit.Before;
-import org.junit.Test;
-
-public class ReviewDbHealthCheckTest {
-  private SchemaFactory<ReviewDb> workingReviewDbFactory;
-  @Inject private ListeningExecutorService executor;
-
-  @Before
-  public void setUp() throws Exception {
-    Injector testInjector = Guice.createInjector(new HealthCheckModule());
-    testInjector.injectMembers(this);
-
-    workingReviewDbFactory = InMemoryDatabase.newDatabase(new LifecycleManager()).create();
-  }
-
-  @Test
-  public void shouldBeHealthyWhenReviewDbIsWorking() {
-    ReviewDbHealthCheck reviewDbCheck =
-        new ReviewDbHealthCheck(executor, DEFAULT_CONFIG, workingReviewDbFactory);
-    assertThat(reviewDbCheck.run().result).isEqualTo(HealthCheck.Result.PASSED);
-  }
-
-  @Test
-  public void shouldBeUnhealthyWhenReviewDbIsFailing() {
-    ReviewDbHealthCheck reviewDbCheck =
-        new ReviewDbHealthCheck(executor, DEFAULT_CONFIG, getFailingReviewDbProvider());
-    assertThat(reviewDbCheck.run().result).isEqualTo(HealthCheck.Result.FAILED);
-  }
-
-  private SchemaFactory<ReviewDb> getFailingReviewDbProvider() {
-    return new SchemaFactory<ReviewDb>() {
-      @Override
-      public ReviewDb open() {
-        return new DisabledReviewDb();
-      }
-    };
-  }
-}