Search in sources :

Example 21 with ProductData

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);
}
Also used : ProductData(org.candlepin.model.dto.ProductData) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ContentData(org.candlepin.model.dto.ContentData) ProductContentData(org.candlepin.model.dto.ProductContentData) Content(org.candlepin.model.Content) ProductContentData(org.candlepin.model.dto.ProductContentData)

Example 22 with ProductData

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;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) SourceSubscription(org.candlepin.model.SourceSubscription) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Branding(org.candlepin.model.Branding) HashSet(java.util.HashSet)

Example 23 with ProductData

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());
}
Also used : ProductData(org.candlepin.model.dto.ProductData) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Date(java.util.Date) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with ProductData

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));
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Map(java.util.Map) Test(org.junit.Test)

Example 25 with ProductData

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));
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Product(org.candlepin.model.Product) Test(org.junit.Test)

Aggregations

ProductData (org.candlepin.model.dto.ProductData)30 Product (org.candlepin.model.Product)22 Pool (org.candlepin.model.Pool)13 Owner (org.candlepin.model.Owner)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 List (java.util.List)10 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)10 HashSet (java.util.HashSet)9 HashMap (java.util.HashMap)8 SourceSubscription (org.candlepin.model.SourceSubscription)8 Subscription (org.candlepin.model.dto.Subscription)8 Date (java.util.Date)7 Consumer (org.candlepin.model.Consumer)7 LinkedList (java.util.LinkedList)5 Content (org.candlepin.model.Content)5 ProductContentData (org.candlepin.model.dto.ProductContentData)5 AutobindData (org.candlepin.resource.dto.AutobindData)5 Map (java.util.Map)4 ContentData (org.candlepin.model.dto.ContentData)4