DefaultCertificateAuthorityService implements CertificateAuthorityService { @Override public CertificateCredentialValue findTransitionalVersion(final String caName) { final List<CredentialVersion> credentialVersions = certificateVersionDataService.findBothActiveCertAndTransitionalCert(caName); if (credentialVersions == null) { throw new EntryNotFoundException(ErrorMessages.Credential.INVALID_ACCESS); } final CertificateCredentialVersion transitionalVersion = (CertificateCredentialVersion) credentialVersions.stream().filter(version -> { if (!(version instanceof CertificateCredentialVersion)) { throw new ParameterizedValidationException(ErrorMessages.NOT_A_CA_NAME); } return ((CertificateCredentialVersion) version).isVersionTransitional(); }).findFirst().orElse(null); if (transitionalVersion == null) { return null; } else if (!transitionalVersion.getParsedCertificate().isCa()) { throw new ParameterizedValidationException(ErrorMessages.CERT_NOT_CA); } return new CertificateCredentialValue( transitionalVersion.getCertificate(), transitionalVersion.getPrivateKey(), transitionalVersion.isCertificateAuthority(), transitionalVersion.isSelfSigned(), transitionalVersion.getGenerated(), transitionalVersion.isVersionTransitional(), transitionalVersion.getVersionCreatedAt()); } @Autowired  DefaultCertificateAuthorityService(final DefaultCertificateVersionDataService certificateVersionDataService); @Override CertificateCredentialValue findActiveVersion(final String caName); @Override CertificateCredentialValue findTransitionalVersion(final String caName);  }
@Test public void findTransitionalVersion_whenATransitionalCaDoesNotExist_returnsNull() { when(certificateVersionDataService.findBothActiveCertAndTransitionalCert(CREDENTIAL_NAME)).thenReturn( Collections.singletonList(certificateCredential)); CertificateCredentialValue transitionalVersion = certificateAuthorityService.findTransitionalVersion(CREDENTIAL_NAME); assertNull(transitionalVersion); }
@Test public void findTransitionalVersion_givenExistingTransitionalCa_returnsTheTransitionalCa() { final CertificateReader certificateReader = mock(CertificateReader.class); when(transitionalCertificateCredential.getParsedCertificate()).thenReturn(certificateReader); when(certificateReader.isCa()).thenReturn(true); when(certificateVersionDataService.findBothActiveCertAndTransitionalCert(CREDENTIAL_NAME)).thenReturn(Arrays.asList(certificateCredential, transitionalCertificateCredential)); when(transitionalCertificateCredential.getCertificate()).thenReturn(SELF_SIGNED_CA_CERT); assertThat(certificateAuthorityService.findTransitionalVersion(CREDENTIAL_NAME).getCertificate(), equalTo(transitionalCertificateCredential.getCertificate())); }