Add test helper methods to set users as code owner

This removes duplicate code in tests and makes them more readable.

For now only the test of CodeOwnerApprovalCheck make use of the new
methods.

For some of the tests we now use a code owner config in the root
directory rather than a code owner config in the '/foo/' directory as
the call is a little simpler and for the tests it makes no difference.

Signed-off-by: Edwin Kempin <ekempin@google.com>
Change-Id: I80b916f6af260f3ea3d1cb956054c81d661aab59
diff --git a/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java b/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
index 61a34f5..0ed3041 100644
--- a/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
+++ b/java/com/google/gerrit/plugins/codeowners/acceptance/AbstractCodeOwnersTest.java
@@ -36,6 +36,7 @@
 import com.google.gerrit.extensions.restapi.RestApiException;
 import com.google.gerrit.plugins.codeowners.JgitPath;
 import com.google.gerrit.plugins.codeowners.acceptance.testsuite.CodeOwnerConfigOperations;
+import com.google.gerrit.plugins.codeowners.acceptance.testsuite.TestCodeOwnerConfigCreation.Builder;
 import com.google.gerrit.plugins.codeowners.config.CodeOwnersPluginConfiguration;
 import com.google.gerrit.plugins.codeowners.config.StatusConfig;
 import com.google.inject.Inject;
@@ -245,6 +246,54 @@
   }
 
   /**
+   * Creates a default code owner config with the given test accounts as code owners.
+   *
+   * @param testAccounts the accounts of the users that should be code owners
+   */
+  protected void setAsDefaultCodeOwners(TestAccount... testAccounts) {
+    setAsCodeOwners(RefNames.REFS_CONFIG, "/", testAccounts);
+  }
+
+  /**
+   * Creates a root code owner config with the given test accounts as code owners.
+   *
+   * @param testAccounts the accounts of the users that should be code owners
+   */
+  protected void setAsRootCodeOwners(TestAccount... testAccounts) {
+    setAsCodeOwners("/", testAccounts);
+  }
+
+  /**
+   * Creates a code owner config at the given path with the given test accounts as code owners.
+   *
+   * @param path the path of the code owner config file
+   * @param testAccounts the accounts of the users that should be code owners
+   */
+  protected void setAsCodeOwners(String path, TestAccount... testAccounts) {
+    setAsCodeOwners("master", path, testAccounts);
+  }
+
+  /**
+   * Creates a code owner config at the given path with the given test accounts as code owners.
+   *
+   * @param branchName the name of the branch in which the code owner config should be created
+   * @param path the path of the code owner config file
+   * @param testAccounts the accounts of the users that should be code owners
+   */
+  private void setAsCodeOwners(String branchName, String path, TestAccount... testAccounts) {
+    Builder newCodeOwnerConfigBuilder =
+        codeOwnerConfigOperations
+            .newCodeOwnerConfig()
+            .project(project)
+            .branch(branchName)
+            .folderPath(path);
+    for (TestAccount testAccount : testAccounts) {
+      newCodeOwnerConfigBuilder.addCodeOwnerEmail(testAccount.email());
+    }
+    newCodeOwnerConfigBuilder.create();
+  }
+
+  /**
    * Creates a new change for the given test account.
    *
    * @param testAccount the account that should own the new change
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java
index 34c7d80..3adaaab 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckTest.java
@@ -31,7 +31,6 @@
 import com.google.gerrit.acceptance.testsuite.request.RequestScopeOperations;
 import com.google.gerrit.entities.BranchNameKey;
 import com.google.gerrit.entities.Change;
-import com.google.gerrit.entities.RefNames;
 import com.google.gerrit.extensions.api.changes.ReviewInput;
 import com.google.gerrit.extensions.api.projects.DeleteBranchesInput;
 import com.google.gerrit.extensions.common.LabelDefinitionInput;
@@ -225,13 +224,7 @@
   public void getStatusForFileAddition_pending() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -263,13 +256,7 @@
   public void getStatusForFileModification_pending() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     createChange("Test Change", JgitPath.of(path).get(), "file content").getChangeId();
@@ -303,13 +290,7 @@
   public void getStatusForFileDeletion_pending() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId = createChangeWithFileDeletion(path);
@@ -340,13 +321,7 @@
   public void getStatusForFileRename_pendingOldPath() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/bar/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/bar/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -383,13 +358,7 @@
   public void getStatusForFileRename_pendingNewPath() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/baz/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/baz/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -424,13 +393,7 @@
 
   @Test
   public void getStatusForFileAddition_approved() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -457,13 +420,7 @@
 
   @Test
   public void getStatusForFileModification_approved() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     createChange("Test Change", JgitPath.of(path).get(), "file content").getChangeId();
@@ -492,13 +449,7 @@
 
   @Test
   public void getStatusForFileDeletion_approved() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId = createChangeWithFileDeletion(path);
@@ -524,13 +475,7 @@
 
   @Test
   public void getStatusForFileRename_approvedOldPath() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/bar/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/bar/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -563,13 +508,7 @@
 
   @Test
   public void getStatusForFileRename_approvedNewPath() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/baz/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/baz/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -615,13 +554,7 @@
 
   private void testImplicitApprovalByPatchSetUploaderOnGetStatusForFileAddition(
       boolean implicitApprovalsEnabled) throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -662,13 +595,7 @@
 
   private void testImplicitApprovalByPatchSetUploaderOnGetStatusForFileModification(
       boolean implicitApprovalsEnabled) throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     createChange("Test Change", JgitPath.of(path).get(), "file content").getChangeId();
@@ -710,13 +637,7 @@
 
   private void testImplicitApprovalByPatchSetUploaderOnGetStatusForFileDeletion(
       boolean implicitApprovalsEnabled) throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId = createChangeWithFileDeletion(path);
@@ -757,13 +678,7 @@
 
   private void testImplicitApprovalByPatchSetUploaderOnStatusForFileRenameOnOldPath(
       boolean implicitApprovalsEnabled) throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/bar/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/bar/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -810,13 +725,7 @@
 
   private void testImplicitApprovalByPatchSetUploaderOnStatusForFileRenameOnNewPath(
       boolean implicitApprovalsEnabled) throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/baz/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsCodeOwners("/foo/baz/", user);
 
     Path oldPath = Paths.get("/foo/bar/abc.txt");
     Path newPath = Paths.get("/foo/baz/abc.txt");
@@ -849,13 +758,7 @@
   @Test
   @GerritConfig(name = "plugin.code-owners.enableImplicitApprovals", value = "true")
   public void getStatusForFileAddition_noImplicitlyApprovalByChangeOwner() throws Exception {
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(admin.email())
-        .create();
+    setAsRootCodeOwners(admin);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -883,13 +786,7 @@
       throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -1405,28 +1302,9 @@
     TestAccount user3 =
         accountCreator.create("user3", "user3@example.com", "User3", /* displayName= */ null);
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user2.email())
-        .create();
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/bar/")
-        .addCodeOwnerEmail(user3.email())
-        .create();
+    setAsCodeOwners("/", user);
+    setAsCodeOwners("/foo/", user2);
+    setAsCodeOwners("/foo/bar/", user3);
 
     Path path = Paths.get("/foo/bar/baz.txt");
     String changeId =
@@ -1600,21 +1478,8 @@
   public void isSubmittable() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(user.email())
-        .create();
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/bar/")
-        .addCodeOwnerEmail(user2.email())
-        .create();
+    setAsCodeOwners("/foo/", user);
+    setAsCodeOwners("/bar/", user2);
 
     String changeId =
         pushFactory
@@ -2055,14 +1920,7 @@
   public void approvedByStickyApprovalOnOldPatchSet() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    // Create a code owner config file with 'user' as code owner
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     // Create a change as a user that is not a code owner.
     Path path = Paths.get("/foo/bar.baz");
@@ -2133,14 +1991,7 @@
   public void codeReviewPlus2CountsAsApprovalIfCodeReviewPlus1IsRequired() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    // Create a code owner config file with 'user' as code owner
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsRootCodeOwners(user);
 
     // Create a change as 'user2' that is not a code owner.
     Path path = Paths.get("/foo/bar.baz");
@@ -2202,14 +2053,7 @@
 
     TestAccount user2 = accountCreator.user2();
 
-    // Create a code owner config file with 'admin' as code owner
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/")
-        .addCodeOwnerEmail(admin.email())
-        .create();
+    setAsRootCodeOwners(admin);
 
     // Create a change as 'user' that is not a code owner.
     Path path = Paths.get("/foo/bar.baz");
@@ -2249,14 +2093,7 @@
   public void noBootstrappingIfDefaultCodeOwnerConfigExists() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    // Create default code owner config file in refs/meta/config.
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch(RefNames.REFS_CONFIG)
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsDefaultCodeOwners(user);
 
     // Create a change as a user that is neither a code owner nor a project owner.
     Path path = Paths.get("/foo/bar.baz");
@@ -2316,14 +2153,7 @@
   public void approvedByDefaultCodeOwner() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    // Create default code owner config file in refs/meta/config.
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch(RefNames.REFS_CONFIG)
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsDefaultCodeOwners(user);
 
     // Create a change as a user that is not a code owner.
     Path path = Paths.get("/foo/bar.baz");
@@ -2377,14 +2207,7 @@
 
   private void testImplicitlyApprovedByDefaultCodeOwner(boolean implicitApprovalsEnabled)
       throws Exception {
-    // Create default code owner config file in refs/meta/config.
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch(RefNames.REFS_CONFIG)
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsDefaultCodeOwners(user);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -2410,14 +2233,7 @@
   public void defaultCodeOwnerAsReviewer() throws Exception {
     TestAccount user2 = accountCreator.user2();
 
-    // Create default code owner config file in refs/meta/config.
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch(RefNames.REFS_CONFIG)
-        .folderPath("/")
-        .addCodeOwnerEmail(user.email())
-        .create();
+    setAsDefaultCodeOwners(user);
 
     // Create a change as a user that is not a code owner.
     Path path = Paths.get("/foo/bar.baz");
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithAllUsersAsFallbackCodeOwnersTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithAllUsersAsFallbackCodeOwnersTest.java
index e087873..d2d2c5b 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithAllUsersAsFallbackCodeOwnersTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithAllUsersAsFallbackCodeOwnersTest.java
@@ -69,14 +69,7 @@
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -130,14 +123,7 @@
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
diff --git a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithSelfApprovalsIgnoredTest.java b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithSelfApprovalsIgnoredTest.java
index 66c3958..9ba465a 100644
--- a/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithSelfApprovalsIgnoredTest.java
+++ b/javatests/com/google/gerrit/plugins/codeowners/backend/CodeOwnerApprovalCheckWithSelfApprovalsIgnoredTest.java
@@ -24,7 +24,6 @@
 import com.google.gerrit.extensions.common.LabelDefinitionInput;
 import com.google.gerrit.plugins.codeowners.JgitPath;
 import com.google.gerrit.plugins.codeowners.acceptance.AbstractCodeOwnersTest;
-import com.google.gerrit.plugins.codeowners.acceptance.testsuite.CodeOwnerConfigOperations;
 import com.google.gerrit.plugins.codeowners.api.CodeOwnerStatus;
 import com.google.gerrit.plugins.codeowners.config.OverrideApprovalConfig;
 import com.google.gerrit.plugins.codeowners.testing.FileCodeOwnerStatusSubject;
@@ -43,7 +42,6 @@
   @Inject private RequestScopeOperations requestScopeOperations;
 
   private CodeOwnerApprovalCheck codeOwnerApprovalCheck;
-  private CodeOwnerConfigOperations codeOwnerConfigOperations;
 
   /** Returns a {@code gerrit.config} that configures all users as fallback code owners. */
   @ConfigSuite.Default
@@ -57,8 +55,6 @@
   @Before
   public void setUpCodeOwnersPlugin() throws Exception {
     codeOwnerApprovalCheck = plugin.getSysInjector().getInstance(CodeOwnerApprovalCheck.class);
-    codeOwnerConfigOperations =
-        plugin.getSysInjector().getInstance(CodeOwnerConfigOperations.class);
   }
 
   @Before
@@ -79,14 +75,7 @@
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -125,14 +114,7 @@
     TestAccount changeOwner =
         accountCreator.create(
             "changeOwner", "changeOwner@example.com", "ChangeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(changeOwner.email())
-        .create();
+    setAsRootCodeOwners(changeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -175,17 +157,11 @@
     TestAccount changeOwner =
         accountCreator.create(
             "changeOwner", "changeOwner@example.com", "ChangeOwner", /* displayName= */ null);
+
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -243,14 +219,7 @@
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =
@@ -276,17 +245,11 @@
     TestAccount changeOwner =
         accountCreator.create(
             "changeOwner", "changeOwner@example.com", "ChangeOwner", /* displayName= */ null);
+
     TestAccount codeOwner =
         accountCreator.create(
             "codeOwner", "codeOwner@example.com", "CodeOwner", /* displayName= */ null);
-
-    codeOwnerConfigOperations
-        .newCodeOwnerConfig()
-        .project(project)
-        .branch("master")
-        .folderPath("/foo/")
-        .addCodeOwnerEmail(codeOwner.email())
-        .create();
+    setAsRootCodeOwners(codeOwner);
 
     Path path = Paths.get("/foo/bar.baz");
     String changeId =