Search in sources :

Example 26 with ProductDTO

use of org.candlepin.dto.api.v1.ProductDTO 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 27 with ProductDTO

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

the class ProductDataTranslatorTest method verifyOutput.

@Override
protected void verifyOutput(ProductData source, ProductDTO dto, boolean childrenGenerated) {
    if (source != null) {
        assertEquals(source.getUuid(), dto.getUuid());
        assertEquals(source.getId(), dto.getId());
        assertEquals(source.getName(), dto.getName());
        assertEquals(source.getMultiplier(), dto.getMultiplier());
        assertEquals(source.getAttributes(), dto.getAttributes());
        assertEquals(source.getDependentProductIds(), dto.getDependentProductIds());
        assertEquals(source.isLocked(), dto.isLocked());
        assertEquals(source.getHref(), dto.getHref());
        if (childrenGenerated) {
            assertNotNull(dto.getProductContent());
            for (ProductContentData pc : source.getProductContent()) {
                for (ProductContentDTO pcdto : dto.getProductContent()) {
                    ContentData content = pc.getContent();
                    ContentDTO cdto = pcdto.getContent();
                    assertNotNull(cdto);
                    assertNotNull(cdto.getUuid());
                    if (cdto.getUuid().equals(content.getUuid())) {
                        assertEquals(pc.isEnabled(), pcdto.isEnabled());
                        // Pass the content off to the ContentTranslatorTest to verify it
                        this.contentDataTranslatorTest.verifyOutput(content, cdto, childrenGenerated);
                    }
                }
            }
        } else {
            assertNull(dto.getProductContent());
        }
    } else {
        assertNull(dto);
    }
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ContentDTO(org.candlepin.dto.api.v1.ContentDTO) ContentData(org.candlepin.model.dto.ContentData) ProductContentData(org.candlepin.model.dto.ProductContentData) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContentData(org.candlepin.model.dto.ProductContentData)

Example 28 with ProductDTO

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

the class ProductImporterTest method testNewProductCreated.

@Test
public void testNewProductCreated() throws Exception {
    ProductDTO product = new ProductDTO();
    product.setId("test-id");
    product.setName("test-name");
    product.setAttribute("attr1", "val1");
    product.setAttribute("attr2", "val2");
    product.setMultiplier(1L);
    Set<String> dependentProdIDs = new HashSet<>();
    dependentProdIDs.add("g23gh23h2");
    dependentProdIDs.add("353g823h");
    product.setDependentProductIds(dependentProdIDs);
    String json = getJsonForProduct(product);
    Reader reader = new StringReader(json);
    ProductDTO created = importer.createObject(mapper, reader, owner);
    assertEquals(product, created);
}
Also used : StringReader(java.io.StringReader) Reader(java.io.Reader) StringReader(java.io.StringReader) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 29 with ProductDTO

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

the class ProductImporterTest method testVendorSetToUnknown.

@Test
public void testVendorSetToUnknown() throws Exception {
    Product product = TestUtil.createProduct();
    addNoVendorContentTo(product);
    String json = getJsonForProduct(product);
    Reader reader = new StringReader(json);
    ProductDTO created = importer.createObject(mapper, reader, owner);
    ContentDTO c = created.getProductContent().iterator().next().getContent();
    assertEquals("unknown", c.getVendor());
}
Also used : ContentDTO(org.candlepin.dto.manifest.v1.ContentDTO) StringReader(java.io.StringReader) Product(org.candlepin.model.Product) Reader(java.io.Reader) StringReader(java.io.StringReader) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO) Test(org.junit.Test)

Example 30 with ProductDTO

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

the class TestUtil method createProduct.

public static Product createProduct(ProductDTO dto) {
    Product product = null;
    if (dto != null) {
        product = new Product(dto.getId(), dto.getName());
        product.setUuid(dto.getUuid());
        product.setMultiplier(dto.getMultiplier());
        product.setAttributes(dto.getAttributes());
        if (dto.getProductContent() != null) {
            for (ProductContentDTO pcd : dto.getProductContent()) {
                if (pcd != null) {
                    Content content = createContent((ContentDTO) pcd.getContent());
                    if (content != null) {
                        product.addContent(content, pcd.isEnabled() != null ? pcd.isEnabled() : true);
                    }
                }
            }
        }
        product.setDependentProductIds(dto.getDependentProductIds());
        product.setLocked(dto.isLocked() != null ? dto.isLocked() : false);
    }
    return product;
}
Also used : Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO)

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