|
1 | 1 | /* |
2 | 2 | * Nextcloud Android Library |
3 | 3 | * |
4 | | - * SPDX-FileCopyrightText: 2021-2024 Nextcloud GmbH and Nextcloud contributors |
| 4 | + * SPDX-FileCopyrightText: 2021-2026 Nextcloud GmbH and Nextcloud contributors |
| 5 | + * SPDX-FileCopyrightText: 2026 Alper Ozturk <alper.ozturk@nextcloud.com> |
5 | 6 | * SPDX-FileCopyrightText: 2021 Tobias Kaminsky <tobias@kaminsky.me> |
6 | 7 | * SPDX-License-Identifier: MIT |
7 | 8 | */ |
8 | 9 | package com.owncloud.android.lib.resources.files |
9 | 10 |
|
10 | 11 | import com.owncloud.android.AbstractIT |
| 12 | +import org.junit.Assert.assertEquals |
| 13 | +import org.junit.Assert.assertFalse |
11 | 14 | import org.junit.Assert.assertSame |
12 | 15 | import org.junit.Assert.assertTrue |
13 | 16 | import org.junit.Test |
14 | 17 | import java.io.File |
15 | 18 |
|
| 19 | +@Suppress("Detekt.MagicNumber") |
16 | 20 | class DownloadFileRemoteOperationIT : AbstractIT() { |
| 21 | + private val cacheDir get() = context.externalCacheDir?.absolutePath |
| 22 | + |
17 | 23 | @Test |
18 | 24 | fun download() { |
19 | 25 | val filePath = createFile("download") |
20 | 26 | val remotePath = "/download.jpg" |
21 | 27 | assertTrue( |
22 | | - @Suppress("Detekt.MagicNumber") |
23 | 28 | UploadFileRemoteOperation(filePath, remotePath, "image/jpg", 1464818400) |
24 | 29 | .execute(client) |
25 | 30 | .isSuccess |
26 | 31 | ) |
27 | 32 |
|
28 | 33 | assertTrue( |
29 | | - DownloadFileRemoteOperation(remotePath, context.externalCacheDir?.absolutePath) |
| 34 | + DownloadFileRemoteOperation(remotePath, cacheDir) |
30 | 35 | .execute(nextcloudClient) |
31 | 36 | .isSuccess |
32 | 37 | ) |
33 | 38 |
|
34 | 39 | val oldFile = File(filePath) |
35 | | - val newFile = File(context.externalCacheDir?.absolutePath + remotePath) |
| 40 | + val newFile = File(cacheDir + remotePath) |
36 | 41 | assertSame(oldFile.length(), newFile.length()) |
37 | 42 | } |
| 43 | + |
| 44 | + @Test |
| 45 | + fun downloadLargeFile() { |
| 46 | + val filePath = createFile("large_download", 1000) |
| 47 | + val remotePath = "/large_download.txt" |
| 48 | + assertTrue( |
| 49 | + UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME) |
| 50 | + .execute(client) |
| 51 | + .isSuccess |
| 52 | + ) |
| 53 | + |
| 54 | + assertTrue( |
| 55 | + DownloadFileRemoteOperation(remotePath, cacheDir) |
| 56 | + .execute(nextcloudClient) |
| 57 | + .isSuccess |
| 58 | + ) |
| 59 | + |
| 60 | + val originalFile = File(filePath) |
| 61 | + val downloadedFile = File(cacheDir + remotePath) |
| 62 | + assertEquals(originalFile.length(), downloadedFile.length()) |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + fun downloadNonExistentFile() { |
| 67 | + val result = |
| 68 | + DownloadFileRemoteOperation("/nonexistent_file_12345.txt", cacheDir) |
| 69 | + .execute(nextcloudClient) |
| 70 | + |
| 71 | + assertFalse(result.isSuccess) |
| 72 | + } |
| 73 | + |
| 74 | + @Test |
| 75 | + fun downloadAndVerifyMetadata() { |
| 76 | + val filePath = createFile("metadata_download") |
| 77 | + val remotePath = "/metadata_download.jpg" |
| 78 | + assertTrue( |
| 79 | + UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME) |
| 80 | + .execute(client) |
| 81 | + .isSuccess |
| 82 | + ) |
| 83 | + |
| 84 | + val operation = DownloadFileRemoteOperation(remotePath, cacheDir) |
| 85 | + assertTrue(operation.execute(nextcloudClient).isSuccess) |
| 86 | + |
| 87 | + assertTrue("ETag should not be empty after download", operation.etag.isNotEmpty()) |
| 88 | + assertTrue("Modification timestamp should be positive after download", operation.modificationTimestamp > 0) |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + fun downloadMultipleFiles() { |
| 93 | + val filePath1 = createFile("multi_download1") |
| 94 | + val remotePath1 = "/multi_download1.jpg" |
| 95 | + val filePath2 = createFile("multi_download2") |
| 96 | + val remotePath2 = "/multi_download2.jpg" |
| 97 | + |
| 98 | + assertTrue( |
| 99 | + UploadFileRemoteOperation(filePath1, remotePath1, "image/jpg", RANDOM_MTIME) |
| 100 | + .execute(client) |
| 101 | + .isSuccess |
| 102 | + ) |
| 103 | + assertTrue( |
| 104 | + UploadFileRemoteOperation(filePath2, remotePath2, "image/jpg", RANDOM_MTIME) |
| 105 | + .execute(client) |
| 106 | + .isSuccess |
| 107 | + ) |
| 108 | + |
| 109 | + assertTrue( |
| 110 | + DownloadFileRemoteOperation(remotePath1, cacheDir) |
| 111 | + .execute(nextcloudClient) |
| 112 | + .isSuccess |
| 113 | + ) |
| 114 | + assertTrue( |
| 115 | + DownloadFileRemoteOperation(remotePath2, cacheDir) |
| 116 | + .execute(nextcloudClient) |
| 117 | + .isSuccess |
| 118 | + ) |
| 119 | + |
| 120 | + val downloaded1 = File(cacheDir + remotePath1) |
| 121 | + val downloaded2 = File(cacheDir + remotePath2) |
| 122 | + assertTrue(downloaded1.exists()) |
| 123 | + assertTrue(downloaded2.exists()) |
| 124 | + assertEquals(File(filePath1).length(), downloaded1.length()) |
| 125 | + assertEquals(File(filePath2).length(), downloaded2.length()) |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + fun downloadAndVerifyContent() { |
| 130 | + val filePath = createFile("content_download", 50) |
| 131 | + val remotePath = "/content_download.txt" |
| 132 | + assertTrue( |
| 133 | + UploadFileRemoteOperation(filePath, remotePath, "text/plain", RANDOM_MTIME) |
| 134 | + .execute(client) |
| 135 | + .isSuccess |
| 136 | + ) |
| 137 | + |
| 138 | + assertTrue( |
| 139 | + DownloadFileRemoteOperation(remotePath, cacheDir) |
| 140 | + .execute(nextcloudClient) |
| 141 | + .isSuccess |
| 142 | + ) |
| 143 | + |
| 144 | + val originalFile = File(filePath) |
| 145 | + val downloadedFile = File(cacheDir + remotePath) |
| 146 | + assertTrue(downloadedFile.exists()) |
| 147 | + assertTrue(originalFile.readBytes().contentEquals(downloadedFile.readBytes())) |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + fun downloadedFileExistsAtExpectedPath() { |
| 152 | + val filePath = createFile("path_check") |
| 153 | + val remotePath = "/path_check.jpg" |
| 154 | + assertTrue( |
| 155 | + UploadFileRemoteOperation(filePath, remotePath, "image/jpg", RANDOM_MTIME) |
| 156 | + .execute(client) |
| 157 | + .isSuccess |
| 158 | + ) |
| 159 | + |
| 160 | + assertTrue( |
| 161 | + DownloadFileRemoteOperation(remotePath, cacheDir) |
| 162 | + .execute(nextcloudClient) |
| 163 | + .isSuccess |
| 164 | + ) |
| 165 | + |
| 166 | + val expectedFile = File(cacheDir + remotePath) |
| 167 | + assertTrue("Downloaded file should exist at expected path", expectedFile.exists()) |
| 168 | + assertTrue("Downloaded file should not be empty", expectedFile.length() >= 0) |
| 169 | + } |
38 | 170 | } |
0 commit comments