RsaGenerator implements CredentialGenerator<RsaCredentialValue> { @Override public RsaCredentialValue generateCredential(final GenerationParameters p) { final RsaGenerationParameters params = (RsaGenerationParameters) p; try { final KeyPair keyPair = keyGenerator.generateKeyPair(params.getKeyLength()); return new RsaCredentialValue(CertificateFormatter.pemOf(keyPair.getPublic()), CertificateFormatter.pemOf(keyPair.getPrivate())); } catch (final Exception e) { throw new RuntimeException(e); } } @Autowired  RsaGenerator(final RsaKeyPairGenerator keyGenerator); @Override RsaCredentialValue generateCredential(final GenerationParameters p);  }
@Test public void generateCredential_shouldReturnAGeneratedCredential() throws Exception { final RsaCredentialValue rsa = subject.generateCredential(new RsaGenerationParameters()); verify(keyPairGenerator).generateKeyPair(2048); assertThat(rsa.getPublicKey(), equalTo(CertificateFormatter.pemOf(keyPair.getPublic()))); assertThat(rsa.getPrivateKey(), equalTo(CertificateFormatter.pemOf(keyPair.getPrivate()))); }
@Test public void generateCredential_shouldUseTheProvidedKeyLength() throws Exception { final RsaGenerationParameters rsaGenerationParameters = new RsaGenerationParameters(); rsaGenerationParameters.setKeyLength(4096); subject.generateCredential(rsaGenerationParameters); verify(keyPairGenerator).generateKeyPair(4096); }