Search in sources :

Example 26 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class HostedTestSubscriptionServiceAdapter method createSubscription.

@Override
public Subscription createSubscription(Subscription s) {
    idMap.put(s.getId(), s);
    if (!ownerMap.containsKey(s.getOwner().getKey())) {
        ownerMap.put(s.getOwner().getKey(), new ArrayList<>());
    }
    ownerMap.get(s.getOwner().getKey()).add(s);
    if (!productMap.containsKey(s.getProduct().getId())) {
        productMap.put(s.getProduct().getId(), new ArrayList<>());
    }
    this.clearUuids(s.getProduct());
    this.clearUuids(s.getDerivedProduct());
    if (CollectionUtils.isNotEmpty(s.getProvidedProducts())) {
        for (ProductData pdata : s.getProvidedProducts()) {
            this.clearUuids(pdata);
        }
    }
    if (CollectionUtils.isNotEmpty(s.getDerivedProvidedProducts())) {
        for (ProductData pdata : s.getDerivedProvidedProducts()) {
            this.clearUuids(pdata);
        }
    }
    productMap.get(s.getProduct().getId()).add(s);
    return s;
}
Also used : ProductData(org.candlepin.model.dto.ProductData)

Example 27 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class EntitlerTest method testDevPoolCreationAtBindNoFailMissingInstalledProduct.

@Test
public void testDevPoolCreationAtBindNoFailMissingInstalledProduct() throws Exception {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<>();
    Product p = TestUtil.createProduct("test-product", "Test Product");
    Product ip1 = TestUtil.createProduct("test-product-installed-1", "Installed Test Product 1");
    Product ip2 = TestUtil.createProduct("test-product-installed-2", "Installed Test Product 2");
    devProdDTOs.add(p.toDTO());
    devProdDTOs.add(ip1.toDTO());
    Pool activePool = TestUtil.createPool(owner, p);
    List<Pool> activeList = new ArrayList<>();
    activeList.add(activePool);
    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip1));
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip2));
    when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false);
    when(poolCurator.hasActiveEntitlementPools(eq(owner.getId()), any(Date.class))).thenReturn(true);
    when(productAdapter.getProductsByIds(any(Owner.class), any(List.class))).thenReturn(devProdDTOs);
    this.mockProducts(owner, p, ip1, ip2);
    this.mockProductImport(owner, p, ip1, ip2);
    this.mockContentImport(owner, Collections.<String, Content>emptyMap());
    Pool expectedPool = entitler.assembleDevPool(devSystem, owner, p.getId());
    when(pm.createPool(any(Pool.class))).thenReturn(expectedPool);
    AutobindData ad = new AutobindData(devSystem, owner);
    entitler.bindByProducts(ad);
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) Consumer(org.candlepin.model.Consumer) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) List(java.util.List) ArrayList(java.util.ArrayList) AutobindData(org.candlepin.resource.dto.AutobindData) Date(java.util.Date) Test(org.junit.Test)

Example 28 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class EntitlerTest method testDevPoolCreationAtBindFailNoSkuProduct.

@Test
public void testDevPoolCreationAtBindFailNoSkuProduct() throws Exception {
    Owner owner = TestUtil.createOwner("o");
    List<ProductData> devProdDTOs = new ArrayList<>();
    Product p = TestUtil.createProduct("test-product", "Test Product");
    Product ip = TestUtil.createProduct("test-product-installed", "Installed Test Product");
    devProdDTOs.add(ip.toDTO());
    Pool activePool = TestUtil.createPool(owner, p);
    List<Pool> activeList = new ArrayList<>();
    activeList.add(activePool);
    Consumer devSystem = TestUtil.createConsumer(owner);
    devSystem.setFact("dev_sku", p.getId());
    devSystem.addInstalledProduct(new ConsumerInstalledProduct(ip));
    when(config.getBoolean(eq(ConfigProperties.STANDALONE))).thenReturn(false);
    when(poolCurator.hasActiveEntitlementPools(eq(owner.getId()), any(Date.class))).thenReturn(true);
    when(productAdapter.getProductsByIds(any(Owner.class), any(List.class))).thenReturn(devProdDTOs);
    when(ownerProductCurator.getProductById(eq(owner), eq(p.getId()))).thenReturn(p);
    when(ownerProductCurator.getProductById(eq(owner), eq(ip.getId()))).thenReturn(ip);
    mockUpdateProduct(p, owner);
    mockUpdateProduct(ip, owner);
    mockProductImport(owner, p, ip);
    mockContentImport(owner, new Content[] {});
    AutobindData ad = new AutobindData(devSystem, owner);
    try {
        entitler.bindByProducts(ad);
    } catch (ForbiddenException fe) {
        assertEquals(i18n.tr("SKU product not available to this development unit: \"{0}\"", p.getId()), fe.getMessage());
    }
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Pool(org.candlepin.model.Pool) List(java.util.List) ArrayList(java.util.ArrayList) AutobindData(org.candlepin.resource.dto.AutobindData) Test(org.junit.Test)

Example 29 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class ProductManager method importProducts.

/**
 * Creates or updates products from the given products DTOs, using the provided content for
 * content lookup and resolution.
 * <p></p>
 * The product DTOs provided in the given map should be mapped by the product's Red Hat ID. If
 * the mappings are incorrect or inconsistent, the result of this method is undefined.
 *
 * @param owner
 *  The owner for which to import the given product
 *
 * @param productData
 *  A mapping of Red Hat product ID to product DTOs to import
 *
 * @param importedContent
 *  A mapping of Red Hat content ID to content instances to use to lookup and resolve content
 *  references on the provided product DTOs.
 *
 * @return
 *  A mapping of Red Hat content ID to content entities representing the imported content
 */
@Transactional
@Traceable
public ImportResult<Product> importProducts(@TraceableParam("owner") Owner owner, Map<String, ProductData> productData, Map<String, Content> importedContent) {
    if (owner == null) {
        throw new IllegalArgumentException("owner is null");
    }
    ImportResult<Product> importResult = new ImportResult<>();
    if (productData == null || productData.isEmpty()) {
        // Nothing to import
        return importResult;
    }
    Map<String, Product> skippedProducts = importResult.getSkippedEntities();
    Map<String, Product> createdProducts = importResult.getCreatedEntities();
    Map<String, Product> updatedProducts = importResult.getUpdatedEntities();
    Map<String, Integer> productVersions = new HashMap<>();
    Map<String, Product> sourceProducts = new HashMap<>();
    Map<String, List<Product>> existingVersions = new HashMap<>();
    List<OwnerProduct> ownerProductBuffer = new LinkedList<>();
    // - Divide imported products into sets of updates and creates
    log.debug("Fetching existing products for update...");
    for (Product product : this.ownerProductCurator.getProductsByIds(owner, productData.keySet())) {
        ProductData update = productData.get(product.getId());
        if (!this.isChangedBy(product, update)) {
            // This product won't be changing, so we'll just pretend it's not being imported at all
            skippedProducts.put(product.getId(), product);
            continue;
        }
        sourceProducts.put(product.getId(), product);
        product = this.applyProductChanges((Product) product.clone(), update, importedContent);
        updatedProducts.put(product.getId(), product);
        productVersions.put(product.getId(), product.getEntityVersion());
    }
    log.debug("Validating new products...");
    for (ProductData update : productData.values()) {
        if (!skippedProducts.containsKey(update.getId()) && !updatedProducts.containsKey(update.getId())) {
            // Ensure the product is minimally populated
            if (update.getId() == null || update.getName() == null) {
                throw new IllegalStateException("Product data is incomplete: " + update);
            }
            Product product = new Product(update.getId(), update.getName());
            // TODO: Remove this shim and stop using DTOs in this class
            product = this.applyProductChanges(product, update, importedContent);
            createdProducts.put(product.getId(), product);
            productVersions.put(product.getId(), product.getEntityVersion());
        }
    }
    log.debug("Checking for existing product versions...");
    for (Product alt : this.ownerProductCurator.getProductsByVersions(owner, productVersions)) {
        List<Product> alternates = existingVersions.get(alt.getId());
        if (alternates == null) {
            alternates = new LinkedList<>();
            existingVersions.put(alt.getId(), alternates);
        }
        alternates.add(alt);
    }
    productVersions.clear();
    productVersions = null;
    // We're about to start modifying the maps, so we need to clone the created set before we
    // start adding the update forks to it.
    Map<String, Product> stagedEntities = new HashMap<>(createdProducts);
    // Process the created group...
    // Check our created set for existing versions:
    // - If there's an existing version, we'll remove the staged entity from the creation
    // set, and stage an owner-product mapping for the existing version
    // - Otherwise, we'll stage the new entity for persistence by leaving it in the created
    // set, and stage an owner-product mapping to the new entity
    Iterator<Product> iterator = stagedEntities.values().iterator();
    createdProductLoop: while (iterator.hasNext()) {
        Product created = iterator.next();
        List<Product> alternates = existingVersions.get(created.getId());
        if (alternates != null) {
            for (Product alt : alternates) {
                if (created.equals(alt)) {
                    ownerProductBuffer.add(new OwnerProduct(owner, alt));
                    createdProducts.put(alt.getId(), alt);
                    iterator.remove();
                    continue createdProductLoop;
                }
            }
        }
        ownerProductBuffer.add(new OwnerProduct(owner, created));
    }
    // - Otherwise, we need to stage the updated entity for persistence
    updatedProductLoop: for (Map.Entry<String, Product> entry : updatedProducts.entrySet()) {
        Product updated = entry.getValue();
        List<Product> alternates = existingVersions.get(updated.getId());
        if (alternates != null) {
            for (Product alt : alternates) {
                if (updated.equals(alt)) {
                    updated = alt;
                    entry.setValue(alt);
                    continue updatedProductLoop;
                }
            }
        }
        // We need to stage the updated entity for persistence. We'll reuse the now-empty
        // createdProducts map for this.
        updated.setUuid(null);
        stagedEntities.put(updated.getId(), updated);
    }
    // Persist our staged entities
    // We probably don't want to evict the products yet, as they'll appear as unmanaged if
    // they're used later. However, the join objects can be evicted safely since they're only
    // really used here.
    log.debug("Persisting product changes...");
    this.productCurator.saveAll(stagedEntities.values(), true, false);
    this.ownerProductCurator.saveAll(ownerProductBuffer, true, true);
    // Perform bulk reference update
    Map<String, String> productUuidMap = new HashMap<>();
    for (Product update : updatedProducts.values()) {
        Product source = sourceProducts.get(update.getId());
        productUuidMap.put(source.getUuid(), update.getUuid());
    }
    this.ownerProductCurator.updateOwnerProductReferences(owner, productUuidMap);
    // Return
    return importResult;
}
Also used : ProductData(org.candlepin.model.dto.ProductData) HashMap(java.util.HashMap) OwnerProduct(org.candlepin.model.OwnerProduct) Product(org.candlepin.model.Product) OwnerProduct(org.candlepin.model.OwnerProduct) LinkedList(java.util.LinkedList) LinkedList(java.util.LinkedList) List(java.util.List) Traceable(org.candlepin.util.Traceable) Transactional(com.google.inject.persist.Transactional)

Example 30 with ProductData

use of org.candlepin.model.dto.ProductData in project candlepin by candlepin.

the class PoolRulesTest method assertProvidedProductsForSub.

private void assertProvidedProductsForSub(Set<ProductData> expectedProducts, Set<Product> providedProducts) {
    assertEquals(expectedProducts.size(), providedProducts.size());
    for (ProductData expected : expectedProducts) {
        boolean found = false;
        for (Product provided : providedProducts) {
            if (provided.getId().equals(expected.getId())) {
                found = true;
                break;
            }
        }
        assertTrue(found);
    }
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Product(org.candlepin.model.Product)

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