Allow tests to check how much Counter0 metrics are increased by a call

E.g. this allows tests to verify how often something is invoked or to
test that metrics work as expected.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I79da799c86325eb64922a92caec8a476e4e6a4ae
(cherry picked from commit 9b8fbcf686bdeb32e4b03bec19fafb171543ea6c)
diff --git a/java/com/google/gerrit/acceptance/InMemoryTestingDatabaseModule.java b/java/com/google/gerrit/acceptance/InMemoryTestingDatabaseModule.java
index a3207e2..de9a43d 100644
--- a/java/com/google/gerrit/acceptance/InMemoryTestingDatabaseModule.java
+++ b/java/com/google/gerrit/acceptance/InMemoryTestingDatabaseModule.java
@@ -20,7 +20,6 @@
 import com.google.gerrit.exceptions.StorageException;
 import com.google.gerrit.extensions.events.LifecycleListener;
 import com.google.gerrit.lifecycle.LifecycleModule;
-import com.google.gerrit.metrics.DisabledMetricMaker;
 import com.google.gerrit.metrics.MetricMaker;
 import com.google.gerrit.server.config.GerritServerConfig;
 import com.google.gerrit.server.config.SitePath;
@@ -64,7 +63,7 @@
       bind(InMemoryRepositoryManager.class).in(SINGLETON);
     }
 
-    bind(MetricMaker.class).to(DisabledMetricMaker.class);
+    bind(MetricMaker.class).to(TestMetricMaker.class);
 
     listener().to(CreateSchema.class);
 
diff --git a/java/com/google/gerrit/acceptance/TestMetricMaker.java b/java/com/google/gerrit/acceptance/TestMetricMaker.java
new file mode 100644
index 0000000..d60ef1a
--- /dev/null
+++ b/java/com/google/gerrit/acceptance/TestMetricMaker.java
@@ -0,0 +1,78 @@
+// Copyright (C) 2021 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.google.gerrit.acceptance;
+
+import com.google.gerrit.metrics.Counter0;
+import com.google.gerrit.metrics.Description;
+import com.google.gerrit.metrics.DisabledMetricMaker;
+import com.google.inject.Singleton;
+import java.util.HashMap;
+import java.util.Map;
+import org.apache.commons.lang.mutable.MutableLong;
+
+/**
+ * {@link com.google.gerrit.metrics.MetricMaker} to be bound in tests.
+ *
+ * <p>Records how often {@link Counter0} metrics are invoked. Metrics of other types are not
+ * recorded.
+ *
+ * <p>Allows test to check how much a {@link Counter0} metrics is increased by an operation.
+ *
+ * <p>Example:
+ *
+ * <pre>
+ * public class MyTest extends AbstractDaemonTest {
+ *   @Inject private TestMetricMaker testMetricMaker;
+ *
+ *   ...
+ *
+ *   @Test
+ *   public void testFoo() throws Exception {
+ *     testMetricMaker.reset();
+ *     doSomething();
+ *     assertThat(testMetricMaker.getCount("foo/bar_count")).isEqualsTo(1);
+ *   }
+ * }
+ * </pre>
+ */
+@Singleton
+public class TestMetricMaker extends DisabledMetricMaker {
+  private final Map<String, MutableLong> counts = new HashMap<>();
+
+  public long getCount(String counter0Name) {
+    return get(counter0Name).longValue();
+  }
+
+  public void reset() {
+    counts.clear();
+  }
+
+  private MutableLong get(String counter0Name) {
+    return counts.computeIfAbsent(counter0Name, name -> new MutableLong(0));
+  }
+
+  @Override
+  public Counter0 newCounter(String name, Description desc) {
+    return new Counter0() {
+      @Override
+      public void incrementBy(long value) {
+        get(name).add(value);
+      }
+
+      @Override
+      public void remove() {}
+    };
+  }
+}