use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.
the class RefresherTest method testPoolOnlyExaminedOnceTwoProducts.
@Test
public void testPoolOnlyExaminedOnceTwoProducts() {
Product product = mock(Product.class);
Product product2 = mock(Product.class);
ProductData productData = mock(ProductData.class);
ProductData productData2 = mock(ProductData.class);
when(product.getUuid()).thenReturn("product id");
when(product2.getUuid()).thenReturn("product id 2");
when(product.toDTO()).thenReturn(productData);
when(product2.toDTO()).thenReturn(productData2);
Pool pool = new Pool();
pool.setSourceSubscription(new SourceSubscription("subId", "master"));
Subscription subscription = new Subscription();
subscription.setId("subId");
Owner owner = TestUtil.createOwner();
subscription.setOwner(owner);
List<Pool> pools = new ArrayList<>();
pools.add(pool);
List<Subscription> subscriptions = new ArrayList<>();
subscriptions.add(subscription);
when(subAdapter.getSubscriptions(eq(productData))).thenReturn(subscriptions);
when(subAdapter.getSubscriptions(eq(productData2))).thenReturn(subscriptions);
when(subAdapter.getSubscription("subId")).thenReturn(subscription);
when(poolManager.lookupBySubscriptionId(owner, "subId")).thenReturn(pools);
Pool mainPool = TestUtil.copyFromSub(subscription);
when(poolManager.convertToMasterPool(subscription)).thenReturn(mainPool);
refresher.add(product);
refresher.add(product2);
refresher.run();
verify(poolManager, times(1)).refreshPoolsForMasterPool(eq(mainPool), eq(true), eq(false), any(Map.class));
}
use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.
the class PoolManagerFunctionalTest method init.
@Before
@Override
public void init() throws Exception {
super.init();
o = createOwner();
ownerCurator.create(o);
this.ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
virtHost = TestUtil.createProduct(PRODUCT_VIRT_HOST, PRODUCT_VIRT_HOST);
virtHostPlatform = TestUtil.createProduct(PRODUCT_VIRT_HOST_PLATFORM, PRODUCT_VIRT_HOST_PLATFORM);
virtGuest = TestUtil.createProduct(PRODUCT_VIRT_GUEST, PRODUCT_VIRT_GUEST);
monitoring = TestUtil.createProduct(PRODUCT_MONITORING, PRODUCT_MONITORING);
monitoring.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
provisioning = TestUtil.createProduct(PRODUCT_PROVISIONING, PRODUCT_PROVISIONING);
provisioning.setAttribute(Pool.Attributes.MULTI_ENTITLEMENT, "yes");
provisioning.setMultiplier(2L);
provisioning.setAttribute(Product.Attributes.INSTANCE_MULTIPLIER, "4");
virtHost.setAttribute(PRODUCT_VIRT_HOST, "");
virtHostPlatform.setAttribute(PRODUCT_VIRT_HOST_PLATFORM, "");
virtGuest.setAttribute(PRODUCT_VIRT_GUEST, "");
monitoring.setAttribute(PRODUCT_MONITORING, "");
provisioning.setAttribute(PRODUCT_PROVISIONING, "");
socketLimitedProduct = TestUtil.createProduct("socket-limited-prod", "Socket Limited Product");
socketLimitedProduct.setAttribute(Product.Attributes.SOCKETS, "2");
productCurator.create(socketLimitedProduct);
productCurator.create(virtHost);
productCurator.create(virtHostPlatform);
productCurator.create(virtGuest);
productCurator.create(monitoring);
productCurator.create(provisioning);
List<Subscription> subscriptions = new LinkedList<>();
ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
Subscription sub1 = TestUtil.createSubscription(o, virtHost, new HashSet<>());
sub1.setId(Util.generateDbUUID());
sub1.setQuantity(5L);
sub1.setStartDate(new Date());
sub1.setEndDate(TestUtil.createDate(3020, 12, 12));
sub1.setModified(new Date());
Subscription sub2 = TestUtil.createSubscription(o, virtHostPlatform, new HashSet<>());
sub2.setId(Util.generateDbUUID());
sub2.setQuantity(5L);
sub2.setStartDate(new Date());
sub2.setEndDate(TestUtil.createDate(3020, 12, 12));
sub2.setModified(new Date());
Subscription sub3 = TestUtil.createSubscription(o, monitoring, new HashSet<>());
sub3.setId(Util.generateDbUUID());
sub3.setQuantity(5L);
sub3.setStartDate(new Date());
sub3.setEndDate(TestUtil.createDate(3020, 12, 12));
sub3.setModified(new Date());
sub4 = TestUtil.createSubscription(o, provisioning, new HashSet<>());
sub4.setId(Util.generateDbUUID());
sub4.setQuantity(5L);
sub4.setStartDate(new Date());
sub4.setEndDate(TestUtil.createDate(3020, 12, 12));
sub4.setModified(new Date());
sub4.getBranding().add(new Branding("product1", "type1", "branding1"));
sub4.getBranding().add(new Branding("product2", "type2", "branding2"));
subscriptions.add(sub1);
subscriptions.add(sub2);
subscriptions.add(sub3);
subscriptions.add(sub4);
poolManager.getRefresher(subAdapter, ownerAdapter).add(o).run();
this.systemType = new ConsumerType(ConsumerTypeEnum.SYSTEM);
consumerTypeCurator.create(systemType);
parentSystem = new Consumer("system", "user", o, systemType);
parentSystem.getFacts().put("total_guests", "0");
consumerCurator.create(parentSystem);
childVirtSystem = new Consumer("virt system", "user", o, systemType);
consumerCurator.create(childVirtSystem);
}
use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.
the class HostedTestSubscriptionServiceAdapter method getSubscriptionIds.
@Override
public List<String> getSubscriptionIds(Owner owner) {
List<String> ids = new ArrayList<>();
List<Subscription> subscriptions = ownerMap.get(owner.getKey());
if (subscriptions != null) {
for (Subscription subscription : subscriptions) {
ids.add(subscription.getId());
}
}
return ids;
}
use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.
the class EntitlementImporter method importObject.
public Subscription importObject(ObjectMapper mapper, Reader reader, Owner owner, Map<String, ProductDTO> productsById, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
EntitlementDTO entitlementDTO = mapper.readValue(reader, EntitlementDTO.class);
Entitlement entitlement = new Entitlement();
populateEntity(entitlement, entitlementDTO);
Subscription subscription = new Subscription();
log.debug("Building subscription for owner: {}", owner);
log.debug("Using pool from entitlement: {}", entitlement.getPool());
// Now that we no longer store Subscriptions in the on-site database, we need to
// manually give the subscription a downstream ID. Note that this may later be
// overwritten by reconciliation code if it determines this Subscription
// should replace and existing one.
subscription.setId(Util.generateDbUUID());
subscription.setUpstreamPoolId(entitlement.getPool().getId());
subscription.setUpstreamEntitlementId(entitlement.getId());
subscription.setUpstreamConsumerId(consumerUuid);
subscription.setOwner(owner);
subscription.setStartDate(entitlement.getStartDate());
subscription.setEndDate(entitlement.getEndDate());
subscription.setAccountNumber(entitlement.getPool().getAccountNumber());
subscription.setContractNumber(entitlement.getPool().getContractNumber());
subscription.setOrderNumber(entitlement.getPool().getOrderNumber());
subscription.setQuantity(entitlement.getQuantity().longValue());
for (Branding b : entitlement.getPool().getBranding()) {
subscription.getBranding().add(new Branding(b.getProductId(), b.getType(), b.getName()));
}
String cdnLabel = meta.getCdnLabel();
if (!StringUtils.isBlank(cdnLabel)) {
Cdn cdn = cdnCurator.lookupByLabel(cdnLabel);
if (cdn != null) {
subscription.setCdn(cdn);
}
}
ProductDTO productDTO = this.findProduct(productsById, entitlement.getPool().getProductId());
subscription.setProduct(this.translator.translate(productDTO, ProductData.class));
// Add any sub product data to the subscription.
if (entitlement.getPool().getDerivedProductId() != null) {
productDTO = this.findProduct(productsById, entitlement.getPool().getDerivedProductId());
subscription.setDerivedProduct(this.translator.translate(productDTO, ProductData.class));
}
associateProvidedProducts(productsById, entitlement, subscription);
Set<EntitlementCertificate> certs = entitlement.getCertificates();
// subscriptions have one cert
int entcnt = 0;
for (EntitlementCertificate cert : certs) {
++entcnt;
CertificateSerial cs = new CertificateSerial();
cs.setCollected(cert.getSerial().isCollected());
cs.setExpiration(cert.getSerial().getExpiration());
cs.setUpdated(cert.getSerial().getUpdated());
cs.setCreated(cert.getSerial().getCreated());
SubscriptionsCertificate sc = new SubscriptionsCertificate();
sc.setKey(cert.getKey());
sc.setCertAsBytes(cert.getCertAsBytes());
sc.setSerial(cs);
subscription.setCertificate(sc);
}
if (entcnt > 1) {
log.error("More than one entitlement cert found for subscription");
}
return subscription;
}
use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.
the class Importer method doExport.
private ImportRecord doExport(Owner owner, File exportDir, ConflictOverrides overrides, String uploadedFileName) throws ImporterException {
Map<String, Object> result = new HashMap<>();
try {
File signature = new File(exportDir, "signature");
if (signature.length() == 0) {
throw new ImportExtractionException(i18n.tr("The archive does not contain the required signature file"));
}
boolean verifiedSignature = pki.verifySHA256WithRSAHashAgainstCACerts(new File(exportDir, "consumer_export.zip"), loadSignature(new File(exportDir, "signature")));
if (!verifiedSignature) {
log.warn("Archive signature check failed.");
if (!overrides.isForced(Conflict.SIGNATURE_CONFLICT)) {
/*
* Normally for import conflicts that can be overridden, we try to
* report them all the first time so if the user intends to override,
* they can do so with just one more request. However in the case of
* a bad signature, we're going to report immediately due to the nature
* of what this might mean.
*/
throw new ImportConflictException(i18n.tr("Archive failed signature check"), Conflict.SIGNATURE_CONFLICT);
} else {
log.warn("Ignoring signature check failure.");
}
}
File consumerExport = new File(exportDir, "consumer_export.zip");
File consumerExportDir = extractArchive(exportDir, consumerExport.getName(), new FileInputStream(consumerExport));
Map<String, File> importFiles = new HashMap<>();
File[] listFiles = consumerExportDir.listFiles();
if (listFiles == null || listFiles.length == 0) {
throw new ImportExtractionException(i18n.tr("The consumer_export archive has no contents"));
}
for (File file : listFiles) {
importFiles.put(file.getName(), file);
}
// Need the rules file as well which is in a nested dir:
File rulesFile = new File(consumerExportDir, ImportFile.RULES_FILE.fileName());
importFiles.put(ImportFile.RULES_FILE.fileName(), rulesFile);
List<Subscription> importSubs = importObjects(owner, importFiles, overrides);
Meta m = mapper.readValue(importFiles.get(ImportFile.META.fileName()), Meta.class);
result.put("subscriptions", importSubs);
result.put("meta", m);
sink.emitImportCreated(owner);
return recordImportSuccess(owner, result, overrides, uploadedFileName);
} catch (FileNotFoundException fnfe) {
log.error("Archive file does not contain consumer_export.zip", fnfe);
throw new ImportExtractionException(i18n.tr("The archive does not contain " + "the required consumer_export.zip file"));
} catch (ConstraintViolationException cve) {
log.error("Failed to import archive", cve);
throw new ImporterException(i18n.tr("Failed to import archive"), cve, result);
} catch (PersistenceException pe) {
log.error("Failed to import archive", pe);
throw new ImporterException(i18n.tr("Failed to import archive"), pe, result);
} catch (IOException e) {
log.error("Exception caught importing archive", e);
throw new ImportExtractionException(i18n.tr("Unable to extract export archive"), e, result);
} catch (CertificateException e) {
log.error("Certificate exception checking archive signature", e);
throw new ImportExtractionException(i18n.tr("Certificate exception checking archive signature"), e, result);
} finally {
if (exportDir != null) {
try {
FileUtils.deleteDirectory(exportDir);
} catch (IOException e) {
log.error("Failed to delete extracted export", e);
}
}
}
}
Aggregations