TrustStoreImpl extends ComponentSupport implements EventAware, TrustStore { @Override public Certificate importTrustCertificate(final Certificate certificate, final String alias) throws KeystoreException { freezeService.checkWritable("Unable to import a certificate while database is frozen."); keyStoreManager.importTrustCertificate(certificate, alias); eventManager.post(new CertificateCreatedEvent(alias, certificate)); log.info("Certificate added successfully in trust-store with Fingerprint: {}, Name: {} and SHA1 Identifier: {} ", alias, getCertificateName(certificate), getCertificateSha1(certificate)); return certificate; } @Inject  TrustStoreImpl(final EventManager eventManager,
                        @Named("ssl") final KeyStoreManager keyStoreManager,
                        final FreezeService freezeService); @Override Certificate importTrustCertificate(final Certificate certificate, final String alias); @Override Certificate importTrustCertificate(final String certificateInPEM, final String alias); @Override Certificate getTrustedCertificate(final String alias); @Override Collection<Certificate> getTrustedCertificates(); @Override void removeTrustCertificate(final String alias); @Override SSLContext getSSLContext(); @Override KeyManager[] getKeyManagers(); @Subscribe void onKeyStoreDataUpdated(final KeyStoreDataEvent event); static final SecureRandom DEFAULT_RANDOM; }
@Test public void importTrustCertificate() throws Exception { underTest.importTrustCertificate(certificate, "test"); verify(keyStoreManager).importTrustCertificate(certificate, "test"); verify(freezeService).checkWritable("Unable to import a certificate while database is frozen."); }
@Test public void importTrustCertificate_frozen() throws Exception { doThrow(frozenException).when(freezeService).checkWritable(anyString()); try { underTest.importTrustCertificate(certificate, "test"); fail(); } catch (OModificationOperationProhibitedException e) { } verify(keyStoreManager, times(0)).importTrustCertificate(any(Certificate.class), anyString()); verifyZeroInteractions(eventManager); }
@Test public void importTrustCertificateStrings() throws KeystoreException, CertificateException { underTest.importTrustCertificate(CERT_IN_PEM_UNIX, "test"); verify(keyStoreManager).importTrustCertificate(isA(Certificate.class), eq("test")); verify(freezeService).checkWritable("Unable to import a certificate while database is frozen."); }
@Test public void importTrustCertificateStrings_frozen() throws Exception { doThrow(frozenException).when(freezeService).checkWritable(anyString()); try { underTest.importTrustCertificate(CERT_IN_PEM_UNIX, "test"); fail(); } catch (OModificationOperationProhibitedException e) { } verify(keyStoreManager, times(0)).importTrustCertificate(any(Certificate.class), anyString()); verifyZeroInteractions(eventManager); }
@Test public void importTrustCertificate_throwCertificateEncodingExceptionWhileCalculateSha1() throws Exception { when(certificate.getEncoded()).thenThrow(new CertificateEncodingException("Throwing exception for test")); Certificate certAdded = underTest.importTrustCertificate(certificate, "test"); assertNotNull(certAdded); verify(keyStoreManager).importTrustCertificate(certificate, "test"); verify(freezeService).checkWritable("Unable to import a certificate while database is frozen."); }
@Test public void importTrustCertificateStrings_throwCertificateEncodingExceptionWhileCalculateSha1() throws KeystoreException, CertificateException { when(certificate.getEncoded()).thenThrow(new CertificateEncodingException("Throwing exception for test")); Certificate certAdded = underTest.importTrustCertificate(CERT_IN_PEM_UNIX, "test"); assertNotNull(certAdded); verify(keyStoreManager).importTrustCertificate(isA(Certificate.class), eq("test")); verify(freezeService).checkWritable("Unable to import a certificate while database is frozen."); }