Add Utils class for tests

Move common test setup logic in a Utils class.

Change-Id: Ibc351af902d059c8f5cebb90adce3db7b4ce79c7
diff --git a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/ConfigSetupUtils.java b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/ConfigSetupUtils.java
new file mode 100644
index 0000000..fc74cef
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/ConfigSetupUtils.java
@@ -0,0 +1,64 @@
+// Copyright (C) 2022 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.gitrepometrics;
+
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doReturn;
+import static org.mockito.Mockito.mock;
+
+import com.google.gerrit.server.config.PluginConfigFactory;
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.List;
+import org.eclipse.jgit.lib.Config;
+
+public class ConfigSetupUtils {
+  final static String pluginName = "git-repo-metrics";
+  private final Path basePath;
+  private final Path gitBasePath;
+  private final List<String> projects;
+
+  public ConfigSetupUtils(List<String> projects) throws IOException {
+    this.basePath = Files.createTempDirectory("git_repo_metrics_");
+    this.gitBasePath = new File(basePath.toFile(), "git").toPath();
+    this.projects = projects;
+  }
+
+  public GitRepoMetricsConfig getGitRepoMetricsConfig() {
+    PluginConfigFactory pluginConfigFactory = mock(PluginConfigFactory.class);
+
+    doReturn(getConfig()).when(pluginConfigFactory).getGlobalPluginConfig(any());
+
+    return new GitRepoMetricsConfig(pluginConfigFactory, "git-repo-metrics");
+  }
+
+  public Config getConfig() {
+    Config c = new Config();
+
+    c.setStringList(pluginName, null, "project", projects);
+    c.setString("gerrit", null, "basePath", gitBasePath.toString());
+    return c;
+  }
+
+  public Path getBasePath() {
+    return basePath;
+  }
+
+  public Path getGitBasePath() {
+    return gitBasePath;
+  }
+}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoMetricsCacheTest.java b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoMetricsCacheTest.java
index 3f202fb..c2e4a4e 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoMetricsCacheTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitRepoMetricsCacheTest.java
@@ -15,33 +15,24 @@
 package com.googlesource.gerrit.plugins.gitrepometrics;
 
 import static com.google.common.truth.Truth.assertThat;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
 import com.google.gerrit.metrics.CallbackMetric0;
 import com.google.gerrit.metrics.Description;
 import com.google.gerrit.metrics.DisabledMetricMaker;
-import com.google.gerrit.server.config.PluginConfigFactory;
 import com.googlesource.gerrit.plugins.gitrepometrics.collectors.GitStats;
+import java.io.IOException;
 import java.util.Collections;
-import org.eclipse.jgit.lib.Config;
 import org.junit.Test;
 
 public class GitRepoMetricsCacheTest {
-  private final String pluginName = "git-repo-metrics";
-  PluginConfigFactory pluginConfigFactory = mock(PluginConfigFactory.class);
-
   GitRepoMetricsCache gitRepoMetricsCacheModule;
   GitRepoMetricsConfig gitRepoMetricsConfig;
   FakeMetricMaker fakeMetricMaker;
 
   @Test
-  public void shouldRegisterMetrics() {
-    Config c = new Config();
-    c.setStringList(pluginName, null, "project", Collections.singletonList("repo1"));
-    doReturn(c).when(pluginConfigFactory).getGlobalPluginConfig(any());
-    gitRepoMetricsConfig = new GitRepoMetricsConfig(pluginConfigFactory, pluginName);
+  public void shouldRegisterMetrics() throws IOException {
+    ConfigSetupUtils configSetupUtils = new ConfigSetupUtils(Collections.singletonList("repo1"));
+    gitRepoMetricsConfig = configSetupUtils.getGitRepoMetricsConfig();
     fakeMetricMaker = new FakeMetricMaker();
     gitRepoMetricsCacheModule = new GitRepoMetricsCache(fakeMetricMaker, gitRepoMetricsConfig);
     gitRepoMetricsCacheModule.initCache();
diff --git a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
index f215614..1975e8c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/GitUpdateListenerTest.java
@@ -40,7 +40,6 @@
 import org.mockito.ArgumentCaptor;
 
 public class GitUpdateListenerTest {
-  private final String pluginName = "git-repo-metrics";
   private final GitRepositoryManager repoManager = new InMemoryRepositoryManager();
   private final ExecutorService mockedExecutorService = mock(ExecutorService.class);
   private GitRepoUpdateListener gitRepoUpdateListener;
@@ -63,7 +62,7 @@
             install(new UpdateGitMetricsTaskModule());
             bind(new TypeLiteral<String>() {})
                 .annotatedWith(PluginName.class)
-                .toInstance(pluginName);
+                .toInstance(ConfigSetupUtils.pluginName);
           }
         };
     Injector injector = Guice.createInjector(m);
diff --git a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTaskTest.java b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTaskTest.java
index 79caa9e..d27c22f 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTaskTest.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/gitrepometrics/UpdateGitMetricsTaskTest.java
@@ -16,14 +16,10 @@
 
 import static com.google.common.truth.Truth.assertThat;
 import static java.nio.file.Files.delete;
-import static org.mockito.ArgumentMatchers.any;
-import static org.mockito.Mockito.doReturn;
-import static org.mockito.Mockito.mock;
 
 import com.google.gerrit.entities.Project;
 import com.google.gerrit.metrics.DisabledMetricMaker;
 import com.google.gerrit.server.config.GerritServerConfig;
-import com.google.gerrit.server.config.PluginConfigFactory;
 import com.google.gerrit.server.config.SitePath;
 import com.google.inject.AbstractModule;
 import com.google.inject.Guice;
@@ -31,7 +27,6 @@
 import com.google.inject.Injector;
 import com.google.inject.Provides;
 import java.io.File;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.util.Collections;
 import org.eclipse.jgit.internal.storage.file.FileRepository;
@@ -41,29 +36,20 @@
 import org.junit.Test;
 
 public class UpdateGitMetricsTaskTest {
-  private final String pluginName = "git-repo-metrics";
   private final String projectName = "testProject";
   private final Project.NameKey projectNameKey = Project.nameKey(projectName);
-  private static PluginConfigFactory pluginConfigFactory = mock(PluginConfigFactory.class);
-  private GitRepoMetricsCache gitRepoMetricsCache;
   private Repository testRepository;
-  Project testProject;
+  private Project testProject;
+  private GitRepoMetricsCache gitRepoMetricsCache;
 
   @Inject private UpdateGitMetricsTask.Factory updateGitMetricsTaskFactory;
 
   @Before
   public void setupRepository() throws Exception {
-    Path basePath = Files.createTempDirectory("git_repo_metrics_");
-    Path gitBasePath = new File(basePath.toFile(), "git").toPath();
-
-    Config c = new Config();
-    c.setStringList(pluginName, null, "project", Collections.singletonList("repo1"));
-    c.setString("gerrit", null, "basePath", gitBasePath.toString());
-    doReturn(c).when(pluginConfigFactory).getGlobalPluginConfig(any());
-
+    ConfigSetupUtils configSetupUtils = new ConfigSetupUtils(Collections.singletonList("repo1"));
     gitRepoMetricsCache =
         new GitRepoMetricsCache(
-            new DisabledMetricMaker(), new GitRepoMetricsConfig(pluginConfigFactory, pluginName));
+            new DisabledMetricMaker(), configSetupUtils.getGitRepoMetricsConfig());
 
     AbstractModule m =
         new AbstractModule() {
@@ -71,23 +57,26 @@
           protected void configure() {
             install(new UpdateGitMetricsTaskModule());
             bind(GitRepoMetricsCache.class).toInstance(gitRepoMetricsCache);
-            bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(c);
+            bind(Config.class)
+                .annotatedWith(GerritServerConfig.class)
+                .toInstance(configSetupUtils.getConfig());
           }
 
           @Provides
           @SitePath
           Path getSitePath() {
-            return gitBasePath;
+            return configSetupUtils.getBasePath();
           }
         };
     Injector injector = Guice.createInjector(m);
     injector.injectMembers(this);
 
     try {
-      testRepository = new FileRepository(new File(gitBasePath.toFile(), projectName));
+      testRepository =
+          new FileRepository(new File(configSetupUtils.getGitBasePath().toFile(), projectName));
       testRepository.create(true);
     } catch (Exception e) {
-      delete(gitBasePath);
+      delete(configSetupUtils.getGitBasePath());
       throw e;
     }
     testProject = Project.builder(projectNameKey).build();