Search in sources :

Example 6 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.

the class HostedTestSubscriptionResource method addBatchContent.

@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
@Path("/owners/{owner_key}/products/{product_id}/batch_content")
@Transactional
public ProductDTO addBatchContent(@PathParam("owner_key") String ownerKey, @PathParam("product_id") String productId, Map<String, Boolean> contentMap) {
    Owner owner = this.getOwnerByKey(ownerKey);
    Product product = this.fetchProduct(owner, productId);
    Collection<ProductContent> productContent = new LinkedList<>();
    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);
        addContentToUpstreamSubscriptions(product, content, enabled);
    }
    if (changed) {
        product = this.productManager.updateProduct(pdto, owner, true);
    }
    return this.translator.translate(product, ProductDTO.class);
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductContent(org.candlepin.model.ProductContent) LinkedList(java.util.LinkedList) 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) Transactional(com.google.inject.persist.Transactional)

Example 7 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.

the class ProductManagerTest method testAddContentToProduct.

@Test
public void testAddContentToProduct() {
    Owner owner = this.createOwner("test-owner-1", "Test Owner 1");
    Product product = this.createProduct("p1", "prod1");
    Content content = this.createContent("c1", "content1", owner);
    this.ownerProductCurator.mapProductToOwners(product, owner);
    ProductDTO pdto = this.modelTranslator.translate(product, ProductDTO.class);
    ContentDTO cdto = this.modelTranslator.translate(content, ContentDTO.class);
    pdto.addContent(cdto, true);
    Product output = this.productManager.updateProduct(pdto, owner, false);
    assertNotEquals(product, output);
    assertTrue(output.hasContent(content.getId()));
    verifyZeroInteractions(this.mockEntCertGenerator);
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Example 8 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.

the class ProductManagerTest method testRemoveContentFromProductForBadOwner.

@Test(expected = IllegalStateException.class)
public void testRemoveContentFromProductForBadOwner() {
    Owner owner = this.createOwner("test-owner", "Test Owner");
    Owner owner2 = this.createOwner("test-owner-2", "Test Owner");
    Product product = TestUtil.createProduct("p1", "prod1");
    Content content = TestUtil.createContent("c1");
    product.addContent(content, true);
    this.contentCurator.create(content);
    this.productCurator.create(product);
    this.ownerProductCurator.mapProductToOwner(product, owner);
    this.ownerContentCurator.mapContentToOwner(content, owner);
    ProductDTO pdto = this.modelTranslator.translate(product, ProductDTO.class);
    boolean removed = pdto.removeContent(content.getId());
    assertTrue(removed);
    Product output = this.productManager.updateProduct(pdto, owner2, false);
}
Also used : Owner(org.candlepin.model.Owner) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Example 9 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.

the class ProductManagerTest method testUpdateProductConvergeWithExisting.

@Test
@Parameters({ "false", "true" })
public void testUpdateProductConvergeWithExisting(boolean regenCerts) {
    Owner owner1 = this.createOwner("test-owner-1", "Test Owner 1");
    Owner owner2 = this.createOwner("test-owner-2", "Test Owner 2");
    Product product1 = this.createProduct("p1", "prod1", owner1);
    Product product2 = this.createProduct("p1", "updated product", owner2);
    ProductDTO update = TestUtil.createProductDTO("p1", "updated product");
    assertTrue(this.ownerProductCurator.isProductMappedToOwner(product1, owner1));
    assertFalse(this.ownerProductCurator.isProductMappedToOwner(product2, owner1));
    assertFalse(this.ownerProductCurator.isProductMappedToOwner(product1, owner2));
    assertTrue(this.ownerProductCurator.isProductMappedToOwner(product2, owner2));
    Product output = this.productManager.updateProduct(update, owner1, regenCerts);
    assertEquals(output.getUuid(), product2.getUuid());
    assertFalse(this.ownerProductCurator.isProductMappedToOwner(product1, owner1));
    assertTrue(this.ownerProductCurator.isProductMappedToOwner(product2, owner1));
    assertFalse(this.ownerProductCurator.isProductMappedToOwner(product1, owner2));
    assertTrue(this.ownerProductCurator.isProductMappedToOwner(product2, owner2));
    if (regenCerts) {
        verify(this.mockEntCertGenerator, times(1)).regenerateCertificatesOf(eq(Arrays.asList(owner1)), anyCollectionOf(Product.class), anyBoolean());
    } else {
        verifyZeroInteractions(this.mockEntCertGenerator);
    }
}
Also used : Owner(org.candlepin.model.Owner) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 10 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO in project candlepin by candlepin.

the class ProductManagerTest method testCreateProduct.

@Test
public void testCreateProduct() {
    Owner owner = this.createOwner("test-owner", "Test Owner");
    ProductDTO dto = TestUtil.createProductDTO("p1", "prod1");
    assertNull(this.ownerProductCurator.getProductById(owner, "p1"));
    Product output = this.productManager.createProduct(dto, owner);
    assertEquals(output, this.ownerProductCurator.getProductById(owner, "p1"));
}
Also used : Owner(org.candlepin.model.Owner) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Aggregations

ProductDTO (org.candlepin.dto.api.v1.ProductDTO)29 Product (org.candlepin.model.Product)28 Test (org.junit.Test)27 Owner (org.candlepin.model.Owner)25 Content (org.candlepin.model.Content)15 ProductContent (org.candlepin.model.ProductContent)13 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)12 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)11 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)7 Parameters (junitparams.Parameters)6 Transactional (com.google.inject.persist.Transactional)5 Reader (java.io.Reader)5 LinkedList (java.util.LinkedList)5 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)5 StringReader (java.io.StringReader)4 HashSet (java.util.HashSet)4 Subscription (org.candlepin.model.dto.Subscription)4 HashMap (java.util.HashMap)3 Consumes (javax.ws.rs.Consumes)3 Path (javax.ws.rs.Path)3