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 # 184485190: S3 Client For FIRE Download Operations #691

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 @@ -48,20 +48,17 @@ internal class FireWebClient(

override fun downloadByPath(
path: String,
): File? = findByPath(path)?.let {
downloadFireFile(path.substringAfterLast("/"), "/objects/blob/path/$path")
}

override fun downloadByFireId(
fireOid: String,
fileName: String,
): File = downloadFireFile(fileName, "/objects/blob/$fireOid")
): File? {
fun toFile(data: ByteArray): File {
val tmpFile = File(tmpDirPath, path.substringAfterLast("/"))
Files.write(tmpFile.toPath(), data)
return tmpFile
}

private fun downloadFireFile(fileName: String, downloadUrl: String): File {
val tmpFile = File(tmpDirPath, fileName)
val fileContent = template.getForObject<ByteArray?>(downloadUrl)
Files.write(tmpFile.toPath(), fileContent ?: byteArrayOf())
return tmpFile
return when (val fileContent = template.getForObjectOrNull<ByteArray?>("/objects/blob/path/$path")) {
null -> null
else -> toFile(fileContent)
}
}

override fun findByMd5(md5: String): List<FireApiFile> =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ interface FireClient {

fun downloadByPath(path: String): File?

fun downloadByFireId(fireOid: String, fileName: String): File

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 @@ -30,11 +30,6 @@ internal class RetryWebClient(
return template.execute(opt) { fireClient.downloadByPath(path) }
}

override fun downloadByFireId(fireOid: String, fileName: String): File {
val opt = "Download file fireOid='$fireOid', fileName='$fileName'"
return template.execute(opt) { fireClient.downloadByFireId(fireOid, fileName) }
}

override fun findByMd5(md5: String): List<FireApiFile> {
val opt = "Find file md5='$md5'"
return template.execute(opt) { fireClient.findByMd5(md5) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,52 +88,29 @@ class FireWebClientTest(
@MockK fireFile: FireApiFile,
) {
val file = tmpFolder.createFile("test.txt", "test content")
val fireFilePath = "S-BSST1/file1.txt"

every { template.getForObject<FireApiFile>("/objects/path/S-BSST1/file1.txt") } returns fireFile
every {
template.getForObject("/objects/blob/path/S-BSST1/file1.txt", ByteArray::class.java)
template.getForObject("/objects/blob/path/$fireFilePath", ByteArray::class.java)
} returns file.readBytes()

val downloadedFile = testInstance.downloadByPath("S-BSST1/file1.txt")!!

assertThat(downloadedFile.readText()).isEqualTo("test content")
assertThat(downloadedFile.absolutePath).isEqualTo("${tmpFolder.root.absolutePath}/file1.txt")
verify(exactly = 1) {
template.getForObject<FireApiFile>("/objects/path/S-BSST1/file1.txt")
template.getForObject("/objects/blob/path/S-BSST1/file1.txt", ByteArray::class.java)
template.getForObject("/objects/blob/path/$fireFilePath", ByteArray::class.java)
}
}

@Test
fun `download by path not found`() {
val fireFilePath = "S-BSST1/file1.txt"
every {
template.getForObject<FireApiFile>("/objects/path/S-BSST1/file1.txt")
template.getForObject("/objects/blob/path/$fireFilePath", ByteArray::class.java)
} throws HttpClientErrorException(NOT_FOUND, "file not found")

assertThat(testInstance.downloadByPath("S-BSST1/file1.txt")).isNull()
verify(exactly = 1) {
template.getForObject<FireApiFile>("/objects/path/S-BSST1/file1.txt")
}
verify(exactly = 0) {
template.getForObject("/objects/blob/path/S-BSST1/file1.txt", ByteArray::class.java)
}
}

@Test
fun `download by fireId`() {
val file = tmpFolder.createFile("test.txt", "test content")

every {
template.getForObject("/objects/blob/fireOId", ByteArray::class.java)
} returns file.readBytes()

val downloadedFile = testInstance.downloadByFireId("fireOId", "file1.txt")

assertThat(downloadedFile.readText()).isEqualTo("test content")
assertThat(downloadedFile.absolutePath).isEqualTo("${tmpFolder.root.absolutePath}/file1.txt")
verify(exactly = 1) {
template.getForObject("/objects/blob/fireOId", ByteArray::class.java)
}
assertThat(testInstance.downloadByPath(fireFilePath)).isNull()
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,16 +68,16 @@ class FireFilesService(
}

private fun getOrCreate(
file: FireFile,
fireFile: FireFile,
expectedPath: String,
): FireFile {
return when (file.firePath) {
expectedPath -> file
null -> setMetadata(file.fireId, file, expectedPath, file.published)
return when (val path = fireFile.firePath) {
expectedPath -> fireFile
null -> setMetadata(fireFile.fireId, fireFile, expectedPath, fireFile.published)
else -> {
val downloaded = client.downloadByFireId(file.fireId, file.fileName)
val saved = client.save(downloaded, file.md5, file.size)
setMetadata(saved.fireOid, file, expectedPath, false)
val file = requireNotNull(client.downloadByPath(path)) { "Could not download file with path $path" }
val saved = client.save(file, fireFile.md5, fireFile.size)
setMetadata(saved.fireOid, fireFile, expectedPath, false)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ internal class FireFilesServiceTest(

val newFile = tempFolder.createFile("file.txt", "content")
val newFireFile = fireApiFile(firePath = null)
every { fireClient.downloadByFireId(file.fireId, file.fileName) } returns newFile
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 }

Expand Down Expand Up @@ -145,7 +145,7 @@ internal class FireFilesServiceTest(

@Test
fun `delete submission files`(
@MockK submission: ExtSubmission
@MockK submission: ExtSubmission,
) {
val file = fireFile(md5 = "md1", firePath = "path_1")

Expand All @@ -161,7 +161,7 @@ internal class FireFilesServiceTest(

@Test
fun `delete submission file`(
@MockK submission: ExtSubmission
@MockK submission: ExtSubmission,
) {
val file = fireFile(firePath = "a file path")
every { fireClient.delete(file.fireId) } answers { nothing }
Expand All @@ -174,7 +174,7 @@ internal class FireFilesServiceTest(
@Test
fun `delete nfs file`(
@MockK nfsFile: NfsFile,
@MockK submission: ExtSubmission
@MockK submission: ExtSubmission,
) {
val exception = assertFails { testInstance.deleteSubmissionFile(submission, nfsFile) }
assertThat(exception.message).isEqualTo("FireFilesService should only handle FireFile")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class SubmissionFilesSource(
private fun getFile(file: ExtFile): File? {
return when (file) {
is NfsFile -> nfsFiles.getFile(file.filePath)
is FireFile -> fireClient.downloadByFireId(file.fireId, file.fileName)
is FireFile -> fireClient.downloadByPath(file.firePath!!)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,5 @@ class SubmissionRequestCleaner(
private fun newFilesMap(new: ExtSubmission) =
filesRequestService
.getSubmissionRequestFiles(new.accNo, new.version, 0)
.groupBy { it.path }
.mapValues { it.value.first().file.md5 }
.associate { it.path to it.file.md5 }
}