Search in sources :

Example 11 with Branding

use of org.candlepin.model.Branding in project candlepin by candlepin.

the class OwnerResource method populateEntity.

/**
 * Populates an entity that is to be created with data from the provided DTO.
 *
 * @param entity
 *  The entity instance to populate
 *
 * @param dto
 *  The DTO containing the data with which to populate the entity
 *
 * @throws IllegalArgumentException
 *  if either entity or dto are null
 */
@SuppressWarnings("checkstyle:methodlength")
protected void populateEntity(Pool entity, PoolDTO dto) {
    if (entity == null) {
        throw new IllegalArgumentException("entity is null");
    }
    if (dto == null) {
        throw new IllegalArgumentException("dto is null");
    }
    if (dto.getId() != null) {
        entity.setId(dto.getId());
    }
    if (dto.getStartDate() != null) {
        entity.setStartDate(dto.getStartDate());
    }
    if (dto.getEndDate() != null) {
        entity.setEndDate(dto.getEndDate());
    }
    if (dto.getQuantity() != null) {
        entity.setQuantity(dto.getQuantity());
    }
    if (dto.getAttributes() != null) {
        if (dto.getAttributes().isEmpty()) {
            entity.setAttributes(Collections.emptyMap());
        } else {
            entity.setAttributes(dto.getAttributes());
        }
    }
    if (dto.getProvidedProducts() != null) {
        if (dto.getProvidedProducts().isEmpty()) {
            entity.setProvidedProducts(Collections.emptySet());
        } else {
            Set<Product> products = new HashSet<>();
            for (PoolDTO.ProvidedProductDTO providedProductDTO : dto.getProvidedProducts()) {
                if (providedProductDTO != null) {
                    Product newProd = findProduct(entity.getOwner(), providedProductDTO.getProductId());
                    products.add(newProd);
                }
            }
            entity.setProvidedProducts(products);
        }
    }
    if (dto.getDerivedProvidedProducts() != null) {
        if (dto.getDerivedProvidedProducts().isEmpty()) {
            entity.setDerivedProvidedProducts(Collections.emptySet());
        } else {
            Set<Product> derivedProducts = new HashSet<>();
            for (PoolDTO.ProvidedProductDTO derivedProvidedProductDTO : dto.getDerivedProvidedProducts()) {
                if (derivedProvidedProductDTO != null) {
                    Product newDerivedProd = findProduct(entity.getOwner(), derivedProvidedProductDTO.getProductId());
                    derivedProducts.add(newDerivedProd);
                }
            }
            entity.setDerivedProvidedProducts(derivedProducts);
        }
    }
    if (dto.getBranding() != null) {
        if (dto.getBranding().isEmpty()) {
            entity.setBranding(Collections.emptySet());
        } else {
            Set<Branding> branding = new HashSet<>();
            for (BrandingDTO brandingDTO : dto.getBranding()) {
                if (brandingDTO != null) {
                    branding.add(new Branding(brandingDTO.getProductId(), brandingDTO.getType(), brandingDTO.getName()));
                }
            }
            entity.setBranding(branding);
        }
    }
}
Also used : BrandingDTO(org.candlepin.dto.api.v1.BrandingDTO) Product(org.candlepin.model.Product) PoolDTO(org.candlepin.dto.api.v1.PoolDTO) Branding(org.candlepin.model.Branding) HashSet(java.util.HashSet)

Example 12 with Branding

use of org.candlepin.model.Branding 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 13 with Branding

use of org.candlepin.model.Branding in project candlepin by candlepin.

the class PoolHelperTest method clonePoolTest.

@Test
public void clonePoolTest() {
    Product product = TestUtil.createProduct();
    Product product2 = TestUtil.createProduct();
    Map<String, String> attributes = new HashMap<>();
    for (int i = 0; i < 3; i++) {
        attributes.put("a" + i, "b" + i);
    }
    Branding branding = new Branding("id", "type", "name");
    Pool pool = TestUtil.createPool(owner, product);
    pool.getBranding().add(branding);
    String quant = "unlimited";
    Pool clone = PoolHelper.clonePool(pool, product2, quant, attributes, "TaylorSwift", null, ent, productCurator);
    assertEquals(owner, clone.getOwner());
    assertEquals(new Long(-1L), clone.getQuantity());
    assertEquals(product2, clone.getProduct());
    assertEquals(attributes.size() + 1, clone.getAttributes().size());
    for (int i = 0; i < 3; i++) {
        assertEquals("b" + i, clone.getAttributeValue("a" + i));
    }
    assertNotEquals(pool.getSourceSubscription(), clone);
    assertEquals(pool.getSourceSubscription().getSubscriptionId(), clone.getSubscriptionId());
    assertEquals(pool.getSourceSubscription().getSubscriptionId(), clone.getSourceSubscription().getSubscriptionId());
    assertEquals("TaylorSwift", clone.getSourceSubscription().getSubscriptionSubKey());
    assertEquals(1, clone.getBranding().size());
    Branding brandingClone = clone.getBranding().iterator().next();
    assertEquals(branding, brandingClone);
}
Also used : HashMap(java.util.HashMap) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Branding(org.candlepin.model.Branding) Test(org.junit.Test)

Example 14 with Branding

use of org.candlepin.model.Branding in project candlepin by candlepin.

the class PoolManagerTest method brandingCopiedWhenCreatingPools.

@Test
public void brandingCopiedWhenCreatingPools() {
    Product product = TestUtil.createProduct();
    Subscription sub = TestUtil.createSubscription(owner, product);
    Branding b1 = new Branding("8000", "OS", "Branded Awesome OS");
    Branding b2 = new Branding("8001", "OS", "Branded Awesome OS 2");
    sub.getBranding().add(b1);
    sub.getBranding().add(b2);
    this.mockProducts(owner, product);
    PoolRules pRules = new PoolRules(manager, mockConfig, entitlementCurator, mockOwnerProductCurator, mockProductCurator);
    List<Pool> pools = pRules.createAndEnrichPools(sub);
    assertEquals(1, pools.size());
    Pool resultPool = pools.get(0);
    assertEquals(2, resultPool.getBranding().size());
    assertTrue(resultPool.getBranding().contains(b1));
    assertTrue(resultPool.getBranding().contains(b2));
}
Also used : PoolRules(org.candlepin.policy.js.pool.PoolRules) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Branding(org.candlepin.model.Branding) Test(org.junit.Test)

Example 15 with Branding

use of org.candlepin.model.Branding 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);
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) Consumer(org.candlepin.model.Consumer) Subscription(org.candlepin.model.dto.Subscription) Branding(org.candlepin.model.Branding) ConsumerType(org.candlepin.model.ConsumerType) LinkedList(java.util.LinkedList) Date(java.util.Date) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) HashSet(java.util.HashSet) Before(org.junit.Before)

Aggregations

Branding (org.candlepin.model.Branding)26 Pool (org.candlepin.model.Pool)15 Product (org.candlepin.model.Product)14 HashSet (java.util.HashSet)9 Entitlement (org.candlepin.model.Entitlement)9 SourceSubscription (org.candlepin.model.SourceSubscription)8 Test (org.junit.Test)7 Owner (org.candlepin.model.Owner)6 Date (java.util.Date)5 Consumer (org.candlepin.model.Consumer)5 ProvidedProduct (org.candlepin.model.ProvidedProduct)5 SubscriptionsCertificate (org.candlepin.model.SubscriptionsCertificate)5 CertificateSerial (org.candlepin.model.CertificateSerial)4 ArrayList (java.util.ArrayList)3 HashMap (java.util.HashMap)3 SourceStack (org.candlepin.model.SourceStack)3 ProductData (org.candlepin.model.dto.ProductData)3 Subscription (org.candlepin.model.dto.Subscription)3 LinkedList (java.util.LinkedList)2 EntitlementDTO (org.candlepin.dto.manifest.v1.EntitlementDTO)2