Importer { protected void validateMetadata(String type, Owner owner, File meta, ConflictOverrides forcedConflicts) throws IOException, ImporterException { Meta m = mapper.readValue(meta, Meta.class); if (type == null) { throw new ImporterException(i18n.tr("Wrong metadata type")); } ExporterMetadata lastrun = null; if (ExporterMetadata.TYPE_SYSTEM.equals(type)) { lastrun = expMetaCurator.getByType(type); } else if (ExporterMetadata.TYPE_PER_USER.equals(type)) { if (owner == null) { throw new ImporterException(i18n.tr("Invalid owner")); } lastrun = expMetaCurator.getByTypeAndOwner(type, owner); } if (lastrun == null) { lastrun = new ExporterMetadata(type, m.getCreated(), owner); lastrun = expMetaCurator.create(lastrun); } else { if (lastrun.getExported().after(m.getCreated())) { if (!forcedConflicts.isForced(Importer.Conflict.MANIFEST_OLD)) { throw new ImportConflictException(i18n.tr("Import is older than existing data"), Importer.Conflict.MANIFEST_OLD); } else { log.warn("Manifest older than existing data."); } } else { long exported = lastrun.getExported().getTime() / 1000; long created = m.getCreated().getTime() / 1000; if (exported == created) { if (!forcedConflicts.isForced(Importer.Conflict.MANIFEST_SAME)) { throw new ImportConflictException(i18n.tr("Import is the same as existing data"), Importer.Conflict.MANIFEST_SAME); } else { log.warn("Manifest same as existing data."); } } } lastrun.setExported(m.getCreated()); expMetaCurator.merge(lastrun); } } @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 validateMetaJson() throws Exception { Date now = new Date(); File file = createFile("meta", "0.0.3", now, "test_user", "prefix"); File actual = createFile("meta.json", "0.0.3", now, "test_user", "prefix"); ExporterMetadata em = new ExporterMetadata(); Date daybefore = getDateBeforeDays(1); em.setExported(daybefore); em.setId("42"); em.setType(ExporterMetadata.TYPE_SYSTEM); doReturn(em) .when(this.mockExporterMetadataCurator) .getByType(eq(ExporterMetadata.TYPE_SYSTEM)); Importer importer = this.buildImporter(); importer.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actual, new ConflictOverrides()); Meta fileMeta = mapper.readValue(file, Meta.class); Meta actualMeta = mapper.readValue(actual, Meta.class); assertEquals(fileMeta.getPrincipalName(), actualMeta.getPrincipalName()); assertEquals(fileMeta.getCreated().getTime(), actualMeta.getCreated().getTime()); assertEquals(fileMeta.getWebAppPrefix(), actualMeta.getWebAppPrefix()); assertTrue(file.delete()); assertTrue(actual.delete()); assertTrue(daybefore.compareTo(em.getExported()) < 0); }
@Test public void firstRun() throws Exception { File file = createFile("meta", "0.0.3", new Date(), "test_user", "prefix"); File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix"); Importer importer = this.buildImporter(); importer.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides()); assertTrue(file.delete()); assertTrue(actualmeta.delete()); verify(this.mockExporterMetadataCurator).create(any(ExporterMetadata.class)); }
@Test public void oldImport() throws Exception { File actualmeta = createFile("meta.json", "0.0.3", getDateBeforeDays(10), "test_user", "prefix"); ExporterMetadata em = new ExporterMetadata(); em.setExported(getDateBeforeDays(3)); em.setId("42"); em.setType(ExporterMetadata.TYPE_SYSTEM); doReturn(em) .when(this.mockExporterMetadataCurator) .getByType(eq(ExporterMetadata.TYPE_SYSTEM)); Importer importer = this.buildImporter(); try { importer.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides()); fail("Expected an ImportConflictException, but no exception was thrown"); } catch (ImportConflictException e) { assertFalse(e.message().getConflicts().isEmpty()); assertEquals(1, e.message().getConflicts().size()); assertTrue(e.message().getConflicts().contains(Importer.Conflict.MANIFEST_OLD)); } }
@Test public void sameImport() throws Exception { Date date = getDateBeforeDays(10); File actualmeta = createFile("meta.json", "0.0.3", date, "test_user", "prefix"); ExporterMetadata em = new ExporterMetadata(); em.setExported(date); em.setId("42"); em.setType(ExporterMetadata.TYPE_SYSTEM); doReturn(em) .when(this.mockExporterMetadataCurator) .getByType(eq(ExporterMetadata.TYPE_SYSTEM)); Importer importer = this.buildImporter(); try { importer.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides()); fail("Expected an ImportConflictException, but no exception was thrown"); } catch (ImportConflictException e) { assertFalse(e.message().getConflicts().isEmpty()); assertEquals(1, e.message().getConflicts().size()); assertTrue(e.message().getConflicts().contains(Importer.Conflict.MANIFEST_SAME)); } }
@Test public void newerImport() throws Exception { Date importDate = getDateBeforeDays(10); File actualmeta = createFile("meta.json", "0.0.3", importDate, "test_user", "prefix"); ExporterMetadata em = new ExporterMetadata(); em.setExported(getDateBeforeDays(30)); em.setId("42"); em.setType(ExporterMetadata.TYPE_SYSTEM); doReturn(em) .when(this.mockExporterMetadataCurator) .getByType(eq(ExporterMetadata.TYPE_SYSTEM)); Importer importer = this.buildImporter(); importer.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides()); assertEquals(em.getExported(), importDate); }
@Test public void nullType() throws ImporterException, IOException { File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix"); Importer importer = this.buildImporter(); assertThrows(ImporterException.class, () -> importer.validateMetadata(null, null, actualmeta, new ConflictOverrides())); assertTrue(actualmeta.delete()); }
@Test public void expectOwner() throws ImporterException, IOException { ConflictOverrides overrides = new ConflictOverrides(); File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix"); Importer importer = this.buildImporter(); assertThrows(ImporterException.class, () -> importer.validateMetadata(ExporterMetadata.TYPE_PER_USER, null, actualmeta, overrides)); verify(this.mockExporterMetadataCurator, never()).create(any(ExporterMetadata.class)); }