AutoRefreshCredentialProvider implements HerdAWSCredentialsProvider { @Override public AwsCredential getAwsCredential() { if (sessionExpirationTime == null || System.currentTimeMillis() >= sessionExpirationTime) { try { currentAwsCredentials = getNewAwsCredential(); } catch (Exception e) { LOGGER.warn("Error retrieving new credentials. {}", e.toString(), e); throw new IllegalStateException(e); } sessionExpirationTime = currentAwsCredentials.getAwsSessionExpirationTime().toGregorianCalendar().getTimeInMillis(); } return currentAwsCredentials; }  abstract AwsCredential getNewAwsCredential(); @Override AwsCredential getAwsCredential();  }
@Test public void testAssertCredentialCachingBehavior() throws Exception { AutoRefreshCredentialProvider autoRefreshCredentialProvider = new AutoRefreshCredentialProvider() { @Override public AwsCredential getNewAwsCredential() throws Exception { AwsCredential awsCredential = new AwsCredential(); GregorianCalendar cal = new GregorianCalendar(); cal.setTimeInMillis(System.currentTimeMillis() + 1000); awsCredential.setAwsSessionExpirationTime(DatatypeFactory.newInstance().newXMLGregorianCalendar(cal)); return awsCredential; } }; AwsCredential firstAwsCredential = autoRefreshCredentialProvider.getAwsCredential(); AwsCredential secondAwsCredential = autoRefreshCredentialProvider.getAwsCredential(); assertEquals(firstAwsCredential, secondAwsCredential); Thread.sleep(1000); secondAwsCredential = autoRefreshCredentialProvider.getAwsCredential(); assertNotEquals(firstAwsCredential, secondAwsCredential); }
@Test public void testAssertException() { AutoRefreshCredentialProvider autoRefreshCredentialProvider = new AutoRefreshCredentialProvider() { @Override public AwsCredential getNewAwsCredential() throws Exception { throw new Exception("test"); } }; try { executeWithoutLogging(AutoRefreshCredentialProvider.class, () -> { autoRefreshCredentialProvider.getAwsCredential(); }); fail(); } catch (Exception e) { assertEquals(IllegalStateException.class, e.getClass()); Throwable cause = e.getCause(); assertEquals(Exception.class, cause.getClass()); assertEquals("test", cause.getMessage()); } }