Importer { public ImportRecord recordImportSuccess(Owner owner, Map<String, Object> data, ConflictOverrides forcedConflicts, String filename) { ImportRecord record = new ImportRecord(owner); Meta meta = (Meta) data.get("meta"); if (meta != null) { record.setGeneratedBy(meta.getPrincipalName()); record.setGeneratedDate(meta.getCreated()); } record.setUpstreamConsumer(createImportUpstreamConsumer(owner, null)); record.setFileName(filename); List<SubscriptionDTO> subscriptions = (List<SubscriptionDTO>) data.get("subscriptions"); boolean activeSubscriptionFound = false, expiredSubscriptionFound = false; Date currentDate = new Date(); for (SubscriptionDTO subscription : subscriptions) { if (subscription.getEndDate() == null || subscription.getEndDate().after(currentDate)) { activeSubscriptionFound = true; } else { expiredSubscriptionFound = true; sink.emitSubscriptionExpired(subscription); } } String msg = i18n.tr("{0} file imported successfully.", owner.getKey()); if (!forcedConflicts.isEmpty()) { msg = i18n.tr("{0} file imported forcibly.", owner.getKey()); } if (!activeSubscriptionFound) { msg += i18n.tr("No active subscriptions found in the file."); record.recordStatus(ImportRecord.Status.SUCCESS_WITH_WARNING, msg); } else if (expiredSubscriptionFound) { msg += i18n.tr("One or more inactive subscriptions found in the file."); record.recordStatus(ImportRecord.Status.SUCCESS_WITH_WARNING, msg); } else { record.recordStatus(ImportRecord.Status.SUCCESS, msg); } this.importRecordCurator.create(record); return record; } @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 testRecordImportSuccess() { String expectedOwnerKey = "TEST_OWNER"; Owner owner = new Owner(expectedOwnerKey); Meta meta = new Meta("1.0", new Date(), "test-user", "candlepin", "testcdn"); List<SubscriptionDTO> subscriptions = new ArrayList<>(); SubscriptionDTO subscription = new SubscriptionDTO(); subscriptions.add(subscription); Map<String, Object> data = new HashMap<>(); data.put("meta", meta); data.put("subscriptions", subscriptions); Importer importer = this.buildImporter(); ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip"); assertEquals(record.getGeneratedBy(), meta.getPrincipalName()); assertEquals(record.getGeneratedDate(), meta.getCreated()); assertEquals(record.getStatus(), ImportRecord.Status.SUCCESS); assertEquals(record.getStatusMessage(), owner.getKey() + " file imported successfully."); assertEquals(record.getFileName(), "test.zip"); verify(this.mockImportRecordCurator).create(eq(record)); verify(this.mockEventSink, never()).emitSubscriptionExpired(subscription); }
@Test public void testRecordImportSetsUpstreamConsumerFromOwner() { String expectedOwnerKey = "TEST_OWNER"; Owner owner = new Owner(expectedOwnerKey); UpstreamConsumer uc = new UpstreamConsumer("uc", owner, new ConsumerType(ConsumerType.ConsumerTypeEnum.CANDLEPIN), "uuid"); uc.setContentAccessMode("mode"); owner.setUpstreamConsumer(uc); Meta meta = new Meta("1.0", new Date(), "test-user", "candlepin", "testcdn"); Map<String, Object> data = new HashMap<>(); data.put("meta", meta); data.put("subscriptions", new ArrayList<Subscription>()); Importer importer = this.buildImporter(); ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip"); ImportUpstreamConsumer iuc = record.getUpstreamConsumer(); assertNotNull(iuc); assertEquals(iuc.getOwnerId(), uc.getOwnerId()); assertEquals(iuc.getName(), uc.getName()); assertEquals(iuc.getUuid(), uc.getUuid()); assertEquals(iuc.getType(), uc.getType()); assertEquals(iuc.getWebUrl(), uc.getWebUrl()); assertEquals(iuc.getApiUrl(), uc.getApiUrl()); assertEquals(iuc.getContentAccessMode(), uc.getContentAccessMode()); verify(this.mockImportRecordCurator).create(eq(record)); }
@Test public void testRecordImportIgnoresUpstreamConsumerIfNotSetOnOwner() { String expectedOwnerKey = "TEST_OWNER"; Owner owner = new Owner(expectedOwnerKey); Meta meta = new Meta("1.0", new Date(), "test-user", "candlepin", "testcdn"); Map<String, Object> data = new HashMap<>(); data.put("meta", meta); data.put("subscriptions", new ArrayList<Subscription>()); Importer importer = this.buildImporter(); ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip"); assertNull(record.getUpstreamConsumer()); verify(this.mockImportRecordCurator).create(eq(record)); }
@Test public void testRecordImportExpiredSubsFound() { String expectedOwnerKey = "TEST_OWNER"; Owner owner = new Owner(expectedOwnerKey); Map<String, Object> data = new HashMap<>(); List<SubscriptionDTO> subscriptions = new ArrayList<>(); SubscriptionDTO subscription1 = new SubscriptionDTO(); subscription1.setEndDate(new Date((new Date()).getTime() + (1000 * 60 * 60 * 24))); subscriptions.add(subscription1); SubscriptionDTO subscription2 = new SubscriptionDTO(); subscription2.setEndDate(new Date((new Date()).getTime() - (1000 * 60 * 60 * 24))); subscriptions.add(subscription2); data.put("subscriptions", subscriptions); Importer importer = this.buildImporter(); ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip"); assertEquals(record.getStatus(), ImportRecord.Status.SUCCESS_WITH_WARNING); assertEquals(record.getStatusMessage(), owner.getKey() + " file imported successfully." + "One or more inactive subscriptions found in the file."); verify(this.mockEventSink, never()).emitSubscriptionExpired(subscription1); verify(this.mockEventSink).emitSubscriptionExpired(subscription2); verify(this.mockImportRecordCurator).create(eq(record)); }
@Test public void testRecordImportNoActiveSubsFound() { String expectedOwnerKey = "TEST_OWNER"; Owner owner = new Owner(expectedOwnerKey); Map<String, Object> data = new HashMap<>(); data.put("subscriptions", new ArrayList<SubscriptionDTO>()); Importer importer = this.buildImporter(); ImportRecord record = importer.recordImportSuccess(owner, data, new ConflictOverrides(), "test.zip"); assertEquals(ImportRecord.Status.SUCCESS_WITH_WARNING, record.getStatus()); assertEquals(owner.getKey() + " file imported successfully." + "No active subscriptions found in the file.", record.getStatusMessage()); verify(this.mockEventSink, never()).emitSubscriptionExpired(any(SubscriptionDTO.class)); verify(this.mockImportRecordCurator).create(eq(record)); }