|
1 | 1 | package com.box.sdk; |
2 | 2 |
|
| 3 | +import static com.box.sdk.BinaryBodyUtils.writeStream; |
3 | 4 | import static com.box.sdk.BoxApiProvider.jwtApiForServiceAccount; |
4 | 5 | import static com.box.sdk.BoxFile.ALL_VERSION_FIELDS; |
| 6 | +import static com.box.sdk.BoxFile.CONTENT_URL_TEMPLATE; |
5 | 7 | import static com.box.sdk.BoxRetentionPolicyAssignment.createAssignmentToFolder; |
6 | 8 | import static com.box.sdk.BoxSharedLink.Access.OPEN; |
7 | 9 | import static com.box.sdk.CleanupTools.deleteFile; |
@@ -225,6 +227,64 @@ public void uploadAndDownloadFileSucceeds() throws IOException { |
225 | 227 |
|
226 | 228 | } |
227 | 229 |
|
| 230 | + @Test |
| 231 | + public void downloadFileUseZstdSucceeds() throws IOException { |
| 232 | + BoxAPIConnection api = jwtApiForServiceAccount(); |
| 233 | + api.setUseZstdCompression(true); |
| 234 | + |
| 235 | + String fileName = "smalltest.pdf"; |
| 236 | + URL fileURL = this.getClass().getResource("/sample-files/" + fileName); |
| 237 | + String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8"); |
| 238 | + byte[] fileContent = readAllBytes(filePath); |
| 239 | + BoxFile file = null; |
| 240 | + try { |
| 241 | + file = uploadSampleFileToUniqueFolder(api, fileName); |
| 242 | + |
| 243 | + ByteArrayOutputStream downloadStream = new ByteArrayOutputStream(); |
| 244 | + ProgressListener mockDownloadListener = mock(ProgressListener.class); |
| 245 | + |
| 246 | + URL url = CONTENT_URL_TEMPLATE.build(api.getBaseURL(), file.getID()); |
| 247 | + BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); |
| 248 | + BoxAPIResponse response = request.send(); |
| 249 | + writeStream(response, downloadStream, mockDownloadListener); |
| 250 | + |
| 251 | + byte[] downloadedFileContent = downloadStream.toByteArray(); |
| 252 | + assertThat(response.getHeaders().get("X-Content-Encoding").get(0), is(equalTo("zstd"))); |
| 253 | + assertThat(downloadedFileContent, is(equalTo(fileContent))); |
| 254 | + } finally { |
| 255 | + deleteFile(file); |
| 256 | + } |
| 257 | + } |
| 258 | + |
| 259 | + @Test |
| 260 | + public void uploadAndDownloadFileDisabledZstdSucceeds() throws IOException { |
| 261 | + BoxAPIConnection api = jwtApiForServiceAccount(); |
| 262 | + api.setUseZstdCompression(false); |
| 263 | + |
| 264 | + String fileName = "smalltest.pdf"; |
| 265 | + URL fileURL = this.getClass().getResource("/sample-files/" + fileName); |
| 266 | + String filePath = URLDecoder.decode(fileURL.getFile(), "utf-8"); |
| 267 | + byte[] fileContent = readAllBytes(filePath); |
| 268 | + BoxFile file = null; |
| 269 | + try { |
| 270 | + file = uploadSampleFileToUniqueFolder(api, fileName); |
| 271 | + |
| 272 | + ByteArrayOutputStream downloadStream = new ByteArrayOutputStream(); |
| 273 | + ProgressListener mockDownloadListener = mock(ProgressListener.class); |
| 274 | + |
| 275 | + URL url = CONTENT_URL_TEMPLATE.build(api.getBaseURL(), file.getID()); |
| 276 | + BoxAPIRequest request = new BoxAPIRequest(api, url, "GET"); |
| 277 | + BoxAPIResponse response = request.send(); |
| 278 | + writeStream(response, downloadStream, mockDownloadListener); |
| 279 | + |
| 280 | + byte[] downloadedFileContent = downloadStream.toByteArray(); |
| 281 | + assertThat(response.getHeaders().get("X-Content-Encoding"), is(nullValue())); |
| 282 | + assertThat(downloadedFileContent, is(equalTo(fileContent))); |
| 283 | + } finally { |
| 284 | + deleteFile(file); |
| 285 | + } |
| 286 | + } |
| 287 | + |
228 | 288 | @Test |
229 | 289 | public void downloadFileRangeSucceeds() throws IOException { |
230 | 290 | BoxAPIConnection api = jwtApiForServiceAccount(); |
|
0 commit comments