Importer { @SuppressWarnings("checkstyle:methodlength") @Transactional(rollbackOn = {IOException.class, ImporterException.class, RuntimeException.class, ImportConflictException.class}) public List<SubscriptionDTO> importObjects(Owner owner, Map<String, File> importFiles, ConflictOverrides overrides) throws IOException, ImporterException { ownerCurator.lock(owner); log.debug("Importing objects for owner: {}", owner); File metadata = importFiles.get(ImportFile.META.fileName()); if (metadata == null) { throw new ImporterException(i18n.tr("The archive does not contain the required meta.json file")); } File consumerTypes = importFiles.get(ImportFile.CONSUMER_TYPE.fileName()); if (consumerTypes == null) { throw new ImporterException( i18n.tr("The archive does not contain the required consumer_types directory")); } File consumerFile = importFiles.get(ImportFile.CONSUMER.fileName()); if (consumerFile == null) { throw new ImporterException( i18n.tr("The archive does not contain the required consumer.json file")); } File products = importFiles.get(ImportFile.PRODUCTS.fileName()); File entitlements = importFiles.get(ImportFile.ENTITLEMENTS.fileName()); if (products != null && entitlements == null) { throw new ImporterException( i18n.tr("The archive does not contain the required entitlements directory")); } List<ImportConflictException> conflictExceptions = new LinkedList<>(); File rules = importFiles.get(ImportFile.RULES_FILE.fileName()); importRules(rules, metadata); importConsumerTypes(consumerTypes.listFiles()); File distributorVersions = importFiles.get(ImportFile.DISTRIBUTOR_VERSIONS.fileName()); if (distributorVersions != null) { importDistributorVersions(distributorVersions.listFiles()); } File cdns = importFiles.get(ImportFile.CONTENT_DELIVERY_NETWORKS.fileName()); if (cdns != null) { importContentDeliveryNetworks(cdns.listFiles()); } try { validateMetadata(ExporterMetadata.TYPE_PER_USER, owner, metadata, overrides); } catch (ImportConflictException e) { conflictExceptions.add(e); } ConsumerDTO consumer = null; try { Meta m = mapper.readValue(metadata, Meta.class); File upstreamFile = importFiles.get(ImportFile.UPSTREAM_CONSUMER.fileName()); File[] dafiles = new File[0]; if (upstreamFile != null) { dafiles = upstreamFile.listFiles(); } consumer = importConsumer(owner, consumerFile, dafiles, overrides, m); } catch (ImportConflictException e) { conflictExceptions.add(e); } if (!conflictExceptions.isEmpty()) { log.error("Conflicts occurred during import that were not overridden:"); for (ImportConflictException e : conflictExceptions) { log.error("{}", e.message().getConflicts()); } throw new ImportConflictException(conflictExceptions); } if (consumer == null) { throw new IllegalStateException("No consumer found during import"); } Meta meta = mapper.readValue(metadata, Meta.class); List<SubscriptionDTO> importSubs; if (importFiles.get(ImportFile.PRODUCTS.fileName()) != null) { ProductImporter importer = new ProductImporter(); Set<ProductDTO> productsToImport = importProducts( importFiles.get(ImportFile.PRODUCTS.fileName()).listFiles(), importer, owner); importSubs = importEntitlements( owner, productsToImport, entitlements.listFiles(), consumer.getUuid(), meta); } else { log.warn("No products found to import, skipping product import."); log.warn("No entitlements in manifest, removing all subscriptions for owner."); importSubs = importEntitlements(owner, new HashSet<>(), new File[]{}, consumer.getUuid(), meta); } final String contentAccessMode = ContentAccessMode .resolveModeName(consumer.getContentAccessMode(), true) .toDatabaseValue(); SubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(importSubs); Refresher refresher = poolManager.getRefresher(subAdapter); refresher.add(owner); refresher.run(); this.contentAccessManager.updateOwnerContentAccess(owner, contentAccessMode, contentAccessMode); return importSubs; } @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 testImportNoMeta() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = this.getTestImportFiles(); importFiles.put(ImportFile.META.fileName(), null); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImporterException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = i18n.tr("The archive does not contain the required meta.json file"); assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testImportNoConsumerTypesDir() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = this.getTestImportFiles(); importFiles.put(ImportFile.CONSUMER_TYPE.fileName(), null); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImporterException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = i18n.tr("The archive does not contain the required consumer_types directory"); assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testImportNoConsumer() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = this.getTestImportFiles(); importFiles.put(ImportFile.CONSUMER.fileName(), null); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImporterException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = i18n.tr("The archive does not contain the required consumer.json file"); assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testImportNoProductDir() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = this.getTestImportFiles(); File ruleDir = mock(File.class); File[] rulesFiles = createMockJsFile(mockJsPath); when(ruleDir.listFiles()).thenReturn(rulesFiles); File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix"); doThrow(new RuntimeException("Done with the test")) .when(this.mockRulesImporter) .importObject(any(Reader.class)); importFiles.put(ImportFile.META.fileName(), actualmeta); importFiles.put(ImportFile.RULES_FILE.fileName(), rulesFiles[0]); importFiles.put(ImportFile.PRODUCTS.fileName(), null); importFiles.put(ImportFile.ENTITLEMENTS.fileName(), null); importFiles.put(ImportFile.UPSTREAM_CONSUMER.fileName(), mock(File.class)); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(RuntimeException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = "Done with the test"; assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testReturnsSubscriptionsFromManifest() throws IOException, ImporterException { Owner owner = new Owner("admin", "Admin Owner"); ConsumerType stype = new ConsumerType(ConsumerTypeEnum.SYSTEM); stype.setId("test-ctype"); this.mockConsumerType(stype); Refresher mockRefresher = mock(Refresher.class); doReturn(mockRefresher) .when(this.mockPoolManager) .getRefresher(any(SubscriptionServiceAdapter.class)); Map<String, File> importFiles = new HashMap<>(); File ruleDir = mock(File.class); File[] rulesFiles = createMockJsFile(mockJsPath); when(ruleDir.listFiles()).thenReturn(rulesFiles); File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix"); importFiles.put(ImportFile.META.fileName(), actualmeta); ConsumerDTO consumerDTO = new ConsumerDTO(); consumerDTO.setUuid("eb5e04bf-be27-44cf-abe3-0c0b1edd523e"); consumerDTO.setName("mymachine"); ConsumerTypeDTO typeDTO = new ConsumerTypeDTO(); typeDTO.setLabel("candlepin"); typeDTO.setManifest(true); consumerDTO.setType(typeDTO); consumerDTO.setUrlWeb("foo.example.com/subscription"); consumerDTO.setUrlApi("/candlepin"); consumerDTO.setContentAccessMode(""); OwnerDTO ownerDTO = new OwnerDTO(); ownerDTO.setKey("admin"); ownerDTO.setDisplayName("Admin Owner"); consumerDTO.setOwner(ownerDTO); ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.CANDLEPIN); ctype.setId("test-ctype"); this.mockConsumerType(ctype); File consumerFile = new File(this.tmpFolder, "consumer.json"); this.mapper.writeValue(consumerFile, consumerDTO); importFiles.put(ImportFile.CONSUMER.fileName(), consumerFile); File cTypes = mock(File.class); when(cTypes.listFiles()).thenReturn(new File[]{}); importFiles.put(ImportFile.CONSUMER_TYPE.fileName(), cTypes); Product prod = new Product("prodId", "prodTest", null); prod.setDependentProductIds(null); File prodFile = new File(this.tmpFolder, "product.json"); this.mapper.writeValue(prodFile, prod); File products = mock(File.class); when(products.listFiles()).thenReturn(new File[]{prodFile}); importFiles.put(ImportFile.PRODUCTS.fileName(), products); Entitlement ent = new Entitlement(); Pool pool = new Pool(); pool.setProduct(prod); ent.setPool(pool); ent.setQuantity(2); File entFile = new File(this.tmpFolder, "entitlement.json"); this.mapper.writeValue(entFile, ent); File entitlements = mock(File.class); when(entitlements.listFiles()).thenReturn(new File[]{entFile}); importFiles.put(ImportFile.ENTITLEMENTS.fileName(), entitlements); importFiles.put(ImportFile.RULES_FILE.fileName(), rulesFiles[0]); ConflictOverrides co = mock(ConflictOverrides.class); Importer importer = this.buildImporter(); List<SubscriptionDTO> subscriptions = importer.importObjects(owner, importFiles, co); assertEquals(1, subscriptions.size()); assertEquals("prodId", subscriptions.get(0).getProduct().getId()); assertEquals(2, subscriptions.get(0).getQuantity().longValue()); }
@Test public void testImportProductNoEntitlementDir() throws IOException, ImporterException { Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = this.getTestImportFiles(); importFiles.put(ImportFile.ENTITLEMENTS.fileName(), null); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(ImporterException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = i18n.tr("The archive does not contain the required entitlements directory"); assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }
@Test public void testImportNoDistributorVersions() throws IOException, ImporterException { doNothing() .when(this.mockRulesImporter) .importObject(any(Reader.class)); doThrow(new RuntimeException("Done with the test")) .when(this.mockExporterMetadataCurator) .getByTypeAndOwner(any(String.class), any(Owner.class)); OwnerCurator oc = mock(OwnerCurator.class); Owner owner = mock(Owner.class); ConflictOverrides co = mock(ConflictOverrides.class); Map<String, File> importFiles = createAndSetImportFiles(); Importer importer = this.buildImporter(); Throwable throwable = assertThrows(RuntimeException.class, () -> importer.importObjects(owner, importFiles, co)); String errmsg = "Done with the test"; assertThat(throwable.getMessage(), StringContains.containsString(errmsg)); }