Search in sources :

Example 11 with ProductContent

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

the class ProductDataTest method testAddProductContentByEntity.

@Test
public void testAddProductContentByEntity() {
    ProductData dto = new ProductData();
    Content[] contentEntities = new Content[] { new Content("c1", "content-1", "test_type", "test_label-1", "test_vendor-1"), new Content("c2", "content-2", "test_type", "test_label-2", "test_vendor-2") };
    ProductContent pcentity1 = new ProductContent(null, contentEntities[0], true);
    ProductContent pcentity2 = new ProductContent(null, contentEntities[1], false);
    ProductContent pcentity3 = new ProductContent(null, contentEntities[1], true);
    ContentData[] content = new ContentData[] { contentEntities[0].toDTO(), contentEntities[1].toDTO() };
    ProductContentData pcdata1 = new ProductContentData(content[0], true);
    ProductContentData pcdata2 = new ProductContentData(content[1], false);
    ProductContentData pcdata3 = new ProductContentData(content[1], true);
    assertNull(dto.getProductContent());
    boolean output = dto.addProductContent(pcentity1);
    Collection<ProductContentData> output2 = dto.getProductContent();
    assertTrue(output);
    assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1), output2));
    output = dto.addProductContent(pcentity1);
    output2 = dto.getProductContent();
    assertFalse(output);
    assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1), output2));
    output = dto.addProductContent(pcentity2);
    output2 = dto.getProductContent();
    assertTrue(output);
    assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1, pcdata2), output2));
    output = dto.addProductContent(pcentity3);
    output2 = dto.getProductContent();
    assertTrue(output);
    assertTrue(Util.collectionsAreEqual(Arrays.asList(pcdata1, pcdata3), output2));
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContent(org.candlepin.model.ProductContent) Test(org.junit.Test)

Example 12 with ProductContent

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

the class ProductContentDataTest method testPopulateWithEntityEnabled.

@Test
public void testPopulateWithEntityEnabled() {
    ProductContentData base = new ProductContentData();
    ProductContent source = new ProductContent();
    source.setEnabled(true);
    base.populate(source);
    assertNull(base.getContent());
    assertTrue(base.isEnabled());
}
Also used : ProductContent(org.candlepin.model.ProductContent) Test(org.junit.Test)

Example 13 with ProductContent

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

the class ProductContentDataTest method testPopulateWithEntityContent.

@Test
public void testPopulateWithEntityContent() {
    Content contentEntity = new Content("id1", "name1", "type1", "label1", "vendor1");
    ContentData contentDTO = contentEntity.toDTO();
    ProductContentData base = new ProductContentData();
    ProductContent source = new ProductContent();
    source.setContent(contentEntity);
    base.populate(source);
    assertEquals(contentDTO, base.getContent());
    // This will always be set after a populate
    assertFalse(base.isEnabled());
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContent(org.candlepin.model.ProductContent) Test(org.junit.Test)

Example 14 with ProductContent

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

the class ProductContentData method populate.

/**
 * Populates this DTO with data from the given source entity.
 *
 * @param source
 *  The source entity from which to copy data
 *
 * @throws IllegalArgumentException
 *  if source is null
 *
 * @return
 *  a reference to this DTO
 */
public ProductContentData populate(ProductContent source) {
    if (source == null) {
        throw new IllegalArgumentException("source is null");
    }
    Content content = source.getContent();
    this.content = content != null ? (this.content != null ? this.content.populate(content) : content.toDTO()) : null;
    this.enabled = source.isEnabled();
    return this;
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content)

Example 15 with ProductContent

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

the class OwnerProductResource method addBatchContent.

@ApiOperation(notes = "Adds one or more Content entities to a Product", value = "addBatchContent")
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/{product_id}/batch_content")
@Transactional
public ProductDTO addBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, @ApiParam(name = "contentMap", required = true) Map<String, Boolean> contentMap) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Product product = this.fetchProduct(owner, productId);
    Collection<ProductContent> productContent = new LinkedList<>();
    if (product.isLocked()) {
        throw new ForbiddenException(i18n.tr("product \"{0}\" is locked", product.getId()));
    }
    this.productCurator.lock(product);
    ProductDTO pdto = this.translator.translate(product, ProductDTO.class);
    // Impl note:
    // This is a wholely inefficient way of doing this. When we return to using ID-based linking
    // and we're not linking the universe with our model, we can just attach the IDs directly
    // without needing all this DTO conversion back and forth.
    // Alternatively, we can shut off Hibernate's auto-commit junk and get in the habit of
    // calling commit methods as necessary so we don't have to work with DTOs internally.
    boolean changed = false;
    for (Entry<String, Boolean> entry : contentMap.entrySet()) {
        Content content = this.fetchContent(owner, entry.getKey());
        boolean enabled = entry.getValue() != null ? entry.getValue() : ProductContent.DEFAULT_ENABLED_STATE;
        ContentDTO cdto = this.translator.translate(content, ContentDTO.class);
        changed |= pdto.addContent(cdto, enabled);
    }
    if (changed) {
        product = this.productManager.updateProduct(pdto, owner, true);
    }
    return this.translator.translate(product, ProductDTO.class);
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Product(org.candlepin.model.Product) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(io.swagger.annotations.ApiOperation) Transactional(com.google.inject.persist.Transactional)

Aggregations

ProductContent (org.candlepin.model.ProductContent)30 Content (org.candlepin.model.Content)20 Test (org.junit.Test)11 Product (org.candlepin.model.Product)9 HashSet (java.util.HashSet)7 HashMap (java.util.HashMap)5 LinkedList (java.util.LinkedList)5 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)4 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)4 Owner (org.candlepin.model.Owner)4 X509ExtensionWrapper (org.candlepin.pki.X509ExtensionWrapper)4 Transactional (com.google.inject.persist.Transactional)3 ArrayList (java.util.ArrayList)3 LinkedHashSet (java.util.LinkedHashSet)3 List (java.util.List)3 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)3 ContentData (org.candlepin.model.dto.ContentData)3 ProductContentData (org.candlepin.model.dto.ProductContentData)3 CertificateSizeException (org.candlepin.util.CertificateSizeException)3 IOException (java.io.IOException)2