Search in sources :

Example 41 with ContentDTO

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

the class ContentManagerTest method testUpdateContent.

@Test
@Parameters({ "false", "true" })
public void testUpdateContent(boolean regenCerts) {
    Owner owner = this.createOwner("test-owner", "Test Owner");
    Product product = this.createProduct("p1", "product-1", owner);
    Content content = this.createContent("c1", "content-1", owner);
    ContentDTO update = TestUtil.createContentDTO("c1", "new content name");
    product.addContent(content, true);
    product = this.productCurator.merge(product);
    Content output = this.contentManager.updateContent(update, owner, regenCerts);
    assertNotEquals(output.getUuid(), content.getUuid());
    assertEquals(output.getName(), update.getName());
    // We expect the original to be kept around as an orphan until the orphan removal job
    // gets around to removing them
    assertNotNull(this.contentCurator.find(content.getUuid()));
    assertEquals(0, this.ownerContentCurator.getOwnerCount(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    // The product should have also changed in the same way as a result of the content change
    assertNotNull(this.productCurator.find(product.getUuid()));
    assertEquals(0, this.ownerProductCurator.getOwnerCount(product));
    assertNotNull(this.ownerProductCurator.getProductById(owner, product.getId()));
    if (regenCerts) {
        verify(this.mockEntCertGenerator, times(1)).regenerateCertificatesOf(eq(Arrays.asList(owner)), anyCollectionOf(Product.class), anyBoolean());
    } else {
        verifyZeroInteractions(this.mockEntCertGenerator);
    }
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 42 with ContentDTO

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

the class ContentManagerTest method testUpdateContentConvergeWithExisting.

@Test
@Parameters({ "false", "true" })
public void testUpdateContentConvergeWithExisting(boolean regenCerts) {
    Owner owner1 = this.createOwner("test-owner-1", "Test Owner 1");
    Owner owner2 = this.createOwner("test-owner-2", "Test Owner 2");
    Product product = this.createProduct("p1", "product-1", owner1);
    Content content1 = this.createContent("c1", "content-1", owner1);
    Content content2 = this.createContent("c1", "updated content", owner2);
    ContentDTO update = TestUtil.createContentDTO("c1", "updated content");
    product.addContent(content1, true);
    product = this.productCurator.merge(product);
    assertTrue(this.ownerContentCurator.isContentMappedToOwner(content1, owner1));
    assertFalse(this.ownerContentCurator.isContentMappedToOwner(content2, owner1));
    assertFalse(this.ownerContentCurator.isContentMappedToOwner(content1, owner2));
    assertTrue(this.ownerContentCurator.isContentMappedToOwner(content2, owner2));
    Content output = this.contentManager.updateContent(update, owner1, regenCerts);
    assertEquals(content2.getUuid(), output.getUuid());
    assertFalse(this.ownerContentCurator.isContentMappedToOwner(content1, owner1));
    assertTrue(this.ownerContentCurator.isContentMappedToOwner(content2, owner1));
    assertFalse(this.ownerContentCurator.isContentMappedToOwner(content1, owner2));
    assertTrue(this.ownerContentCurator.isContentMappedToOwner(content2, owner2));
    if (regenCerts) {
        verify(this.mockEntCertGenerator, times(1)).regenerateCertificatesOf(eq(Arrays.asList(owner1)), anyCollectionOf(Product.class), anyBoolean());
    } else {
        verifyZeroInteractions(this.mockEntCertGenerator);
    }
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) Parameters(junitparams.Parameters) Test(org.junit.Test)

Example 43 with ContentDTO

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

the class ContentManagerTest method testUpdateContentThatDoesntExist.

@Test(expected = IllegalStateException.class)
public void testUpdateContentThatDoesntExist() {
    Owner owner = this.createOwner("test-owner", "Test Owner");
    Content content = TestUtil.createContent("c1", "content-1");
    ContentDTO update = TestUtil.createContentDTO("c1", "new_name");
    assertFalse(this.ownerContentCurator.isContentMappedToOwner(content, owner));
    this.contentManager.updateContent(update, owner, false);
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Test(org.junit.Test)

Example 44 with ContentDTO

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

the class OwnerContentResource method createBatchContent.

@ApiOperation(notes = "Creates Contents in bulk", value = "createBatchContent")
@POST
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@Path("/batch")
@Transactional
public Collection<ContentDTO> createBatchContent(@PathParam("owner_key") String ownerKey, @ApiParam(name = "contents", required = true) List<ContentDTO> contents) {
    Collection<ContentDTO> result = new LinkedList<>();
    Owner owner = this.getOwnerByKey(ownerKey);
    for (ContentDTO content : contents) {
        Content entity = this.createContentImpl(owner, content);
        result.add(this.translator.translate(entity, ContentDTO.class));
    }
    ownerManager.refreshOwnerForContentAccess(owner);
    return result;
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) LinkedList(java.util.LinkedList) Path(javax.ws.rs.Path) POST(javax.ws.rs.POST) Produces(javax.ws.rs.Produces) Consumes(javax.ws.rs.Consumes) ApiOperation(io.swagger.annotations.ApiOperation) Transactional(com.google.inject.persist.Transactional)

Example 45 with ContentDTO

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

the class ProductManager method isChangedBy.

/**
 * Determines whether or not this entity would be changed if the given DTO were applied to this
 * object.
 *
 * @param dto
 *  The product DTO to check for changes
 *
 * @throws IllegalArgumentException
 *  if dto is null
 *
 * @return
 *  true if this product would be changed by the given DTO; false otherwise
 */
public static boolean isChangedBy(Product entity, ProductDTO dto) {
    // Check simple properties first
    if (dto.getId() != null && !dto.getId().equals(entity.getId())) {
        return true;
    }
    if (dto.getName() != null && !dto.getName().equals(entity.getName())) {
        return true;
    }
    if (dto.getMultiplier() != null && !dto.getMultiplier().equals(entity.getMultiplier())) {
        return true;
    }
    if (dto.isLocked() != null && !dto.isLocked().equals(entity.isLocked())) {
        return true;
    }
    Collection<String> dependentProductIds = dto.getDependentProductIds();
    if (dependentProductIds != null && !Util.collectionsAreEqual(entity.getDependentProductIds(), dependentProductIds)) {
        return true;
    }
    // Impl note:
    // Depending on how strict we are regarding case-sensitivity and value-representation,
    // this may get us in to trouble. We may need to iterate through the attributes, performing
    // case-insensitive key/value comparison and similiarities (i.e. management_enabled: 1 is
    // functionally identical to Management_Enabled: true, but it will be detected as a change
    // in attributes.
    Map<String, String> attributes = dto.getAttributes();
    if (attributes != null && !attributes.equals(entity.getAttributes())) {
        return true;
    }
    Collection<ProductContentDTO> productContent = dto.getProductContent();
    if (productContent != null) {
        Comparator comparator = new Comparator() {

            public int compare(Object lhs, Object rhs) {
                ProductContent existing = (ProductContent) lhs;
                ProductContentDTO update = (ProductContentDTO) rhs;
                if (existing != null && update != null) {
                    Content content = existing.getContent();
                    ContentDTO cdto = update.getContent();
                    if (content != null && cdto != null) {
                        if (cdto.getUuid() != null ? cdto.getUuid().equals(content.getUuid()) : (cdto.getId() != null && cdto.getId().equals(content.getId()))) {
                            return (update.isEnabled() != null && !update.isEnabled().equals(existing.isEnabled())) || ContentManager.isChangedBy(content, cdto) ? 1 : 0;
                        }
                    }
                }
                return 1;
            }
        };
        if (!Util.collectionsAreEqual((Collection) entity.getProductContent(), (Collection) productContent, comparator)) {
            return true;
        }
    }
    return false;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) Collection(java.util.Collection) ProductContent(org.candlepin.model.ProductContent) Comparator(java.util.Comparator)

Aggregations

ContentDTO (org.candlepin.dto.api.v1.ContentDTO)35 Content (org.candlepin.model.Content)30 Test (org.junit.Test)28 Owner (org.candlepin.model.Owner)26 Product (org.candlepin.model.Product)14 ProductContent (org.candlepin.model.ProductContent)9 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)8 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)7 ContentDTO (org.candlepin.dto.manifest.v1.ContentDTO)7 LinkedList (java.util.LinkedList)5 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)5 Transactional (com.google.inject.persist.Transactional)4 Parameters (junitparams.Parameters)4 ContentData (org.candlepin.model.dto.ContentData)4 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 Produces (javax.ws.rs.Produces)3 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)3 ProductContentData (org.candlepin.model.dto.ProductContentData)3