use of org.candlepin.model.ExporterMetadataCurator in project candlepin by candlepin.
the class ImporterTest method testReturnsSubscriptionsFromManifest.
@Test
public void testReturnsSubscriptionsFromManifest() throws IOException, ImporterException {
Owner owner = new Owner("admin", "Admin Owner");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
when(emc.lookupByTypeAndOwner("per_user", owner)).thenReturn(null);
ConsumerType stype = new ConsumerType(ConsumerTypeEnum.SYSTEM);
stype.setId("test-ctype");
when(consumerTypeCurator.lookupByLabel(eq("system"))).thenReturn(stype);
when(consumerTypeCurator.find(eq(stype.getId()))).thenReturn(stype);
OwnerCurator oc = mock(OwnerCurator.class);
when(oc.lookupWithUpstreamUuid(any(String.class))).thenReturn(null);
PoolManager pm = mock(PoolManager.class);
Refresher refresher = mock(Refresher.class);
when(pm.getRefresher(any(SubscriptionServiceAdapter.class), any(OwnerServiceAdapter.class))).thenReturn(refresher);
Map<String, File> importFiles = new HashMap<>();
File ruleDir = mock(File.class);
File[] rulesFiles = createMockJsFile(mockJsPath);
when(ruleDir.listFiles()).thenReturn(rulesFiles);
File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix");
importFiles.put(ImportFile.META.fileName(), actualmeta);
ConsumerDTO consumerDTO = new ConsumerDTO();
consumerDTO.setUuid("eb5e04bf-be27-44cf-abe3-0c0b1edd523e");
consumerDTO.setName("mymachine");
ConsumerTypeDTO typeDTO = new ConsumerTypeDTO();
typeDTO.setLabel("candlepin");
typeDTO.setManifest(true);
consumerDTO.setType(typeDTO);
consumerDTO.setUrlWeb("foo.example.com/subscription");
consumerDTO.setUrlApi("/candlepin");
consumerDTO.setContentAccessMode("");
OwnerDTO ownerDTO = new OwnerDTO();
ownerDTO.setKey("admin");
ownerDTO.setDisplayName("Admin Owner");
consumerDTO.setOwner(ownerDTO);
ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.CANDLEPIN);
ctype.setId("test-ctype");
when(consumerTypeCurator.lookupByLabel(eq("candlepin"))).thenReturn(ctype);
when(consumerTypeCurator.find(eq(ctype.getId()))).thenReturn(ctype);
File consumerFile = new File(folder.getRoot(), "consumer.json");
mapper.writeValue(consumerFile, consumerDTO);
importFiles.put(ImportFile.CONSUMER.fileName(), consumerFile);
File cTypes = mock(File.class);
when(cTypes.listFiles()).thenReturn(new File[] {});
importFiles.put(ImportFile.CONSUMER_TYPE.fileName(), cTypes);
Product prod = new Product("prodId", "prodTest", null);
prod.setDependentProductIds(null);
File prodFile = new File(folder.getRoot(), "product.json");
mapper.writeValue(prodFile, prod);
File products = mock(File.class);
when(products.listFiles()).thenReturn(new File[] { prodFile });
importFiles.put(ImportFile.PRODUCTS.fileName(), products);
Entitlement ent = new Entitlement();
Pool pool = new Pool();
pool.setProduct(prod);
ent.setPool(pool);
ent.setQuantity(2);
File entFile = new File(folder.getRoot(), "entitlement.json");
mapper.writeValue(entFile, ent);
File entitlements = mock(File.class);
when(entitlements.listFiles()).thenReturn(new File[] { entFile });
importFiles.put(ImportFile.ENTITLEMENTS.fileName(), entitlements);
RulesImporter ri = mock(RulesImporter.class);
importFiles.put(ImportFile.RULES_FILE.fileName(), rulesFiles[0]);
ConflictOverrides co = mock(ConflictOverrides.class);
Importer i = new Importer(consumerTypeCurator, pc, ri, oc, null, null, pm, null, config, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
List<Subscription> subscriptions = i.importObjects(owner, importFiles, co);
assertEquals(1, subscriptions.size());
assertEquals("prodId", subscriptions.get(0).getProduct().getId());
assertEquals(2, subscriptions.get(0).getQuantity().longValue());
}
use of org.candlepin.model.ExporterMetadataCurator in project candlepin by candlepin.
the class ImporterTest method expectOwner.
@Test(expected = ImporterException.class)
public void expectOwner() throws ImporterException, IOException {
File actualmeta = createFile("meta.json", "0.0.3", new Date(), "test_user", "prefix");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
when(emc.lookupByTypeAndOwner(ExporterMetadata.TYPE_PER_USER, null)).thenReturn(null);
Importer i = new Importer(null, null, null, null, null, null, null, null, null, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
// null Type should cause exception
i.validateMetadata(ExporterMetadata.TYPE_PER_USER, null, actualmeta, new ConflictOverrides());
verify(emc, never()).create(any(ExporterMetadata.class));
}
use of org.candlepin.model.ExporterMetadataCurator in project candlepin by candlepin.
the class ImporterTest method newerImport.
@Test
public void newerImport() throws Exception {
// this tests bz #790751
Date importDate = getDateBeforeDays(10);
// actualmeta is the mock for the import itself
File actualmeta = createFile("meta.json", "0.0.3", importDate, "test_user", "prefix");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
// em is the mock for lastrun (i.e., the most recent import in CP)
ExporterMetadata em = new ExporterMetadata();
em.setExported(getDateBeforeDays(30));
em.setId("42");
em.setType(ExporterMetadata.TYPE_SYSTEM);
when(emc.lookupByType(ExporterMetadata.TYPE_SYSTEM)).thenReturn(em);
Importer i = new Importer(null, null, null, null, null, null, null, null, null, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
i.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides());
assertEquals(importDate, em.getExported());
}
use of org.candlepin.model.ExporterMetadataCurator in project candlepin by candlepin.
the class ImporterTest method oldImport.
@Test
public void oldImport() throws Exception {
// actualmeta is the mock for the import itself
File actualmeta = createFile("meta.json", "0.0.3", getDateBeforeDays(10), "test_user", "prefix");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
// emc is the mock for lastrun (i.e., the most recent import in CP)
ExporterMetadata em = new ExporterMetadata();
em.setExported(getDateBeforeDays(3));
em.setId("42");
em.setType(ExporterMetadata.TYPE_SYSTEM);
when(emc.lookupByType(ExporterMetadata.TYPE_SYSTEM)).thenReturn(em);
Importer i = new Importer(null, null, null, null, null, null, null, null, null, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
try {
i.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actualmeta, new ConflictOverrides());
fail();
} catch (ImportConflictException e) {
assertFalse(e.message().getConflicts().isEmpty());
assertEquals(1, e.message().getConflicts().size());
assertTrue(e.message().getConflicts().contains(Importer.Conflict.MANIFEST_OLD));
}
}
use of org.candlepin.model.ExporterMetadataCurator in project candlepin by candlepin.
the class ImporterTest method validateMetaJson.
@Test
public void validateMetaJson() throws Exception {
/* read file
* read in version
* read in created date
* make sure created date is XYZ
* make sure version is > ABC
*/
Date now = new Date();
File file = createFile("meta", "0.0.3", now, "test_user", "prefix");
File actual = createFile("meta.json", "0.0.3", now, "test_user", "prefix");
ExporterMetadataCurator emc = mock(ExporterMetadataCurator.class);
ExporterMetadata em = new ExporterMetadata();
Date daybefore = getDateBeforeDays(1);
em.setExported(daybefore);
em.setId("42");
em.setType(ExporterMetadata.TYPE_SYSTEM);
when(emc.lookupByType(ExporterMetadata.TYPE_SYSTEM)).thenReturn(em);
Importer i = new Importer(null, null, null, null, null, null, null, null, null, emc, null, null, i18n, null, null, su, null, this.mockSubReconciler, this.ec, this.translator);
i.validateMetadata(ExporterMetadata.TYPE_SYSTEM, null, actual, new ConflictOverrides());
Meta fileMeta = mapper.readValue(file, Meta.class);
Meta actualMeta = mapper.readValue(actual, Meta.class);
assertEquals(fileMeta.getPrincipalName(), actualMeta.getPrincipalName());
assertEquals(fileMeta.getCreated().getTime(), actualMeta.getCreated().getTime());
assertEquals(fileMeta.getWebAppPrefix(), actualMeta.getWebAppPrefix());
assertTrue(file.delete());
assertTrue(actual.delete());
assertTrue(daybefore.compareTo(em.getExported()) < 0);
}
Aggregations