Test behaviour with multiple configured required approvals

If in a JGit config file multiple values are configured for a key, but
only a single value is read (by Config#getString(String, String,
String)) the last value is returned by JGit. Cover this behaviour for
required approvals by tests so that we do not break this behavior when
we are going to support multiple values as override approval, but not as
required approval (to be implemented by follow-up changes).

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I437144bdcf054aa145adfa963f77c9e094771a55
diff --git a/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java b/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
index 2f1be86..e06e1bb 100644
--- a/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
+++ b/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
@@ -16,6 +16,7 @@
 
 import static com.google.gerrit.server.group.SystemGroupBackend.REGISTERED_USERS;
 
+import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import com.google.gerrit.acceptance.GitUtil;
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
@@ -40,6 +41,7 @@
 import com.google.inject.Inject;
 import java.nio.file.Path;
 import java.util.Map;
+import java.util.function.Consumer;
 import org.eclipse.jgit.internal.storage.dfs.InMemoryRepository;
 import org.eclipse.jgit.junit.TestRepository;
 import org.eclipse.jgit.lib.Config;
@@ -145,9 +147,30 @@
   protected void setCodeOwnersConfig(
       Project.NameKey project, @Nullable String subsection, String key, String value)
       throws Exception {
+    updateCodeOwnersConfig(
+        project,
+        codeOwnersConfig ->
+            codeOwnersConfig.setString(
+                CodeOwnersPluginConfiguration.SECTION_CODE_OWNERS, subsection, key, value));
+  }
+
+  protected void setCodeOwnersConfig(
+      Project.NameKey project,
+      @Nullable String subsection,
+      String key,
+      ImmutableList<String> values)
+      throws Exception {
+    updateCodeOwnersConfig(
+        project,
+        codeOwnersConfig ->
+            codeOwnersConfig.setStringList(
+                CodeOwnersPluginConfiguration.SECTION_CODE_OWNERS, subsection, key, values));
+  }
+
+  private void updateCodeOwnersConfig(Project.NameKey project, Consumer<Config> configUpdater)
+      throws Exception {
     Config codeOwnersConfig = new Config();
-    codeOwnersConfig.setString(
-        CodeOwnersPluginConfiguration.SECTION_CODE_OWNERS, subsection, key, value);
+    configUpdater.accept(codeOwnersConfig);
     try (TestRepository<Repository> testRepo =
         new TestRepository<>(repoManager.openRepository(project))) {
       Ref ref = testRepo.getRepository().exactRef(RefNames.REFS_CONFIG);
diff --git a/javatests/com/google/gerrit/plugins/codeowners/config/CodeOwnersPluginConfigurationTest.java b/javatests/com/google/gerrit/plugins/codeowners/config/CodeOwnersPluginConfigurationTest.java
index 7c6ab3a..dab2b0d 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/config/CodeOwnersPluginConfigurationTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/config/CodeOwnersPluginConfigurationTest.java
@@ -19,6 +19,7 @@
 import static com.google.gerrit.testing.GerritJUnit.assertThrows;
 import static com.google.gerrit.truth.OptionalSubject.assertThat;
 
+import com.google.common.collect.ImmutableList;
 import com.google.gerrit.acceptance.config.GerritConfig;
 import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
 import com.google.gerrit.common.Nullable;
@@ -535,6 +536,20 @@
   }
 
   @Test
+  public void getRequiredApprovalMultipleConfiguredOnProjectLevel() throws Exception {
+    setCodeOwnersConfig(
+        project,
+        /* subsection= */ null,
+        RequiredApprovalConfig.KEY_REQUIRED_APPROVAL,
+        ImmutableList.of("Code-Review+2", "Code-Review+1"));
+
+    // If multiple values are set for a key, the last value wins.
+    RequiredApproval requiredApproval = codeOwnersPluginConfiguration.getRequiredApproval(project);
+    assertThat(requiredApproval).hasLabelNameThat().isEqualTo("Code-Review");
+    assertThat(requiredApproval).hasValueThat().isEqualTo(1);
+  }
+
+  @Test
   @GerritConfig(name = "plugin.code-owners.requiredApproval", value = "Code-Review+1")
   public void requiredApprovalConfiguredOnProjectLevelOverridesDefaultRequiredApproval()
       throws Exception {
@@ -675,6 +690,22 @@
   }
 
   @Test
+  public void getOverrideApprovalMultipleConfiguredOnProjectLevel() throws Exception {
+    setCodeOwnersConfig(
+        project,
+        /* subsection= */ null,
+        OverrideApprovalConfig.KEY_OVERRIDE_APPROVAL,
+        ImmutableList.of("Code-Review+2", "Code-Review+1"));
+
+    // If multiple values are set for a key, the last value wins.
+    Optional<RequiredApproval> requiredApproval =
+        codeOwnersPluginConfiguration.getOverrideApproval(project);
+    assertThat(requiredApproval).isPresent();
+    assertThat(requiredApproval).value().hasLabelNameThat().isEqualTo("Code-Review");
+    assertThat(requiredApproval).value().hasValueThat().isEqualTo(1);
+  }
+
+  @Test
   @GerritConfig(name = "plugin.code-owners.overrideApproval", value = "INVALID")
   public void getOverrideApprovalIfInvalidOverrideApprovalIsConfigured() throws Exception {
     assertThat(codeOwnersPluginConfiguration.getOverrideApproval(project)).isEmpty();