Search in sources :

Example 1 with OwnerServiceAdapter

use of org.candlepin.service.OwnerServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshPoolsWithRemovedSubscriptions.

@Test
public void testRefreshPoolsWithRemovedSubscriptions() {
    Product prod = this.createProduct(owner);
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    // This line is only present as a result of a (temporary?) fix for BZ 1452694. Once a
    // better fix has been implemented, the upstream pool ID can be removed.
    sub.setUpstreamPoolId("upstream_pool_id");
    subscriptions.add(sub);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    List<Pool> pools = poolCurator.listByOwnerAndProduct(owner, prod.getId());
    assertEquals(1, pools.size());
    Pool newPool = pools.get(0);
    String poolId = newPool.getId();
    // Now delete the subscription:
    subscriptions.remove(sub);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    assertNull("Pool not having subscription should have been deleted", poolCurator.find(poolId));
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Example 2 with OwnerServiceAdapter

use of org.candlepin.service.OwnerServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshPoolsWithChangedSubscriptions.

@Test
public void testRefreshPoolsWithChangedSubscriptions() {
    Product prod = this.createProduct(owner);
    Pool pool = createPool(owner, prod, 1000L, TestUtil.createDate(2009, 11, 30), TestUtil.createDate(2015, 11, 30));
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub);
    assertTrue(pool.getQuantity() < sub.getQuantity());
    assertTrue(pool.getStartDate() != sub.getStartDate());
    assertTrue(pool.getEndDate() != sub.getEndDate());
    pool.getSourceSubscription().setSubscriptionId(sub.getId());
    poolCurator.merge(pool);
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    pool = poolCurator.find(pool.getId());
    assertEquals(sub.getId(), pool.getSubscriptionId());
    assertEquals(sub.getQuantity(), pool.getQuantity());
    assertEquals(sub.getStartDate(), pool.getStartDate());
    assertEquals(sub.getEndDate(), pool.getEndDate());
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Example 3 with OwnerServiceAdapter

use of org.candlepin.service.OwnerServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshPoolsWithRemovedMasterPool.

// test covers scenario from bug 1012386
@Test
public void testRefreshPoolsWithRemovedMasterPool() {
    Product prod = this.createProduct(owner);
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    productCurator.merge(prod);
    config.setProperty(ConfigProperties.STANDALONE, "false");
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    List<Pool> pools = poolCurator.lookupBySubscriptionId(owner, sub.getId());
    assertEquals(2, pools.size());
    String bonusId = "";
    String masterId = "";
    for (Pool p : pools) {
        if (p.getSourceSubscription().getSubscriptionSubKey().equals("master")) {
            poolCurator.delete(p);
            masterId = p.getId();
        } else {
            bonusId = p.getId();
        }
    }
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    assertNull("Original Master Pool should be gone", poolCurator.find(masterId));
    assertNotNull("Bonus Pool should be the same", poolCurator.find(bonusId));
    // master pool should have been recreated
    pools = poolCurator.lookupBySubscriptionId(owner, sub.getId());
    assertEquals(2, pools.size());
    boolean newMaster = false;
    for (Pool p : pools) {
        if (p.getSourceSubscription().getSubscriptionSubKey().equals("master")) {
            newMaster = true;
        }
    }
    assertTrue(newMaster);
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Example 4 with OwnerServiceAdapter

use of org.candlepin.service.OwnerServiceAdapter in project candlepin by candlepin.

the class OwnerResourceTest method testRefreshPoolsWithRemovedBonusPool.

// test covers a corollary scenario from bug 1012386
@Test
public void testRefreshPoolsWithRemovedBonusPool() {
    Product prod = this.createProduct(owner);
    prod.setAttribute(Product.Attributes.VIRT_LIMIT, "4");
    productCurator.merge(prod);
    config.setProperty(ConfigProperties.STANDALONE, "false");
    List<Subscription> subscriptions = new LinkedList<>();
    ImportSubscriptionServiceAdapter subAdapter = new ImportSubscriptionServiceAdapter(subscriptions);
    OwnerServiceAdapter ownerAdapter = new DefaultOwnerServiceAdapter(this.ownerCurator, this.i18n);
    Subscription sub = TestUtil.createSubscription(owner, prod, new HashSet<>());
    sub.setId(Util.generateDbUUID());
    sub.setQuantity(2000L);
    sub.setStartDate(TestUtil.createDate(2010, 2, 9));
    sub.setEndDate(TestUtil.createDate(3000, 2, 9));
    sub.setModified(TestUtil.createDate(2010, 2, 12));
    subscriptions.add(sub);
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    List<Pool> pools = poolCurator.lookupBySubscriptionId(owner, sub.getId());
    assertEquals(2, pools.size());
    String bonusId = "";
    String masterId = "";
    for (Pool p : pools) {
        if (p.getSourceSubscription().getSubscriptionSubKey().equals("derived")) {
            poolCurator.delete(p);
            bonusId = p.getId();
        } else {
            masterId = p.getId();
        }
    }
    // Trigger the refresh:
    poolManager.getRefresher(subAdapter, ownerAdapter).add(owner).run();
    assertNull("Original bonus pool should be gone", poolCurator.find(bonusId));
    assertNotNull("Master pool should be the same", poolCurator.find(masterId));
    // master pool should have been recreated
    pools = poolCurator.lookupBySubscriptionId(owner, sub.getId());
    assertEquals(2, pools.size());
    boolean newBonus = false;
    for (Pool p : pools) {
        if (p.getSourceSubscription().getSubscriptionSubKey().equals("derived")) {
            newBonus = true;
        }
    }
    assertTrue(newBonus);
}
Also used : ImportSubscriptionServiceAdapter(org.candlepin.service.impl.ImportSubscriptionServiceAdapter) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Product(org.candlepin.model.Product) Pool(org.candlepin.model.Pool) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.candlepin.model.dto.Subscription) LinkedList(java.util.LinkedList) DefaultOwnerServiceAdapter(org.candlepin.service.impl.DefaultOwnerServiceAdapter) Test(org.junit.Test)

Example 5 with OwnerServiceAdapter

use of org.candlepin.service.OwnerServiceAdapter 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)

Aggregations

LinkedList (java.util.LinkedList)9 Subscription (org.candlepin.model.dto.Subscription)9 OwnerServiceAdapter (org.candlepin.service.OwnerServiceAdapter)9 ImportSubscriptionServiceAdapter (org.candlepin.service.impl.ImportSubscriptionServiceAdapter)9 Product (org.candlepin.model.Product)8 DefaultOwnerServiceAdapter (org.candlepin.service.impl.DefaultOwnerServiceAdapter)8 Pool (org.candlepin.model.Pool)7 Test (org.junit.Test)7 Matchers.anyString (org.mockito.Matchers.anyString)3 Owner (org.candlepin.model.Owner)2 Transactional (com.google.inject.persist.Transactional)1 File (java.io.File)1 HashSet (java.util.HashSet)1 Refresher (org.candlepin.controller.Refresher)1 OwnerDTO (org.candlepin.dto.api.v1.OwnerDTO)1 ConsumerDTO (org.candlepin.dto.manifest.v1.ConsumerDTO)1 ProductDTO (org.candlepin.dto.manifest.v1.ProductDTO)1 Consumer (org.candlepin.model.Consumer)1 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)1 SubscriptionServiceAdapter (org.candlepin.service.SubscriptionServiceAdapter)1