use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.
the class Entitler method getDevProductMap.
/**
* Looks up all Products matching the specified SKU and the consumer's
* installed products.
*
* @param consumer the consumer to pull the installed product id list from.
* @param sku the product id of the SKU.
* @return a {@link DeveloperProducts} object that contains the Product objects
* from the adapter.
*/
private DeveloperProducts getDevProductMap(Consumer consumer, Owner owner, String sku) {
List<String> devProductIds = new ArrayList<>();
devProductIds.add(sku);
for (ConsumerInstalledProduct ip : consumer.getInstalledProducts()) {
devProductIds.add(ip.getProductId());
}
Map<String, ProductData> productMap = new HashMap<>();
Map<String, ContentData> contentMap = new HashMap<>();
log.debug("Importing products for dev pool resolution...");
for (ProductData product : this.productAdapter.getProductsByIds(owner, devProductIds)) {
if (product == null) {
continue;
}
if (sku.equals(product.getId()) && StringUtils.isEmpty(product.getAttributeValue(Product.Attributes.SUPPORT_LEVEL))) {
// if there is no SLA, apply the default
product.setAttribute(Product.Attributes.SUPPORT_LEVEL, this.DEFAULT_DEV_SLA);
}
// Product is coming from an upstream source; lock it so only upstream can make
// further changes to it.
product.setLocked(true);
ProductData existingProduct = productMap.get(product.getId());
if (existingProduct != null && !existingProduct.equals(product)) {
log.warn("Multiple versions of the same product received during dev pool resolution; " + "discarding duplicate: {} => {}, {}", product.getId(), existingProduct, product);
} else {
productMap.put(product.getId(), product);
Collection<ProductContentData> pcdCollection = product.getProductContent();
if (pcdCollection != null) {
for (ProductContentData pcd : pcdCollection) {
if (pcd == null) {
log.error("product contains a null product-content mapping: {}", product);
throw new IllegalStateException("product contains a null product-content mapping: " + product);
}
ContentData content = pcd.getContent();
// population validation for us.
if (content == null || content.getId() == null) {
log.error("product contains a null or incomplete product-content mapping: {}", product);
throw new IllegalStateException("product contains a null or incomplete " + "product-content mapping: " + product);
}
// We need to lock the incoming content here, but doing so will affect
// the equality comparison for products. We'll correct them later.
ContentData existingContent = contentMap.get(content.getId());
if (existingContent != null && !existingContent.equals(content)) {
log.warn("Multiple versions of the same content received during dev pool " + "resolution; discarding duplicate: {} => {}, {}", content.getId(), existingContent, content);
} else {
contentMap.put(content.getId(), content);
}
}
}
}
}
log.debug("Importing {} content...", contentMap.size());
for (ContentData cdata : contentMap.values()) {
cdata.setLocked(true);
}
Map<String, Content> importedContent = this.contentManager.importContent(owner, contentMap, productMap.keySet()).getImportedEntities();
log.debug("Importing {} product(s)...", productMap.size());
Map<String, Product> importedProducts = this.productManager.importProducts(owner, productMap, importedContent).getImportedEntities();
log.debug("Resolved {} dev product(s) for sku: {}", productMap.size(), sku);
return new DeveloperProducts(sku, importedProducts);
}
use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.
the class CandlepinPoolManager method convertToMasterPoolImpl.
/**
* Builds a pool instance from the given subscription, using the specified owner and products
* for resolution.
* <p></p>
* The provided owner and products will be used to match and resolve the owner and product
* DTOs present on the subscription. If the subscription uses DTOs which cannot be resolved,
* this method will throw an exception.
*
* @param sub
* The subscription to convert to a pool
*
* @param owner
* The owner the pool will be assigned to
*/
private Pool convertToMasterPoolImpl(Subscription sub, Owner owner, Map<String, Product> productMap) {
if (sub == null) {
throw new IllegalArgumentException("subscription is null");
}
if (owner == null || (owner.getKey() == null && owner.getId() == null)) {
throw new IllegalArgumentException("owner is null or incomplete");
}
if (productMap == null) {
throw new IllegalArgumentException("productMap is null");
}
Pool pool = new Pool();
// Validate and resolve owner...
if (sub.getOwner() == null || (sub.getOwner().getId() != null ? !owner.getId().equals(sub.getOwner().getId()) : !owner.getKey().equals(sub.getOwner().getKey()))) {
throw new IllegalStateException("Subscription references an invalid owner: " + sub.getOwner());
}
pool.setOwner(owner);
pool.setQuantity(sub.getQuantity());
pool.setStartDate(sub.getStartDate());
pool.setEndDate(sub.getEndDate());
pool.setContractNumber(sub.getContractNumber());
pool.setAccountNumber(sub.getAccountNumber());
pool.setOrderNumber(sub.getOrderNumber());
// Copy over subscription details
pool.setSourceSubscription(new SourceSubscription(sub.getId(), "master"));
// Copy over upstream details
pool.setUpstreamPoolId(sub.getUpstreamPoolId());
pool.setUpstreamEntitlementId(sub.getUpstreamEntitlementId());
pool.setUpstreamConsumerId(sub.getUpstreamConsumerId());
pool.setCdn(sub.getCdn());
pool.setCertificate(sub.getCertificate());
// Add in branding
if (sub.getBranding() != null) {
Set<Branding> branding = new HashSet<>();
for (Branding brand : sub.getBranding()) {
// Impl note:
// We create a new instance here since we don't have a separate branding DTO (yet),
// and we need to be certain that we don't try to move or change a branding object
// associated with another pool.
branding.add(new Branding(brand.getProductId(), brand.getType(), brand.getName()));
}
pool.setBranding(branding);
}
if (sub.getProduct() == null || sub.getProduct().getId() == null) {
throw new IllegalStateException("Subscription has no product, or its product is incomplete: " + sub.getProduct());
}
Product product = productMap.get(sub.getProduct().getId());
if (product == null) {
throw new IllegalStateException("Subscription references a product which cannot be resolved: " + sub.getProduct());
}
pool.setProduct(product);
if (sub.getDerivedProduct() != null) {
product = productMap.get(sub.getDerivedProduct().getId());
if (product == null) {
throw new IllegalStateException("Subscription's derived product references a product which " + "cannot be resolved: " + sub.getDerivedProduct());
}
pool.setDerivedProduct(product);
}
if (sub.getProvidedProducts() != null) {
Set<Product> products = new HashSet<>();
for (ProductData pdata : sub.getProvidedProducts()) {
if (pdata != null) {
product = productMap.get(pdata.getId());
if (product == null) {
throw new IllegalStateException("Subscription's provided products references a " + "product which cannot be resolved: " + pdata);
}
products.add(product);
}
}
pool.setProvidedProducts(products);
}
if (sub.getDerivedProvidedProducts() != null) {
Set<Product> products = new HashSet<>();
for (ProductData pdata : sub.getDerivedProvidedProducts()) {
if (pdata != null) {
product = productMap.get(pdata.getId());
if (product == null) {
throw new IllegalStateException("Subscription's derived provided products " + "references a product which cannot be resolved: " + pdata);
}
products.add(product);
}
}
pool.setDerivedProvidedProducts(products);
}
return pool;
}
use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.
the class PoolManagerTest method testFabricateSubscriptionFromPool.
@Test
public void testFabricateSubscriptionFromPool() {
Product product = TestUtil.createProduct("product", "Product");
Product provided1 = TestUtil.createProduct("provided-1", "Provided 1");
Product provided2 = TestUtil.createProduct("provided-2", "Provided 2");
product.setLocked(true);
provided1.setLocked(true);
provided2.setLocked(true);
ProductData productDTO = product.toDTO();
ProductData provided1DTO = provided1.toDTO();
ProductData provided2DTO = provided2.toDTO();
Pool pool = mock(Pool.class);
HashSet<Product> provided = new HashSet<>();
HashSet<ProductData> providedDTOs = new HashSet<>();
provided.add(provided1);
provided.add(provided2);
providedDTOs.add(provided1DTO);
providedDTOs.add(provided2DTO);
Long quantity = new Long(42);
Date startDate = new Date(System.currentTimeMillis() - 86400000);
Date endDate = new Date(System.currentTimeMillis() + 86400000);
Date updated = new Date();
String subscriptionId = "test-subscription-1";
when(pool.getOwner()).thenReturn(owner);
when(pool.getProduct()).thenReturn(product);
when(mockProductCurator.getPoolProvidedProductsCached(any(Pool.class))).thenReturn(provided);
when(pool.getQuantity()).thenReturn(quantity);
when(pool.getStartDate()).thenReturn(startDate);
when(pool.getEndDate()).thenReturn(endDate);
when(pool.getUpdated()).thenReturn(updated);
when(pool.getSubscriptionId()).thenReturn(subscriptionId);
// TODO: Add other attributes to check here.
Subscription fabricated = manager.fabricateSubscriptionFromPool(pool);
pool.populateAllTransientProvidedProducts(mockProductCurator);
assertEquals(owner, fabricated.getOwner());
assertEquals(productDTO, fabricated.getProduct());
assertEquals(providedDTOs, fabricated.getProvidedProducts());
assertEquals(quantity, fabricated.getQuantity());
assertEquals(startDate, fabricated.getStartDate());
assertEquals(endDate, fabricated.getEndDate());
assertEquals(updated, fabricated.getModified());
assertEquals(subscriptionId, fabricated.getId());
}
use of org.candlepin.model.dto.ProductData 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.ProductData in project candlepin by candlepin.
the class RefresherTest method testProductOnlyExaminedOnce.
@Test
public void testProductOnlyExaminedOnce() {
Product product = mock(Product.class);
ProductData productData = mock(ProductData.class);
when(product.toDTO()).thenReturn(productData);
refresher.add(product);
refresher.add(product);
refresher.run();
verify(subAdapter, times(1)).getSubscriptions(eq(productData));
}
Aggregations