Exporter { private void exportProducts(File baseDir, Consumer consumer) throws IOException { File productDir = new File(baseDir.getCanonicalPath(), "products"); productDir.mkdir(); Map<String, Product> products = new HashMap<>(); for (Entitlement entitlement : consumer.getEntitlements()) { Pool pool = entitlement.getPool(); for (Product providedProduct : productCurator.getPoolProvidedProductsCached(pool)) { products.put(providedProduct.getId(), providedProduct); } Product product = pool.getProduct(); products.put(product.getId(), product); Product derivedProduct = pool.getDerivedProduct(); if (derivedProduct != null) { products.put(derivedProduct.getId(), derivedProduct); } for (Product derivedProvidedProduct : productCurator .getPoolDerivedProvidedProductsCached(pool)) { products.put(derivedProvidedProduct.getId(), derivedProvidedProduct); } } for (Product product : products.values()) { String path = productDir.getCanonicalPath(); String productId = product.getId(); File file = new File(path, productId + ".json"); FileWriter writer = null; try { writer = new FileWriter(file); productExporter.export(mapper, writer, product); } finally { if (writer != null) { writer.close(); } } if (StringUtils.isNumeric(product.getId())) { Owner owner = ownerCurator.findOwnerById(consumer.getOwnerId()); CertificateInfo cert = productAdapter.getProductCertificate(owner.getKey(), product.getId()); if (cert != null) { file = new File(productDir.getCanonicalPath(), product.getId() + ".pem"); writer = new FileWriter(file); productCertExporter.export(writer, cert); writer.close(); } } } } @Inject  Exporter(ConsumerTypeCurator consumerTypeCurator, OwnerCurator ownerCurator, MetaExporter meta,
        ConsumerExporter consumerExporter, ConsumerTypeExporter consumerType,
        RulesExporter rules, EntitlementCertExporter entCert,
        EntitlementCertServiceAdapter entCertAdapter, ProductExporter productExporter,
        ProductServiceAdapter productAdapter, ProductCertExporter productCertExporter,
        EntitlementCurator entitlementCurator, EntitlementExporter entExporter,
        PKIUtility pki, Configuration config, ExportRules exportRules,
        PrincipalProvider principalProvider, DistributorVersionCurator distVerCurator,
        DistributorVersionExporter distVerExporter,
        CdnCurator cdnCurator,
        CdnExporter cdnExporter,
        ProductCurator productCurator,
        SyncUtils syncUtils, ExportExtensionAdapter extensionAdapter,
        ModelTranslator translator); File getFullExport(Consumer consumer, String cdnLabel, String webUrl,
        String apiUrl, Map<String, String> extensionData); File getEntitlementExport(Consumer consumer, Set<Long> serials);  }
@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); Pool pool = mock(Pool.class); Rules mrules = mock(Rules.class); Principal principal = mock(Principal.class); IdentityCertificate idcert = new IdentityCertificate(); Set<Entitlement> entitlements = new HashSet<>(); entitlements.add(ent); Owner owner = TestUtil.createOwner("Example-Corporation"); Product prod = TestUtil.createProduct("12345", "RHEL Product"); prod.setMultiplier(1L); prod.setCreated(new Date()); prod.setUpdated(new Date()); prod.setAttributes(Collections.<String, String>emptyMap()); Product prod1 = TestUtil.createProduct("MKT-prod", "RHEL Product"); prod1.setMultiplier(1L); prod1.setCreated(new Date()); prod1.setUpdated(new Date()); prod1.setAttributes(Collections.<String, String>emptyMap()); Product subProduct = TestUtil.createProduct("MKT-sub-prod", "Sub Product"); subProduct.setMultiplier(1L); subProduct.setCreated(new Date()); subProduct.setUpdated(new Date()); subProduct.setAttributes(Collections.<String, String>emptyMap()); Product subProvidedProduct = TestUtil.createProduct("332211", "Sub Product"); subProvidedProduct.setMultiplier(1L); subProvidedProduct.setCreated(new Date()); subProvidedProduct.setUpdated(new Date()); subProvidedProduct.setAttributes(Collections.<String, String>emptyMap()); ProductCertificate pcert = new ProductCertificate(); pcert.setKey("euh0876puhapodifbvj094"); pcert.setCert("hpj-08ha-w4gpoknpon*)&^%#"); pcert.setCreated(new Date()); pcert.setUpdated(new Date()); Set<Product> ppset = new HashSet<>(); ppset.add(prod); Set<Product> sppSet = new HashSet<>(); sppSet.add(subProvidedProduct); when(pool.getId()).thenReturn("MockedPoolId"); when(pool.getProvidedProducts()).thenReturn(ppset); when(pc.getPoolProvidedProductsCached(pool)).thenReturn(ppset); when(pool.getProduct()).thenReturn(prod1); when(pool.getDerivedProvidedProducts()).thenReturn(sppSet); when(pc.getPoolDerivedProvidedProductsCached(pool)).thenReturn(sppSet); when(pool.getDerivedProduct()).thenReturn(subProduct); 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.getProductCertificate(any(String.class), any(String.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()); CandlepinQuery cqmock = mock(CandlepinQuery.class); when(cqmock.iterator()).thenReturn(Arrays.asList(new ConsumerType("system")).iterator()); when(ctc.listAll()).thenReturn(cqmock); CandlepinQuery emptyIteratorMock = mock(CandlepinQuery.class); when(emptyIteratorMock.iterate()).thenReturn(new MockResultIterator(Arrays.asList().iterator())); when(emptyIteratorMock.iterator()).thenReturn(Arrays.asList().iterator()); when(cdnc.listAll()).thenReturn(emptyIteratorMock); when(ctc.listAll()).thenReturn(emptyIteratorMock); when(consumer.getOwnerId()).thenReturn(owner.getId()); when(oc.findOwnerById(eq(owner.getId()))).thenReturn(owner); 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); File export = e.getFullExport(consumer, null, null, null, new HashMap<>()); assertNotNull(export); verifyContent(export, "export/products/12345.pem", new VerifyProductCert("12345.pem")); assertFalse(verifyHasEntry(export, "export/products/MKT-prod.pem")); verifyContent(export, "export/products/332211.pem", new VerifyProductCert("332211.pem")); assertFalse(verifyHasEntry(export, "export/products/MKT-sub-prod.pem")); FileUtils.deleteDirectory(export.getParentFile()); assertTrue(new File("/tmp/consumer_export.zip").delete()); assertTrue(new File("/tmp/12345.pem").delete()); assertTrue(new File("/tmp/332211.pem").delete()); }