Search in sources :

Example 6 with EventBuilder

use of org.candlepin.audit.EventBuilder in project candlepin by candlepin.

the class PoolManagerTest method doesntMergeDeletedPools.

@Test
public void doesntMergeDeletedPools() {
    reset(mockPoolCurator);
    Map<String, EventBuilder> poolEvents = new HashMap<>();
    List<PoolUpdate> updatedPools = new ArrayList<>();
    Pool deletedPool = Mockito.mock(Pool.class);
    Pool normalPool = Mockito.mock(Pool.class);
    when(normalPool.getId()).thenReturn("normal-pool-id");
    Set<String> existingPoolIds = new HashSet<>();
    existingPoolIds.add("normal-pool-id");
    when(mockPoolCurator.getExistingPoolIdsByIds(any(Iterable.class))).thenReturn(existingPoolIds);
    when(mockPoolCurator.exists(deletedPool)).thenReturn(false);
    when(mockPoolCurator.exists(normalPool)).thenReturn(true);
    PoolUpdate deletedPu = Mockito.mock(PoolUpdate.class);
    PoolUpdate normalPu = Mockito.mock(PoolUpdate.class);
    when(deletedPu.getPool()).thenReturn(deletedPool);
    when(normalPu.getPool()).thenReturn(normalPool);
    updatedPools.add(deletedPu);
    updatedPools.add(normalPu);
    manager.processPoolUpdates(poolEvents, updatedPools);
    verify(mockPoolCurator, never()).merge(deletedPool);
    verify(mockPoolCurator, times(1)).merge(normalPool);
}
Also used : EventBuilder(org.candlepin.audit.EventBuilder) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) PoolUpdate(org.candlepin.policy.js.pool.PoolUpdate) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 7 with EventBuilder

use of org.candlepin.audit.EventBuilder in project candlepin by candlepin.

the class ConsumerResource method regenerateIdentityCertificate.

/**
 * Identity Certificate regeneration for a given consumer.
 * Includes persistence of the certificate and consumer
 *
 * @param consumer consumer that will get new identity cert
 * @return a Consumer object
 */
private Consumer regenerateIdentityCertificate(Consumer consumer) {
    EventBuilder eventBuilder = eventFactory.getEventBuilder(Target.CONSUMER, Type.MODIFIED).setEventData(consumer);
    IdentityCertificate ic = generateIdCert(consumer, true);
    consumer.setIdCert(ic);
    consumerCurator.update(consumer);
    sink.queueEvent(eventBuilder.setEventData(consumer).buildEvent());
    return consumer;
}
Also used : EventBuilder(org.candlepin.audit.EventBuilder) IdentityCertificate(org.candlepin.model.IdentityCertificate)

Example 8 with EventBuilder

use of org.candlepin.audit.EventBuilder in project candlepin by candlepin.

the class EntitlementRules method resolveProductShares.

private Map<String, Product> resolveProductShares(Owner sharingOwner, Owner recipient, Set<Product> products) {
    Map<String, Product> sharedProductsIdMap = new HashMap<>();
    Map<String, Product> sharedProductsUuidMap = new HashMap<>();
    Map<String, Product> resolvedProducts = new HashMap<>();
    List<Event> events = new LinkedList<>();
    List<ProductShare> sharesToDelete = new LinkedList<>();
    List<ProductShare> sharesToCreate = new LinkedList<>();
    Map<String, ProductShare> existingSharesMap = new HashMap<>();
    Map<String, String> productRefsToUpdate = new HashMap<>();
    for (Product product : products) {
        sharedProductsIdMap.put(product.getId(), product);
        sharedProductsUuidMap.put(product.getUuid(), product);
    }
    List<Product> recipientProducts = ownerProductCurator.getProductsByIds(recipient, sharedProductsIdMap.keySet()).list();
    for (Product product : recipientProducts) {
        Map<String, Product> conflictedRecipientProducts = new HashMap<>();
        if (sharedProductsUuidMap.containsKey(product.getUuid())) {
            // Recipient has a product with the same ID already.  If they are the same instance
            // use then nothing needs doing.  Everything is already in place.
            resolvedProducts.put(product.getId(), product);
            log.debug("Owner {} has the same product {} as the sharer {}", recipient.getKey(), product.getId(), sharingOwner.getKey());
        } else {
            // The recipient and owner have two products with the same ID but they are different
            // instances since their uuids do not match.
            conflictedRecipientProducts.put(product.getId(), product);
        }
        if (conflictedRecipientProducts.size() > 0) {
            List<ProductShare> existingShares = shareCurator.findProductSharesByRecipient(recipient, conflictedRecipientProducts.keySet());
            for (ProductShare pShare : existingShares) {
                existingSharesMap.put(pShare.getProduct().getId(), pShare);
            }
        }
        for (String id : conflictedRecipientProducts.keySet()) {
            if (!existingSharesMap.containsKey(id)) {
                // If the recipient's product isn't from a share, let the recipient just continue to
                // use its existing product definition.
                log.debug("Owner {} already has product {} defined that is not a share", recipient.getKey(), id);
                resolvedProducts.put(id, conflictedRecipientProducts.get(id));
            } else {
                // If the recipient's product is a share then two owners are sharing into the same
                // recipient and we must resolve the conflict.
                Product sharingOwnerProduct = sharedProductsIdMap.get(id);
                Product existingProduct = conflictedRecipientProducts.get(id);
                ProductShare existingShare = existingSharesMap.get(id);
                log.debug("Owner {} already has a share for Product {} from owner {}. Solving conflict.", recipient.getKey(), id, existingShare.getOwner());
                EventBuilder builder = eventFactory.getEventBuilder(Event.Target.PRODUCT, Event.Type.MODIFIED);
                builder.setEventData(existingProduct);
                sharesToDelete.add(existingShare);
                sharesToCreate.add(new ProductShare(sharingOwner, sharingOwnerProduct, recipient));
                // Now we need to reconcile all of recipient's pools that were using the old product.
                Product resolvedProduct = productManager.updateProduct(sharingOwnerProduct.toDTO(), recipient, true);
                builder.setEventData(resolvedProduct);
                resolvedProducts.put(resolvedProduct.getId(), resolvedProduct);
                events.add(builder.buildEvent());
            }
        }
    }
    Set<String> idsNonExisting = new HashSet<>(sharedProductsIdMap.keySet());
    idsNonExisting.removeAll(resolvedProducts.keySet());
    for (String id : idsNonExisting) {
        // The recipient doesn't have a definition for the product at all.  Link the recipient
        // and product and create a record of a share.
        log.debug("Linking product {} from owner {} to owner {} as product share", id, sharingOwner.getKey(), recipient.getKey());
        Product sharedProduct = sharedProductsIdMap.get(id);
        ownerProductCurator.mapProductToOwner(sharedProduct, recipient);
        sharesToCreate.add(new ProductShare(sharingOwner, sharedProduct, recipient));
        resolvedProducts.put(id, sharedProduct);
    }
    shareCurator.bulkDelete(sharesToDelete);
    shareCurator.saveOrUpdateAll(sharesToCreate, false, false);
    for (Event event : events) {
        eventSink.queueEvent(event);
    }
    return resolvedProducts;
}
Also used : HashMap(java.util.HashMap) ProductShare(org.candlepin.model.ProductShare) Product(org.candlepin.model.Product) LinkedList(java.util.LinkedList) EventBuilder(org.candlepin.audit.EventBuilder) Event(org.candlepin.audit.Event) HashSet(java.util.HashSet)

Aggregations

EventBuilder (org.candlepin.audit.EventBuilder)8 HashMap (java.util.HashMap)4 HashSet (java.util.HashSet)4 Event (org.candlepin.audit.Event)4 Pool (org.candlepin.model.Pool)4 PoolUpdate (org.candlepin.policy.js.pool.PoolUpdate)4 Transactional (com.google.inject.persist.Transactional)2 Date (java.util.Date)2 IdentityCertificate (org.candlepin.model.IdentityCertificate)2 Test (org.junit.Test)2 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 BadRequestException (org.candlepin.common.exceptions.BadRequestException)1 NotFoundException (org.candlepin.common.exceptions.NotFoundException)1 ConsumerType (org.candlepin.model.ConsumerType)1 Environment (org.candlepin.model.Environment)1 Owner (org.candlepin.model.Owner)1 Product (org.candlepin.model.Product)1 ProductShare (org.candlepin.model.ProductShare)1 Release (org.candlepin.model.Release)1