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 ID # 185273996: Fire s3 path should be relative #714

Merged
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
Expand Up @@ -19,7 +19,7 @@ class S3Client(
return tmp
}

private fun getFireObjectByPath(path: String?): S3Object {
private fun getFireObjectByPath(path: String): S3Object {
val getObjectRequest = GetObjectRequest(bucketName, path)
return amazonS3Client.getObject(getObjectRequest)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ interface FireWebClient {
fun setPath(fireOid: String, path: String)

fun unsetPath(fireOid: String)

fun findByMd5(md5: String): List<FireApiFile>

fun findByPath(path: String): FireApiFile?
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,4 @@ val ExtSubmission.isCollection
val ExtSubmission.computedTitle
get(): String? = title ?: section.title

fun ExtSubmission.expectedPath(file: ExtFile): String = "/$relPath/${file.relPath}"
fun ExtSubmission.expectedFirePath(file: ExtFile): String = "$relPath/${file.relPath}"
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import ebi.ac.uk.extended.model.ExtSubmission
import ebi.ac.uk.extended.model.FireFile
import ebi.ac.uk.extended.model.NfsFile
import ebi.ac.uk.extended.model.asFireFile
import ebi.ac.uk.extended.model.expectedPath
import ebi.ac.uk.extended.model.expectedFirePath
import uk.ac.ebi.fire.client.integration.web.FireClient

class FireFilesService(
Expand All @@ -23,8 +23,8 @@ class FireFilesService(
*/
override fun persistSubmissionFile(sub: ExtSubmission, file: ExtFile): FireFile {
return when (file) {
is FireFile -> getOrCreate(file, sub.expectedPath(file))
is NfsFile -> return getOrCreate(file, sub.expectedPath(file))
is FireFile -> getOrCreate(file, sub.expectedFirePath(file))
is NfsFile -> return getOrCreate(file, sub.expectedFirePath(file))
}
}

Expand Down Expand Up @@ -54,7 +54,7 @@ class FireFilesService(

private fun setMetadata(fireOid: String, file: ExtFile, expectedPath: String, published: Boolean): FireFile {
client.setPath(fireOid, expectedPath)
return file.asFireFile(fireOid, expectedPath, published)
return file.asFireFile(fireId = fireOid, firePath = expectedPath, published = published)
}

override fun deleteSubmissionFile(sub: ExtSubmission, file: ExtFile) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import org.junit.jupiter.api.BeforeEach
import org.junit.jupiter.api.Nested
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
import uk.ac.ebi.extended.serialization.service.ExtSerializationService
import uk.ac.ebi.fire.client.integration.web.FireClient
import uk.ac.ebi.fire.client.model.FileSystemEntry
import uk.ac.ebi.fire.client.model.FireApiFile
Expand All @@ -31,7 +30,6 @@ internal class FireFilesServiceTest(
private val tempFolder: TemporaryFolder,
@MockK private val fireClient: FireClient,
@MockK private val submission: ExtSubmission,
@MockK private val serializationService: ExtSerializationService,
) {
private val testInstance = FireFilesService(fireClient)

Expand All @@ -46,7 +44,7 @@ internal class FireFilesServiceTest(
inner class WhenFireFile {
@Test
fun `when fire file has the expected path`() {
val file = fireFile(firePath = "/001/Files/folder/file.txt")
val file = fireFile(firePath = "001/Files/folder/file.txt")

val result = testInstance.persistSubmissionFile(submission, file)

Expand All @@ -62,33 +60,33 @@ internal class FireFilesServiceTest(
val newFireFile = fireApiFile(firePath = null)
every { fireClient.downloadByPath("/another-path/file.txt") } returns newFile
every { fireClient.save(newFile, file.md5, file.size) } answers { newFireFile }
every { fireClient.setPath(newFireFile.fireOid, "/001/Files/folder/file.txt") } answers { nothing }
every { fireClient.setPath(newFireFile.fireOid, "001/Files/folder/file.txt") } answers { nothing }

val result = testInstance.persistSubmissionFile(submission, file)

assertThat(result.fileName).isEqualTo(file.fileName)
assertThat(result.fireId).isEqualTo(newFireFile.fireOid)
assertThat(result.firePath).isEqualTo("/001/Files/folder/file.txt")
assertThat(result.firePath).isEqualTo("001/Files/folder/file.txt")
}

@Test
fun `when fire file has not path`() {
val file = fireFile(firePath = null)
every { fireClient.setPath(file.fireId, "/001/Files/folder/file.txt") } answers { nothing }
every { fireClient.setPath(file.fireId, "001/Files/folder/file.txt") } answers { nothing }

val result = testInstance.persistSubmissionFile(submission, file)

assertThat(result.fileName).isEqualTo(file.fileName)
assertThat(result.fireId).isEqualTo(file.fireId)
assertThat(result.firePath).isEqualTo("/001/Files/folder/file.txt")
assertThat(result.firePath).isEqualTo("001/Files/folder/file.txt")
}
}

@Nested
inner class WhenNfsFile {
@Test
fun `when fire file has the expected path`() {
val fireApiFile = fireApiFile(firePath = "/001/Files/folder/file.txt")
val fireApiFile = fireApiFile(firePath = "001/Files/folder/file.txt")
val file = createNfsFile("file.txt", "Files/folder/file.txt", tempFolder.createFile("file.txt", "content"))
every { fireClient.findByMd5(file.md5) } answers { listOf(fireApiFile) }

Expand All @@ -98,38 +96,38 @@ internal class FireFilesServiceTest(
assertThat(result.size).isEqualTo(file.size)
assertThat(result.fileName).isEqualTo(file.fileName)
assertThat(result.fireId).isEqualTo(fireApiFile.fireOid)
assertThat(result.firePath).isEqualTo("/001/Files/folder/file.txt")
assertThat(result.firePath).isEqualTo("001/Files/folder/file.txt")
}

@Test
fun `when fire file has not the expected path`() {
val fireApiFile = fireApiFile(firePath = "/another/file.txt")
val fireApiFile = fireApiFile(firePath = "another/file.txt")
val file = createNfsFile("file.txt", "Files/folder/file.txt", tempFolder.createFile("file.txt", "content"))
every { fireClient.findByMd5(file.md5) } answers { listOf(fireApiFile) }

val newFile = fireApiFile(firePath = null)
every { fireClient.save(file.file, file.md5, file.size) } answers { newFile }
every { fireClient.setPath(newFile.fireOid, "/001/Files/folder/file.txt") } answers { nothing }
every { fireClient.setPath(newFile.fireOid, "001/Files/folder/file.txt") } answers { nothing }

val result = testInstance.persistSubmissionFile(submission, file)

assertThat(result.fileName).isEqualTo(file.fileName)
assertThat(result.fireId).isEqualTo(newFile.fireOid)
assertThat(result.firePath).isEqualTo("/001/Files/folder/file.txt")
assertThat(result.firePath).isEqualTo("001/Files/folder/file.txt")
}

@Test
fun `when fire file has not path`() {
val file = createNfsFile("file.txt", "Files/folder/file.txt", tempFolder.createFile("file.txt", "content"))
val fireFile = fireApiFile(firePath = null)
every { fireClient.findByMd5(file.md5) } answers { listOf(fireFile) }
every { fireClient.setPath(fireFile.fireOid, "/001/Files/folder/file.txt") } answers { nothing }
every { fireClient.setPath(fireFile.fireOid, "001/Files/folder/file.txt") } answers { nothing }

val result = testInstance.persistSubmissionFile(submission, file)

assertThat(result.fileName).isEqualTo(file.fileName)
assertThat(result.fireId).isEqualTo(fireFile.fireOid)
assertThat(result.firePath).isEqualTo("/001/Files/folder/file.txt")
assertThat(result.firePath).isEqualTo("001/Files/folder/file.txt")
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,25 @@ import java.nio.file.Paths

internal fun DocFile.toExtFile(released: Boolean, subRelPath: String): ExtFile = when (this) {
is FireDocFile -> FireFile(
fireId,
"/$subRelPath/$relPath",
released,
filePath,
relPath,
md5,
fileSize,
ExtFileType.fromString(fileType),
attributes.toExtAttributes()
fireId = fireId,
firePath = "$subRelPath/$relPath",
published = released,
filePath = filePath,
relPath = relPath,
md5 = md5,
size = fileSize,
type = ExtFileType.fromString(fileType),
attributes = attributes.toExtAttributes()
)

is NfsDocFile -> NfsFile(
filePath,
relPath,
Paths.get(fullPath).toFile(),
fullPath,
md5,
fileSize,
attributes.toExtAttributes()
filePath = filePath,
relPath = relPath,
file = Paths.get(fullPath).toFile(),
fullPath = fullPath,
md5 = md5,
size = fileSize,
attributes = attributes.toExtAttributes()
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ internal class SubmissionMongoQueryServiceTest(
assertThat(fireFile.fireId).isEqualTo("fire-oid")
assertThat(fireFile.filePath).isEqualTo("fireReferenced.txt")
assertThat(fireFile.relPath).isEqualTo("Files/fireReferenced.txt")
assertThat(fireFile.firePath).isEqualTo("/${submission.relPath}/Files/fireReferenced.txt")
assertThat(fireFile.firePath).isEqualTo("${submission.relPath}/Files/fireReferenced.txt")
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ class SubmissionFileSourceTest(
line("File", "DataFile555.txt")
line("dbMd5", "abc-123")
line("dbId", "unique-id")
line("dbPath", "/S-FSTST/006/S-FSTST6/Files/DataFile555.txt")
line("dbPath", "S-FSTST/006/S-FSTST6/Files/DataFile555.txt")
line("dbPublished", true)
line("dbSize", 145)
line()
Expand All @@ -492,7 +492,7 @@ class SubmissionFileSourceTest(
assertThat(fireFile.md5).isEqualTo("abc-123")
assertThat(fireFile.fireId).isEqualTo("unique-id")
assertThat(fireFile.size).isEqualTo(145)
assertThat(fireFile.firePath).isEqualTo("/S-FSTST/006/S-FSTST6/Files/DataFile555.txt")
assertThat(fireFile.firePath).isEqualTo("S-FSTST/006/S-FSTST6/Files/DataFile555.txt")
assertThat(fireFile.relPath).isEqualTo("Files/DataFile555.txt")
assertThat(fireFile.filePath).isEqualTo("DataFile555.txt")
assertThat(fireFile.attributes).isEmpty()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,31 +20,26 @@ class FireMockFileSystem(

fun findFileByFireId(fireOid: String): Path = dbFolder.resolve(fireOid)

fun setPath(fireOid: String, absolutePath: String) {
fun setPath(fireOid: String, path: String) {
val fireFile = dbFolder.resolve(fireOid)

val relativePath = absolutePath.removePrefix("/")
Files.copy(fireFile, getOrCreateSubFolder(relativePath))
fireS3Service.upload(fireFile.toFile(), absolutePath)
Files.copy(fireFile, getOrCreateSubFolder(path))
fireS3Service.upload(fireFile.toFile(), path)
}

fun delete(absolutePath: String) {
val relativePath = absolutePath.removePrefix("/")
Files.deleteIfExists(ftpFolder.resolve(relativePath))
Files.delete(submissionFolder.resolve(relativePath))
fireS3Service.deleteFile(relativePath)
fun delete(path: String) {
Files.deleteIfExists(ftpFolder.resolve(path))
Files.delete(submissionFolder.resolve(path))
fireS3Service.deleteFile(path)
}

fun publish(absolutePath: String) {
val relativePath = absolutePath.removePrefix("/")
val source = submissionFolder.resolve(relativePath)
val target = getOrCreateFtpFolder(relativePath)
fun publish(path: String) {
val source = submissionFolder.resolve(path)
val target = getOrCreateFtpFolder(path)
Files.copy(source, target)
}

fun unpublish(absolutePath: String) {
val relativePath = absolutePath.removePrefix("/")
Files.delete(ftpFolder.resolve(relativePath))
fun unpublish(path: String) {
Files.delete(ftpFolder.resolve(path))
}

private fun getOrCreateSubFolder(relPath: String): Path {
Expand Down