Importer { public ImportRecord loadExport(Owner owner, File archive, ConflictOverrides overrides, String uploadedFileName) throws ImporterException { try { return doExport(owner, unpackExportFile(archive.getName(), new FileInputStream(archive)), overrides, uploadedFileName); } catch (FileNotFoundException e) { log.error(String.format("Could not find import archive: %s", archive.getAbsolutePath())); throw new ImporterException(i18n.tr("Uploaded manifest file does not exist."), e); } } @Inject  Importer(ConsumerTypeCurator consumerTypeCurator, ProductCurator productCurator,
        RulesImporter rulesImporter, OwnerCurator ownerCurator, IdentityCertificateCurator idCertCurator,
        ContentCurator contentCurator, PoolManager pm, PKIUtility pki, Configuration config,
        ExporterMetadataCurator emc, CertificateSerialCurator csc, EventSink sink, I18n i18n,
        DistributorVersionCurator distVerCurator, CdnCurator cdnCurator, SyncUtils syncUtils,
        ImportRecordCurator importRecordCurator, SubscriptionReconciler subscriptionReconciler,
        EntitlementCurator entitlementCurator, ContentAccessManager contentAccessManager,
        ModelTranslator translator); ImportRecord loadExport(Owner owner, File archive, ConflictOverrides overrides,
        String uploadedFileName); ImportRecord loadStoredExport(ManifestFile export, Owner owner, ConflictOverrides overrides,
        String uploadedFileName); ImportRecord recordImportSuccess(Owner owner, Map<String, Object> data,
        ConflictOverrides forcedConflicts, String filename); void recordImportFailure(Owner owner, Throwable error, String filename); @SuppressWarnings("checkstyle:methodlength") @Transactional(rollbackOn = {IOException.class, ImporterException.class, RuntimeException.class, ImportConflictException.class}) // WARNING: Keep this method public, otherwise @Transactional is ignored: List<SubscriptionDTO> importObjects(Owner owner, Map<String, File> importFiles,
        ConflictOverrides overrides);  }
@Test public void testImportWithNonZipArchive() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); File archive = new File(this.tmpFolder, "non_zip_file.zip"); FileWriter fw = new FileWriter(archive); fw.write("Just a flat file"); fw.close(); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImportExtractionException.class, () -> importer.loadExport(owner, archive, co, "original_file.zip")); String m = i18n.tr("The archive {0} is not a properly compressed file or is empty", "non_zip_file.zip"); assertThat(throwable.getMessage(), StringContains.containsString(m)); }
@Test public void testImportZipArchiveNoContent() throws IOException, ImporterException { Importer importer = this.buildImporter(); Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); File archive = new File(this.tmpFolder, "file.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry("no_content")); out.close(); Throwable throwable = assertThrows(ImportExtractionException.class, () -> importer.loadExport(owner, archive, co, "original_file.zip")); String m = i18n.tr("The archive does not contain the required signature file"); assertThat(throwable.getMessage(), StringContains.containsString(m)); }
@Test public void testImportBadSignature() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); File archive = new File(this.tmpFolder, "file.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry("signature")); out.write("This is the placeholder for the signature file".getBytes()); File ceArchive = new File(this.tmpFolder, "consumer_export.zip"); FileOutputStream fos = new FileOutputStream(ceArchive); fos.write("This is just a flat file".getBytes()); fos.close(); addFileToArchive(out, ceArchive); out.close(); Importer importer = this.buildImporter(); assertThrows(ImportConflictException.class, () -> importer.loadExport(owner, archive, co, "original_file.zip")); }
@Test public void testImportBadConsumerZip() throws Exception { doReturn(true) .when(this.mockPKIUtility) .verifySHA256WithRSAHashAgainstCACerts(any(File.class), any(byte [].class)); Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); File archive = new File(this.tmpFolder, "file.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry("signature")); out.write("This is the placeholder for the signature file".getBytes()); File ceArchive = new File(this.tmpFolder, "consumer_export.zip"); FileOutputStream fos = new FileOutputStream(ceArchive); fos.write("This is just a flat file".getBytes()); fos.close(); addFileToArchive(out, ceArchive); out.close(); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImportExtractionException.class, () -> importer.loadExport(owner, archive, co, "original_file.zip")); String errmsg = "not a properly compressed file or is empty"; assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testImportZipSigAndEmptyConsumerZip() throws Exception { doReturn(true) .when(this.mockPKIUtility) .verifySHA256WithRSAHashAgainstCACerts(any(File.class), any(byte [].class)); Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); File archive = new File(this.tmpFolder, "file.zip"); ZipOutputStream out = new ZipOutputStream(new FileOutputStream(archive)); out.putNextEntry(new ZipEntry("signature")); out.write("This is the placeholder for the signature file".getBytes()); File ceArchive = new File(this.tmpFolder, "consumer_export.zip"); ZipOutputStream cezip = new ZipOutputStream(new FileOutputStream(ceArchive)); cezip.putNextEntry(new ZipEntry("no_content")); cezip.close(); addFileToArchive(out, ceArchive); out.close(); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImportExtractionException.class, () -> importer.loadExport(owner, archive, co, "original_file.zip")); String errmsg = "consumer_export archive has no contents"; assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }