### Question:
SpiffeKeyManagerFactory extends KeyManagerFactorySpi { @Override protected KeyManager[] engineGetKeyManagers() { val x509Source = getX509Source(); val spiffeKeyManager = new SpiffeKeyManager(x509Source); return new KeyManager[]{spiffeKeyManager}; }  KeyManager[] engineGetKeyManagers(@NonNull final X509SvidSource x509SvidSource);  }### Answer:
@Test void engineGetKeyManagers_usingX509SourceManager() throws NoSuchFieldException, IllegalAccessException { Field field = X509SourceManager.class.getDeclaredField("x509Source"); field.setAccessible(true); X509Source source = new X509SourceStub(); field.set(null, source); KeyManager[] keyManagers = new SpiffeKeyManagerFactory().engineGetKeyManagers(); SpiffeKeyManager keyManager = (SpiffeKeyManager) keyManagers[0]; X509Certificate[] chain = keyManager.getCertificateChain(DEFAULT_ALIAS); X509Certificate certificate = chain[0]; assertEquals(source.getX509Svid().getChain().get(0), certificate); }
@Test void engineGetKeyManagers_passingAX509SvidSource() throws URISyntaxException, X509SvidException { Path cert = Paths.get(toUri("testdata/cert.pem")); Path key = Paths.get(toUri("testdata/key.pem")); X509Svid svid = X509Svid.load(cert, key); X509SvidSource x509SvidSource = () -> svid; KeyManager[] keyManagers = new SpiffeKeyManagerFactory().engineGetKeyManagers(x509SvidSource); SpiffeKeyManager keyManager = (SpiffeKeyManager) keyManagers[0]; assertEquals(svid.getChainArray()[0], keyManager.getCertificateChain(DEFAULT_ALIAS)[0]); }
@Test void engineGetKeyManagers_nullParameter() { try { new SpiffeKeyManagerFactory().engineGetKeyManagers(null); } catch (NullPointerException e) { assertEquals("x509SvidSource is marked non-null but is null", e.getMessage()); } }