### Question:
CertificateChainConfigValidator { public void validate(Set<CertificateConfigurable<? extends CertificateConfigurable<?>>> configs) { configs.stream().forEach(this::validate); } @Inject  CertificateChainConfigValidator(final TrustStoreForCertificateProvider trustStoreForCertificateProvider, final CertificateChainValidator certificateChainValidator); void validate(Set<CertificateConfigurable<? extends CertificateConfigurable<?>>> configs);  }### Answer:
@Test public void validateLogsErrorForMissingCertificatesWhenNotSelfService() { Logger logger = (Logger) LoggerFactory.getLogger(CertificateChainConfigValidator.class); ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); listAppender.start(); logger.addAppender(listAppender); TransactionConfig config = aTransactionConfigData().withEncryptionCertificate("").withSelfService(false).build(); Certificate signingCert = config.getSignatureVerificationCertificates().stream().findFirst().get(); when(certificateChainValidator.validate(signingCert.getX509Certificate().get(), trustStore)).thenReturn(CertificateValidity.valid()); validator.validate(Set.of(config)); List<ILoggingEvent> logsList = listAppender.list; assertThat(logsList.get(0).getLevel().toString()).isEqualTo(Level.ERROR.toString()); }
@Test public void validateLogsWarningForX509CertsThatFailChainValidation() { Logger logger = (Logger) LoggerFactory.getLogger(CertificateChainConfigValidator.class); ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); listAppender.start(); logger.addAppender(listAppender); TransactionConfig config = aTransactionConfigData().withSelfService(false).build(); Certificate encryptionCert = config.getEncryptionCertificate(); Certificate signingCert = config.getSignatureVerificationCertificates().stream().findFirst().get(); CertPathValidatorException certPathValidatorException = new CertPathValidatorException("Bad Cert Chain"); when(certificateChainValidator.validate(encryptionCert.getX509Certificate().get(), trustStore)).thenReturn(CertificateValidity.invalid(certPathValidatorException)); when(certificateChainValidator.validate(signingCert.getX509Certificate().get(), trustStore)).thenReturn(CertificateValidity.valid()); validator.validate(Set.of(config)); List<ILoggingEvent> logsList = listAppender.list; assertThat(logsList.get(0).getLevel().toString()).isEqualTo(Level.WARN.toString()); assertThat(logsList.get(0).getMessage()).contains("Bad Cert Chain"); }
@Test public void validateDoesNotLogWhenX509CertsArePresentAndValid() { Logger logger = (Logger) LoggerFactory.getLogger(CertificateChainConfigValidator.class); ListAppender<ILoggingEvent> listAppender = new ListAppender<>(); listAppender.start(); logger.addAppender(listAppender); TransactionConfig config = aTransactionConfigData().withSelfService(false).build(); Certificate encryptionCert = config.getEncryptionCertificate(); Certificate signingCert = config.getSignatureVerificationCertificates().stream().findFirst().get(); when(certificateChainValidator.validate(encryptionCert.getX509Certificate().get(), trustStore)).thenReturn(CertificateValidity.valid()); when(certificateChainValidator.validate(signingCert.getX509Certificate().get(), trustStore)).thenReturn(CertificateValidity.valid()); validator.validate(Set.of(config)); List<ILoggingEvent> logsList = listAppender.list; assertThat(logsList.isEmpty()).isTrue(); }