}
@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));
}