### Question:
P12Util { public static PrivateKeyEntry getFirstPrivateKeyEntryFromP12InputStream(final InputStream p12InputStream, final String password) throws KeyStoreException, IOException { Objects.requireNonNull(password, "Password may be blank, but must not be null."); final KeyStore keyStore = KeyStore.getInstance("PKCS12"); try { keyStore.load(p12InputStream, password.toCharArray()); } catch (final NoSuchAlgorithmException | CertificateException e) { throw new KeyStoreException(e); } final Enumeration<String> aliases = keyStore.aliases(); final KeyStore.PasswordProtection passwordProtection = new KeyStore.PasswordProtection(password.toCharArray()); while (aliases.hasMoreElements()) { final String alias = aliases.nextElement(); KeyStore.Entry entry; try { try { entry = keyStore.getEntry(alias, passwordProtection); } catch (final UnsupportedOperationException e) { entry = keyStore.getEntry(alias, null); } } catch (final UnrecoverableEntryException | NoSuchAlgorithmException e) { throw new KeyStoreException(e); } if (entry instanceof KeyStore.PrivateKeyEntry) { return (PrivateKeyEntry) entry; } } throw new KeyStoreException("Key store did not contain any private key entries."); }  static PrivateKeyEntry getFirstPrivateKeyEntryFromP12InputStream(final InputStream p12InputStream, final String password);  }### Answer:
@Test void testGetPrivateKeyEntryFromP12InputStream() throws Exception { try (final InputStream p12InputStream = P12UtilTest.class.getResourceAsStream(SINGLE_TOPIC_CLIENT_KEYSTORE_FILENAME)) { final PrivateKeyEntry privateKeyEntry = P12Util.getFirstPrivateKeyEntryFromP12InputStream(p12InputStream, KEYSTORE_PASSWORD); assertNotNull(privateKeyEntry); } }
@Test void testGetPrivateKeyEntryFromP12InputStreamWithMultipleKeys() throws Exception { try (final InputStream p12InputStream = P12UtilTest.class.getResourceAsStream(MULTIPLE_KEY_KEYSTORE_FILENAME)) { final PrivateKeyEntry privateKeyEntry = P12Util.getFirstPrivateKeyEntryFromP12InputStream(p12InputStream, KEYSTORE_PASSWORD); assertNotNull(privateKeyEntry); } }
@Test void testGetPrivateKeyEntryFromP12InputStreamWithNoKeys() throws Exception { try (final InputStream p12InputStream = P12UtilTest.class.getResourceAsStream(NO_KEY_KEYSTORE_FILENAME)) { assertThrows(KeyStoreException.class, () -> P12Util.getFirstPrivateKeyEntryFromP12InputStream(p12InputStream, KEYSTORE_PASSWORD)); } }