|
| 1 | +package fi.hel.haitaton.hanke.attachment.common |
| 2 | + |
| 3 | +import assertk.assertThat |
| 4 | +import assertk.assertions.contains |
| 5 | +import assertk.assertions.isEqualTo |
| 6 | +import assertk.assertions.isFalse |
| 7 | +import assertk.assertions.isTrue |
| 8 | +import com.azure.storage.blob.BlobServiceClient |
| 9 | +import com.azure.storage.blob.models.BlobHttpHeaders |
| 10 | +import fi.hel.haitaton.hanke.attachment.azure.AzureContainerServiceClient |
| 11 | +import fi.hel.haitaton.hanke.attachment.azure.AzuriteTestContainer |
| 12 | +import fi.hel.haitaton.hanke.attachment.azure.BlobFileClient |
| 13 | +import fi.hel.haitaton.hanke.attachment.azure.Container |
| 14 | +import fi.hel.haitaton.hanke.attachment.azure.Containers |
| 15 | +import org.junit.jupiter.api.BeforeAll |
| 16 | +import org.junit.jupiter.api.BeforeEach |
| 17 | +import org.junit.jupiter.api.Test |
| 18 | +import org.junit.jupiter.api.TestInstance |
| 19 | +import org.springframework.http.MediaType |
| 20 | +import org.springframework.test.context.ActiveProfiles |
| 21 | + |
| 22 | +@ActiveProfiles("test") |
| 23 | +@TestInstance(TestInstance.Lifecycle.PER_CLASS) |
| 24 | +class BlobMetadataFixerITest { |
| 25 | + |
| 26 | + private lateinit var blobServiceClient: BlobServiceClient |
| 27 | + private lateinit var fileClient: BlobFileClient |
| 28 | + private lateinit var fixer: BlobMetadataFixer |
| 29 | + |
| 30 | + // Use separate test container names to avoid interfering with other tests |
| 31 | + private val containers: Containers = |
| 32 | + Containers( |
| 33 | + decisions = "paatokset-fixer-test", |
| 34 | + hakemusAttachments = "hakemusliitteet-fixer-test", |
| 35 | + hankeAttachments = "hankeliitteet-fixer-test", |
| 36 | + ) |
| 37 | + |
| 38 | + @BeforeAll |
| 39 | + fun setup() { |
| 40 | + val connectionString = AzuriteTestContainer.getConnectionString() |
| 41 | + blobServiceClient = AzureContainerServiceClient(connectionString, "").blobServiceClient() |
| 42 | + fileClient = BlobFileClient(blobServiceClient, containers) |
| 43 | + fixer = BlobMetadataFixer(blobServiceClient, containers) |
| 44 | + |
| 45 | + for (container in containers) { |
| 46 | + blobServiceClient.getBlobContainerClient(container).createIfNotExists() |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @BeforeEach |
| 51 | + fun cleanupBlobs() { |
| 52 | + // Clean up any existing test blobs |
| 53 | + for (containerName in containers) { |
| 54 | + val containerClient = blobServiceClient.getBlobContainerClient(containerName) |
| 55 | + containerClient.listBlobs().forEach { blob -> |
| 56 | + if (blob.name.startsWith("test-fixer/")) { |
| 57 | + containerClient.getBlobClient(blob.name).delete() |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + fun `fixAllBlobs scans all containers and returns combined statistics`() { |
| 65 | + // Upload test blobs with properly encoded filenames |
| 66 | + fileClient.upload( |
| 67 | + Container.HANKE_LIITTEET, |
| 68 | + "test-fixer/file1.pdf", |
| 69 | + "file1.pdf", |
| 70 | + MediaType.APPLICATION_PDF, |
| 71 | + "content1".toByteArray(), |
| 72 | + ) |
| 73 | + fileClient.upload( |
| 74 | + Container.HAKEMUS_LIITTEET, |
| 75 | + "test-fixer/file2.pdf", |
| 76 | + "file2.pdf", |
| 77 | + MediaType.APPLICATION_PDF, |
| 78 | + "content2".toByteArray(), |
| 79 | + ) |
| 80 | + |
| 81 | + val result = fixer.fixAllBlobs() |
| 82 | + |
| 83 | + // Should scan at least the 2 test blobs we uploaded |
| 84 | + assertThat(result.scannedCount).isEqualTo(2) |
| 85 | + // These are already properly encoded, so no fixes needed |
| 86 | + assertThat(result.fixedCount).isEqualTo(0) |
| 87 | + assertThat(result.errorCount).isEqualTo(0) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun `fixBlobsInContainer fixes blobs with unencoded commas`() { |
| 92 | + val container = Container.HANKE_LIITTEET |
| 93 | + val path = "test-fixer/file,with,commas.pdf" |
| 94 | + val filename = "file,with,commas.pdf" |
| 95 | + |
| 96 | + // Upload blob with properly encoded filename |
| 97 | + fileClient.upload( |
| 98 | + container, |
| 99 | + path, |
| 100 | + filename, |
| 101 | + MediaType.APPLICATION_PDF, |
| 102 | + "content".toByteArray(), |
| 103 | + ) |
| 104 | + |
| 105 | + // Manually corrupt the Content-Disposition header to simulate old format |
| 106 | + val containerClient = blobServiceClient.getBlobContainerClient(containers.hankeAttachments) |
| 107 | + val blobClient = containerClient.getBlobClient(path) |
| 108 | + val corruptedHeaders = BlobHttpHeaders() |
| 109 | + corruptedHeaders.setContentType(MediaType.APPLICATION_PDF_VALUE) |
| 110 | + // Simulate old format with unencoded comma |
| 111 | + corruptedHeaders.setContentDisposition("attachment; filename*=UTF-8''file,with,commas.pdf") |
| 112 | + blobClient.setHttpHeaders(corruptedHeaders) |
| 113 | + |
| 114 | + // Verify it needs fixing |
| 115 | + val beforeProperties = blobClient.properties |
| 116 | + assertThat(BlobMetadataFixer.needsFixing(beforeProperties.contentDisposition)).isTrue() |
| 117 | + |
| 118 | + // Run fixer |
| 119 | + val result = fixer.fixBlobsInContainer(container) |
| 120 | + |
| 121 | + // Verify it was fixed |
| 122 | + assertThat(result.scannedCount).isEqualTo(1) |
| 123 | + assertThat(result.fixedCount).isEqualTo(1) |
| 124 | + assertThat(result.errorCount).isEqualTo(0) |
| 125 | + |
| 126 | + // Verify the header is now properly encoded (no unencoded commas) |
| 127 | + val afterProperties = blobClient.properties |
| 128 | + assertThat(BlobMetadataFixer.needsFixing(afterProperties.contentDisposition)).isFalse() |
| 129 | + } |
| 130 | + |
| 131 | + @Test |
| 132 | + fun `fixBlobsInContainer fixes blobs with simple filename format`() { |
| 133 | + val container = Container.HANKE_LIITTEET |
| 134 | + val path = "test-fixer/simple.pdf" |
| 135 | + |
| 136 | + // Upload blob |
| 137 | + fileClient.upload( |
| 138 | + container, |
| 139 | + path, |
| 140 | + "simple.pdf", |
| 141 | + MediaType.APPLICATION_PDF, |
| 142 | + "content".toByteArray(), |
| 143 | + ) |
| 144 | + |
| 145 | + // Manually set old simple format |
| 146 | + val containerClient = blobServiceClient.getBlobContainerClient(containers.hankeAttachments) |
| 147 | + val blobClient = containerClient.getBlobClient(path) |
| 148 | + val oldHeaders = BlobHttpHeaders() |
| 149 | + oldHeaders.setContentType(MediaType.APPLICATION_PDF_VALUE) |
| 150 | + oldHeaders.setContentDisposition("attachment; filename=\"simple.pdf\"") |
| 151 | + blobClient.setHttpHeaders(oldHeaders) |
| 152 | + |
| 153 | + // Verify it needs fixing |
| 154 | + val beforeProperties = blobClient.properties |
| 155 | + assertThat(BlobMetadataFixer.needsFixing(beforeProperties.contentDisposition)).isTrue() |
| 156 | + |
| 157 | + // Run fixer |
| 158 | + val result = fixer.fixBlobsInContainer(container) |
| 159 | + |
| 160 | + // Verify it was fixed |
| 161 | + assertThat(result.scannedCount).isEqualTo(1) |
| 162 | + assertThat(result.fixedCount).isEqualTo(1) |
| 163 | + assertThat(result.errorCount).isEqualTo(0) |
| 164 | + |
| 165 | + // Verify the header now includes RFC 5987 format |
| 166 | + val afterProperties = blobClient.properties |
| 167 | + assertThat(BlobMetadataFixer.needsFixing(afterProperties.contentDisposition)).isFalse() |
| 168 | + assertThat(afterProperties.contentDisposition).contains("filename*=UTF-8''simple.pdf") |
| 169 | + } |
| 170 | + |
| 171 | + @Test |
| 172 | + fun `fixBlobsInContainer skips blobs with no Content-Disposition`() { |
| 173 | + val container = Container.HANKE_LIITTEET |
| 174 | + val path = "test-fixer/no-disposition.pdf" |
| 175 | + |
| 176 | + // Upload blob |
| 177 | + fileClient.upload( |
| 178 | + container, |
| 179 | + path, |
| 180 | + "test.pdf", |
| 181 | + MediaType.APPLICATION_PDF, |
| 182 | + "content".toByteArray(), |
| 183 | + ) |
| 184 | + |
| 185 | + // Remove Content-Disposition |
| 186 | + val containerClient = blobServiceClient.getBlobContainerClient(containers.hankeAttachments) |
| 187 | + val blobClient = containerClient.getBlobClient(path) |
| 188 | + val headers = BlobHttpHeaders() |
| 189 | + headers.setContentType(MediaType.APPLICATION_PDF_VALUE) |
| 190 | + headers.setContentDisposition(null) |
| 191 | + blobClient.setHttpHeaders(headers) |
| 192 | + |
| 193 | + // Run fixer |
| 194 | + val result = fixer.fixBlobsInContainer(container) |
| 195 | + |
| 196 | + // Should scan but not fix (just warn) |
| 197 | + assertThat(result.scannedCount).isEqualTo(1) |
| 198 | + assertThat(result.fixedCount).isEqualTo(0) |
| 199 | + assertThat(result.errorCount).isEqualTo(0) |
| 200 | + } |
| 201 | + |
| 202 | + @Test |
| 203 | + fun `fixBlobsInContainer counts errors when filename extraction fails`() { |
| 204 | + val container = Container.HANKE_LIITTEET |
| 205 | + val path = "test-fixer/invalid-disposition.pdf" |
| 206 | + |
| 207 | + // Upload blob |
| 208 | + fileClient.upload( |
| 209 | + container, |
| 210 | + path, |
| 211 | + "test.pdf", |
| 212 | + MediaType.APPLICATION_PDF, |
| 213 | + "content".toByteArray(), |
| 214 | + ) |
| 215 | + |
| 216 | + // Set invalid Content-Disposition that can't be parsed |
| 217 | + val containerClient = blobServiceClient.getBlobContainerClient(containers.hankeAttachments) |
| 218 | + val blobClient = containerClient.getBlobClient(path) |
| 219 | + val headers = BlobHttpHeaders() |
| 220 | + headers.setContentType(MediaType.APPLICATION_PDF_VALUE) |
| 221 | + headers.setContentDisposition("invalid-disposition-format") |
| 222 | + blobClient.setHttpHeaders(headers) |
| 223 | + |
| 224 | + // Run fixer |
| 225 | + val result = fixer.fixBlobsInContainer(container) |
| 226 | + |
| 227 | + // Should scan and encounter error (can't extract filename) |
| 228 | + assertThat(result.scannedCount).isEqualTo(1) |
| 229 | + assertThat(result.fixedCount).isEqualTo(0) |
| 230 | + assertThat(result.errorCount).isEqualTo(1) |
| 231 | + } |
| 232 | + |
| 233 | + @Test |
| 234 | + fun `fixBlobsInContainer skips properly encoded blobs`() { |
| 235 | + val container = Container.HANKE_LIITTEET |
| 236 | + val path = "test-fixer/already-encoded.pdf" |
| 237 | + |
| 238 | + // Upload blob with properly encoded filename containing special characters |
| 239 | + fileClient.upload( |
| 240 | + container, |
| 241 | + path, |
| 242 | + "file,with,commas.pdf", |
| 243 | + MediaType.APPLICATION_PDF, |
| 244 | + "content".toByteArray(), |
| 245 | + ) |
| 246 | + |
| 247 | + // Verify it's properly encoded |
| 248 | + val containerClient = blobServiceClient.getBlobContainerClient(containers.hankeAttachments) |
| 249 | + val blobClient = containerClient.getBlobClient(path) |
| 250 | + val beforeProperties = blobClient.properties |
| 251 | + assertThat(BlobMetadataFixer.needsFixing(beforeProperties.contentDisposition)).isFalse() |
| 252 | + |
| 253 | + // Run fixer |
| 254 | + val result = fixer.fixBlobsInContainer(container) |
| 255 | + |
| 256 | + // Should scan but not fix |
| 257 | + assertThat(result.scannedCount).isEqualTo(1) |
| 258 | + assertThat(result.fixedCount).isEqualTo(0) |
| 259 | + assertThat(result.errorCount).isEqualTo(0) |
| 260 | + |
| 261 | + // Verify header unchanged |
| 262 | + val afterProperties = blobClient.properties |
| 263 | + assertThat(afterProperties.contentDisposition) |
| 264 | + .isEqualTo(beforeProperties.contentDisposition) |
| 265 | + } |
| 266 | +} |
0 commit comments