Search in sources :

Example 6 with MockResultIterator

use of org.candlepin.test.MockResultIterator in project candlepin by candlepin.

the class JobResourceTest method getStatusesByUuid.

@Test
public void getStatusesByUuid() {
    List<JobStatus> statuses = new ArrayList<>();
    JobStatus status = new JobStatus();
    statuses.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(statuses);
    when(query.iterate()).thenReturn(new MockResultIterator(statuses.iterator()));
    when(query.iterate(anyInt(), anyBoolean())).thenReturn(new MockResultIterator(statuses.iterator()));
    when(jobCurator.findByConsumerUuid(eq("abcd"))).thenReturn(query);
    this.mockCPQueryTransform(query);
    Collection<JobStatusDTO> real = jobResource.getStatuses(null, "abcd", null).list();
    assertNotNull(real);
    assertEquals(1, real.size());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) ArrayList(java.util.ArrayList) TransformedCandlepinQuery(org.candlepin.model.TransformedCandlepinQuery) CandlepinQuery(org.candlepin.model.CandlepinQuery) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 7 with MockResultIterator

use of org.candlepin.test.MockResultIterator in project candlepin by candlepin.

the class JobResourceTest method getStatusesByOwner.

@Test
public void getStatusesByOwner() {
    List<JobStatus> statuses = new ArrayList<>();
    JobStatus status = new JobStatus();
    statuses.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(statuses);
    when(query.iterate()).thenReturn(new MockResultIterator(statuses.iterator()));
    when(query.iterate(anyInt(), anyBoolean())).thenReturn(new MockResultIterator(statuses.iterator()));
    when(jobCurator.findByOwnerKey(eq("admin"))).thenReturn(query);
    this.mockCPQueryTransform(query);
    Collection<JobStatusDTO> real = jobResource.getStatuses("admin", null, null).list();
    assertNotNull(real);
    assertEquals(1, real.size());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) ArrayList(java.util.ArrayList) TransformedCandlepinQuery(org.candlepin.model.TransformedCandlepinQuery) CandlepinQuery(org.candlepin.model.CandlepinQuery) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 8 with MockResultIterator

use of org.candlepin.test.MockResultIterator in project candlepin by candlepin.

the class ExporterTest method exportProducts.

@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(Owner.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());
    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);
    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);
    // FINALLY test this badboy
    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);
    // VERIFY
    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());
}
Also used : Owner(org.candlepin.model.Owner) KeyPair(org.candlepin.model.KeyPair) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ProductCertificate(org.candlepin.model.ProductCertificate) Product(org.candlepin.model.Product) CertificateSerial(org.candlepin.model.CertificateSerial) CandlepinQuery(org.candlepin.model.CandlepinQuery) Rules(org.candlepin.model.Rules) ExportRules(org.candlepin.policy.js.export.ExportRules) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) Pool(org.candlepin.model.Pool) Entitlement(org.candlepin.model.Entitlement) ConsumerType(org.candlepin.model.ConsumerType) File(java.io.File) Principal(org.candlepin.auth.Principal) IdentityCertificate(org.candlepin.model.IdentityCertificate) HashSet(java.util.HashSet) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 9 with MockResultIterator

use of org.candlepin.test.MockResultIterator in project candlepin by candlepin.

the class ExporterTest method exportMetadata.

@Test
public void exportMetadata() throws ExportCreationException, IOException {
    config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
    Date start = new Date();
    Rules mrules = mock(Rules.class);
    Consumer consumer = mock(Consumer.class);
    Principal principal = mock(Principal.class);
    IdentityCertificate idcert = new IdentityCertificate();
    when(mrules.getRules()).thenReturn("foobar");
    when(pki.getSHA256WithRSAHash(any(InputStream.class))).thenReturn("signature".getBytes());
    when(rc.getRules()).thenReturn(mrules);
    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());
    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(cdnc.listAll()).thenReturn(emptyIteratorMock);
    // FINALLY test this badboy
    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);
    // VERIFY
    assertNotNull(export);
    assertTrue(export.exists());
    verifyContent(export, "export/meta.json", new VerifyMetadata(start));
    // cleanup the mess
    FileUtils.deleteDirectory(export.getParentFile());
    assertTrue(new File("/tmp/consumer_export.zip").delete());
    assertTrue(new File("/tmp/meta.json").delete());
}
Also used : KeyPair(org.candlepin.model.KeyPair) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) CertificateSerial(org.candlepin.model.CertificateSerial) CandlepinQuery(org.candlepin.model.CandlepinQuery) Rules(org.candlepin.model.Rules) ExportRules(org.candlepin.policy.js.export.ExportRules) Date(java.util.Date) Consumer(org.candlepin.model.Consumer) ConsumerType(org.candlepin.model.ConsumerType) File(java.io.File) Principal(org.candlepin.auth.Principal) IdentityCertificate(org.candlepin.model.IdentityCertificate) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Example 10 with MockResultIterator

use of org.candlepin.test.MockResultIterator in project candlepin by candlepin.

the class JobResourceTest method getStatusesByPrincipal.

@Test
public void getStatusesByPrincipal() {
    List<JobStatus> statuses = new ArrayList<>();
    JobStatus status = new JobStatus();
    statuses.add(status);
    CandlepinQuery query = mock(CandlepinQuery.class);
    when(query.list()).thenReturn(statuses);
    when(query.iterate()).thenReturn(new MockResultIterator(statuses.iterator()));
    when(query.iterate(anyInt(), anyBoolean())).thenReturn(new MockResultIterator(statuses.iterator()));
    when(jobCurator.findByPrincipalName(eq("admin"))).thenReturn(query);
    this.mockCPQueryTransform(query);
    Collection<JobStatusDTO> real = jobResource.getStatuses(null, null, "admin").list();
    assertNotNull(real);
    assertEquals(1, real.size());
}
Also used : JobStatus(org.candlepin.pinsetter.core.model.JobStatus) ArrayList(java.util.ArrayList) TransformedCandlepinQuery(org.candlepin.model.TransformedCandlepinQuery) CandlepinQuery(org.candlepin.model.CandlepinQuery) JobStatusDTO(org.candlepin.dto.api.v1.JobStatusDTO) MockResultIterator(org.candlepin.test.MockResultIterator) Test(org.junit.Test)

Aggregations

CandlepinQuery (org.candlepin.model.CandlepinQuery)10 MockResultIterator (org.candlepin.test.MockResultIterator)10 Test (org.junit.Test)9 File (java.io.File)6 FileInputStream (java.io.FileInputStream)6 InputStream (java.io.InputStream)6 Date (java.util.Date)6 ZipInputStream (java.util.zip.ZipInputStream)6 Principal (org.candlepin.auth.Principal)6 CertificateSerial (org.candlepin.model.CertificateSerial)6 Consumer (org.candlepin.model.Consumer)6 IdentityCertificate (org.candlepin.model.IdentityCertificate)6 Rules (org.candlepin.model.Rules)6 ExportRules (org.candlepin.policy.js.export.ExportRules)6 ConsumerType (org.candlepin.model.ConsumerType)5 KeyPair (org.candlepin.model.KeyPair)5 ArrayList (java.util.ArrayList)4 JobStatusDTO (org.candlepin.dto.api.v1.JobStatusDTO)3 TransformedCandlepinQuery (org.candlepin.model.TransformedCandlepinQuery)3 JobStatus (org.candlepin.pinsetter.core.model.JobStatus)3