DefaultIdentityCertServiceAdapter implements
    IdentityCertServiceAdapter { @Override public IdentityCertificate regenerateIdentityCert(Consumer consumer) throws GeneralSecurityException, IOException { IdentityCertificate certificate = null; if (consumer.getIdCert() != null) { certificate = idCertCurator.get(consumer.getIdCert().getId()); } if (certificate != null) { consumer.setIdCert(null); idCertCurator.delete(certificate); } return generate(consumer); } @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 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.get(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.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.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)); }
@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.get(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.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.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)); }