Search in sources :

Example 6 with ConsumerDTO

use of org.candlepin.dto.manifest.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithNullUuidOnConsumerShouldThrowException.

@Test(expected = ImporterException.class)
public void importConsumerWithNullUuidOnConsumerShouldThrowException() throws ImporterException {
    Owner owner = new Owner();
    ConsumerDTO consumer = new ConsumerDTO();
    consumer.setUuid(null);
    importer.store(owner, consumer, new ConflictOverrides(), null);
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) Test(org.junit.Test)

Example 7 with ConsumerDTO

use of org.candlepin.dto.manifest.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importConsumerWithMismatchedUuidShouldNotThrowExceptionIfForced.

@Test
public void importConsumerWithMismatchedUuidShouldNotThrowExceptionIfForced() throws ImporterException {
    Owner owner = mock(Owner.class);
    OwnerDTO ownerDTO = mock(OwnerDTO.class);
    ConsumerDTO consumer = mock(ConsumerDTO.class);
    ConsumerTypeDTO type = mock(ConsumerTypeDTO.class);
    when(owner.getUpstreamUuid()).thenReturn("another-test-uuid");
    when(consumer.getUuid()).thenReturn("test-uuid");
    when(consumer.getOwner()).thenReturn(ownerDTO);
    when(consumer.getType()).thenReturn(type);
    IdentityCertificate idCert = new IdentityCertificate();
    idCert.setSerial(new CertificateSerial());
    importer.store(owner, consumer, new ConflictOverrides(Importer.Conflict.DISTRIBUTOR_CONFLICT), idCert);
    // now verify that the owner has the upstream consumer set
    ArgumentCaptor<UpstreamConsumer> arg = ArgumentCaptor.forClass(UpstreamConsumer.class);
    verify(owner).setUpstreamConsumer(arg.capture());
    assertEquals("test-uuid", arg.getValue().getUuid());
    verify(curator).merge(owner);
}
Also used : Owner(org.candlepin.model.Owner) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) CertificateSerial(org.candlepin.model.CertificateSerial) UpstreamConsumer(org.candlepin.model.UpstreamConsumer) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 8 with ConsumerDTO

use of org.candlepin.dto.manifest.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerImporterTest method importHandlesUnknownPropertiesGracefully.

@Test
public void importHandlesUnknownPropertiesGracefully() throws Exception {
    // Override default config to error out on unknown properties:
    Map<String, String> configProps = new HashMap<>();
    configProps.put(ConfigProperties.FAIL_ON_UNKNOWN_IMPORT_PROPERTIES, "false");
    mapper = TestSyncUtils.getTestSyncUtils(new MapConfiguration(configProps));
    ConsumerDTO consumer = importer.createObject(mapper, new StringReader("{\"uuid\":\"test-uuid\", \"unknown\":\"notreal\"}"));
    assertEquals("test-uuid", consumer.getUuid());
}
Also used : HashMap(java.util.HashMap) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) MapConfiguration(org.candlepin.common.config.MapConfiguration) StringReader(java.io.StringReader) Test(org.junit.Test)

Example 9 with ConsumerDTO

use of org.candlepin.dto.manifest.v1.ConsumerDTO 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 10 with ConsumerDTO

use of org.candlepin.dto.manifest.v1.ConsumerDTO in project candlepin by candlepin.

the class ConsumerTest method ensureUpdatedDateChangesOnUpdate.

@Test
public void ensureUpdatedDateChangesOnUpdate() throws Exception {
    Date beforeUpdateDate = consumer.getUpdated();
    // Create a new consumer, can't re-use reference to the old:
    ConsumerDTO newConsumer = new ConsumerDTO();
    newConsumer.setUuid(consumer.getUuid());
    newConsumer.setFact("FACT", "FACT_VALUE");
    consumerResource.updateConsumer(consumer.getUuid(), newConsumer, mock(Principal.class));
    Consumer lookedUp = consumerCurator.find(consumer.getId());
    Date lookedUpDate = lookedUp.getUpdated();
    assertEquals("FACT_VALUE", lookedUp.getFact("FACT"));
    assertTrue("Last updated date was not changed.", beforeUpdateDate.before(lookedUpDate));
}
Also used : ConsumerDTO(org.candlepin.dto.api.v1.ConsumerDTO) Date(java.util.Date) ConsumerPrincipal(org.candlepin.auth.ConsumerPrincipal) Principal(org.candlepin.auth.Principal) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)70 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)64 Consumer (org.candlepin.model.Consumer)37 UserPrincipal (org.candlepin.auth.UserPrincipal)19 Owner (org.candlepin.model.Owner)19 ConsumerType (org.candlepin.model.ConsumerType)16 HashSet (java.util.HashSet)15 ConsumerDTO (org.candlepin.dto.manifest.v1.ConsumerDTO)15 Principal (org.candlepin.auth.Principal)14 NoAuthPrincipal (org.candlepin.auth.NoAuthPrincipal)13 TrustedUserPrincipal (org.candlepin.auth.TrustedUserPrincipal)12 Set (java.util.Set)10 ArrayList (java.util.ArrayList)8 Date (java.util.Date)8 TestUtil.createConsumerDTO (org.candlepin.test.TestUtil.createConsumerDTO)8 ConsumerTypeDTO (org.candlepin.dto.api.v1.ConsumerTypeDTO)7 ConsumerTypeDTO (org.candlepin.dto.manifest.v1.ConsumerTypeDTO)7 OwnerDTO (org.candlepin.dto.manifest.v1.OwnerDTO)7 UpstreamConsumer (org.candlepin.model.UpstreamConsumer)7 Entitlement (org.candlepin.model.Entitlement)5