Merge branch 'stable-2.16'

* stable-2.16:
  LfsIT: Make use of @ConfigSuite.Default
  Split generation of FS backend data directory to own class

Change-Id: I347c9029c88c33453d4e455f0a259d94fd2f9ced
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java
new file mode 100644
index 0000000..d8e6366
--- /dev/null
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LfsFsDataDirectoryManager.java
@@ -0,0 +1,68 @@
+// Copyright (C) 2019 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.lfs.fs;
+
+import com.google.common.base.Strings;
+import com.google.gerrit.extensions.annotations.PluginData;
+import com.google.inject.Inject;
+import com.google.inject.Singleton;
+import com.googlesource.gerrit.plugins.lfs.LfsBackend;
+import com.googlesource.gerrit.plugins.lfs.LfsConfigurationFactory;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+@Singleton
+public class LfsFsDataDirectoryManager {
+  private static final String KEY_DIRECTORY = "directory";
+
+  private final LfsConfigurationFactory configFactory;
+  private final Path defaultDataDir;
+
+  @Inject
+  LfsFsDataDirectoryManager(
+      LfsConfigurationFactory configFactory, @PluginData Path defaultDataDir) {
+    this.configFactory = configFactory;
+    this.defaultDataDir = defaultDataDir;
+  }
+
+  public Path ensureForBackend(LfsBackend backend) throws IOException {
+    return getForBackend(backend, true);
+  }
+
+  public Path getForBackend(LfsBackend backend, boolean ensure) throws IOException {
+    String dataDir =
+        configFactory.getGlobalConfig().getString(backend.type.name(), backend.name, KEY_DIRECTORY);
+    if (Strings.isNullOrEmpty(dataDir)) {
+      return defaultDataDir;
+    }
+
+    if (ensure) {
+      // note that the following method not only creates missing
+      // directory/directories but throws exception when path
+      // exists and points to file
+      Path ensured = Files.createDirectories(Paths.get(dataDir));
+
+      // we should at least make sure that directory is readable
+      if (!Files.isReadable(ensured)) {
+        throw new IOException("Path '" + ensured.toAbsolutePath() + "' cannot be accessed");
+      }
+
+      return ensured;
+    }
+    return Paths.get(dataDir);
+  }
+}
diff --git a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
index f67e0bb..069d163 100644
--- a/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
+++ b/src/main/java/com/googlesource/gerrit/plugins/lfs/fs/LocalLargeFileRepository.java
@@ -17,20 +17,14 @@
 import static org.eclipse.jgit.lfs.lib.Constants.DOWNLOAD;
 import static org.eclipse.jgit.lfs.lib.Constants.UPLOAD;
 
-import com.google.common.base.Strings;
 import com.google.gerrit.extensions.annotations.PluginCanonicalWebUrl;
-import com.google.gerrit.extensions.annotations.PluginData;
 import com.google.inject.Inject;
 import com.google.inject.assistedinject.Assisted;
 import com.googlesource.gerrit.plugins.lfs.LfsBackend;
 import com.googlesource.gerrit.plugins.lfs.LfsConfigurationFactory;
-import com.googlesource.gerrit.plugins.lfs.LfsGlobalConfig;
 import com.googlesource.gerrit.plugins.lfs.auth.AuthInfo;
 import com.googlesource.gerrit.plugins.lfs.auth.ExpiringAction;
 import java.io.IOException;
-import java.nio.file.Files;
-import java.nio.file.Path;
-import java.nio.file.Paths;
 import java.time.Instant;
 import org.eclipse.jgit.lfs.lib.AnyLongObjectId;
 import org.eclipse.jgit.lfs.server.Response;
@@ -50,15 +44,13 @@
 
   @Inject
   LocalLargeFileRepository(
+      LfsFsDataDirectoryManager dataDirManager,
       LfsConfigurationFactory configFactory,
       LfsFsRequestAuthorizer authorizer,
       @PluginCanonicalWebUrl String url,
-      @PluginData Path defaultDataDir,
       @Assisted LfsBackend backend)
       throws IOException {
-    super(
-        getContentUrl(url, backend),
-        getOrCreateDataDir(configFactory.getGlobalConfig(), backend, defaultDataDir));
+    super(getContentUrl(url, backend), dataDirManager.ensureForBackend(backend));
     this.authorizer = authorizer;
     this.servletUrlPattern = "/" + getContentPath(backend) + "*";
     this.expiresIn =
@@ -99,24 +91,4 @@
   private static String getContentPath(LfsBackend backend) {
     return String.format(CONTENT_PATH_TEMPLATE, backend.name());
   }
-
-  private static Path getOrCreateDataDir(
-      LfsGlobalConfig config, LfsBackend backendConfig, Path defaultDataDir) throws IOException {
-    String dataDir = config.getString(backendConfig.type.name(), backendConfig.name, "directory");
-    if (Strings.isNullOrEmpty(dataDir)) {
-      return defaultDataDir;
-    }
-
-    // note that the following method not only creates missing
-    // directory/directories but throws exception when path
-    // exists and points to file
-    Path ensured = Files.createDirectories(Paths.get(dataDir));
-
-    // we should at least make sure that directory is readable
-    if (!Files.isReadable(ensured)) {
-      throw new IOException("Path '" + ensured.toAbsolutePath() + "' cannot be accessed");
-    }
-
-    return ensured;
-  }
 }
diff --git a/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsIT.java b/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsIT.java
index 265f47d..c71a83c 100644
--- a/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsIT.java
+++ b/src/test/java/com/googlesource/gerrit/plugins/lfs/LfsIT.java
@@ -14,10 +14,11 @@
 
 package com.googlesource.gerrit.plugins.lfs;
 
-import com.google.gerrit.acceptance.GerritConfig;
 import com.google.gerrit.acceptance.LightweightPluginDaemonTest;
 import com.google.gerrit.acceptance.TestPlugin;
 import com.google.gerrit.reviewdb.client.Project;
+import com.google.gerrit.testing.ConfigSuite;
+import org.eclipse.jgit.lib.Config;
 import org.junit.Test;
 
 @TestPlugin(
@@ -26,20 +27,24 @@
     httpModule = "com.googlesource.gerrit.plugins.lfs.HttpModule",
     sshModule = "com.googlesource.gerrit.plugins.lfs.SshModule")
 public class LfsIT extends LightweightPluginDaemonTest {
+  @ConfigSuite.Default
+  public static Config enablePlugin() {
+    Config cfg = new Config();
+    cfg.setString("lfs", null, "plugin", "lfs");
+    return cfg;
+  }
+
   @Test
-  @GerritConfig(name = "lfs.plugin", value = "lfs")
   public void globalConfigCanBeReadByAdmin() throws Exception {
     adminRestSession.get(globalConfig(allProjects)).assertOK();
   }
 
   @Test
-  @GerritConfig(name = "lfs.plugin", value = "lfs")
   public void globalConfigCannotBeReadByNonAdmin() throws Exception {
     userRestSession.get(globalConfig(allProjects)).assertNotFound();
   }
 
   @Test
-  @GerritConfig(name = "lfs.plugin", value = "lfs")
   public void globalConfigCannotBeReadOnOtherProject() throws Exception {
     adminRestSession.get(globalConfig(project)).assertNotFound();
   }