|
21 | 21 | import java.util.List; |
22 | 22 | import java.util.Map; |
23 | 23 | import java.util.Set; |
| 24 | +import java.util.ArrayList; |
24 | 25 |
|
25 | 26 | import static io.frictionlessdata.datapackage.Profile.PROFILE_DATA_PACKAGE_DEFAULT; |
26 | 27 | import static io.frictionlessdata.datapackage.Profile.PROFILE_DATA_RESOURCE_DEFAULT; |
@@ -235,6 +236,122 @@ public void testNonTabularResource3() throws Exception { |
235 | 236 |
|
236 | 237 | } |
237 | 238 |
|
| 239 | + |
| 240 | + @Test |
| 241 | + @DisplayName("Test creating ZIP-compressed datapackage with PDF as FilebasedResource") |
| 242 | + public void testZipCompressedDatapackageWithPdfResource() throws Exception { |
| 243 | + // Create temporary directories |
| 244 | + Path tempDirPath = Files.createTempDirectory("datapackage-source-"); |
| 245 | + Path tempOutputPath = Files.createTempDirectory("datapackage-output-"); |
| 246 | + |
| 247 | + // Copy PDF to temporary directory |
| 248 | + byte[] pdfContent = TestUtil.getResourceContent("/fixtures/files/sample.pdf"); |
| 249 | + File pdfFile = new File(tempDirPath.toFile(), "sample.pdf"); |
| 250 | + Files.write(pdfFile.toPath(), pdfContent); |
| 251 | + |
| 252 | + // Create FilebasedResource directly for the PDF |
| 253 | + FilebasedResource pdfResource = new FilebasedResource( |
| 254 | + "pdf-document", |
| 255 | + List.of(new File("sample.pdf")), |
| 256 | + tempDirPath.toFile() |
| 257 | + ); |
| 258 | + |
| 259 | + // Create a package with the resource |
| 260 | + List<Resource> resources = new ArrayList<>(); |
| 261 | + resources.add(pdfResource); |
| 262 | + io.frictionlessdata.datapackage.Package pkg = new io.frictionlessdata.datapackage.Package(resources); |
| 263 | + pkg.setName("pdf-package"); |
| 264 | + pkg.setProfile(Profile.PROFILE_DATA_PACKAGE_DEFAULT); |
| 265 | + |
| 266 | + // Set default resource profile and format |
| 267 | + pdfResource.setProfile(Profile.PROFILE_DATA_RESOURCE_DEFAULT); |
| 268 | + pdfResource.setFormat("pdf"); |
| 269 | + pdfResource.setTitle("Sample PDF Document"); |
| 270 | + pdfResource.setDescription("A test PDF file"); |
| 271 | + pdfResource.setMediaType("application/pdf"); |
| 272 | + pdfResource.setEncoding("UTF-8"); |
| 273 | + |
| 274 | + // Write as ZIP-compressed package |
| 275 | + File zipFile = new File(tempOutputPath.toFile(), "datapackage.zip"); |
| 276 | + pkg.write(zipFile, true); // true for compression |
| 277 | + |
| 278 | + // Verify ZIP file was created |
| 279 | + Assertions.assertTrue(zipFile.exists()); |
| 280 | + Assertions.assertTrue(zipFile.length() > 0); |
| 281 | + |
| 282 | + // Read the package back from ZIP |
| 283 | + io.frictionlessdata.datapackage.Package readPackage = new io.frictionlessdata.datapackage.Package(zipFile.toPath(), true); |
| 284 | + |
| 285 | + // Verify package properties |
| 286 | + Assertions.assertEquals("pdf-package", readPackage.getName()); |
| 287 | + Assertions.assertEquals(1, readPackage.getResources().size()); |
| 288 | + |
| 289 | + // Verify PDF resource |
| 290 | + Resource<?> readResource = readPackage.getResource("pdf-document"); |
| 291 | + Assertions.assertNotNull(readResource); |
| 292 | + Assertions.assertEquals(Profile.PROFILE_DATA_RESOURCE_DEFAULT, readResource.getProfile()); |
| 293 | + Assertions.assertEquals("pdf", readResource.getFormat()); |
| 294 | + Assertions.assertEquals("Sample PDF Document", readResource.getTitle()); |
| 295 | + Assertions.assertEquals("application/pdf", readResource.getMediaType()); |
| 296 | + } |
| 297 | + |
| 298 | + @Test |
| 299 | + @DisplayName("Test creating uncompressed datapackage with PDF as FilebasedResource") |
| 300 | + public void testUnCompressedDatapackageWithPdfResource() throws Exception { |
| 301 | + // Create temporary directories |
| 302 | + Path tempDirPath = Files.createTempDirectory("datapackage-source-"); |
| 303 | + Path tempOutputPath = Files.createTempDirectory("datapackage-output-"); |
| 304 | + |
| 305 | + // Copy PDF to temporary directory |
| 306 | + byte[] pdfContent = TestUtil.getResourceContent("/fixtures/files/sample.pdf"); |
| 307 | + File pdfFile = new File(tempDirPath.toFile(), "sample.pdf"); |
| 308 | + Files.write(pdfFile.toPath(), pdfContent); |
| 309 | + |
| 310 | + // Create FilebasedResource directly for the PDF |
| 311 | + FilebasedResource pdfResource = new FilebasedResource( |
| 312 | + "pdf-document", |
| 313 | + List.of(new File("sample.pdf")), |
| 314 | + tempDirPath.toFile() |
| 315 | + ); |
| 316 | + |
| 317 | + // Create a package with the resource |
| 318 | + List<Resource> resources = new ArrayList<>(); |
| 319 | + resources.add(pdfResource); |
| 320 | + io.frictionlessdata.datapackage.Package pkg = new io.frictionlessdata.datapackage.Package(resources); |
| 321 | + pkg.setName("pdf-package"); |
| 322 | + pkg.setProfile(Profile.PROFILE_DATA_PACKAGE_DEFAULT); |
| 323 | + |
| 324 | + // Set default resource profile and format |
| 325 | + pdfResource.setProfile(Profile.PROFILE_DATA_RESOURCE_DEFAULT); |
| 326 | + pdfResource.setFormat("pdf"); |
| 327 | + pdfResource.setTitle("Sample PDF Document"); |
| 328 | + pdfResource.setDescription("A test PDF file"); |
| 329 | + pdfResource.setMediaType("application/pdf"); |
| 330 | + pdfResource.setEncoding("UTF-8"); |
| 331 | + |
| 332 | + // Write as ZIP-compressed package |
| 333 | + File packageFile = new File(tempOutputPath.toFile(), "datapackage"); |
| 334 | + pkg.write(packageFile, false); // false for compression |
| 335 | + |
| 336 | + Assertions.assertTrue(packageFile.exists());; |
| 337 | + |
| 338 | + // Read the package back from ZIP |
| 339 | + io.frictionlessdata.datapackage.Package readPackage = new io.frictionlessdata.datapackage.Package(packageFile.toPath(), true); |
| 340 | + |
| 341 | + // Verify package properties |
| 342 | + Assertions.assertEquals("pdf-package", readPackage.getName()); |
| 343 | + Assertions.assertEquals(1, readPackage.getResources().size()); |
| 344 | + |
| 345 | + // Verify PDF resource |
| 346 | + Resource<?> readResource = readPackage.getResource("pdf-document"); |
| 347 | + Assertions.assertNotNull(readResource); |
| 348 | + Assertions.assertEquals(Profile.PROFILE_DATA_RESOURCE_DEFAULT, readResource.getProfile()); |
| 349 | + Assertions.assertEquals("pdf", readResource.getFormat()); |
| 350 | + Assertions.assertEquals("Sample PDF Document", readResource.getTitle()); |
| 351 | + Assertions.assertEquals("application/pdf", readResource.getMediaType()); |
| 352 | + } |
| 353 | + |
| 354 | + |
238 | 355 | /** |
239 | 356 | * A non-tabular resource |
240 | 357 | */ |
|
0 commit comments