use of org.candlepin.controller.Refresher in project candlepin by candlepin.
the class ImporterTest method testReturnsSubscriptionsFromManifest.
@Test
public void testReturnsSubscriptionsFromManifest() throws IOException, ImporterException {
Owner owner = new Owner("admin", "Admin Owner");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
when(emc.lookupByTypeAndOwner("per_user", owner)).thenReturn(null);
ConsumerType stype = new ConsumerType(ConsumerTypeEnum.SYSTEM);
stype.setId("test-ctype");
when(consumerTypeCurator.lookupByLabel(eq("system"))).thenReturn(stype);
when(consumerTypeCurator.find(eq(stype.getId()))).thenReturn(stype);
OwnerCurator oc = mock(OwnerCurator.class);
when(oc.lookupWithUpstreamUuid(any(String.class))).thenReturn(null);
PoolManager pm = mock(PoolManager.class);
Refresher refresher = mock(Refresher.class);
when(pm.getRefresher(any(SubscriptionServiceAdapter.class), any(OwnerServiceAdapter.class))).thenReturn(refresher);
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");
when(consumerTypeCurator.lookupByLabel(eq("candlepin"))).thenReturn(ctype);
when(consumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
File consumerFile = new File(folder.getRoot(), "consumer.json");
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(folder.getRoot(), "product.json");
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(folder.getRoot(), "entitlement.json");
mapper.writeValue(entFile, ent);
File entitlements = mock(File.class);
when(entitlements.listFiles()).thenReturn(new File[] { entFile });
importFiles.put(ImportFile.ENTITLEMENTS.fileName(), entitlements);
RulesImporter ri = mock(RulesImporter.class);
importFiles.put(ImportFile.RULES_FILE.fileName(), rulesFiles[0]);
ConflictOverrides co = mock(ConflictOverrides.class);
Importer i = new Importer(consumerTypeCurator, pc, ri, oc, null, null, pm, null, config, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
List<Subscription> subscriptions = i.importObjects(owner, importFiles, co);
assertEquals(1, subscriptions.size());
assertEquals("prodId", subscriptions.get(0).getProduct().getId());
assertEquals(2, subscriptions.get(0).getQuantity().longValue());
}
use of org.candlepin.controller.Refresher in project candlepin by candlepin.
the class Importer method importObjects.
@SuppressWarnings("checkstyle:methodlength")
@Transactional(rollbackOn = { IOException.class, ImporterException.class, RuntimeException.class, ImportConflictException.class })
public // WARNING: Keep this method public, otherwise @Transactional is ignored:
List<Subscription> 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"));
}
// system level elements
/*
* Checking a system wide last import date breaks multi-tenant deployments whenever
* one org imports a manifest slightly older than another org who has already
* imported. Disabled for now. See bz #769644.
*/
// validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, metadata, force);
// If any calls find conflicts we'll assemble them into one exception detailing all
// the conflicts which occurred, so the caller can override them all at once
// if desired:
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());
}
// per user elements
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);
}
// At this point we're done checking for any potential conflicts:
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");
}
// If the consumer has no entitlements, this products directory will end up empty.
// This also implies there will be no entitlements to import.
Meta meta = mapper.readValue(metadata, Meta.class);
List<Subscription> 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);
}
// Setup our import subscription adapter with the subscriptions imported:
final String contentAccessMode = StringUtils.isEmpty(consumer.getContentAccessMode()) ? ContentAccessCertServiceAdapter.DEFAULT_CONTENT_ACCESS_MODE : consumer.getContentAccessMode();
SubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(importSubs);
OwnerServiceAdapter ownerAdapter = new OwnerServiceAdapter() {
@Override
public boolean isOwnerKeyValidForCreation(String ownerKey) {
return true;
}
@Override
public String getContentAccessMode(String ownerKey) {
return contentAccessMode;
}
@Override
public String getContentAccessModeList(String ownerKey) {
return contentAccessMode;
}
};
Refresher refresher = poolManager.getRefresher(subAdapter, ownerAdapter);
refresher.add(owner);
refresher.run();
return importSubs;
}
use of org.candlepin.controller.Refresher in project candlepin by candlepin.
the class RefreshPoolsForProductJob method toExecute.
@Override
public void toExecute(JobExecutionContext context) throws JobExecutionException {
String productUuid = context.getMergedJobDataMap().getString(JobStatus.TARGET_ID);
Boolean lazy = context.getMergedJobDataMap().getBoolean(LAZY_REGEN);
StringBuilder result = new StringBuilder();
Product product = this.productCurator.find(productUuid);
if (product != null) {
Refresher refresher = poolManager.getRefresher(this.subAdapter, this.ownerAdapter, lazy);
refresher.add(product);
refresher.run();
result.append("Pools refreshed for product: ").append(productUuid).append("\n");
} else {
result.append("Unable to refresh pools for product \"").append(productUuid).append("\": Could not find a product with the specified UUID");
}
context.setResult(result.toString());
}
Aggregations