Skip to content
Open
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,38 +1,170 @@
/*
* Nextcloud Android Library
*
* SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2021-2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com>
* SPDX-FileCopyrightText: 2021 Tobias Kaminsky <tobias@kaminsky.me>
* SPDX-License-Identifier: MIT
*/
package com.owncloud.android.lib.resources.files

import com.owncloud.android.AbstractIT
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertSame
import org.junit.Assert.assertTrue
import org.junit.Test
import java.io.File

@Suppress("Detekt.MagicNumber")
class DownloadFileRemoteOperationIT : AbstractIT() {
private val cacheDir get() = context.externalCacheDir?.absolutePath

@Test
fun download() {
val filePath = createFile("download")
val remotePath = "/download.jpg"
assertTrue(
@Suppress("Detekt.MagicNumber")
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", 1464818400)
.execute(client)
.isSuccess
)

assertTrue(
DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath)
DownloadFileRemoteOperation(remotePath, cacheDir)
.execute(nextcloudClient)
.isSuccess
)

val oldFile = File(filePath)
val newFile = File(context.externalCacheDir?.absolutePath + remotePath)
val newFile = File(cacheDir + remotePath)
assertSame(oldFile.length(), newFile.length())
}

@Test
fun downloadLargeFile() {
val filePath = createFile("large_download", 1000)
val remotePath = "/large_download.txt"
assertTrue(
UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME)
.execute(client)
.isSuccess
)

assertTrue(
DownloadFileRemoteOperation(remotePath, cacheDir)
.execute(nextcloudClient)
.isSuccess
)

val originalFile = File(filePath)
val downloadedFile = File(cacheDir + remotePath)
assertEquals(originalFile.length(), downloadedFile.length())
}

@Test
fun downloadNonExistentFile() {
val result =
DownloadFileRemoteOperation("/nonexistent_file_12345.txt", cacheDir)
.execute(nextcloudClient)

assertFalse(result.isSuccess)
}

@Test
fun downloadAndVerifyMetadata() {
val filePath = createFile("metadata_download")
val remotePath = "/metadata_download.jpg"
assertTrue(
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME)
.execute(client)
.isSuccess
)

val operation = DownloadFileRemoteOperation(remotePath, cacheDir)
assertTrue(operation.execute(nextcloudClient).isSuccess)

assertTrue("ETag should not be empty after download", operation.etag.isNotEmpty())
assertTrue("Modification timestamp should be positive after download", operation.modificationTimestamp > 0)
}

@Test
fun downloadMultipleFiles() {
val filePath1 = createFile("multi_download1")
val remotePath1 = "/multi_download1.jpg"
val filePath2 = createFile("multi_download2")
val remotePath2 = "/multi_download2.jpg"

assertTrue(
UploadFileRemoteOperation(filePath1, remotePath1, "image/jpg", RANDOM_MTIME)
.execute(client)
.isSuccess
)
assertTrue(
UploadFileRemoteOperation(filePath2, remotePath2, "image/jpg", RANDOM_MTIME)
.execute(client)
.isSuccess
)

assertTrue(
DownloadFileRemoteOperation(remotePath1, cacheDir)
.execute(nextcloudClient)
.isSuccess
)
assertTrue(
DownloadFileRemoteOperation(remotePath2, cacheDir)
.execute(nextcloudClient)
.isSuccess
)

val downloaded1 = File(cacheDir + remotePath1)
val downloaded2 = File(cacheDir + remotePath2)
assertTrue(downloaded1.exists())
assertTrue(downloaded2.exists())
assertEquals(File(filePath1).length(), downloaded1.length())
assertEquals(File(filePath2).length(), downloaded2.length())
}

@Test
fun downloadAndVerifyContent() {
val filePath = createFile("content_download", 50)
val remotePath = "/content_download.txt"
assertTrue(
UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME)
.execute(client)
.isSuccess
)

assertTrue(
DownloadFileRemoteOperation(remotePath, cacheDir)
.execute(nextcloudClient)
.isSuccess
)

val originalFile = File(filePath)
val downloadedFile = File(cacheDir + remotePath)
assertTrue(downloadedFile.exists())
assertTrue(originalFile.readBytes().contentEquals(downloadedFile.readBytes()))
}

@Test
fun downloadedFileExistsAtExpectedPath() {
val filePath = createFile("path_check")
val remotePath = "/path_check.jpg"
assertTrue(
UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME)
.execute(client)
.isSuccess
)

assertTrue(
DownloadFileRemoteOperation(remotePath, cacheDir)
.execute(nextcloudClient)
.isSuccess
)

val expectedFile = File(cacheDir + remotePath)
assertTrue("Downloaded file should exist at expected path", expectedFile.exists())
assertTrue("Downloaded file should not be empty", expectedFile.length() >= 0)
}
}

This file was deleted.

Loading
Loading