Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pivotal-173792650: Replace base path for multiple absolute paths #239

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package ebi.ac.uk.paths

import ebi.ac.uk.extended.model.ExtSubmission
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.nio.file.Paths

@ExtendWith(MockKExtension::class)
internal class SubmissionFolderResolverTest(@MockK val submission: ExtSubmission) {
private val testInstance = SubmissionFolderResolver(Paths.get("/tmp/nfs"))
internal class SubmissionFolderResolverTest {
private val testInstance = SubmissionFolderResolver(
Paths.get("/tmp/nfs/submission"),
Paths.get("/tmp/nfs/submission/ftp")
)

@Test
fun getSubmissionFolder() {
val submissionPath = "part1/part2"
every { submission.relPath } returns submissionPath

assertThat(testInstance.getSubmissionFolder(submission).toString())
assertThat(testInstance.getSubmissionFolder(submissionPath).toString())
.isEqualTo("/tmp/nfs/submission/part1/part2")
}

@Test
fun getSubFilePath() {
val submissionPath = "part1/part2"
every { submission.relPath } returns submissionPath

assertThat(testInstance.getSubFilePath(submissionPath, "/test-file.txt").toString())
.isEqualTo("/tmp/nfs/submission/part1/part2/Files/test-file.txt")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,25 +1,20 @@
package ebi.ac.uk.paths

import ebi.ac.uk.extended.model.ExtSubmission
import java.nio.file.Path

const val FILES_PATH = "Files"
const val SUBMISSION_PATH = "submission"
const val FTP_FOLDER = "submission/ftp"

class SubmissionFolderResolver(private val basePath: Path) {
class SubmissionFolderResolver(
private val submissionFolder: Path,
private val ftpFolder: Path
) {

fun getSubmissionFtpFolder(submission: ExtSubmission): Path =
basePath.resolve(FTP_FOLDER).resolve(submission.relPath)
fun getSubmissionFtpFolder(submissionRelPath: String): Path = ftpFolder.resolve(submissionRelPath)

fun getSubmissionFolder(submission: ExtSubmission): Path =
basePath.resolve(SUBMISSION_PATH).resolve(submission.relPath)

fun getSubmissionFolder(submissionRelPath: String): Path =
basePath.resolve(SUBMISSION_PATH).resolve(submissionRelPath)
fun getSubmissionFolder(submissionRelPath: String): Path = submissionFolder.resolve(submissionRelPath)

fun getSubFilePath(relPath: String, fileName: String): Path =
basePath.resolve(SUBMISSION_PATH).resolve(relPath).resolve(FILES_PATH).resolve(escapeFileName(fileName))
submissionFolder.resolve(relPath).resolve(FILES_PATH).resolve(escapeFileName(fileName))

private fun escapeFileName(fileName: String) = fileName.removePrefix("/")
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,21 @@ import org.junit.jupiter.api.Test
import java.nio.file.Paths

class FolderResolverTest {
private val testInstance = SubmissionFolderResolver(Paths.get("/nfs/biostudies"))
private val testInstance = SubmissionFolderResolver(
submissionFolder = Paths.get("/nfs/biostudies/submission"),
ftpFolder = Paths.get("/nfs/bisotudies/ftp"))

@Test
fun `get submission file path`() {
assertThat(testInstance
.getSubFilePath("ABCxxx123/ABC-123", "File1.txt"))
.isEqualTo(Paths.get("/nfs/biostudies/submission/ABCxxx123/ABC-123/Files/File1.txt"))
}

@Test
fun `get submission ftp path`() {
assertThat(testInstance
.getSubmissionFtpFolder("ABCxxx123/ABC-123"))
.isEqualTo(Paths.get("/nfs/bisotudies/ftp/ABCxxx123/ABC-123"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class FileSystemService(
private val ftpLinksService: FtpFilesService
) {
fun persistSubmissionFiles(submission: ExtSubmission, mode: FileMode) {
ftpLinksService.cleanFtpFolder(submission)
ftpLinksService.cleanFtpFolder(submission.relPath)
filesService.persistSubmissionFiles(submission, mode)
if (submission.released) ftpLinksService.createFtpFolder(submission)
if (submission.released) ftpLinksService.createFtpFolder(submission.relPath)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ac.uk.ebi.biostd.persistence.service.filesystem

import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.io.FileUtils
import ebi.ac.uk.paths.SubmissionFolderResolver
import java.io.File
Expand All @@ -11,19 +10,19 @@ private val ALL_READ: Set<PosixFilePermission> = PosixFilePermissions.fromString

class FtpFilesService(private val folderResolver: SubmissionFolderResolver) {

fun createFtpFolder(submission: ExtSubmission) {
val submissionFolder = folderResolver.getSubmissionFolder(submission.relPath).toFile()
val ftpFolder = getFtpFolder(submission)
fun createFtpFolder(relPath: String) {
val submissionFolder = folderResolver.getSubmissionFolder(relPath).toFile()
val ftpFolder = getFtpFolder(relPath)
FileUtils.createHardLink(submissionFolder, ftpFolder)
}

private fun getFtpFolder(submission: ExtSubmission): File =
private fun getFtpFolder(relPath: String): File =
FileUtils.getOrCreateFolder(
folderResolver.getSubmissionFtpFolder(submission),
folderResolver.getSubmissionFtpFolder(relPath),
ALL_READ
).toFile()

fun cleanFtpFolder(submission: ExtSubmission) {
FileUtils.deleteFile(folderResolver.getSubmissionFtpFolder(submission).toFile())
fun cleanFtpFolder(relPath: String) {
FileUtils.deleteFile(folderResolver.getSubmissionFtpFolder(relPath).toFile())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ class FilesServiceTest(
private lateinit var extSubmission: ExtSubmission

private val testInstance =
FilesService(SubmissionFolderResolver(temporaryFolder.root.toPath()), mockSerializationService)
FilesService(SubmissionFolderResolver(
Paths.get("${temporaryFolder.root.toPath()}/submission"),
Paths.get("${temporaryFolder.root.toPath()}/ftp")
), mockSerializationService)

@BeforeEach
fun beforeEach() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package ac.uk.ebi.biostd.persistence.service.filesystem

import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.io.ext.asFileList
import ebi.ac.uk.io.ext.createDirectory
import ebi.ac.uk.io.ext.createNewFile
Expand All @@ -9,59 +8,54 @@ import ebi.ac.uk.test.clean
import ebi.ac.uk.util.collections.second
import io.github.glytching.junit.extension.folder.TemporaryFolder
import io.github.glytching.junit.extension.folder.TemporaryFolderExtension
import io.mockk.every
import io.mockk.impl.annotations.MockK
import io.mockk.junit5.MockKExtension
import org.assertj.core.api.Assertions.assertThat
import org.junit.jupiter.api.BeforeAll
import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import java.io.File

private const val REL_PATH = "My/Path/To/Submission"

@ExtendWith(TemporaryFolderExtension::class, MockKExtension::class)
internal class FtpFilesServiceTest(
private val temporaryFolder: TemporaryFolder,
@MockK private val submission: ExtSubmission
private val temporaryFolder: TemporaryFolder
) {

private lateinit var expectedDirectory: File
private lateinit var expectedFile1: File
private lateinit var expectedFile2: File

private val folderResolver = SubmissionFolderResolver(temporaryFolder.root.toPath())
private val folderResolver = SubmissionFolderResolver(
temporaryFolder.root.toPath().resolve("submission"),
temporaryFolder.root.toPath().resolve("ftp"))
private val testInstance = FtpFilesService(folderResolver)

@BeforeAll
fun beforeAll() {
every { submission.relPath } returns "My/Path/To/Submission"
}

@BeforeEach
fun beforeEach() {
temporaryFolder.clean()

val submissionFolder = folderResolver.getSubmissionFolder(submission).toFile().apply { mkdirs() }
val submissionFolder = folderResolver.getSubmissionFolder(REL_PATH).toFile().apply { mkdirs() }
expectedDirectory = submissionFolder.createDirectory("my-directory")
expectedFile1 = expectedDirectory.createNewFile("file.txt", "file-content")
expectedFile2 = expectedDirectory.createNewFile("file-2.txt", "file-text")
}

@Test
fun createFtpFolder() {
testInstance.createFtpFolder(submission)
testInstance.createFtpFolder(REL_PATH)

assertFolder(folderResolver.getSubmissionFolder(submission).toFile())
assertFolder(folderResolver.getSubmissionFtpFolder(submission).toFile())
assertFolder(folderResolver.getSubmissionFolder(REL_PATH).toFile())
assertFolder(folderResolver.getSubmissionFtpFolder(REL_PATH).toFile())
}

@Test
fun cleanFtpFolder() {
testInstance.createFtpFolder(submission)
testInstance.cleanFtpFolder(submission)
testInstance.createFtpFolder(REL_PATH)
testInstance.cleanFtpFolder(REL_PATH)

assertFolder(folderResolver.getSubmissionFolder(submission).toFile())
assertThat(folderResolver.getSubmissionFtpFolder(submission).toFile()).doesNotExist()
assertFolder(folderResolver.getSubmissionFolder(REL_PATH).toFile())
assertThat(folderResolver.getSubmissionFtpFolder(REL_PATH).toFile()).doesNotExist()
}

private fun assertFolder(ftpFolder: File) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class SecurityProperties {
lateinit var environment: String
lateinit var filesDirPath: String
lateinit var captchaKey: String
lateinit var magicFolderPath: String

var checkCaptcha: Boolean = false
var requireActivation: Boolean = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import org.assertj.core.api.Assertions.assertThat
import java.nio.file.Paths

internal class AllInOneSubmissionHelper(
private val basePath: String,
private val submissionPath: String,
private val submissionRepository: SubmissionRepository
) {
internal fun assertSavedSubmission(accNo: String, method: ExtSubmissionMethod = ExtSubmissionMethod.PAGE_TAB) {
Expand All @@ -25,7 +25,7 @@ internal class AllInOneSubmissionHelper(
}

private fun assertSubmissionFiles(submission: ExtSubmission) {
val submissionFolderPath = "$basePath/submission/${submission.relPath}"
val submissionFolderPath = "$submissionPath/${submission.relPath}"
val accNo = submission.accNo

assertAllInOneSubmissionXml(getSubFileContent("$submissionFolderPath/$accNo.xml"), accNo)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import io.github.glytching.junit.extension.folder.TemporaryFolder
import org.junit.jupiter.api.BeforeAll

internal open class BaseIntegrationTest(private val tempFolder: TemporaryFolder) {
protected lateinit var basePath: String
protected lateinit var submissionPath: String
protected lateinit var ftpPath: String

@BeforeAll
fun init() {
val dropbox = tempFolder.createDirectory("dropbox")
val temp = tempFolder.createDirectory("tmp")
basePath = tempFolder.root.absolutePath
submissionPath = "${tempFolder.root.absolutePath}/submission"
ftpPath = "${tempFolder.root.absolutePath}/ftpPath"

System.setProperty("app.basepath", tempFolder.root.absolutePath)
System.setProperty("app.submissionPath", submissionPath)
System.setProperty("app.ftpPath", ftpPath)
System.setProperty("app.tempDirPath", temp.absolutePath)
System.setProperty("app.security.filesDirPath", dropbox.absolutePath)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ import org.springframework.test.annotation.DirtiesContext
import org.springframework.test.context.junit.jupiter.SpringExtension

@ExtendWith(TemporaryFolderExtension::class)
internal class ProjectSubmitTest(private val tempFolder: TemporaryFolder) : BaseIntegrationTest(tempFolder) {
internal class ProjectSubmitTest(tempFolder: TemporaryFolder) : BaseIntegrationTest(tempFolder) {
@Nested
@Import(PersistenceConfig::class)
@ExtendWith(SpringExtension::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ internal class AllInOneMultipartFileSubmissionTest(
@BeforeAll
fun init() {
webClient = getWebClient(serverPort, SuperUser)
allInOneSubmissionHelper = AllInOneSubmissionHelper(basePath, submissionRepository)
allInOneSubmissionHelper = AllInOneSubmissionHelper(submissionPath, submissionRepository)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal class AllInOneSubmissionTest(private val tempFolder: TemporaryFolder) :
@BeforeAll
fun init() {
webClient = getWebClient(serverPort, SuperUser)
allInOneSubmissionHelper = AllInOneSubmissionHelper(basePath, submissionRepository)
allInOneSubmissionHelper = AllInOneSubmissionHelper(submissionPath, submissionRepository)
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ internal class FileListSubmissionTest(private val tempFolder: TemporaryFolder) :
private fun assertSubmissionFiles(accNo: String, testFile: String) {
val fileListName = "FileList"
val createdSubmission = submissionRepository.getActiveExtByAccNo(accNo)
val submissionFolderPath = "$basePath/submission/${createdSubmission.relPath}"
val submissionFolderPath = "$submissionPath/${createdSubmission.relPath}"

assertThat(createdSubmission.section.fileList?.fileName).isEqualTo(fileListName)
assertThat(createdSubmission.section.fileList).isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ internal class MultipartFileSubmissionApiTest(
private fun assertSubmissionFiles(accNo: String, testFile: String) {
val fileListName = "FileList"
val createdSubmission = submissionRepository.getActiveExtByAccNo(accNo)
val submissionFolderPath = "$basePath/submission/${createdSubmission.relPath}"
val submissionFolderPath = "$submissionPath/${createdSubmission.relPath}"

assertThat(createdSubmission.section.fileList?.fileName).isEqualTo(fileListName)
assertThat(createdSubmission.section.fileList).isEqualTo(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ spring:
port: 5672

app:
basepath:
submissionPath:
ftpPath:
tempDirPath:
instanceBaseUrl: http://localhost:8080
security:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ import org.springframework.context.annotation.Configuration
import org.springframework.data.jpa.repository.config.EnableJpaRepositories
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate
import uk.ac.ebi.stats.persistence.repositories.SubmissionStatsRepository
import java.nio.file.Paths

@Suppress("TooManyFunctions")
@Configuration
Expand Down Expand Up @@ -65,7 +66,7 @@ class PersistenceConfig(
) = ToDbSubmissionMapper(tagsRepo, tagsRefRepo, userRepo)

@Bean
fun toExtSubmissionMapper() = ToExtSubmissionMapper(applicationProperties.submissionsPath)
fun toExtSubmissionMapper() = ToExtSubmissionMapper(Paths.get(applicationProperties.submissionPath))

@Bean
fun submissionRepository(toExtSubmissionMapper: ToExtSubmissionMapper) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@ class SubmitterConfig {
class FilesHandlerConfig(private val appProperties: ApplicationProperties) {
@Bean
@Lazy
fun folderResolver() = SubmissionFolderResolver(Paths.get(appProperties.basepath))
fun folderResolver() = SubmissionFolderResolver(
submissionFolder = Paths.get(appProperties.submissionPath),
ftpFolder = Paths.get(appProperties.ftpPath))
}

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package ac.uk.ebi.biostd.common.property

import ebi.ac.uk.notifications.integration.NotificationProperties
import ebi.ac.uk.paths.SUBMISSION_PATH
import ebi.ac.uk.security.integration.SecurityProperties
import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.boot.context.properties.NestedConfigurationProperty
import java.nio.file.Path
import java.nio.file.Paths

@ConfigurationProperties(prefix = "app")
open class ApplicationProperties {
lateinit var basepath: String
lateinit var tempDirPath: String
lateinit var submissionPath: String
lateinit var ftpPath: String
lateinit var instanceBaseUrl: String

val submissionsPath: Path
get() = Paths.get(basepath).resolve(SUBMISSION_PATH)

@NestedConfigurationProperty
var security: SecurityProperties = SecurityProperties()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ spring:
max-request-size: 10240MB

app:
basepath: # Absolute path to the folder to be used as storage
tempDirPath: # Absolute of the temp folder to store files
submissionPath:
ftpPath:
tempDirPath:
instanceBaseUrl: http://localhost:8080
security:
captchaKey: # Captcha validation token. Not required for local environment
Expand Down
Loading