CertificatesLoader implements EntityLoader { @Override public void load(Bundle bundle, File rootDir) { final File certificatesDir = findConfigFileOrDir(rootDir, "certificates"); if (certificatesDir != null && certificatesDir.exists()) { final String[] certs = certificatesDir.list(); if (certs != null && certs.length > 0) { final Map<String, SupplierWithIO<InputStream>> map = new HashMap<>(); stream(certs).forEach(cert -> { if (checkCertFormat(cert)) { map.put(getBaseName(cert), () -> new FileInputStream(new File(certificatesDir, cert))); } else { throw new ConfigLoadException(cert + " must be a valid certificate extension."); } }); bundle.putAllCertificateFiles(map); } } } @Inject  CertificatesLoader(FileUtils fileUtils); @Override void load(Bundle bundle, File rootDir); @Override void load(Bundle bundle, String name, String value); @Override Object loadSingle(String name, File entitiesFile); @Override Map<String, Object> load(File entitiesFile); @Override String getEntityType();  }
@Test void load() throws IOException { createCertificates("cert1.cer", "cert2.cer", "cert3.cer"); CertificatesLoader loader = new CertificatesLoader(FileUtils.INSTANCE); Bundle bundle = new Bundle(); loader.load(bundle, rootProjectDir.getRoot()); assertFalse(bundle.getCertificateFiles().isEmpty()); assertEquals(3, bundle.getCertificateFiles().size()); assertNotNull(bundle.getCertificateFiles().get("cert1")); assertNotNull(bundle.getCertificateFiles().get("cert2")); assertNotNull(bundle.getCertificateFiles().get("cert3")); }
@Test void loadNoCerts() { CertificatesLoader loader = new CertificatesLoader(FileUtils.INSTANCE); Bundle bundle = new Bundle(); loader.load(bundle, rootProjectDir.getRoot()); assertTrue(bundle.getCertificateFiles().isEmpty()); }
@Test void loadInvalidCert() throws IOException { createCertificates("cert1.cert"); CertificatesLoader loader = new CertificatesLoader(FileUtils.INSTANCE); Bundle bundle = new Bundle(); assertThrows(ConfigLoadException.class, () -> loader.load(bundle, rootProjectDir.getRoot())); }
@Test void loadFromEnvironment() { CertificatesLoader loader = new CertificatesLoader(FileUtils.INSTANCE); Bundle bundle = new Bundle(); loader.load(bundle, "cert1.cer", "CERTIFICATE"); assertFalse(bundle.getCertificateFiles().isEmpty()); assertEquals(1, bundle.getCertificateFiles().size()); assertNotNull(bundle.getCertificateFiles().get("cert1")); }
@Test void loadFromEnvironmentInvalidName() { CertificatesLoader loader = new CertificatesLoader(FileUtils.INSTANCE); Bundle bundle = new Bundle(); assertThrows(ConfigLoadException.class, () -> loader.load(bundle, "cert1.cert", "CERTIFICATE")); }