Refactor backup creation to BackupRef

Rename BackupBranch to BackupRef, and refactor backup creation out
of RefUpdateListener.

Moving the functionality into the BackupRef class will enable some
amount of configuration to how the backups are performed, and avoids
overloading too much logic in the RefUpdateListener.

Change-Id: Iced4ff7e563b6978bb29e2301a3672dc183b971e
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupBranch.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupBranch.java
deleted file mode 100644
index dbed47a..0000000
--- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupBranch.java
+++ /dev/null
@@ -1,45 +0,0 @@
-/*
- *  The MIT License
- *
- *  Copyright 2015 Sony Mobile Communications AB. All rights reserved.
- *
- *  Permission is hereby granted, free of charge, to any person obtaining a copy
- *  of this software and associated documentation files (the "Software"), to deal
- *  in the Software without restriction, including without limitation the rights
- *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- *  copies of the Software, and to permit persons to whom the Software is
- *  furnished to do so, subject to the following conditions:
- *
- *  The above copyright notice and this permission notice shall be included in
- *  all copies or substantial portions of the Software.
- *
- *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
- *  THE SOFTWARE.
- */
-package com.googlesource.gerrit.plugins.refprotection;
-
-import static org.eclipse.jgit.lib.Constants.R_HEADS;
-import static org.eclipse.jgit.lib.Constants.R_REFS;
-import static org.eclipse.jgit.lib.Constants.R_TAGS;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-public class BackupBranch {
-  public static final String R_BACKUPS = R_REFS + "backups/";
-
-  public static String get(String branchName) {
-    if (branchName.startsWith(R_HEADS) || branchName.startsWith(R_TAGS)) {
-      return String.format("%s-%s",
-          R_BACKUPS + branchName.replaceFirst(R_REFS, ""),
-          new SimpleDateFormat("YYYYMMdd-HHmmss").format(new Date()));
-    }
-
-    return branchName;
-  }
-}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
new file mode 100644
index 0000000..b35e930
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/BackupRef.java
@@ -0,0 +1,86 @@
+/*
+ *  The MIT License
+ *
+ *  Copyright 2015 Sony Mobile Communications AB. All rights reserved.
+ *
+ *  Permission is hereby granted, free of charge, to any person obtaining a copy
+ *  of this software and associated documentation files (the "Software"), to deal
+ *  in the Software without restriction, including without limitation the rights
+ *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ *  copies of the Software, and to permit persons to whom the Software is
+ *  furnished to do so, subject to the following conditions:
+ *
+ *  The above copyright notice and this permission notice shall be included in
+ *  all copies or substantial portions of the Software.
+ *
+ *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ *  THE SOFTWARE.
+ */
+package com.googlesource.gerrit.plugins.refprotection;
+
+import com.google.gerrit.extensions.events.GitReferenceUpdatedListener.Event;
+import com.google.gerrit.extensions.restapi.AuthException;
+import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.ResourceConflictException;
+import com.google.gerrit.server.project.CreateBranch;
+import com.google.gerrit.server.project.ProjectResource;
+import com.google.inject.Inject;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.eclipse.jgit.lib.Constants.R_HEADS;
+import static org.eclipse.jgit.lib.Constants.R_REFS;
+import static org.eclipse.jgit.lib.Constants.R_TAGS;
+
+import java.io.IOException;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+public class BackupRef {
+  public static final String R_BACKUPS = R_REFS + "backups/";
+  private static final Logger log =
+      LoggerFactory.getLogger(BackupRef.class);
+  private final CreateBranch.Factory createBranchFactory;
+
+  @Inject
+  BackupRef(CreateBranch.Factory createBranchFactory) {
+    this.createBranchFactory = createBranchFactory;
+  }
+
+  public void createBackup(Event event, ProjectResource project) {
+    String refName = event.getRefName();
+    String backupRef = get(refName);
+
+    // No-op if the backup branch name is same as the original
+    if (backupRef.equals(refName)) {
+      return;
+    }
+
+    CreateBranch.Input input = new CreateBranch.Input();
+    input.ref = backupRef;
+    input.revision = event.getOldObjectId();
+
+    try {
+      createBranchFactory.create(backupRef).apply(project, input);
+    } catch (BadRequestException | AuthException | ResourceConflictException
+        | IOException e) {
+      log.error(e.getMessage(), e);
+    }
+  }
+
+  static String get(String refName) {
+    if (refName.startsWith(R_HEADS) || refName.startsWith(R_TAGS)) {
+      return String.format("%s-%s",
+          R_BACKUPS + refName.replaceFirst(R_REFS, ""),
+          new SimpleDateFormat("YYYYMMdd-HHmmss").format(new Date()));
+    }
+
+    return refName;
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
index 09d9c94..6484b38 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/refprotection/RefUpdateListener.java
@@ -27,13 +27,9 @@
 import static org.eclipse.jgit.lib.Constants.R_TAGS;
 
 import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
-import com.google.gerrit.extensions.restapi.AuthException;
-import com.google.gerrit.extensions.restapi.BadRequestException;
-import com.google.gerrit.extensions.restapi.ResourceConflictException;
 import com.google.gerrit.reviewdb.client.Project;
 import com.google.gerrit.server.CurrentUser;
 import com.google.gerrit.server.git.GitRepositoryManager;
-import com.google.gerrit.server.project.CreateBranch;
 import com.google.gerrit.server.project.NoSuchProjectException;
 import com.google.gerrit.server.project.ProjectControl;
 import com.google.gerrit.server.project.ProjectResource;
@@ -53,20 +49,20 @@
 
   private static final Logger log =
       LoggerFactory.getLogger(RefUpdateListener.class);
-  private final CreateBranch.Factory createBranchFactory;
   private final ProjectControl.GenericFactory projectControl;
   private final CurrentUser user;
   private final GitRepositoryManager repoManager;
+  private final BackupRef backupRef;
 
   @Inject
-  RefUpdateListener(CreateBranch.Factory createBranchFactory,
-      ProjectControl.GenericFactory p,
+  RefUpdateListener(ProjectControl.GenericFactory p,
       CurrentUser user,
-      GitRepositoryManager repoManager) {
-    this.createBranchFactory = createBranchFactory;
+      GitRepositoryManager repoManager,
+      BackupRef backupRef) {
     this.projectControl = p;
     this.user = user;
     this.repoManager = repoManager;
+    this.backupRef = backupRef;
   }
 
   @Override
@@ -77,7 +73,7 @@
         ProjectResource project =
             new ProjectResource(projectControl.controlFor(nameKey, user));
         if (isRefDeleted(event) || isNonFastForwardUpdate(event, project)) {
-          createBackupBranch(event, project);
+          backupRef.createBackup(event, project);
         }
       } catch (NoSuchProjectException | IOException e) {
         log.error(e.getMessage(), e);
@@ -86,32 +82,6 @@
   }
 
   /**
-   * Create a backup branch for the given ref.
-   *
-   * @param event the Event
-   */
-  private void createBackupBranch(Event event, ProjectResource project) {
-    String branchName = event.getRefName();
-    String backupRef = BackupBranch.get(branchName);
-
-    // No-op if the backup branch name is same as the original
-    if (backupRef.equals(branchName)) {
-      return;
-    }
-
-    CreateBranch.Input input = new CreateBranch.Input();
-    input.ref = backupRef;
-    input.revision = event.getOldObjectId();
-
-    try {
-      createBranchFactory.create(backupRef).apply(project, input);
-    } catch (BadRequestException | AuthException | ResourceConflictException
-        | IOException e) {
-      log.error(e.getMessage(), e);
-    }
-  }
-
-  /**
    * Is the event on a relevant ref?
    *
    * @param event the Event
diff --git a/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupBranchNameTest.java b/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupBranchNameTest.java
deleted file mode 100644
index b2bc9a2..0000000
--- a/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupBranchNameTest.java
+++ /dev/null
@@ -1,28 +0,0 @@
-package com.googlesource.gerrit.plugins.refprotection;
-
-import static com.google.common.truth.Truth.assertThat;
-
-import org.junit.Test;
-
-public class BackupBranchNameTest {
-
-  @Test
-  public void backupBranchNameForTag() throws Exception {
-    String name = BackupBranch.get("refs/tags/v1.0");
-    String expected_prefix = BackupBranch.R_BACKUPS + "tags/v1.0-";
-    assertThat(name).startsWith(expected_prefix);
-  }
-
-  @Test
-  public void backupBranchNameForBranch() throws Exception {
-    String name = BackupBranch.get("refs/heads/master");
-    String expected_prefix = BackupBranch.R_BACKUPS + "heads/master-";
-    assertThat(name).startsWith(expected_prefix);
-  }
-
-  @Test
-  public void backupBranchNameForUnsupportedNamespace() throws Exception {
-    String ref = "refs/changes/45/12345/1";
-    assertThat(BackupBranch.get(ref)).isEqualTo(ref);
-  }
-}
diff --git a/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupRefNameTest.java b/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupRefNameTest.java
new file mode 100644
index 0000000..0ad095f
--- /dev/null
+++ b/src/test/java/com/googlesource/gerrit/plugins/refprotection/BackupRefNameTest.java
@@ -0,0 +1,28 @@
+package com.googlesource.gerrit.plugins.refprotection;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import org.junit.Test;
+
+public class BackupRefNameTest {
+
+  @Test
+  public void backupRefNameForTag() throws Exception {
+    String name = BackupRef.get("refs/tags/v1.0");
+    String expected_prefix = BackupRef.R_BACKUPS + "tags/v1.0-";
+    assertThat(name).startsWith(expected_prefix);
+  }
+
+  @Test
+  public void backupRefNameForBranch() throws Exception {
+    String name = BackupRef.get("refs/heads/master");
+    String expected_prefix = BackupRef.R_BACKUPS + "heads/master-";
+    assertThat(name).startsWith(expected_prefix);
+  }
+
+  @Test
+  public void backupRefNameForUnsupportedNamespace() throws Exception {
+    String ref = "refs/changes/45/12345/1";
+    assertThat(BackupRef.get(ref)).isEqualTo(ref);
+  }
+}