Search in sources :

Example 41 with Consumer

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

the class DefaultIdentityCertServiceAdapterTest method testGenerate.

@Test
public void testGenerate() throws GeneralSecurityException, IOException {
    Consumer consumer = mock(Consumer.class);
    when(consumer.getId()).thenReturn("42");
    when(consumer.getUuid()).thenReturn(Util.generateUUID());
    KeyPair kp = createKeyPair();
    when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
    when(idcur.find(consumer.getId())).thenReturn(null);
    when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {

        public CertificateSerial answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            CertificateSerial cs = (CertificateSerial) args[0];
            cs.setId(42L);
            return cs;
        }
    });
    when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
    when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
    when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {

        public IdentityCertificate answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            IdentityCertificate ic = (IdentityCertificate) args[0];
            ic.setId("42");
            return ic;
        }
    });
    IdentityCertificate ic = dicsa.generateIdentityCert(consumer);
    assertNotNull(ic);
    assertEquals("priv", ic.getKey());
    assertEquals("x509cert", ic.getCert());
    assertNotNull(ic.getCertAsBytes());
    assertNotNull(ic.getKeyAsBytes());
    verify(consumer).setIdCert(ic);
    verify(csc).create(any(CertificateSerial.class));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 42 with Consumer

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

the class DefaultIdentityCertServiceAdapterTest method testRegenerateCallsDeletes.

@Test
public void testRegenerateCallsDeletes() throws GeneralSecurityException, IOException {
    Consumer consumer = mock(Consumer.class);
    IdentityCertificate mockic = mock(IdentityCertificate.class);
    when(consumer.getIdCert()).thenReturn(mockic);
    when(mockic.getId()).thenReturn("43");
    when(idcur.find(mockic.getId())).thenReturn(mockic);
    KeyPair kp = createKeyPair();
    when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
    when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {

        public CertificateSerial answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            CertificateSerial cs = (CertificateSerial) args[0];
            cs.setId(42L);
            return cs;
        }
    });
    when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
    when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
    when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {

        public IdentityCertificate answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            IdentityCertificate ic = (IdentityCertificate) args[0];
            ic.setId("42");
            return ic;
        }
    });
    IdentityCertificate ic = dicsa.regenerateIdentityCert(consumer);
    verify(consumer).setIdCert(null);
    verify(idcur).delete(mockic);
    assertNotSame(ic, mockic);
    assertEquals("priv", ic.getKey());
    assertEquals("x509cert", ic.getCert());
    assertNotNull(ic.getCertAsBytes());
    assertNotNull(ic.getKeyAsBytes());
    verify(consumer).setIdCert(ic);
    verify(csc).create(any(CertificateSerial.class));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 43 with Consumer

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

the class DefaultIdentityCertServiceAdapterTest method testRegenerate.

@Test
public void testRegenerate() throws GeneralSecurityException, IOException {
    Consumer consumer = mock(Consumer.class);
    when(consumer.getId()).thenReturn("42L");
    when(consumer.getUuid()).thenReturn(Util.generateUUID());
    when(idcur.find(consumer.getId())).thenReturn(null);
    KeyPair kp = createKeyPair();
    when(kpc.getConsumerKeyPair(consumer)).thenReturn(kp);
    when(csc.create(any(CertificateSerial.class))).thenAnswer(new Answer<CertificateSerial>() {

        public CertificateSerial answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            CertificateSerial cs = (CertificateSerial) args[0];
            cs.setId(42L);
            return cs;
        }
    });
    when(pki.getPemEncoded(any(X509Certificate.class))).thenReturn("x509cert".getBytes());
    when(pki.getPemEncoded(any(PrivateKey.class))).thenReturn("priv".getBytes());
    when(idcur.create(any(IdentityCertificate.class))).thenAnswer(new Answer<IdentityCertificate>() {

        public IdentityCertificate answer(InvocationOnMock invocation) {
            Object[] args = invocation.getArguments();
            IdentityCertificate ic = (IdentityCertificate) args[0];
            ic.setId("42");
            return ic;
        }
    });
    IdentityCertificate ic = dicsa.regenerateIdentityCert(consumer);
    assertNotNull(ic);
    verify(consumer, never()).setIdCert(null);
    verify(idcur, never()).delete(any(IdentityCertificate.class));
    assertEquals("priv", ic.getKey());
    assertEquals("x509cert", ic.getCert());
    assertNotNull(ic.getCertAsBytes());
    assertNotNull(ic.getKeyAsBytes());
    verify(consumer).setIdCert(ic);
    verify(csc).create(any(CertificateSerial.class));
}
Also used : KeyPair(java.security.KeyPair) PrivateKey(java.security.PrivateKey) Consumer(org.candlepin.model.Consumer) InvocationOnMock(org.mockito.invocation.InvocationOnMock) CertificateSerial(org.candlepin.model.CertificateSerial) X509Certificate(java.security.cert.X509Certificate) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 44 with Consumer

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

the class DefaultIdentityCertServiceAdapterTest method testReturnExisting.

@Test
public void testReturnExisting() throws GeneralSecurityException, IOException {
    Consumer consumer = mock(Consumer.class);
    IdentityCertificate mockic = mock(IdentityCertificate.class);
    when(consumer.getIdCert()).thenReturn(mockic);
    when(idcur.find(mockic.getId())).thenReturn(mockic);
    when(idcur.find(consumer.getId())).thenReturn(mockic);
    IdentityCertificate ic = dicsa.generateIdentityCert(consumer);
    assertNotNull(ic);
    assertEquals(ic, mockic);
}
Also used : Consumer(org.candlepin.model.Consumer) IdentityCertificate(org.candlepin.model.IdentityCertificate) Test(org.junit.Test)

Example 45 with Consumer

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

the class ExporterTest method exportConsumer.

@Test
public void exportConsumer() throws ExportCreationException, IOException {
    config.setProperty(ConfigProperties.SYNC_WORK_DIR, "/tmp/");
    config.setProperty(ConfigProperties.PREFIX_WEBURL, "localhost:8443/weburl");
    config.setProperty(ConfigProperties.PREFIX_APIURL, "localhost:8443/apiurl");
    Rules mrules = mock(Rules.class);
    Consumer consumer = mock(Consumer.class);
    Principal principal = mock(Principal.class);
    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");
    // specific to this test
    IdentityCertificate idcert = new IdentityCertificate();
    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);
    ConsumerType ctype = new ConsumerType(ConsumerTypeEnum.CANDLEPIN);
    ctype.setId("test-ctype");
    KeyPair keyPair = createKeyPair();
    when(consumer.getKeyPair()).thenReturn(keyPair);
    when(pki.getPemEncoded(keyPair.getPrivateKey())).thenReturn("privateKey".getBytes());
    when(pki.getPemEncoded(keyPair.getPublicKey())).thenReturn("publicKey".getBytes());
    when(consumer.getUuid()).thenReturn("8auuid");
    when(consumer.getName()).thenReturn("consumer_name");
    when(consumer.getContentAccessMode()).thenReturn("access_mode");
    when(consumer.getTypeId()).thenReturn(ctype.getId());
    when(ctc.getConsumerType(eq(consumer))).thenReturn(ctype);
    when(ctc.find(eq(ctype.getId()))).thenReturn(ctype);
    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);
    verifyContent(export, "export/consumer.json", new VerifyConsumer("consumer.json"));
}
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)

Aggregations

Consumer (org.candlepin.model.Consumer)470 Test (org.junit.Test)345 Entitlement (org.candlepin.model.Entitlement)162 Owner (org.candlepin.model.Owner)126 Pool (org.candlepin.model.Pool)114 Product (org.candlepin.model.Product)112 Date (java.util.Date)102 ConsumerType (org.candlepin.model.ConsumerType)94 LinkedList (java.util.LinkedList)73 ArrayList (java.util.ArrayList)71 HashSet (java.util.HashSet)69 ConsumerInstalledProduct (org.candlepin.model.ConsumerInstalledProduct)69 HashMap (java.util.HashMap)48 ApiOperation (io.swagger.annotations.ApiOperation)43 Produces (javax.ws.rs.Produces)43 ConsumerDTO (org.candlepin.dto.api.v1.ConsumerDTO)40 ApiResponses (io.swagger.annotations.ApiResponses)38 GuestId (org.candlepin.model.GuestId)37 Path (javax.ws.rs.Path)36 List (java.util.List)35