Search in sources :

Example 91 with Content

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

Example 92 with Content

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

the class ProductTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ProductDTO populate(ModelTranslator modelTranslator, Product source, ProductDTO destination) {
    destination = super.populate(modelTranslator, source, destination);
    destination.setUuid(source.getUuid());
    destination.setMultiplier(source.getMultiplier());
    destination.setId(source.getId());
    destination.setName(source.getName());
    destination.setAttributes(source.getAttributes());
    destination.setDependentProductIds(source.getDependentProductIds());
    if (modelTranslator != null) {
        Collection<ProductContent> productContent = source.getProductContent();
        destination.setProductContent(Collections.emptyList());
        if (productContent != null) {
            ObjectTranslator<Content, ContentDTO> contentTranslator = modelTranslator.findTranslatorByClass(Content.class, ContentDTO.class);
            for (ProductContent pc : productContent) {
                if (pc != null) {
                    ContentDTO dto = contentTranslator.translate(modelTranslator, pc.getContent());
                    if (dto != null) {
                        destination.addContent(dto, pc.isEnabled());
                    }
                }
            }
        }
    } else {
        destination.setProductContent(Collections.emptyList());
    }
    return destination;
}
Also used : ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContent(org.candlepin.model.ProductContent)

Example 93 with Content

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

the class ProductTranslator method populate.

/**
 * {@inheritDoc}
 */
@Override
public ProductDTO populate(ModelTranslator modelTranslator, Product source, ProductDTO destination) {
    destination = super.populate(modelTranslator, source, destination);
    destination.setUuid(source.getUuid());
    destination.setId(source.getId());
    destination.setName(source.getName());
    destination.setMultiplier(source.getMultiplier());
    destination.setHref(source.getHref());
    destination.setLocked(source.isLocked());
    destination.setAttributes(source.getAttributes());
    destination.setDependentProductIds(source.getDependentProductIds());
    if (modelTranslator != null) {
        Collection<ProductContent> productContent = source.getProductContent();
        destination.setProductContent(Collections.<ProductContentDTO>emptyList());
        if (productContent != null) {
            ObjectTranslator<Content, ContentDTO> contentTranslator = modelTranslator.findTranslatorByClass(Content.class, ContentDTO.class);
            for (ProductContent pc : productContent) {
                if (pc != null) {
                    ContentDTO dto = contentTranslator.translate(modelTranslator, pc.getContent());
                    if (dto != null) {
                        destination.addContent(dto, pc.isEnabled());
                    }
                }
            }
        }
    } else {
        destination.setProductContent(Collections.<ProductContentDTO>emptyList());
    }
    return destination;
}
Also used : ProductContentDTO(org.candlepin.dto.api.v1.ProductDTO.ProductContentDTO) ProductContent(org.candlepin.model.ProductContent) Content(org.candlepin.model.Content) ProductContent(org.candlepin.model.ProductContent)

Example 94 with Content

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

the class OwnerProductResourceTest method testCreateProductWithContent.

@Test
public void testCreateProductWithContent() {
    Owner owner = this.createOwner("Example-Corporation");
    Content content = this.createContent("content-1", "content-1", owner);
    ProductDTO pdto = this.buildTestProductDTO();
    ContentDTO cdto = this.modelTranslator.translate(content, ContentDTO.class);
    pdto.addContent(cdto, true);
    assertNull(this.ownerProductCurator.getProductById(owner.getKey(), pdto.getId()));
    ProductDTO result = this.ownerProductResource.createProduct(owner.getKey(), pdto);
    Product entity = this.ownerProductCurator.getProductById(owner, pdto.getId());
    ProductDTO expected = this.modelTranslator.translate(entity, ProductDTO.class);
    assertNotNull(result);
    assertNotNull(entity);
    assertEquals(expected, result);
    assertNotNull(result.getProductContent());
    assertEquals(1, result.getProductContent().size());
    assertEquals(cdto, result.getProductContent().iterator().next().getContent());
}
Also used : ContentDTO(org.candlepin.dto.api.v1.ContentDTO) Owner(org.candlepin.model.Owner) Content(org.candlepin.model.Content) Product(org.candlepin.model.Product) ProductDTO(org.candlepin.dto.api.v1.ProductDTO) Test(org.junit.Test)

Example 95 with Content

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

the class ContentDataTest method testPopulateWithEntity.

@Test
@Parameters(method = "getValuesPopulationByEntity")
public void testPopulateWithEntity(String valueName, Object input, Object defaultValue) throws Exception {
    Method accessor = null;
    Method mutator = null;
    try {
        accessor = ContentData.class.getDeclaredMethod("get" + valueName, null);
    } catch (NoSuchMethodException e) {
        accessor = ContentData.class.getDeclaredMethod("is" + valueName, null);
    }
    try {
        mutator = Content.class.getDeclaredMethod("set" + valueName, input.getClass());
    } catch (NoSuchMethodException e) {
        if (Collection.class.isAssignableFrom(input.getClass())) {
            mutator = Content.class.getDeclaredMethod("set" + valueName, Collection.class);
        } else if (Boolean.class.isAssignableFrom(input.getClass())) {
            mutator = Content.class.getDeclaredMethod("set" + valueName, boolean.class);
        } else {
            throw e;
        }
    }
    ContentData base = new ContentData();
    Content source = new Content();
    mutator.invoke(source, input);
    base.populate(source);
    // Verify only the specified field was set
    for (Method method : ContentData.class.getDeclaredMethods()) {
        if (method.getName().matches("^(get|is)\\w+")) {
            Object output = method.invoke(base, null);
            if (method.getName().equals(accessor.getName())) {
                if (input instanceof Collection) {
                    assertTrue(output instanceof Collection);
                    assertTrue(Util.collectionsAreEqual((Collection) input, (Collection) output));
                } else {
                    assertEquals(input, output);
                }
            } else {
                for (Object[] values : this.getValuesPopulationByEntity()) {
                    if (method.getName().endsWith((String) values[0])) {
                        if (values[2] instanceof Collection) {
                            assertTrue(output instanceof Collection);
                            assertTrue(Util.collectionsAreEqual((Collection) values[2], (Collection) output));
                        } else {
                            assertEquals(values[2], output);
                        }
                    }
                }
            }
        }
    }
}
Also used : Content(org.candlepin.model.Content) Collection(java.util.Collection) Method(java.lang.reflect.Method) Parameters(junitparams.Parameters) Test(org.junit.Test)

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