Search in sources :

Example 41 with Content

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

the class OwnerContentResourceTest method deleteLockedContent.

@Test(expected = ForbiddenException.class)
public void deleteLockedContent() {
    Owner owner = this.createOwner("test_owner");
    Content content = this.createContent("test_content", "test_content", owner);
    content.setLocked(true);
    this.contentCurator.merge(content);
    Environment environment = this.createEnvironment(owner, "test_env", "test_env", null, null, Arrays.asList(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    try {
        this.ownerContentResource.remove(owner.getKey(), content.getId());
    } catch (ForbiddenException e) {
        assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
        this.environmentCurator.evict(environment);
        environment = this.environmentCurator.find(environment.getId());
        assertEquals(1, environment.getEnvironmentContent().size());
        throw e;
    }
}
Also used : Owner(org.candlepin.model.Owner) ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content) Environment(org.candlepin.model.Environment) Test(org.junit.Test)

Example 42 with Content

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

the class OwnerContentResourceTest method deleteContent.

@Test
public void deleteContent() {
    Owner owner = this.createOwner("test_owner");
    Content content = this.createContent("test_content", "test_content", owner);
    Environment environment = this.createEnvironment(owner, "test_env", "test_env", null, null, Arrays.asList(content));
    assertNotNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    this.ownerContentResource.remove(owner.getKey(), content.getId());
    assertNull(this.ownerContentCurator.getContentById(owner, content.getId()));
    this.environmentCurator.evict(environment);
    environment = this.environmentCurator.find(environment.getId());
    assertEquals(0, environment.getEnvironmentContent().size());
}
Also used : Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Environment(org.candlepin.model.Environment) Test(org.junit.Test)

Example 43 with Content

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

the class AutobindRulesTest method createV3OnlyPool.

/*
     * Create a pool with too much content for a V1 certificate, consumer must be V3
     * capable.
     */
public Pool createV3OnlyPool() {
    Product mktProduct = TestUtil.createProduct(productId, "A test product");
    Product engProduct = TestUtil.createProduct(Integer.toString(TestUtil.randomInt()), "An ENG product");
    engProduct.setProductContent(null);
    for (int i = 0; i < X509ExtensionUtil.V1_CONTENT_LIMIT + 1; i++) {
        Content content = TestUtil.createContent("fake" + i);
        content.setLabel("fake" + i);
        content.setType("yum");
        content.setVendor("vendor");
        content.setContentUrl("");
        content.setGpgUrl("");
        content.setArches("");
        engProduct.addContent(content, true);
    }
    Pool pool = TestUtil.createPool(owner, mktProduct);
    pool.setId("DEAD-BEEFX");
    pool.addProvidedProduct(engProduct);
    when(mockProductCurator.getPoolProvidedProductsCached(pool.getId())).thenReturn(Collections.singleton(engProduct));
    return pool;
}
Also used : Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool)

Example 44 with Content

use of org.candlepin.model.Content 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)

Example 45 with Content

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

the class OwnerContentResource method createContentImpl.

/**
 * Creates or merges the given Content object.
 *
 * @param owner
 *  The owner for which to create the new content
 *
 * @param content
 *  The content to create or merge
 *
 * @return
 *  the newly created and/or merged Content object.
 */
private Content createContentImpl(Owner owner, ContentDTO content) {
    // TODO: check if arches have changed ??
    Content entity = null;
    if (content.getId() == null || content.getId().trim().length() == 0) {
        content.setId(this.idGenerator.generateId());
        entity = this.contentManager.createContent(content, owner);
    } else {
        Content existing = this.ownerContentCurator.getContentById(owner, content.getId());
        if (existing != null) {
            if (existing.isLocked()) {
                throw new ForbiddenException(i18n.tr("content \"{0}\" is locked", existing.getId()));
            }
            entity = this.contentManager.updateContent(content, owner, true);
        } else {
            entity = this.contentManager.createContent(content, owner);
        }
    }
    return entity;
}
Also used : ForbiddenException(org.candlepin.common.exceptions.ForbiddenException) Content(org.candlepin.model.Content)

Aggregations

Content (org.candlepin.model.Content)97 Test (org.junit.Test)45 ProductContent (org.candlepin.model.ProductContent)41 Product (org.candlepin.model.Product)40 Owner (org.candlepin.model.Owner)39 ContentDTO (org.candlepin.dto.api.v1.ContentDTO)25 HashMap (java.util.HashMap)18 EnvironmentContent (org.candlepin.model.EnvironmentContent)17 HashSet (java.util.HashSet)14 LinkedList (java.util.LinkedList)11 ProductDTO (org.candlepin.dto.api.v1.ProductDTO)10 ArrayList (java.util.ArrayList)9 Matchers.anyString (org.mockito.Matchers.anyString)9 Transactional (com.google.inject.persist.Transactional)8 Produces (javax.ws.rs.Produces)8 Parameters (junitparams.Parameters)8 ApiOperation (io.swagger.annotations.ApiOperation)7 ForbiddenException (org.candlepin.common.exceptions.ForbiddenException)7 Path (javax.ws.rs.Path)6 ProductContentDTO (org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)6