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);
}
}
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);
}
}
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);
}
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;
}
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;
}
Aggregations