Search in sources :

Example 21 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class InstalledProductStatusCalculatorTest method mockEntitlement.

private Entitlement mockEntitlement(Owner owner, Consumer consumer, Product product, DateRange range, Product... providedProducts) {
    Set<Product> provided = new HashSet<>();
    for (Product pp : providedProducts) {
        provided.add(pp);
    }
    final Pool p = new Pool(owner, product, provided, new Long(1000), range.getStartDate(), range.getEndDate(), "1000", "1000", "1000");
    p.setId("" + lastPoolId++);
    Entitlement e = new Entitlement(p, consumer, owner, 1);
    when(poolCurator.provides(p, product.getId())).thenReturn(true);
    for (Product pp : providedProducts) {
        when(poolCurator.provides(p, pp.getId())).thenReturn(true);
    }
    Random gen = new Random();
    int id = gen.nextInt(Integer.MAX_VALUE);
    e.setId(String.valueOf(id));
    return e;
}
Also used : Random(java.util.Random) Product(org.candlepin.model.Product) ConsumerInstalledProduct(org.candlepin.model.ConsumerInstalledProduct) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) HashSet(java.util.HashSet)

Example 22 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class OwnerResourceTest method createEntitlementWithQ.

/**
 * @param pool
 * @param owner
 * @param consumer
 * @return
 */
private Entitlement createEntitlementWithQ(Pool pool, Owner owner, Consumer consumer, int quantity, String date) throws ParseException {
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
    Entitlement e1 = createEntitlement(owner, consumer, pool, null);
    e1.setQuantity(quantity);
    pool.getEntitlements().add(e1);
    this.entitlementCurator.create(e1);
    e1.getPool().setConsumed(e1.getPool().getConsumed() + quantity);
    this.poolCurator.merge(e1.getPool());
    e1.setCreated(dateFormat.parse(date));
    this.entitlementCurator.merge(e1);
    return e1;
}
Also used : Entitlement(org.candlepin.model.Entitlement) SimpleDateFormat(java.text.SimpleDateFormat)

Example 23 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class EntitlementImporterTest method fullImport.

@Test
public void fullImport() throws Exception {
    Product parentProduct = TestUtil.createProduct();
    Product derivedProduct = TestUtil.createProduct();
    Product provided1 = TestUtil.createProduct();
    Set<Product> providedProducts = new HashSet<>();
    providedProducts.add(new Product(provided1));
    Product derivedProvided1 = TestUtil.createProduct();
    Set<Product> derivedProvidedProducts = new HashSet<>();
    derivedProvidedProducts.add(new Product(derivedProvided1));
    Pool pool = TestUtil.createPool(owner, parentProduct, new HashSet<>(), derivedProduct, new HashSet<>(), 3);
    pool.setProvidedProducts(providedProducts);
    pool.setDerivedProvidedProducts(derivedProvidedProducts);
    Entitlement ent = TestUtil.createEntitlement(owner, consumer, pool, cert);
    ent.setQuantity(3);
    EntitlementDTO dtoEnt = this.translator.translate(ent, EntitlementDTO.class);
    when(om.readValue(reader, EntitlementDTO.class)).thenReturn(dtoEnt);
    // Create our expected products
    Map<String, ProductDTO> productsById = buildProductCache(parentProduct, provided1, derivedProduct, derivedProvided1);
    Subscription sub = importer.importObject(om, reader, owner, productsById, consumerDTO.getUuid(), meta);
    assertEquals(pool.getId(), sub.getUpstreamPoolId());
    assertEquals(consumer.getUuid(), sub.getUpstreamConsumerId());
    assertEquals(ent.getId(), sub.getUpstreamEntitlementId());
    assertEquals(owner, sub.getOwner());
    assertEquals(ent.getStartDate(), sub.getStartDate());
    assertEquals(ent.getEndDate(), sub.getEndDate());
    assertEquals(pool.getAccountNumber(), sub.getAccountNumber());
    assertEquals(pool.getContractNumber(), sub.getContractNumber());
    assertEquals(pool.getOrderNumber(), sub.getOrderNumber());
    assertEquals(ent.getQuantity().intValue(), sub.getQuantity().intValue());
    assertEquals(parentProduct.toDTO(), sub.getProduct());
    assertEquals(providedProducts.size(), sub.getProvidedProducts().size());
    assertEquals(provided1.getId(), sub.getProvidedProducts().iterator().next().getId());
    assertEquals(derivedProduct.toDTO(), sub.getDerivedProduct());
    assertEquals(1, sub.getDerivedProvidedProducts().size());
    assertEquals(derivedProvided1.getId(), sub.getDerivedProvidedProducts().iterator().next().getId());
    assertNotNull(sub.getCertificate());
    CertificateSerial serial = sub.getCertificate().getSerial();
    assertEquals(cert.getSerial().isCollected(), serial.isCollected());
    assertEquals(cert.getSerial().getExpiration(), serial.getExpiration());
    assertEquals(cert.getSerial().getCreated(), serial.getCreated());
    assertEquals(cert.getSerial().getUpdated(), serial.getUpdated());
    assertEquals(sub.getCdn().getLabel(), meta.getCdnLabel());
}
Also used : EntitlementDTO(org.candlepin.dto.manifest.v1.EntitlementDTO) Product(org.candlepin.model.Product) CertificateSerial(org.candlepin.model.CertificateSerial) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) Subscription(org.candlepin.model.dto.Subscription) ProductDTO(org.candlepin.dto.manifest.v1.ProductDTO) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 24 with Entitlement

use of org.candlepin.model.Entitlement 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());
}
Also used : Owner(org.candlepin.model.Owner) HashMap(java.util.HashMap) ExporterMetadataCurator(org.candlepin.model.ExporterMetadataCurator) Product(org.candlepin.model.Product) OwnerCurator(org.candlepin.model.OwnerCurator) ConsumerDTO(org.candlepin.dto.manifest.v1.ConsumerDTO) OwnerServiceAdapter(org.candlepin.service.OwnerServiceAdapter) Pool(org.candlepin.model.Pool) ConsumerType(org.candlepin.model.ConsumerType) Subscription(org.candlepin.model.dto.Subscription) SubscriptionServiceAdapter(org.candlepin.service.SubscriptionServiceAdapter) Refresher(org.candlepin.controller.Refresher) PoolManager(org.candlepin.controller.PoolManager) Date(java.util.Date) ConsumerTypeDTO(org.candlepin.dto.manifest.v1.ConsumerTypeDTO) OwnerDTO(org.candlepin.dto.manifest.v1.OwnerDTO) Entitlement(org.candlepin.model.Entitlement) ImportFile(org.candlepin.sync.Importer.ImportFile) File(java.io.File) Test(org.junit.Test)

Example 25 with Entitlement

use of org.candlepin.model.Entitlement in project candlepin by candlepin.

the class ExporterTest method doNotExportDirtyEntitlements.

@Test(expected = ExportCreationException.class)
public void doNotExportDirtyEntitlements() throws Exception {
    config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
    Consumer consumer = mock(Consumer.class);
    Entitlement ent = mock(Entitlement.class);
    Principal principal = mock(Principal.class);
    IdentityCertificate idcert = new IdentityCertificate();
    List<Entitlement> entitlements = new ArrayList<>();
    entitlements.add(ent);
    when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes());
    when(pprov.get()).thenReturn(principal);
    when(principal.getUsername()).thenReturn("testUser");
    when(ec.listByConsumer(consumer)).thenReturn(entitlements);
    when(ent.isDirty()).thenReturn(true);
    idcert.setSerial(new CertificateSerial(10L, new Date()));
    idcert.setKey("euh0876puhapodifbvj094");
    idcert.setCert("hpj-08ha-w4gpoknpon*)&^%#");
    idcert.setCreated(new Date());
    idcert.setUpdated(new Date());
    when(consumer.getIdCert()).thenReturn(idcert);
    KeyPair keyPair = createKeyPair();
    when(consumer.getKeyPair()).thenReturn(keyPair);
    when(pki.getPemEncoded(keyPair.getPrivateKey())).thenReturn("privateKey".getBytes());
    when(pki.getPemEncoded(keyPair.getPublicKey())).thenReturn("publicKey".getBytes());
    CandlepinQuery cqmock = mock(CandlepinQuery.class);
    when(cqmock.iterator()).thenReturn(Arrays.asList(new ConsumerType("system")).iterator());
    when(ctc.listAll()).thenReturn(cqmock);
    Exporter e = new Exporter(ctc, oc, me, ce, cte, re, ece, ecsa, pe, psa, pce, ec, ee, pki, config, exportRules, pprov, dvc, dve, cdnc, cdne, pc, su, exportExtensionAdapter, translator);
    e.getFullExport(consumer);
}
Also used : KeyPair(org.candlepin.model.KeyPair) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) CertificateSerial(org.candlepin.model.CertificateSerial) CandlepinQuery(org.candlepin.model.CandlepinQuery) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Entitlement(org.candlepin.model.Entitlement) ConsumerType(org.candlepin.model.ConsumerType) Principal(org.candlepin.auth.Principal) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Aggregations

Entitlement (org.candlepin.model.Entitlement)301 Test (org.junit.Test)201 Consumer (org.candlepin.model.Consumer)164 Pool (org.candlepin.model.Pool)125 LinkedList (java.util.LinkedList)84 Product (org.candlepin.model.Product)68 Date (java.util.Date)62 ArrayList (java.util.ArrayList)61 HashSet (java.util.HashSet)59 HashMap (java.util.HashMap)55 Owner (org.candlepin.model.Owner)44 PoolQuantity (org.candlepin.model.PoolQuantity)35 ConsumerType (org.candlepin.model.ConsumerType)34 List (java.util.List)30 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)24 Matchers.anyString (org.mockito.Matchers.anyString)17 Set (java.util.Set)16 Subscription (org.candlepin.model.dto.Subscription)16 ApiOperation (io.swagger.annotations.ApiOperation)15 ApiResponses (io.swagger.annotations.ApiResponses)15