DefaultCertificateAuthorityService implements CertificateAuthorityService { @Override public CertificateCredentialValue findActiveVersion(final String caName) { final CredentialVersion mostRecent = certificateVersionDataService.findActive(caName); if (mostRecent == null) { throw new EntryNotFoundException(ErrorMessages.Credential.CERTIFICATE_ACCESS); } if (!(mostRecent instanceof CertificateCredentialVersion)) { throw new ParameterizedValidationException(ErrorMessages.NOT_A_CA_NAME); } final CertificateCredentialVersion certificateCredential = (CertificateCredentialVersion) mostRecent; if (!certificateCredential.getParsedCertificate().isCa()) { throw new ParameterizedValidationException(ErrorMessages.CERT_NOT_CA); } return new CertificateCredentialValue( certificateCredential.getCertificate(), certificateCredential.getPrivateKey(), certificateCredential.isCertificateAuthority(), certificateCredential.isSelfSigned(), certificateCredential.getGenerated(), certificateCredential.isVersionTransitional(), certificateCredential.getVersionCreatedAt()); } @Autowired  DefaultCertificateAuthorityService(final DefaultCertificateVersionDataService certificateVersionDataService); @Override CertificateCredentialValue findActiveVersion(final String caName); @Override CertificateCredentialValue findTransitionalVersion(final String caName);  }
@Test public void findActiveVersion_whenACaDoesNotExist_throwsException() { when(certificateVersionDataService.findActive(any(String.class))).thenReturn(null); try { certificateAuthorityService.findActiveVersion("any ca name"); } catch (final EntryNotFoundException pe) { assertThat(pe.getMessage(), equalTo(ErrorMessages.Credential.CERTIFICATE_ACCESS)); } }
@Test public void findActiveVersion_whenCaNameRefersToNonCa_throwsException() { when(certificateVersionDataService.findActive(any(String.class))).thenReturn(mock(PasswordCredentialVersion.class)); try { certificateAuthorityService.findActiveVersion("any non-ca name"); } catch (final ParameterizedValidationException pe) { assertThat(pe.getMessage(), equalTo(ErrorMessages.NOT_A_CA_NAME)); } }
@Test public void findActiveVersion_givenExistingCa_returnsTheCa() { final CertificateReader certificateReader = mock(CertificateReader.class); when(certificateVersionDataService.findActive(CREDENTIAL_NAME)).thenReturn(certificateCredential); when(certificateCredential.getPrivateKey()).thenReturn("my-key"); when(certificateCredential.getParsedCertificate()).thenReturn(certificateReader); when(certificateReader.isCa()).thenReturn(true); when(certificateCredential.isCertificateAuthority()).thenReturn(true); when(certificateCredential.isSelfSigned()).thenReturn(true); when(certificateCredential.getCertificate()).thenReturn(SELF_SIGNED_CA_CERT); when(certificateCredential.getGenerated()).thenReturn(false); when(certificateCredential.isVersionTransitional()).thenReturn(false); assertThat(certificateAuthorityService.findActiveVersion(CREDENTIAL_NAME), samePropertyValuesAs(certificate)); }
@Test public void findActiveVersion_whenCredentialIsNotACa_throwsException() { when(certificateVersionDataService.findActive("actually-a-password")) .thenReturn(new PasswordCredentialVersion()); try { certificateAuthorityService.findActiveVersion("actually-a-password"); } catch (final ParameterizedValidationException pe) { assertThat(pe.getMessage(), equalTo(ErrorMessages.NOT_A_CA_NAME)); } catch (final Exception e) { fail("expected EntryNotFoundException, but got " + e.getClass()); } }
@Test public void findActiveVersion_whenCertificateIsNotACa_throwsException() { final CertificateCredentialVersion notACertificateAuthority = mock(CertificateCredentialVersion.class); when(notACertificateAuthority.getParsedCertificate()).thenReturn(mock(CertificateReader.class)); when(notACertificateAuthority.getCertificate()).thenReturn(SIMPLE_SELF_SIGNED_TEST_CERT); when(certificateVersionDataService.findActive(CREDENTIAL_NAME)) .thenReturn(notACertificateAuthority); try { certificateAuthorityService.findActiveVersion(CREDENTIAL_NAME); } catch (final ParameterizedValidationException pe) { assertThat(pe.getMessage(), equalTo(ErrorMessages.CERT_NOT_CA)); } }