@Before public void createObjects() { beginTransaction(); prod1 = TestUtil.createProduct(); prod2 = TestUtil.createProduct(); productCurator.create(prod1); productCurator.create(prod2); owner = new Owner("testowner"); ownerCurator.create(owner); Set<ProvidedProduct> providedProducts = new HashSet<ProvidedProduct>(); ProvidedProduct providedProduct = new ProvidedProduct(prod2.getId(), prod2.getName()); providedProducts.add(providedProduct); pool = TestUtil.createPool(owner, prod1, providedProducts, 1000); providedProduct.setPool(pool); poolCurator.create(pool); owner = pool.getOwner(); consumer = TestUtil.createConsumer(owner); productCurator.create(prod1); poolCurator.create(pool); consumerTypeCurator.create(consumer.getType()); consumerCurator.create(consumer); commitTransaction(); }
private void exportProducts(File baseDir, Consumer consumer) throws IOException { File productDir = new File(baseDir.getCanonicalPath(), "products"); productDir.mkdir(); Map<String, Product> products = new HashMap<String, Product>(); for (Entitlement entitlement : consumer.getEntitlements()) { for (ProvidedProduct providedProduct : entitlement.getPool().getProvidedProducts()) { // Don't want to call the adapter if not needed, it can be expensive. if (!products.containsKey(providedProduct.getProductId())) { products.put( providedProduct.getProductId(), productAdapter.getProductById(providedProduct.getProductId())); } } // Don't forget the 'main' product! String productId = entitlement.getPool().getProductId(); if (!products.containsKey(productId)) { products.put(productId, productAdapter.getProductById(productId)); } } for (Product product : products.values()) { String path = productDir.getCanonicalPath(); String productId = product.getId(); File file = new File(path, productId + ".json"); FileWriter writer = new FileWriter(file); productExporter.export(mapper, writer, product); writer.close(); // Real products have a numeric id. if (StringUtils.isNumeric(product.getId())) { ProductCertificate cert = productAdapter.getProductCertificate(product); // XXX: not all product adapters implement getProductCertificate, // so just skip over this if we get null back // XXX: need to decide if the cert should always be in the export, or never. if (cert != null) { file = new File(productDir.getCanonicalPath(), product.getId() + ".pem"); writer = new FileWriter(file); productCertExporter.export(writer, cert); writer.close(); } } } }
private Set<Product> getProvidedProducts(Pool pool, Subscription sub) { Set<Product> providedProducts = new HashSet<Product>(); // TODO: eliminate the use of subscription here by looking up products in a batch // somehow, and we can eliminate all use of subscriptions during bind. if (sub != null) { // need to use the sub provided products if creating an // entitlement for derived pool who's sub specifies a // sub product. boolean derived = pool.hasAttribute("pool_derived"); providedProducts = derived && sub.getDerivedProduct() != null ? sub.getDerivedProvidedProducts() : sub.getProvidedProducts(); } else { // If this pool doesn't have a subscription associated with it, we need to // lookup all the Product objects manually: for (ProvidedProduct providedProduct : pool.getProvidedProducts()) { providedProducts.add(productAdapter.getProductById(providedProduct.getProductId())); } } return providedProducts; }
@Test public void testLookupPoolsProvidingProduct() { Product parentProduct = TestUtil.createProduct("1", "product-1"); Product childProduct = TestUtil.createProduct("2", "product-2"); productCurator.create(childProduct); productCurator.create(parentProduct); Set<ProvidedProduct> providedProducts = new HashSet<ProvidedProduct>(); ProvidedProduct providedProduct = new ProvidedProduct(childProduct.getId(), childProduct.getName()); providedProducts.add(providedProduct); Pool pool = TestUtil.createPool(owner, parentProduct, providedProducts, 5); providedProduct.setPool(pool); poolCurator.create(pool); List<Pool> results = poolCurator.listAvailableEntitlementPools( null, owner, childProduct.getId(), null, false, false); assertEquals(1, results.size()); assertEquals(pool.getId(), results.get(0).getId()); }
private Entitlement setupListModifyingEntitlement() { Date startDate = createDate(2010, 1, 1); Date endDate = createDate(2011, 1, 1); Product parentModifierProd = createModifyingProduct(parentProduct.getId()); Product childModifierProd = createModifyingProduct(providedProduct1.getId()); Pool testPool = createPoolAndSub(owner, parentModifierProd, 1L, startDate, endDate); // Add some provided products to this pool which also modify something: ProvidedProduct p1 = new ProvidedProduct(childModifierProd.getId(), childModifierProd.getName()); p1.setPool(testPool); testPool.addProvidedProduct(p1); poolCurator.create(testPool); EntitlementCertificate cert = createEntitlementCertificate("key", "certificate"); Entitlement ent = createEntitlement(owner, consumer, testPool, cert); entitlementCurator.create(ent); return ent; }
@SuppressWarnings("unchecked") @Test public void exportProducts() throws Exception { config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/"); Consumer consumer = mock(Consumer.class); Entitlement ent = mock(Entitlement.class); ProvidedProduct pp = mock(ProvidedProduct.class); Pool pool = mock(Pool.class); Rules mrules = mock(Rules.class); Principal principal = mock(Principal.class); IdentityCertificate idcert = new IdentityCertificate(); Set<ProvidedProduct> ppset = new HashSet<ProvidedProduct>(); ppset.add(pp); Set<Entitlement> entitlements = new HashSet<Entitlement>(); entitlements.add(ent); Product prod = new Product("12345", "RHEL Product"); prod.setMultiplier(1L); prod.setCreated(new Date()); prod.setUpdated(new Date()); prod.setHref("http://localhost"); prod.setAttributes(Collections.EMPTY_SET); Product prod1 = new Product("MKT-prod", "RHEL Product"); prod1.setMultiplier(1L); prod1.setCreated(new Date()); prod1.setUpdated(new Date()); prod1.setHref("http://localhost"); prod1.setAttributes(Collections.EMPTY_SET); ProductCertificate pcert = new ProductCertificate(); pcert.setKey("euh0876puhapodifbvj094"); pcert.setCert("hpj-08ha-w4gpoknpon*)&^%#"); pcert.setCreated(new Date()); pcert.setUpdated(new Date()); when(pp.getProductId()).thenReturn("12345"); when(pool.getProvidedProducts()).thenReturn(ppset); when(pool.getProductId()).thenReturn("MKT-prod"); when(ent.getPool()).thenReturn(pool); when(mrules.getRules()).thenReturn("foobar"); when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes()); when(rc.getRules()).thenReturn(mrules); when(consumer.getEntitlements()).thenReturn(entitlements); when(psa.getProductById("12345")).thenReturn(prod); when(psa.getProductById("MKT-prod")).thenReturn(prod1); when(psa.getProductCertificate(any(Product.class))).thenReturn(pcert); when(pprov.get()).thenReturn(principal); when(principal.getUsername()).thenReturn("testUser"); 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()); // FINALLY test this badboy Exporter e = new Exporter( ctc, me, ce, cte, re, ece, ecsa, pe, psa, pce, ec, ee, pki, config, exportRules, pprov); File export = e.getFullExport(consumer); // VERIFY assertNotNull(export); verifyContent(export, "export/products/12345.pem", new VerifyProductCert("12345.pem")); assertFalse(verifyHasEntry(export, "export/products/MKT-prod.pem")); FileUtils.deleteDirectory(export.getParentFile()); assertTrue(new File("/tmp/consumer_export.zip").delete()); assertTrue(new File("/tmp/12345.pem").delete()); }