KeystoreCreator { static void createKeyStoreIfNecessary(String keyStoreDirPath, String privateKeyFolderPath, Collection<PrivateKey> privateKeys, FileUtils fileUtils, String systemPropertiesPath) { PrivateKey.loadFromDirectory(privateKeys, new File(privateKeyFolderPath), true); KeystoreHelper keystoreHelper = InjectionRegistry.getInjector().getInstance(KeystoreHelper.class); final byte[] keyStore = keystoreHelper.createKeyStore(privateKeys); if (keyStore.length == 0) { return; } final File keyStoreDirectory = new File(keyStoreDirPath); if (!keyStoreDirectory.exists() && !keyStoreDirectory.mkdirs()) { throw new KeyStoreCreationException("Could not create directory '" + keyStoreDirPath + "' for KeyStore"); } File keyStoreFile = new File(keyStoreDirectory, KEYSTORE_FILE_NAME); OutputStream stream = fileUtils.getOutputStream(keyStoreFile); try { IOUtils.write(keyStore, stream); } catch (IOException e) { throw new KeyStoreCreationException("Unexpected error writing key store", e); } finally { closeQuietly(stream); } updateSystemPropertiesFile(keystoreHelper, keyStoreFile, systemPropertiesPath); } private  KeystoreCreator();   }
@Test void createKeystore_noKeys() { KeystoreCreator.createKeyStoreIfNecessary(temporaryFolder.getRoot().getPath(), getClass().getClassLoader().getResource("privateKeys").getPath(), emptyList(), fileUtils, systemProperties.getPath()); assertFalse(new File(temporaryFolder.getRoot(), KEYSTORE_FILE_NAME).exists()); }
@Test void createKeystore_checkKeystoreFile() { when(fileUtils.getOutputStream(any(File.class))).thenCallRealMethod(); KeystoreCreator.createKeyStoreIfNecessary(temporaryFolder.getRoot().getPath(), getClass().getClassLoader().getResource("privateKeys").getPath(), singletonList(createPrivateKey()), fileUtils, systemProperties.getPath()); assertTrue(new File(temporaryFolder.getRoot(), KEYSTORE_FILE_NAME).exists()); }
@Test void createKeystore_errorWriting() { when(fileUtils.getOutputStream(any(File.class))).thenReturn(new OutputStream() { @Override public void write(int b) throws IOException { throw new IOException(); } }); assertThrows(KeyStoreCreationException.class, () -> KeystoreCreator.createKeyStoreIfNecessary(temporaryFolder.getRoot().getPath(), getClass().getClassLoader().getResource("privateKeys").getPath(), singletonList(createPrivateKey()), fileUtils, systemProperties.getPath())); }