use of org.candlepin.dto.rules.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);
}
use of org.candlepin.dto.rules.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);
}
use of org.candlepin.dto.rules.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());
}
use of org.candlepin.dto.rules.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;
}
use of org.candlepin.dto.rules.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));
}
Aggregations