Search in sources :

Example 36 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class RefresherTest method testPoolOnlyExaminedOnceProductAndOwner.

@Test
public void testPoolOnlyExaminedOnceProductAndOwner() {
    Owner owner = TestUtil.createOwner();
    Product product = mock(Product.class);
    ProductData productData = mock(ProductData.class);
    when(product.getUuid()).thenReturn("product id");
    when(product.toDTO()).thenReturn(productData);
    Pool pool = new Pool();
    pool.setSourceSubscription(new SourceSubscription("subId", "master"));
    pool.setOwner(owner);
    Subscription subscription = new Subscription();
    subscription.setId("subId");
    subscription.setOwner(owner);
    List<Pool> pools = new ArrayList<>();
    pools.add(pool);
    List<Subscription> subscriptions = new ArrayList<>();
    subscriptions.add(subscription);
    when(subAdapter.getSubscriptions(eq(productData))).thenReturn(subscriptions);
    when(subAdapter.getSubscriptions(owner)).thenReturn(subscriptions);
    when(subAdapter.getSubscription("subId")).thenReturn(subscription);
    when(poolManager.lookupBySubscriptionId(owner, "subId")).thenReturn(pools);
    refresher.add(owner);
    refresher.add(product);
    refresher.run();
    verify(poolManager, times(1)).refreshPoolsWithRegeneration(eq(subAdapter), eq(owner), eq(false));
    verify(poolManager, times(0)).updatePoolsForMasterPool(any(List.class), any(Pool.class), eq(pool.getQuantity()), eq(false), any(Map.class));
}
Also used : ProductData(org.candlepin.model.dto.ProductData) Owner(org.candlepin.model.Owner) SourceSubscription(org.candlepin.model.SourceSubscription) ArrayList(java.util.ArrayList) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) ArrayList(java.util.ArrayList) List(java.util.List) Subscription(org.candlepin.model.dto.Subscription) SourceSubscription(org.candlepin.model.SourceSubscription) Map(java.util.Map) Test(org.junit.Test)

Example 37 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerFunctionalTest method testRefreshPoolsWithChangedProductShouldUpdatePool.

@Test
public void testRefreshPoolsWithChangedProductShouldUpdatePool() {
    Product product1 = TestUtil.createProduct("product 1", "Product 1");
    Product product2 = TestUtil.createProduct("product 2", "Product 2");
    productCurator.create(product1);
    productCurator.create(product2);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    Subscription subscription = TestUtil.createSubscription(o, product1, new HashSet<>());
    subscription.setId(Util.generateDbUUID());
    subscription.setQuantity(5L);
    subscription.setStartDate(new Date());
    subscription.setEndDate(TestUtil.createDate(3020, 12, 12));
    subscription.setModified(new Date());
    subscriptions.add(subscription);
    // set up initial pool
    poolManager.getRefresher(subAdapter, ownerAdapter).add(o).run();
    List<Pool> pools = poolCurator.listByOwnerAndProduct(o, product1.getId());
    assertEquals(1, pools.size());
    // now alter the product behind the sub, and make sure the pool is also updated
    subscription.setProduct(product2.toDTO());
    // set up initial pool
    poolManager.getRefresher(subAdapter, ownerAdapter).add(o).run();
    pools = poolCurator.listByOwnerAndProduct(o, product2.getId());
    assertEquals(1, pools.size());
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) Date(java.util.Date) Test(org.junit.Test)

Example 38 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class PoolManagerFunctionalTest method testEntitleByProductsWithModifierAndModifiee.

@Test
public void testEntitleByProductsWithModifierAndModifiee() throws EntitlementRefusedException {
    Product modifier = TestUtil.createProduct("modifier", "modifier");
    Set<String> modified = new HashSet<>();
    modified.add(PRODUCT_VIRT_HOST);
    Content content = TestUtil.createContent("modifier-content", "modifier-content");
    content.setModifiedProductIds(modified);
    modifier.addContent(content, true);
    contentCurator.create(content);
    productCurator.create(modifier);
    this.ownerContentCurator.mapContentToOwner(content, this.o);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    Subscription sub = TestUtil.createSubscription(o, modifier, new HashSet<>());
    sub.setQuantity(5L);
    sub.setStartDate(new Date());
    sub.setEndDate(TestUtil.createDate(3020, 12, 12));
    sub.setModified(new Date());
    sub.setId(Util.generateDbUUID());
    subscriptions.add(sub);
    poolManager.getRefresher(subAdapter, ownerAdapter).add(o).run();
    // This test simulates https://bugzilla.redhat.com/show_bug.cgi?id=676870
    // where entitling first to the modifier then to the modifiee causes the modifier's
    // entitlement cert to get regenerated, but since it's all in the same http call,
    // this ends up causing a hibernate failure (the old cert is asked to be deleted,
    // but it hasn't been saved yet). Since getting the pool ordering right is tricky
    // inside an entitleByProducts call, we do it in two singular calls here.
    AutobindData data = AutobindData.create(parentSystem, o).on(new Date()).forProducts(new String[] { "modifier" });
    poolManager.entitleByProducts(data);
    try {
        data = AutobindData.create(parentSystem, o).on(new Date()).forProducts(new String[] { PRODUCT_VIRT_HOST });
        poolManager.entitleByProducts(data);
    } catch (EntityNotFoundException e) {
        throw e;
    // fail("Hibernate failed to properly save entitlement certs!");
    }
// If we get here, no exception was raised, so we're happy!
}
Also used : Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) EntityNotFoundException(javax.persistence.EntityNotFoundException) LinkedList(java.util.LinkedList) Date(java.util.Date) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) Content(org.candlepin.model.Content) AutobindData(org.candlepin.resource.dto.AutobindData) Subscription(org.candlepin.model.dto.Subscription) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 39 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class Importer method importObjects.

@SuppressWarnings("checkstyle:methodlength")
@Transactional(rollbackOn = { IOException.class, ImporterException.class, RuntimeException.class, ImportConflictException.class })
public // WARNING: Keep this method public, otherwise @Transactional is ignored:
List<Subscription> importObjects(Owner owner, Map<String, File> importFiles, ConflictOverrides overrides) throws IOException, ImporterException {
    ownerCurator.lock(owner);
    log.debug("Importing objects for owner: {}", owner);
    File metadata = importFiles.get(ImportFile.META.fileName());
    if (metadata == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required meta.json file"));
    }
    File consumerTypes = importFiles.get(ImportFile.CONSUMER_TYPE.fileName());
    if (consumerTypes == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required consumer_types directory"));
    }
    File consumerFile = importFiles.get(ImportFile.CONSUMER.fileName());
    if (consumerFile == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required consumer.json file"));
    }
    File products = importFiles.get(ImportFile.PRODUCTS.fileName());
    File entitlements = importFiles.get(ImportFile.ENTITLEMENTS.fileName());
    if (products != null && entitlements == null) {
        throw new ImporterException(i18n.tr("The archive does not contain the required entitlements directory"));
    }
    // system level elements
    /*
         * Checking a system wide last import date breaks multi-tenant deployments whenever
         * one org imports a manifest slightly older than another org who has already
         * imported. Disabled for now. See bz #769644.
         */
    // validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, metadata, force);
    // If any calls find conflicts we'll assemble them into one exception detailing all
    // the conflicts which occurred, so the caller can override them all at once
    // if desired:
    List<ImportConflictException> conflictExceptions = new LinkedList<>();
    File rules = importFiles.get(ImportFile.RULES_FILE.fileName());
    importRules(rules, metadata);
    importConsumerTypes(consumerTypes.listFiles());
    File distributorVersions = importFiles.get(ImportFile.DISTRIBUTOR_VERSIONS.fileName());
    if (distributorVersions != null) {
        importDistributorVersions(distributorVersions.listFiles());
    }
    File cdns = importFiles.get(ImportFile.CONTENT_DELIVERY_NETWORKS.fileName());
    if (cdns != null) {
        importContentDeliveryNetworks(cdns.listFiles());
    }
    // per user elements
    try {
        validateMetadata(ExporterMetadata.TYPE_PER_USER, owner, metadata, overrides);
    } catch (ImportConflictException e) {
        conflictExceptions.add(e);
    }
    ConsumerDTO consumer = null;
    try {
        Meta m = mapper.readValue(metadata, Meta.class);
        File upstreamFile = importFiles.get(ImportFile.UPSTREAM_CONSUMER.fileName());
        File[] dafiles = new File[0];
        if (upstreamFile != null) {
            dafiles = upstreamFile.listFiles();
        }
        consumer = importConsumer(owner, consumerFile, dafiles, overrides, m);
    } catch (ImportConflictException e) {
        conflictExceptions.add(e);
    }
    // At this point we're done checking for any potential conflicts:
    if (!conflictExceptions.isEmpty()) {
        log.error("Conflicts occurred during import that were not overridden:");
        for (ImportConflictException e : conflictExceptions) {
            log.error("{}", e.message().getConflicts());
        }
        throw new ImportConflictException(conflictExceptions);
    }
    if (consumer == null) {
        throw new IllegalStateException("No consumer found during import");
    }
    // If the consumer has no entitlements, this products directory will end up empty.
    // This also implies there will be no entitlements to import.
    Meta meta = mapper.readValue(metadata, Meta.class);
    List<Subscription> importSubs;
    if (importFiles.get(ImportFile.PRODUCTS.fileName()) != null) {
        ProductImporter importer = new ProductImporter();
        Set<ProductDTO> productsToImport = importProducts(importFiles.get(ImportFile.PRODUCTS.fileName()).listFiles(), importer, owner);
        importSubs = importEntitlements(owner, productsToImport, entitlements.listFiles(), consumer.getUuid(), meta);
    } else {
        log.warn("No products found to import, skipping product import.");
        log.warn("No entitlements in manifest, removing all subscriptions for owner.");
        importSubs = importEntitlements(owner, new HashSet<>(), new File[] {}, consumer.getUuid(), meta);
    }
    // Setup our import subscription adapter with the subscriptions imported:
    final String contentAccessMode = StringUtils.isEmpty(consumer.getContentAccessMode()) ? ContentAccessCertServiceAdapter.DEFAULT_CONTENT_ACCESS_MODE : consumer.getContentAccessMode();
    SubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(importSubs);
    OwnerServiceAdapter ownerAdapter = new OwnerServiceAdapter() {

        @Override
        public boolean isOwnerKeyValidForCreation(String ownerKey) {
            return true;
        }

        @Override
        public String getContentAccessMode(String ownerKey) {
            return contentAccessMode;
        }

        @Override
        public String getContentAccessModeList(String ownerKey) {
            return contentAccessMode;
        }
    };
    Refresher refresher = poolManager.getRefresher(subAdapter, ownerAdapter);
    refresher.add(owner);
    refresher.run();
    return importSubs;
}
Also used : Refresher(org.candlepin.controller.Refresher) LinkedList(java.util.LinkedList) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) Subscription(org.candlepin.model.dto.Subscription) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO) ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) SubscriptionServiceAdapter(org.candlepin.service.SubscriptionServiceAdapter) HashSet(java.util.HashSet) Transactional(com.google.inject.persist.Transactional)

Example 40 with Subscription

use of org.candlepin.model.dto.Subscription in project candlepin by candlepin.

the class Importer method importEntitlements.

protected List<Subscription> importEntitlements(Owner owner, Set<ProductDTO> products, File[] entitlements, String consumerUuid, Meta meta) throws IOException, SyncDataFormatException {
    log.debug("Importing entitlements for owner: {}", owner);
    EntitlementImporter importer = new EntitlementImporter(csCurator, cdnCurator, i18n, productCurator, entitlementCurator, translator);
    Map<String, ProductDTO> productsById = new HashMap<>();
    for (ProductDTO product : products) {
        log.debug("Adding product owned by {} to ID map", owner.getKey());
        // Note: This may actually be causing problems with subscriptions receiving the wrong
        // version of a product
        productsById.put(product.getId(), product);
    }
    List<Subscription> subscriptionsToImport = new ArrayList<>();
    for (File entitlement : entitlements) {
        Reader reader = null;
        try {
            log.debug("Import entitlement: {}", entitlement.getName());
            reader = new FileReader(entitlement);
            subscriptionsToImport.add(importer.importObject(mapper, reader, owner, productsById, consumerUuid, meta));
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
    }
    // Reconcile the subscriptions so they line up with pools we're tracking
    this.subscriptionReconciler.reconcile(owner, subscriptionsToImport);
    return subscriptionsToImport;
}
Also used : HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Reader(java.io.Reader) FileReader(java.io.FileReader) FileReader(java.io.FileReader) Subscription(org.candlepin.model.dto.Subscription) ManifestFile(org.candlepin.sync.file.ManifestFile) File(java.io.File) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO)

Aggregations

Subscription (org.candlepin.model.dto.Subscription)101 Test (org.junit.Test)77 Pool (org.candlepin.model.Pool)60 Product (org.candlepin.model.Product)48 SourceSubscription (org.candlepin.model.SourceSubscription)36 LinkedList (java.util.LinkedList)35 ArrayList (java.util.ArrayList)34 Owner (org.candlepin.model.Owner)33 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)27 HashMap (java.util.HashMap)20 HashSet (java.util.HashSet)18 Matchers.anyString (org.mockito.Matchers.anyString)18 Entitlement (org.candlepin.model.Entitlement)16 Date (java.util.Date)15 PoolType (org.candlepin.model.Pool.PoolType)13 ImportSubscriptionServiceAdapter (org.candlepin.service.impl.ImportSubscriptionServiceAdapter)13 List (java.util.List)11 OwnerServiceAdapter (org.candlepin.service.OwnerServiceAdapter)11 ConsumerType (org.candlepin.model.ConsumerType)10 ProductData (org.candlepin.model.dto.ProductData)9