DefaultIdentityCertServiceAdapter implements
    IdentityCertServiceAdapter { private IdentityCertificate generate(Consumer consumer) throws GeneralSecurityException, IOException { Calendar cal = Calendar.getInstance(); cal.add(Calendar.HOUR, -1); Date startDate = cal.getTime(); Date endDate = this.endDateGenerator.apply(new Date()); CertificateSerial serial = new CertificateSerial(endDate); serialCurator.create(serial); String dn = createDN(consumer); IdentityCertificate identityCert = new IdentityCertificate(); KeyPair keyPair = keyPairCurator.getConsumerKeyPair(consumer); X509Certificate x509cert = pki.createX509Certificate(dn, null, null, startDate, endDate, keyPair, BigInteger.valueOf(serial.getId()), consumer.getName()); identityCert.setCert(new String(pki.getPemEncoded(x509cert))); identityCert.setKey(new String(pki.getPemEncoded(keyPair.getPrivate()))); identityCert.setSerial(serial); consumer.setIdCert(identityCert); return idCertCurator.create(identityCert); } @SuppressWarnings("unchecked") @Inject  DefaultIdentityCertServiceAdapter(PKIUtility pki,
        IdentityCertificateCurator identityCertCurator,
        KeyPairCurator keyPairCurator,
        CertificateSerialCurator serialCurator,
        @Named("endDateGenerator") Function endDtGen); @Override void deleteIdentityCert(Consumer consumer); @Override IdentityCertificate generateIdentityCert(Consumer consumer); @Override IdentityCertificate regenerateIdentityCert(Consumer consumer);  }
@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.get(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.createX509Certificate(any(String.class), nullable(Set.class), nullable(Set.class), any(Date.class), any(Date.class), any(KeyPair.class), any(BigInteger.class), nullable(String.class))) .thenReturn(mock(X509Certificate.class)); 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)); }